Repository navigation
Expand file tree
/
Copy pathStringView.c
More file actions
337 lines (287 loc) · 6.62 KB
/
Copy pathStringView.c
File metadata and controls
337 lines (287 loc) · 6.62 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
#include "StringView.h"
/* Construction */
inline StringView sv(const char *cstr)
{
return (StringView) {
.data = cstr,
.count = strlen(cstr)
};
}
inline char* sv_to_cstr( StringView sv )
{
char *str = (char *) malloc(sizeof(char) * (sv.count + 1));
if( str )
memcpy(str, sv.data, sv.count);
str[sv.count] = '\0';
return str;
}
/* Acces */
inline char sv_at( StringView sv, size_t index )
{
if( sv.count <= index )
return 0;
return sv.data[index];
}
inline char sv_front( StringView sv )
{
if( sv.count != 0 )
return sv.data[0];
return 0;
}
inline char sv_back( StringView sv )
{
if( sv.count != 0 )
return sv.data[sv.count-1];
return 0;
}
/* Chop */
inline void sv_chop_right(StringView *sv, size_t n)
{
if ( n > sv->count ) n = sv->count;
sv->count -= n;
}
inline void sv_chop_left(StringView *sv, size_t n)
{
if ( n > sv->count ) n = sv->count;
sv->count -= n;
sv->data += n;
}
StringView sv_chop_by_delim( StringView *sv, char delim )
{
size_t i = 0;
while ( i < sv->count && sv->data[i] != delim )
i+= 1;
StringView result = {
.data = sv->data,
.count = i
};
sv_chop_left( sv, i < sv->count ? i + 1 : i );
return result;
}
StringView sv_chop_by_type( StringView *sv, int (*istype)(int c) )
{
size_t i = 0;
while ( i < sv->count && !istype(sv->data[i]) )
i+= 1;
StringView result = {
.data = sv->data,
.count = i
};
sv_chop_left( sv, i );
return result;
}
inline StringView sv_substr( StringView sv, size_t start, size_t count )
{
if( start >= sv.count )
start = sv.count;
if( count >= sv.count )
count = sv.count - start;
return (StringView) { .data = sv.data + start, .count = count };
}
inline StringView sv_prefix( StringView sv, size_t count )
{
if( sv.count < count )
count = sv.count;
return (StringView) { .data = sv.data, .count = count };
}
inline StringView sv_suffix( StringView sv, size_t count )
{
sv_chop_left( &sv, sv.count - count );
return sv;
}
/* Trim */
inline void sv_trim_left( StringView *sv )
{
while ( sv->count != 0 && isspace(sv->data[0]) )
sv_chop_left(sv, 1);
}
inline void sv_trim_right( StringView *sv )
{
while ( sv->count != 0 && isspace(sv->data[sv->count-1]) )
sv_chop_right(sv, 1);
}
inline void sv_trim( StringView *sv )
{
sv_trim_left(sv);
sv_trim_right(sv);
}
inline StringView sv_trimmed_left( StringView sv )
{
sv_trim_left( &sv );
return sv;
}
inline StringView sv_trimmed_right( StringView sv )
{
sv_trim_right( &sv );
return sv;
}
inline StringView sv_trimmed( StringView sv )
{
sv_trim_left( &sv );
sv_trim_right( &sv );
return sv;
}
/* Recherche */
inline size_t sv_find( StringView sv, char c )
{
char *ptr = memchr( (void *)sv.data, c, sv.count );
if( ptr )
return ptr-sv.data;
return SV_NOT_FOUND;
}
inline size_t sv_find_from( StringView sv, char c, size_t start )
{
char *ptr = memchr((void*)sv.data+start, c, sv.count );
if( ptr )
return ptr-sv.data;
return SV_NOT_FOUND;
}
size_t sv_find_sv( StringView sv, StringView needle )
{
if( needle.count > sv.count )
return SV_NOT_FOUND;
for( size_t i = 0, j = 0; i < sv.count; ++i, j = 0 ) {
while( i+j < sv.count && j < needle.count && sv.data[i] == needle.data[j] ) {
j++;
}
if( j == needle.count )
return i;
}
return SV_NOT_FOUND;
}
size_t sv_find_sv_from( StringView sv, StringView needle, size_t start )
{
if( needle.count > sv.count )
return SV_NOT_FOUND;
for( size_t i = start, j = 0; i < sv.count; ++i, j = 0 ) {
while( i+j < sv.count && j < needle.count && sv.data[i] == needle.data[j] ) {
j++;
}
if( j == needle.count )
return i;
}
return SV_NOT_FOUND;
}
inline size_t sv_rfind( StringView sv, char c )
{
for( size_t i = sv.count - 1; true; --i ) {
if( sv.data[i] == c ) return i;
if( i == 0 ) {
return SV_NOT_FOUND;
}
}
}
/* Comparison */
inline bool sv_equal( StringView sv1, StringView sv2 )
{
return ! (sv1.count == sv2.count &&
memcmp(sv1.data, sv2.data, sv1.count));
}
inline bool sv_equal_nocase( StringView sv1, StringView sv2 )
{
if( sv1.count != sv2.count ) return false;
for( size_t i = 0; i < sv1.count; ++i )
if( tolower(sv1.data[i]) != tolower(sv2.data[i]) )
return false;
return true;
}
int sv_compare( StringView sv1, StringView sv2 )
{
int min = ( sv1.count >= sv2.count ) ? sv2.count : sv1.count;
int diff = memcmp(sv1.data, sv2.data, min);
if( diff ) return diff;
if( sv1.count < sv2.count ) return -sv2.data[min];
if( sv2.count < sv1.count ) return sv1.data[min];
return 0;
}
int sv_compare_nocase( StringView sv1, StringView sv2 )
{
int min = ( sv1.count >= sv2.count ) ? sv2.count : sv1.count;
char val1, val2;
for( size_t i = 0; i < min; i++ ) {
val1 = tolower( sv1.data[1] );
val2 = tolower( sv2.data[1] );
if( val1 != val2 ) return val1 - val2;
}
if( sv1.count < sv2.count ) return -sv2.data[min];
if( sv2.count < sv1.count ) return sv1.data[min];
return 0;
}
/* Classification */
inline bool sv_all( StringView sv, int (*predicate)(int) )
{
for( size_t i = 0; i < sv.count; ++i )
if( !predicate(sv.data[i]) ) return false;
return true;
}
inline bool sv_any( StringView sv, int (*predicate)(int) )
{
for( size_t i = 0; i < sv.count; ++i )
if( predicate(sv.data[i]) ) return true;
return false;
}
inline bool sv_none( StringView sv, int (*predicate)(int) )
{
return !sv_any( sv, predicate );
}
/* Parsing numérique */
bool sv_to_int( StringView sv, int *out )
{
int sign = 1;
size_t i = 0;
*out = 0;
StringView res = sv_chop_by_type( &sv, isalpha );
if( sv.count != 0 ) return false;
sv_trim( &res );
if( i < res.count && res.data[i] == '-' ) {
sign = -1;
res.data++;
} else if( i < res.count && res.data[i] == '+' ) {
res.data++;
}
sv_trim_left( &res );
while( i < res.count && isdigit(res.data[i]) ) {
int next = *out *10 + (res.data[i] - '0')*sign;
if( (*out > 0 && next > 0) || *out == 0 || (*out < 0 && next < 0))
*out = next;
else {
*out = 0;
return false;
}
i++;
}
if( i == 0 ) return false;
return true;
}
bool sv_to_uint( StringView sv, unsigned int *out )
{
size_t i = 0;
*out = 0;
StringView res = sv_chop_by_type( &sv, isalpha );
if( sv.count != 0 ) return false;
sv_trim( &res );
if( i < res.count && res.data[i] == '-' ) {
return false;
} else if( i < res.count && res.data[i] == '+' ) {
res.data++;
}
sv_trim_left( &res );
while( i < res.count && isdigit(res.data[i]) ) {
unsigned int next = *out *10 + (res.data[i] - '0');
if( next < *out ) {
*out = 0;
return false;
}
*out = next;
i++;
}
if( i == 0 ) return false;
return true;
}