-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathctl_string.h
More file actions
286 lines (254 loc) · 7.12 KB
/
Copy pathctl_string.h
File metadata and controls
286 lines (254 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* A vector class
SPDX-License-Identifier: MIT */
#ifndef __CTL_STRING__H__
#define __CTL_STRING__H__
#ifdef T
#warning "Template type T defined for <./string.h>"
#undef T
#endif
// #ifdef INCLUDE_ALGORITHM
// #pragma message "string.h: INCLUDE_ALGORITHM"
// #else
// #pragma message "string.h: no INCLUDE_ALGORITHM"
// #endif
#define CTL_STR
#define HOLD
#define POD
#define T char
#define vec_char str
#define MUST_ALIGN_16(T) (sizeof(T) == sizeof(char))
#define str_init str___INIT
#define str_equal str___EQUAL
#define str_find str___FIND
#define str_begin str_it_begin
#define str_end str_it_end
#define str_ctl_foreach(self, it) \
if ((self)->size) \
for (char* it = &(self)->vector[0]; \
it < &(self)->vector[(self)->size]; it++)
#define str_ctl_foreach_range(it, first, last) \
if (last) \
for (char* it = first; it < last; it++)
#include <./ctl_vector.h>
#undef str_init
#undef str_equal
#undef str_find
#undef str_begin
#undef str_end
#undef vec_char
#include <stdint.h>
#include <string.h>
static inline char* str_begin(str* self) {
return &self->vector[0];
}
static inline char* str_end(str* self) {
return &self->vector[self->size];
}
// if we compare char by char, for algorithms like sort
static inline int str_char_compare(char* a, char* b) {
return *a < *b;
}
static inline int str_char_equal(char* a, char* b) {
return *a == *b;
}
static inline str str_init(const char* c_str) {
str self = str___INIT();
size_t len = strlen(c_str);
#ifndef _LIBCPP_STD_VER
size_t min = 15;
#else
size_t min = 22;
#endif
self.compare = str_char_compare;
self.equal = str_char_equal;
str_reserve(&self, len < min ? min : len);
for (const char* s = c_str; *s; s++)
str_push_back(&self, *s);
return self;
}
static inline void str_append(str* self, const char* s) {
size_t start = self->size;
size_t len = strlen(s);
str_resize(self, self->size + len, '\0');
for (size_t i = 0; i < len; i++)
self->vector[start + i] = s[i];
}
static inline void str_insert_str(str* self, size_t index, const char* s) {
size_t start = self->size;
size_t len = strlen(s);
str_resize(self, self->size + len, '\0');
self->size = start;
while (len != 0) {
len--;
str_insert_index(self, index, s[len]);
}
}
static inline void str_replace(str* self, size_t index, size_t size,
const char* s) {
size_t end = index + size;
if (end >= self->size)
end = self->size;
for (size_t i = index; i < end; i++)
str_erase_index(self, index);
str_insert_str(self, index, s);
}
static inline char* str_c_str(str* self) {
return str_data(self);
}
static inline size_t str_find(str* self, const char* s) {
char* c_str = self->vector;
char* found = strstr(c_str, s);
if (found)
return found - c_str;
return SIZE_MAX;
}
static inline int str_count(str* self, char c) {
size_t count = 0;
for (size_t i = 0; i < self->size; i++)
if (self->vector[i] == c)
count++;
return count;
}
static inline size_t str_rfind(str* self, const char* s) {
char* c_str = self->vector;
for (size_t i = self->size; i != SIZE_MAX; i--) {
char* found = strstr(&c_str[i], s);
if (found)
return found - c_str;
}
return SIZE_MAX;
}
// i.e. strcspn, but returning the first found match
static inline bool str_find_first_of_range(str_it* range1, str_it* range2) {
if (str_it_done(range1) || str_it_done(range2))
return false;
str* self = range1->container;
str* other = range2->container;
// temp. set \0 at range1->end and range2->end.
// this looks expensive, but glibc is much faster than naively as below.
// it splits range2 into 4 parallelizable tables.
char e1 = 0, e2 = 0;
size_t off;
if (range1->end != &self->vector[self->size]) {
e1 = *range1->end;
*range1->end = '\0';
}
if (UNLIKELY(range2->end != &other->vector[other->size])) {
e2 = *range2->end;
*range2->end = '\0';
}
off = strcspn(range1->ref, range2->ref);
if (e1)
*range1->end = e1;
if (UNLIKELY(e2))
*range2->end = e2;
size_t start1 = range1->ref - self->vector;
off += start1;
if (&self->vector[off] < range1->end) {
range1->ref = &self->vector[off];
return true;
} else {
str_it_set_done(range1);
return false;
}
}
// see algorithm.h for the range variant
static inline size_t str_find_first_of(str* self, const char* s) {
#if 1
size_t i = strcspn(self->vector, s);
return i >= self->size ? SIZE_MAX : i;
#else
for (size_t i = 0; i < self->size; i++)
for (const char* p = s; *p; p++)
if (self->vector[i] == *p)
return i;
return SIZE_MAX;
#endif
}
static inline size_t str_find_last_of(str* self, const char* s) {
for (size_t i = self->size; i != SIZE_MAX; i--)
for (const char* p = s; *p; p++)
if (self->vector[i] == *p)
return i;
return SIZE_MAX;
}
static inline size_t str_find_first_not_of(str* self, const char* s) {
for (size_t i = 0; i < self->size; i++) {
size_t count = 0;
for (const char* p = s; *p; p++)
if (self->vector[i] == *p)
count++;
if (count == 0)
return i;
}
return SIZE_MAX;
}
static inline size_t str_find_last_not_of(str* self, const char* s) {
for (size_t i = self->size - 1; i != SIZE_MAX; i--) {
size_t count = 0;
for (const char* p = s; *p; p++)
if (self->vector[i] == *p)
count++;
if (count == 0)
return i;
}
return SIZE_MAX;
}
static inline str str_substr(str* self, size_t index, size_t size) {
str substr = str_init("");
#ifndef _LIBCPP_STD_VER // gcc shrinks, llvm not
#endif
str_resize(&substr, size, '\0');
for (size_t i = 0; i < size; i++)
substr.vector[i] = self->vector[index + i];
return substr;
}
/* STL clash */
static inline int str_compare(str* self, const char* s) {
return strcmp(self->vector, s);
}
/* STL clash
static inline int
str_equal(str* self, const char* s)
{
return str_compare(self, other) == 0;
}
*/
static inline int str_key_compare(str* self, str* other) {
return strcmp(self->vector, other->vector);
}
static inline int str_equal(str* self, str* other) {
return strcmp(self->vector, other->vector) == 0;
}
#undef POD
#ifndef HOLD
#undef vec_char
#undef T
#else
#undef HOLD
#endif
#undef CTL_STR
#else
// someone requested string.h without ALGORITHM before. but now we need it
// #if defined INCLUDE_ALGORITHM && !defined CTL_ALGORITHM
// #pragma message "2nd string.h: want INCLUDE_ALGORITHM"
// #define CTL_VEC
// #define CTL_STR
// #define POD
// #define T char
// #define vec_char str
// #define MUST_ALIGN_16(T) 1
// #define INIT_SIZE 15
// #define A str
// #define I JOIN(A, it)
// #define GI JOIN(A, it)
//
// #include <./algorithm.h>
//
// #undef POD
// #undef vec_char
// #undef T
// #undef CTL_STR
// #undef CTL_VEC
// #endif
#endif // once