Skip to content

Commit 4f8c002

Browse files
committed
Completed Modifiers, resolved and refactored my crappy code
1 parent 90a1358 commit 4f8c002

8 files changed

Lines changed: 5258 additions & 241 deletions

File tree

‎.gitmodules‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎Main.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
int main(void)
44
{
5-
cstring str = cstr_init("TEK KNIGHT");
6-
cstr_insert(&str, 4, "KING");
5+
cstring str = cstr_init("");
6+
cstring other = cstr_init("");
7+
78

8-
cstring str_n = intl_assign_str(&str, "The FrenchMan, The Female");
9-
cstr_println(str_n);
10-
printf("Capacity of str = %lu\n", cstr_capacity(str_n));
9+
cstr_swap(&str, &other);
10+
11+
printf("str = %s ==&== Size = %lu\n", str, cstr_size(str));
12+
printf("other = %s ==&== Size = %lu\n", other, cstr_size(other));
1113
}

‎README.md‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22
A string library in C with C++ standard features.
33

44

5-
## I will get back to it
6-
- [ ] Add Iterators to insert
7-
- [ ] Replace
8-
- [ ] assign
9-
- [ ] append
10-
- [ ] copy
11-
- [ ] resize
12-
- [ ] swap
5+
## Quick Start
6+
```
7+
git clone https://github.com/lucky017/cstring.git
8+
cd cstring
9+
make
10+
```
11+
Link the resulting static library with your binaries.
12+
13+
14+
## I will get back to
1315
- [ ] search functions
1416
- [ ] Operations
1517
- [ ] input & output
1618
- [ ] Numeric conversions
1719
- [ ] Literals
18-
- [ ] Hash support for cstring
20+
- [ ] Hash support for cstring
21+
- [ ] Any Fuck-ups I have done in this library
22+
23+
## License
24+
cstring library is licensed under the MIT License, see [LICENSE](https://github.com/lucky017/cstring/blob/master/LICENSE) for more information.

‎cstring.c‎

Lines changed: 213 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,127 @@
22
#include "cstring.h"
33

44

5+
// =========================================================
6+
// PUBLIC API
7+
// ========================================================
8+
9+
10+
void __unused
11+
cstr_push_back(cstring *str, const char ch)
12+
{
13+
intl_append_cnt_ch(str, 1, ch);
14+
}
15+
16+
void __unused
17+
cstr_pop_back(cstring *str)
18+
{
19+
intl_erase_ch_iter(str, (cstr_iterator){cstr_end(*str).it - 1});
20+
}
21+
22+
size_t __unused
23+
cstr_copy(cstring *str, const_cstring other, const size_t count,
24+
const size_t pos)
25+
{
26+
cstring str_ = *str;
27+
size_t count_ = CSTRMIN(cstr_size(str_),
28+
CSTRMIN(count, pvt_cstr_strlen(other) - pos)
29+
);
30+
31+
pvt_cstr_memmove(str_, other + pos, count_);
32+
return count_;
33+
}
34+
35+
536
/*
6-
* Assign
37+
* Input & Output
38+
* ---------------
39+
*/
40+
void __unused
41+
cstr_getstr(cstring *str, size_t cnt, FILE* stream)
42+
{
43+
cstring str_02_ = *str;
44+
pvt_cstr_assert(cnt < cstr_capacity(str_02_),
45+
"count is larger than capacity to read.");
46+
47+
char l_str_array[cnt];
48+
fgets(l_str_array, (int)cnt, stream);
49+
pvt_copy_(str_02_, &l_str_array[0], cnt);
50+
}
51+
52+
53+
void __unused
54+
cstr_println(cstring str)
55+
{
56+
fputs(str, stdout);
57+
putc('\n', stdout);
58+
}
59+
60+
/**
61+
* Modifiers
62+
* ----------
63+
*/
64+
65+
void __unused
66+
cstr_resize(cstring *str, const size_t count, const char ch)
67+
{
68+
cstring str_ = *str;
69+
size_t idx_ = CSTRMIN(count, cstr_size(str_)),
70+
size_ = CSTRMAX(count, cstr_size(str_));
71+
72+
char ch_ = (count == size_) ? ch : (char)0;
73+
cstr_reserve(str, count);
74+
pvt_fill_(str_, idx_, size_ - idx_, ch_);
75+
}
76+
77+
78+
void __unused
79+
cstr_swap(cstring *str, cstring *other)
80+
{
81+
cstring str1_ = *str, str2_ = *other;
82+
const size_t size1_ = cstr_size(str1_),
83+
size2_ = cstr_size(str2_);
84+
cstr_reserve(&str1_, size2_);
85+
cstr_reserve(&str2_, size1_);
86+
87+
size_t max_ = CSTRMAX(size1_, size2_);
88+
for(size_t i = 0; i < max_; ++i) {
89+
char tmp = str1_[i];
90+
str1_[i] = str2_[i];
91+
str2_[i] = tmp;
92+
}
93+
94+
pvt_set_total_size_(str1_, size2_);
95+
pvt_set_total_size_(str2_, size1_);
96+
97+
*str = str1_;
98+
*other = str2_;
99+
}
100+
101+
/**
102+
* Operations
103+
* -----------
104+
*/
105+
106+
cstring __unused
107+
cstr_substr(const_cstring str, const size_t pos, const size_t count)
108+
{
109+
pvt_cstr_assert(pos < cstr_size(str), "Index out of Bounds");
110+
cstring substr_ = NULL;
111+
size_t count_ = CSTRMIN(count, cstr_size(str) - pos);
112+
113+
pvt_buf_grow_(substr_, count_);
114+
pvt_copy_(substr_, str + pos, count_);
115+
return substr_;
116+
}
117+
118+
119+
// =========================================================
120+
// PRIVATE FUNCTIONS
121+
// ========================================================
122+
123+
/*
124+
* Functions to assign
125+
* -------------------
7126
*/
8127

9128
cstring __unused
@@ -36,12 +155,11 @@ intl_assign_cnt_ch(cstring *str, const size_t count, const char ch)
36155

37156

38157
/*
39-
* functions to multiple args Constructors
40-
******************************************
158+
* Functions to multiple args Constructors
159+
* ---------------------------------------
41160
*/
42161

43-
CSTR_NODISCARD
44-
__attribute__((malloc))
162+
CSTR_NODISCARD __attribute__((malloc))
45163
cstring __unused
46164
intl_init_cpy_ch(const size_t cnt, const char ch)
47165
{
@@ -55,8 +173,7 @@ intl_init_cpy_ch(const size_t cnt, const char ch)
55173
return str_;
56174
}
57175

58-
CSTR_NODISCARD
59-
__attribute__((malloc))
176+
CSTR_NODISCARD __attribute__((malloc))
60177
cstring __unused
61178
intl_copy_constructor(const_cstring other)
62179
{
@@ -68,8 +185,7 @@ intl_copy_constructor(const_cstring other)
68185
return str_;
69186
}
70187

71-
CSTR_NODISCARD
72-
__attribute__((malloc))
188+
CSTR_NODISCARD __attribute__((malloc))
73189
cstring __unused
74190
intl_init_cpy_str_w_iter(cstr_iterator begin,
75191
cstr_iterator end)
@@ -85,8 +201,7 @@ intl_init_cpy_str_w_iter(cstr_iterator begin,
85201
return str_;
86202
}
87203

88-
CSTR_NODISCARD
89-
__attribute__((malloc))
204+
CSTR_NODISCARD __attribute__((malloc))
90205
cstring __unused
91206
intl_init_cpy_str_w_off(const_cstring other,
92207
const size_t start, const size_t offset)
@@ -103,40 +218,37 @@ intl_init_cpy_str_w_off(const_cstring other,
103218
}
104219

105220

106-
/*********************************
107-
* ---- Modifiers ----
108-
*/
109-
110221
/*
111-
* functions to erase
112-
*********************
222+
* Functions to erase
223+
* -------------------
113224
*/
114225

115226
cstr_iterator __unused
116-
intl_erase_range_iter(cstring *str, cstr_iterator first,
117-
cstr_iterator last)
227+
intl_erase_range_citer(cstring *str, cstr_const_iterator first,
228+
cstr_const_iterator last)
118229
{
119230
cstring str_ = *str;
120-
if((first.it < cstr_begin(str_).it) ||
121-
(last.it > cstr_end(str_).it) ||
231+
if((first.it < cstr_cbegin(str_).it) ||
232+
(last.it > cstr_cend(str_).it) ||
122233
(first.it > last.it)) {
123234
return cstr_end(str_);
124235
}
125236

126-
pvt_cstr_memmove(first.it, last.it,
237+
pvt_cstr_memmove((cstring)first.it, (cstring)last.it,
127238
cstr_distance(last, cstr_end(str_)));
128239

129240
size_t size_ = cstr_size(str_) - cstr_distance(first, last);
241+
130242
str_[size_] = 0;
131243
pvt_set_total_size_(str_, size_);
132-
return last;
244+
return (cstr_iterator){ .it = (cstring)last.it };
133245
}
134246

135247

136248

137249
/*
138-
* functions to insert with multiple args
139-
*****************************************
250+
* Functions to insert
251+
* --------------------
140252
*/
141253

142254
cstring __unused
@@ -159,24 +271,90 @@ intl_insert_cnt_ch(cstring *str, const size_t index,
159271
}
160272

161273
cstring __unused
162-
intl_insert_cnt_str(cstring *str, const size_t index,
274+
intl_insert_str_range(cstring *str, const size_t index,
163275
const size_t count, const_cstring other)
164276
{
165277
cstring str_ = *str;
166278
size_t size_ = cstr_size(str_),
167-
other_size_ = pvt_cstr_strlen(other),
168-
count_ = count * other_size_;
279+
count_ = CSTRMIN(count, pvt_cstr_strlen(other));
169280
pvt_cstr_assert(index <= size_,
170281
"index for insert is out of bounds");
171-
282+
172283
cstr_reserve(str, size_ + count_);
173-
pvt_cstr_memmove(&str_[count_], &str_[index], size_ - index);
174-
175-
for(size_t i = 0; i < count; ++i)
176-
pvt_cstr_memmove(&str_[index + (i * other_size_)],
177-
other, other_size_);
178-
284+
pvt_cstr_memmove(&str_[index + count_], &str_[index],
285+
size_ - index + 1);
286+
287+
pvt_cstr_memmove(&str_[index], other, count_);
288+
179289
pvt_set_total_size_(str_, size_ + count_);
180290
str_[size_ + count_] = (char)0;
181291
return str_;
182292
}
293+
294+
cstr_iterator __unused
295+
intl_insert_iter_cnt_ch(cstring *str, cstr_iterator pos,
296+
const size_t count, const char ch)
297+
{
298+
cstring str_ = *str;
299+
cstr_iterator end = cstr_end(str_);
300+
pvt_cstr_assert(pos.it <= end.it,
301+
"Iterators are out of range [first, last)");
302+
303+
size_t size_ = cstr_size(str_),
304+
count_ = cstr_distance(pos, end);
305+
306+
cstr_reserve(&str_, size_ + count);
307+
pvt_cstr_memmove(pos.it + count, pos.it, count_);
308+
pvt_fill_(str_, size_ - count_, count, ch);
309+
310+
pvt_set_total_size_(str_, size_ + count);
311+
str_[size_ + count] = (char)0;
312+
*str = str_;
313+
return cstr_begin(str_ + (size_ - count_));
314+
}
315+
316+
317+
/**
318+
* Functions to replace
319+
* ---------------------
320+
*/
321+
322+
cstring __unused
323+
intl_replace_it_fill_ch(cstring *str, cstr_const_iterator first,
324+
cstr_const_iterator last, const size_t count2,
325+
const char ch)
326+
{
327+
cstring str_ = *str;
328+
if((first.it < cstr_cbegin(str_).it) ||
329+
(last.it > cstr_cend(str_).it) ||
330+
(first.it > last.it)) {
331+
return str_;
332+
}
333+
334+
size_t count_ = CSTRMIN(count2, cstr_distance(first, last));
335+
336+
cstring ptr = (cstring)first.it;
337+
for(size_t i = 0; i < count_; ++i) ptr[i] = ch;
338+
return str_;
339+
}
340+
341+
cstring __unused
342+
intl_replace_it_str_off(cstring *str, cstr_const_iterator first,
343+
cstr_const_iterator last, const_cstring other,
344+
const size_t count2)
345+
{
346+
cstring str_ = *str;
347+
if((first.it < cstr_cbegin(str_).it) ||
348+
(last.it > cstr_cend(str_).it) ||
349+
(first.it > last.it)) {
350+
return str_;
351+
}
352+
353+
size_t count_ = CSTRMIN(cstr_distance(first, last),
354+
CSTRMIN(count2, pvt_cstr_strlen(other)));
355+
356+
cstring ptr = (cstring)first.it;
357+
for(size_t i = 0; i < count_; ++i) ptr[i] = other[i];
358+
359+
return str_;
360+
}

0 commit comments

Comments
 (0)