-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstringlib.c
587 lines (459 loc) · 12 KB
/
stringlib.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*******************************************************************************
*** Author: Tyler Barrus
*** email: [email protected]
*** License: MIT 2019
*******************************************************************************/
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "stringlib.h"
void s_free_array_of_strings(char** a, int num) {
int i;
for (i = 0; i < num; ++i) {
free(a[i]);
a[i] = NULL;
}
free(a);
}
char* s_duplicate(const char* s) {
if (s == NULL)
return NULL;
size_t len = strlen(s); /* ensure room for NULL terminated */
char* buf = (char*)malloc((len + 1) * sizeof(char));
if (buf == NULL)
return NULL;
strcpy(buf, s);
buf[len] = '\0';
return buf;
}
char* s_reverse(char* s) {
if (s == NULL)
return NULL;
int i, j;
for (i = 0, j = strlen(s) - 1; i < j; ++i, --j) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
return s;
}
size_t s_trim(char* s) {
if (s == NULL || s[0] == '\0')
return 0;
size_t res = 0, j = 0, length = strlen(s);
/* remove trailing spaces */
while (length > 0 && isspace(s[length - 1]) != 0) {
s[--length] = '\0';
++res;
}
/* count the number of leading spaces */
while (isspace(s[j]) != 0) ++j;
/* set our return value */
res += j;
/* now we need to move things */
if (j > 0) {
size_t i = 0;
while (j <= length) s[i++] = s[j++];
}
return res;
}
char* s_standardize_whitespace(char* s, const char c) {
if (s == NULL)
return NULL;
size_t i = 0;
while (s[i] != '\0') {
if (isspace(s[i]))
s[i] = c;
++i;
}
return s;
}
char* s_snprintf(const char* fmt, ...) {
if (fmt == NULL)
return NULL;
va_list args;
va_start(args, fmt);
size_t len = vsnprintf(NULL, 0, fmt, args);
va_end(args);
char* buf = (char*)malloc((len + 1) * sizeof(char));
if (buf == NULL)
return NULL; /* must be an error state */
va_start(args, fmt);
vsnprintf(buf, len + 1, fmt, args);
va_end(args);
return buf;
}
char* s_remove_unwanted_chars(char* s, const char* unwanted) {
if (s == NULL || unwanted == NULL)
return s;
size_t i = 0, j = 0, unwanted_len = strlen(unwanted);
while (s[j] != '\0') {
int changes = 0;
size_t q;
for (q = 0; q < unwanted_len; ++q) {
if (s[j] == unwanted[q]) {
changes = 1;
++j;
break;
}
}
s[i] = s[j];
if (changes == 0) {
++i;
++j;
}
}
s[i] = '\0';
return s;
}
char* s_replace_unwanted_chars(char* s, const char* unwanted, const char c) {
if (s == NULL || unwanted == NULL)
return s;
size_t i = 0, j = 0, unwanted_len = strlen(unwanted);
while (s[j] != '\0') {
size_t q;
for (q = 0; q < unwanted_len; ++q) {
if (s[j] == unwanted[q]) {
s[j] = c;
break;
}
}
++i;
++j;
}
return s;
}
char* s_toupper(char* s) {
if (s == NULL || s[0] == '\0')
return s;
size_t i;
for (i = 0; s[i] != '\0'; ++i)
s[i] = toupper(s[i]);
return s;
}
char* s_tolower(char* s) {
if (s == NULL || s[0] == '\0')
return s;
size_t i;
for (i = 0; s[i] != '\0'; ++i)
s[i] = tolower(s[i]);
return s;
}
int s_find(const char* s, const char c) {
if (s == NULL)
return -1;
char* loc = strchr((char*)s, c);
if (loc == NULL)
return -1;
return loc - s;
}
int s_find_reverse(const char* s, const char c) {
if (s == NULL)
return -1;
char* loc = strrchr((char*)s, c);
if (loc == NULL)
return -1;
return loc - s;
}
int s_find_cnt(const char* s, const char c) {
if (s == NULL)
return -1;
int res = 0;
char* loc = strchr((char*)s, c);
while (loc != NULL) {
++res;
loc = strchr(loc + 1, c);
}
return res;
}
int s_find_alt(const char*s, const char c, int idx) {
if (s == NULL)
return -1;
int i = 0;
char* loc = strchr((char*)s, c);
while (loc != NULL) {
if (++i == idx)
break;
loc = strchr(loc + 1, c);
}
if (loc == NULL)
return -1;
return loc - s;
}
int s_find_str(const char* s, const char* sub) {
if (s == NULL || sub == NULL)
return -1;
char* loc = strstr((char*)s, sub);
if (loc == NULL)
return -1;
return loc - s;
}
char* s_strrstr(const char* s, const char* sub) {
if (s == NULL || sub == NULL)
return NULL;
int len = strlen(sub);
char* res = NULL;
char* loc = strstr((char*)s, sub);
while (loc != NULL) {
res = loc;
loc = strstr(loc + len, sub);
}
return res;
}
int s_find_str_reverse(const char* s, const char* sub) {
if (s == NULL || sub == NULL)
return -1;
char* loc = s_strrstr(s, sub);
if (loc == NULL)
return -1;
return loc - s;
}
int s_find_cnt_str(const char* s, const char* sub) {
if (s == NULL || sub == NULL)
return 0;
int res = 0;
int len = strlen(sub);
char* loc = strstr((char*)s, sub);
while (loc != NULL) {
++res;
loc = strstr(loc + len, sub);
}
return res;
}
int s_find_alt_str(const char*s, const char* sub, int idx) {
if (s == NULL || sub == NULL)
return -1;
int i = 0;
int len = strlen(sub);
char* loc = strstr((char*)s, sub);
while (loc != NULL) {
if (++i == idx)
break;
loc = strstr(loc + len, sub);
}
if (loc == NULL)
return -1;
return loc - s;
}
int s_find_any(const char* s, const char* s2) {
if (s == NULL || s2 == NULL)
return -1;
char* loc = strpbrk((char*)s, s2);
if (loc == NULL)
return -1;
return loc - s;
}
int s_find_any_reverse(const char* s, const char* s2) {
if (s == NULL || s2 == NULL)
return -1;
char* res = NULL;
char* loc = strpbrk((char*)s, s2);
if (loc == NULL) /* quick exit */
return -1;
while (loc != NULL) {
res = loc; /* it matches a single element... */
loc = strpbrk(loc + 1, s2);
}
return res - s;
}
int s_find_cnt_any(const char* s, const char* s2) {
if (s == NULL || s2 == NULL)
return 0;
int res = 0;
char* loc = strpbrk((char*)s, s2);
while (loc != NULL) {
++res;
loc = strpbrk(loc + 1, s2);
}
return res;
}
int s_find_alt_any(const char*s, const char* s2, int idx) {
if (s == NULL || s2 == NULL)
return -1;
int i = 0;
char* loc = strpbrk((char*)s, s2);
while (loc != NULL) {
if (++i == idx)
break;
loc = strpbrk(loc + 1, s2);
}
if (loc == NULL)
return -1;
return loc - s;
}
char* s_append(char* s1, const char* s2) {
s1 = s_append_alt(&s1, s2);
return s1;
}
char* s_append_alt(char* (*s1), const char* s2) {
if (s2 == NULL)
return (*s1);
char* res;
if ((*s1) == NULL)
res = (char*)calloc(strlen(s2) + 1, sizeof(char));
else
res = (char*)realloc(*s1, strlen(*s1) + strlen(s2) + 1);
strcat(res, s2);
/* set s1 pointer to the res pointer */
*s1 = res;
res = NULL;
return *s1;
}
char* s_concat(const char* s1, const char* s2) {
if (s1 == NULL || s2 == NULL)
return NULL;
char* ret = s_duplicate(s1);
return s_append(ret, s2);
}
int s_cmp(const char* s1, const char* s2) {
return s_cmp_alt(s1, s2, CASE_SENSITIVE); /* we want it case sensitive */
}
int s_cmp_alt(const char* s1, const char* s2, int casesensitive) {
if (s1 == s2) // then they point to the same thing!
return 0;
else if (s1 == NULL) // give it some behavior
return -1;
else if (s2 == NULL)
return 1;
if (casesensitive) {
return strcmp(s1, s2);
}
char* t1 = s_duplicate(s1);
char* t2 = s_duplicate(s2);
int res = strcmp(s_tolower(t1), s_tolower(t2));
free(t1);
free(t2);
return res;
}
char* s_extract_substring(const char* s, size_t start, size_t length) {
if (s == NULL)
return NULL;
size_t len = strlen(s);
if (start >= len)
return NULL;
if (start + length > len)
return s_duplicate(s + start);
char* ret = (char*)calloc(length + 1, sizeof(char));
return strncpy(ret, s + start, length);
}
char* s_extract_substring_str(const char* s, const char* sub, size_t length) {
int start = s_find_str(s, sub);
if (start == -1)
return NULL;
return s_extract_substring(s, start, length);
}
char* s_extract_substring_c(const char* s, const char c, size_t length) {
int start = s_find(s, c);
if (start == -1)
return NULL;
return s_extract_substring(s, start, length);
}
char** s_split_string_c(const char* s, const char c, int* num) {
if (s == NULL || num == NULL)
return NULL;
int max_size = s_find_cnt(s, c);
char** results = (char**)calloc(max_size + 1, sizeof(char*)); /* will be cut down for empty lines... */
const char* loc = s;
int cnt = 0;
int len = s_find(loc, c);
while (len != -1) {
if (len == 0) {
len = s_find(++loc, c);
continue;
}
results[cnt++] = s_extract_substring(loc, 0, len);
loc += len + 1;
len = s_find(loc, c);
}
if (loc[0] != '\0')
results[cnt++] = s_duplicate(loc);
*num = cnt;
char** v = (char**)realloc(results, cnt * sizeof(char*));
if (v == NULL)
return results;
return v;
}
char** s_split_string_str(const char* s, const char* sub, int* num) {
if (s == NULL || sub == NULL || num == NULL)
return NULL;
int max_size = s_find_cnt_str(s, sub);
char** results = (char**)calloc(max_size + 1, sizeof(char*)); /* will be cut down for empty lines... */
int sub_len = strlen(sub);
const char* loc = s;
int cnt = 0;
int len = s_find_str(loc, sub);
while (len != -1) {
if (len == 0) {
loc += sub_len;
len = s_find_str(loc, sub);
continue;
}
results[cnt++] = s_extract_substring(loc, 0, len);
loc += len + sub_len;
len = s_find_str(loc, sub);
}
if (loc[0] != '\0')
results[cnt++] = s_duplicate(loc);
*num = cnt;
char** v = (char**)realloc(results, cnt * sizeof(char*));
if (v == NULL)
return results;
return v;
}
char** s_split_string_any(const char* s, const char* s2, int* num) {
if (s == NULL || num == NULL)
return NULL;
const char* find;
if (s2 == NULL)
find = " \n\r\f\v\t";
else
find = s2;
int max_size = s_find_cnt_any(s, find);
char** results = (char**)calloc(max_size + 1, sizeof(char*));
const char* loc = s;
int cnt = 0;
int len = s_find_any(loc, find);
while (len != -1) {
if (len == 0) {
len = s_find_any(++loc, find);
continue;
}
results[cnt++] = s_extract_substring(loc, 0, len);
loc += len + 1;
len = s_find_any(loc, find);
}
if (loc[0] != '\0')
results[cnt++] = s_duplicate(loc);
*num = cnt;
char** v = (char**)realloc(results, cnt * sizeof(char*));
if (v == NULL)
return results;
return v;
}
char** s_split_lines(const char* s, int* num) {
return s_split_string_any(s, "\n\r\f", num);
}
char* s_single_space(char* s) {
if (s == NULL || s[0] == '\0')
return s;
s_trim(s);
int i = 0, j = 0, found = 0;
while (s[j] != '\0') {
int space = isspace(s[j]);
if (space != 0 && found > 0) {
++found;
++j;
} else if (space != 0) {
++found;
s[i++] = ' ';
++j;
} else {
s[i++] = s[j++];
found = 0;
}
}
s[i] = '\0';
return s;
}