-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathstrview.c
571 lines (464 loc) · 14.2 KB
/
strview.c
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
*/
#include <limits.h>
#include <ctype.h>
#include "strview.h"
//********************************************************************************************************
// Local defines
//********************************************************************************************************
// #include <stdio.h>
// #define DBG(_fmtarg, ...) printf("%s:%.4i - "_fmtarg"\n" , __FILE__, __LINE__ ,##__VA_ARGS__)
//********************************************************************************************************
// Private prototypes
//********************************************************************************************************
static bool contains_char(strview_t str, char c);
static strview_t split_first_delim(strview_t* strview_ptr, strview_t delims, bool exclude_quotes);
static strview_t split_last_delim(strview_t* strview_ptr, strview_t delims, bool exclude_quotes);
static strview_t split_index(strview_t* strview_ptr, int index);
static strview_t find_first(strview_t haystack, strview_t needle, int(*comp_func)(const void*,const void*, size_t));
static strview_t find_last(strview_t haystack, strview_t needle, int(*comp_func)(const void*,const void*, size_t));
static int memcmp_nocase(const void* a, const void* b, size_t size);
//********************************************************************************************************
// Public functions
//********************************************************************************************************
strview_t cstr(const char* c_str)
{
return c_str ? (strview_t){.data = c_str, .size = strlen(c_str)} : STRVIEW_INVALID;
}
char* strview_to_cstr(char* dst, size_t dst_size, strview_t str)
{
size_t copy_size;
size_t src_size = str.size < 0 ? 0:str.size;
if(dst_size && dst)
{
if(str.size < 0)
str.size = 0;
copy_size = (dst_size-1) < src_size ? (dst_size-1):src_size;
memcpy(dst, str.data, copy_size);
dst[copy_size] = 0;
};
return dst;
}
bool strview_is_valid(strview_t str)
{
return !!str.data;
}
void strview_swap(strview_t* a, strview_t* b)
{
strview_t tmp = *a;
*a = *b;
*b = tmp;
}
bool strview_is_match_strview(strview_t str1, strview_t str2)
{
return (str1.size == str2.size) && (str1.data == str2.data || !memcmp(str1.data, str2.data, str1.size));
}
bool strview_is_match_cstr(strview_t str1, const char* str2)
{
return strview_is_match_strview(str1, cstr(str2));
}
bool strview_is_match_nocase_strview(strview_t str1, strview_t str2)
{
return (str1.size == str2.size) && (str1.data == str2.data || !memcmp_nocase(str1.data, str2.data, str1.size));
}
bool strview_is_match_nocase_cstr(strview_t str1, const char* str2)
{
return strview_is_match_nocase_strview(str1, cstr(str2));
}
bool strview_starts_with_strview(strview_t str1, strview_t str2)
{
bool result;
if(!strview_is_valid(str2))
result = !strview_is_valid(str1);
else
result = (str1.size >= str2.size) && (str1.data == str2.data || !memcmp(str1.data, str2.data, str2.size));
return result;
}
bool strview_starts_with_cstr(strview_t str1, const char* str2)
{
return strview_starts_with_strview(str1, cstr(str2));
}
bool strview_starts_with_nocase_strview(strview_t str1, strview_t str2)
{
bool result;
if(!strview_is_valid(str2))
result = !strview_is_valid(str1);
else
result = (str1.size >= str2.size) && (str1.data == str2.data || !memcmp_nocase(str1.data, str2.data, str2.size));
return result;
}
bool strview_starts_with_nocase_cstr(strview_t str1, const char* str2)
{
return strview_starts_with_nocase_strview(str1, cstr(str2));
}
int strview_compare(strview_t str1, strview_t str2)
{
int compare_size = str1.size < str2.size ? str1.size:str2.size;
int result = 0;
if(compare_size)
result = memcmp(str1.data, str2.data, compare_size);
if(!result && str1.size != str2.size)
result = str1.size > str2.size ? +1:-1;
return result;
}
bool strview_contains(strview_t haystack, strview_t needle)
{
return strview_is_valid(strview_find_first(haystack, needle));
}
bool strview_contains_nocase(strview_t haystack, strview_t needle)
{
return strview_is_valid(strview_find_first_nocase(haystack, needle));
}
strview_t strview_sub(strview_t str, int begin, int end)
{
strview_t result = (strview_t){.size = 0, .data = str.data};
if(str.data && str.size)
{
if(begin < 0)
begin = str.size + begin;
if(end < 0)
end = str.size + end;
if(begin <= end && begin < str.size && end >= 0)
{
if(begin < 0)
begin = 0;
if(end > str.size)
end = str.size;
result.size = end-begin;
result.data = &str.data[begin];
}
else
result.data = NULL;
};
return result;
}
strview_t strview_trim_start_cstr(strview_t str, const char* chars_to_trim)
{
return strview_trim_start_strview(str, cstr(chars_to_trim));
}
strview_t strview_trim_start_strview(strview_t str, strview_t chars_to_trim)
{
while(str.size && contains_char(chars_to_trim, *str.data))
{
str.data++;
str.size--;
};
return str;
}
strview_t strview_trim_end_cstr(strview_t str, const char* chars_to_trim)
{
return strview_trim_end_strview(str, cstr(chars_to_trim));
}
strview_t strview_trim_end_strview(strview_t str, strview_t chars_to_trim)
{
while(str.size && contains_char(chars_to_trim, str.data[str.size-1]))
str.size--;
return str;
}
strview_t strview_trim_cstr(strview_t str, const char* chars_to_trim)
{
return strview_trim_strview(str, cstr(chars_to_trim));
}
strview_t strview_trim_strview(strview_t str, strview_t chars_to_trim)
{
str = strview_trim_start_strview(str, chars_to_trim);
str = strview_trim_end_strview(str, chars_to_trim);
return str;
}
strview_t strview_find_first_strview(strview_t haystack, strview_t needle)
{
return find_first(haystack, needle, memcmp);
}
strview_t strview_find_first_nocase_strview(strview_t haystack, strview_t needle)
{
return find_first(haystack, needle, memcmp_nocase);
}
strview_t strview_find_first_nocase_cstr(strview_t haystack, const char* needle)
{
return find_first(haystack, cstr(needle), memcmp_nocase);
}
strview_t strview_find_first_cstr(strview_t haystack, const char* needle)
{
return strview_find_first_strview(haystack, cstr(needle));
}
strview_t strview_find_last_strview(strview_t haystack, strview_t needle)
{
return find_last(haystack, needle, memcmp);
}
strview_t strview_find_last_nocase_strview(strview_t haystack, strview_t needle)
{
return find_last(haystack, needle, memcmp_nocase);
}
strview_t strview_find_last_nocase_cstr(strview_t haystack, const char *needle)
{
return find_last(haystack, cstr(needle), memcmp_nocase);
}
strview_t strview_find_last_cstr(strview_t haystack, const char* needle)
{
return strview_find_last_strview(haystack, cstr(needle));
}
strview_t strview_split_first_delim(strview_t* strview_ptr, const char* delims, bool exclude_quotes)
{
strview_t result = STRVIEW_INVALID;
if(strview_ptr)
result = split_first_delim(strview_ptr, cstr(delims), exclude_quotes);
return result;
}
int strview_split_all(int dst_size, strview_t dst[dst_size], strview_t src, const char* delims, bool exclude_quotes)
{
int count = 0;
while(strview_is_valid(src) && count < dst_size)
dst[count++] = strview_split_first_delim(&src, delims, exclude_quotes);
return count;
}
strview_t strview_split_last_delim(strview_t* strview_ptr, const char* delims, bool exclude_quotes)
{
strview_t result = STRVIEW_INVALID;
if(strview_ptr)
result = split_last_delim(strview_ptr, cstr(delims), exclude_quotes);
return result;
}
strview_t strview_split_index(strview_t* strview_ptr, int index)
{
strview_t result = STRVIEW_INVALID;
if(strview_ptr)
result = split_index(strview_ptr, index);
return result;
}
char strview_pop_first_char(strview_t* strview_ptr)
{
char result = 0;
if(strview_ptr && strview_ptr->size)
result = split_index(strview_ptr, 1).data[0];
return result;
}
strview_t strview_split_line(strview_t* strview_ptr, char* eol)
{
strview_t result = STRVIEW_INVALID;
strview_t src;
char e = 0;
if(strview_ptr && strview_ptr->size)
{
src = *strview_ptr;
if(eol && *eol)
{
if(*eol + src.data[0] == '\r'+'\n')
strview_pop_first_char(&src);
};
result = strview_split_first_delim(&src, "\r\n", false);
if(strview_is_valid(src)) //a line ending was found
{
e = result.data[result.size];
if(e + src.data[0] == '\r'+'\n')
{
strview_pop_first_char(&src);
e = 0;
};
if(eol)
*eol = e;
*strview_ptr = src;
}
else //a line ending was not found, restore the source, and return an invalid strview_t
{
*strview_ptr = result;
result = STRVIEW_INVALID;
};
};
return result;
}
strview_t strview_split_left(strview_t* strview_ptr, strview_t pos)
{
strview_t result = STRVIEW_INVALID;
if(strview_ptr && strview_is_valid(*strview_ptr) && strview_is_valid(pos))
{
if(strview_ptr->data <= pos.data && pos.data <= &strview_ptr->data[strview_ptr->size])
result = split_index(strview_ptr, (int)(pos.data - strview_ptr->data));
};
return result;
}
strview_t strview_split_right(strview_t* strview_ptr, strview_t pos)
{
strview_t result = STRVIEW_INVALID;
strview_t src;
const char* split_point;
if(strview_ptr && strview_is_valid(*strview_ptr) && strview_is_valid(pos))
{
src = *strview_ptr;
split_point = &pos.data[pos.size];
if(src.data <= split_point && split_point <= &src.data[src.size])
{
result = split_index(&src, (int)(split_point - src.data));
strview_swap(&result, &src);
};
*strview_ptr = src;
};
return result;
}
//********************************************************************************************************
// Private functions
//********************************************************************************************************
static bool contains_char(strview_t str, char c)
{
bool found = false;
const char* ptr = str.data;
while(!found && ptr != &str.data[str.size])
{
found = *ptr == c;
ptr++;
};
return found;
}
static int memcmp_nocase(const void* a, const void* b, size_t size)
{
int result = 0;
const unsigned char *achar = a;
const unsigned char *bchar = b;
while(size-- && !result)
result = toupper(*achar++) - toupper(*bchar++);
return result;
}
static strview_t split_first_delim(strview_t* strview_ptr, strview_t delims, bool exclude_quotes)
{
strview_t result;
bool found = false;
const char* ptr;
bool in_quotes = false;
ptr = strview_ptr->data;
if(strview_ptr->data && delims.data)
{
// try to find the delim
while(ptr != &strview_ptr->data[strview_ptr->size] && !found)
{
in_quotes ^= (*ptr == '\"') && exclude_quotes;
found = !in_quotes && contains_char(delims, *ptr);
ptr += !found;
};
};
if(found)
{
result.data = strview_ptr->data;
result.size = ptr - strview_ptr->data;
strview_ptr->data = ptr;
strview_ptr->size -= result.size;
// at this stage, the remainder still includes the delim
strview_ptr->size--;
if(strview_ptr->size) //only point to the character after the delim if there is one
strview_ptr->data++;
}
else
{
result = *strview_ptr;
*strview_ptr = STRVIEW_INVALID;
};
return result;
}
static strview_t split_last_delim(strview_t* strview_ptr, strview_t delims, bool exclude_quotes)
{
strview_t result;
bool found = false;
const char* ptr;
bool in_quotes = false;
if(strview_ptr->data && strview_ptr->size && delims.data)
{
// starting from the last character, try to find the delim backwards
ptr = &strview_ptr->data[strview_ptr->size-1];
while(ptr != strview_ptr->data-1 && !found)
{
in_quotes ^= (*ptr == '\"') && exclude_quotes;
found = !in_quotes && contains_char(delims, *ptr);
ptr -= !found;
};
};
if(found)
{
result.data = ptr;
result.size = &strview_ptr->data[strview_ptr->size] - ptr;
strview_ptr->size -= result.size;
// at this stage, the result still starts with the delim
result.size--;
if(result.size)
result.data++; //only point to the the character after the delim if there is one
}
else
{
result = *strview_ptr;
*strview_ptr = STRVIEW_INVALID;
};
return result;
}
static strview_t split_index(strview_t* strview_ptr, int index)
{
strview_t result = STRVIEW_INVALID;
strview_t remainder = *strview_ptr;
bool neg = index < 0;
if(neg)
index = strview_ptr->size + index;
if(index < 0)
index = 0;
if(index > strview_ptr->size)
index = strview_ptr->size;
result.data = remainder.data;
result.size = index;
remainder.data += index;
remainder.size -= index;
if(!neg)
*strview_ptr = remainder;
else
{
*strview_ptr = result;
result = remainder;
};
return result;
}
strview_t strview_dequote(strview_t src)
{
strview_t result = src;
char quote_char;
if(result.size > 1)
quote_char = result.data[0];
while( (result.size > 1)
&& (result.data[0] == quote_char)
&& (result.data[result.size-1] == quote_char) )
result = strview_sub(result, 1, -1);
return result;
}
static strview_t find_first(strview_t haystack, strview_t needle, int(*comp_func)(const void*,const void*, size_t))
{
strview_t result = STRVIEW_INVALID;
const char* remaining_hay = haystack.data;
bool found = false;
if(haystack.data && needle.data)
{
while((&haystack.data[haystack.size] - remaining_hay >= needle.size) && !found)
{
found = !comp_func(remaining_hay, needle.data, needle.size);
remaining_hay += !found;
};
};
if(found)
{
result.data = remaining_hay;
result.size = needle.size;
};
return result;
}
static strview_t find_last(strview_t haystack, strview_t needle, int(*comp_func)(const void*,const void*, size_t))
{
strview_t result = STRVIEW_INVALID;
const char* remaining_hay = &haystack.data[haystack.size - needle.size];
bool found = false;
if(haystack.data && needle.data)
{
while((remaining_hay >= haystack.data) && !found)
{
found = !comp_func(remaining_hay, needle.data, needle.size);
remaining_hay -= !found;
};
};
if(found)
{
result.data = remaining_hay;
result.size = needle.size;
};
return result;
}