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
|
#include "spm.h"
/**
* Determine how many times the character `ch` appears in `sptr` string
* @param sptr string to scan
* @param ch character to find
* @return count of characters found
*/
int num_chars(const char *sptr, int ch) {
int result = 0;
for (int i = 0; sptr[i] != '\0'; i++) {
if (sptr[i] == ch) {
result++;
}
}
return result;
}
/**
* Scan for `pattern` string at the beginning of `sptr`
*
* @param sptr string to scan
* @param pattern string to search for
* @return 0 = success, -1 = failure
*/
int startswith(const char *sptr, const char *pattern) {
for (size_t i = 0; i < strlen(pattern); i++) {
if (sptr[i] != pattern[i]) {
return -1;
}
}
return 0;
}
/**
* Scan for `pattern` string at the end of `sptr`
*
* @param sptr string to scan
* @param pattern string to search for
* @return 0 = success, -1 = failure
*/
int endswith(const char *sptr, const char *pattern) {
size_t sptr_size = strlen(sptr);
size_t pattern_size = strlen(pattern);
for (size_t s = sptr_size - pattern_size, p = 0 ; s < sptr_size; s++, p++) {
if (sptr[s] != pattern[p]) {
return -1;
}
}
return 0;
}
/**
* Deletes any characters matching `chars` from `sptr` string
*
* @param sptr string to be modified in-place
* @param chars a string containing characters (e.g. " \n" would delete whitespace and line feeds)
*/
void strchrdel(char *sptr, const char *chars) {
while (*sptr != '\0') {
for (int i = 0; chars[i] != '\0'; i++) {
if (*sptr == chars[i]) {
memmove(sptr, sptr + 1, strlen(sptr));
}
}
sptr++;
}
}
/**
* Find the integer offset of the first occurrence of `ch` in `sptr`
*
* ~~~{.c}
* char buffer[255];
* char string[] = "abc=123";
* long int separator_offset = strchroff(string, '=');
* for (long int i = 0; i < separator_offset); i++) {
* buffer[i] = string[i];
* }
* ~~~
*
* @param sptr string to scan
* @param ch character to find
* @return offset to character in string, or 0 on failure
*/
long int strchroff(const char *sptr, int ch) {
char *orig = strdup(sptr);
char *tmp = orig;
long int result = 0;
while (*tmp != '\0') {
if (*tmp == ch) {
break;
}
tmp++;
}
result = tmp - orig;
free(orig);
return result;
}
/**
* This function scans `sptr` from right to left removing any matches to `suffix`
* from the string.
*
* @param sptr string to be modified
* @param suffix string to be removed from `sptr`
*/
void strdelsuffix(char *sptr, const char *suffix) {
if (!sptr || !suffix) {
return;
}
size_t sptr_len = strlen(sptr);
size_t suffix_len = strlen(suffix);
intptr_t target_offset = sptr_len - suffix_len;
// Prevent access to memory below input string
if (target_offset < 0) {
return;
}
// Create a pointer to
char *target = sptr + target_offset;
if (!strcmp(target, suffix)) {
// Purge the suffix
memset(target, '\0', suffix_len);
// Recursive call continues removing suffix until it is gone
strip(sptr);
}
}
/**
* Split a string by every delimiter in `delim` string.
*
* Callee must free memory using `split_free()`
*
* @param sptr string to split
* @param delim characters to split on
* @return success=parts of string, failure=NULL
*/
char** split(char *_sptr, const char* delim)
{
size_t split_alloc = 0;
// Duplicate the input string and save a copy of the pointer to be freed later
char *orig = strdup(_sptr);
char *sptr = orig;
if (!sptr) {
return NULL;
}
// Determine how many delimiters are present
for (size_t i = 0; i < strlen(delim); i++) {
split_alloc += num_chars(sptr, delim[i]);
}
// Preallocate enough records based on the number of delimiters
char **result = (char **)calloc(split_alloc + 2, sizeof(char *));
if (!result) {
free(sptr);
return NULL;
}
// Separate the string into individual parts and store them in the result array
int i = 0;
char *token = NULL;
while((token = strsep(&sptr, delim)) != NULL) {
result[i] = (char *)calloc(1, sizeof(char) * strlen(token) + 1);
if (!result[i]) {
free(sptr);
return NULL;
}
strncpy(result[i], token, strlen(token)); // copy the string contents into the record
i++; // next record
}
free(orig);
return result;
}
/**
* Frees memory allocated by `split()`
* @param ptr pointer to array
*/
void split_free(char **ptr) {
for (int i = 0; ptr[i] != NULL; i++) {
free(ptr[i]);
}
free(ptr);
}
/**
* Extract the string encapsulated by characters listed in `delims`
*
* ~~~{.c}
* char *str = "this is [some data] in a string";
* char *data = substring_between(string, "[]");
* // data = "some data";
* ~~~
*
* @param sptr string to parse
* @param delims two characters surrounding a string
* @return success=text between delimiters, failure=NULL
*/
char *substring_between(char *sptr, const char *delims) {
// Ensure we have enough delimiters to continue
size_t delim_count = strlen(delims);
if (delim_count != 2) {
return NULL;
}
// Create pointers to the delimiters
char *start = strpbrk(sptr, &delims[0]);
char *end = strpbrk(sptr, &delims[1]);
// Ensure the string has both delimiters
if (!start || !end) {
return NULL;
}
start++; // ignore leading delimiter
// Get length of the substring
size_t length = end - start;
char *result = (char *)calloc(length + 1, sizeof(char));
if (!result) {
return NULL;
}
// Copy the contents of the substring to the result
char *tmp = result;
while (start != end) {
*tmp = *start;
tmp++;
start++;
}
return result;
}
/*
* Helper function for `strsort`
*/
static int _strsort_compare(const void *a, const void *b) {
const char *aa = *(const char**)a;
const char *bb = *(const char**)b;
int result = strcmp(aa, bb);
return result;
}
/**
* Sort an array of strings alphabetically
* @param arr
*/
void strsort(char **arr) {
size_t arr_size = 0;
// Determine size of array
for (size_t i = 0; arr[i] != NULL; i++) {
arr_size = i;
}
qsort(arr, arr_size, sizeof(char *), _strsort_compare);
}
/*
* Helper function for `strsortlen`
*/
static int _strsortlen_asc_compare(const void *a, const void *b) {
const char *aa = *(const char**)a;
const char *bb = *(const char**)b;
size_t len_a = strlen(aa);
size_t len_b = strlen(bb);
return len_a > len_b;
}
/*
* Helper function for `strsortlen`
*/
static int _strsortlen_dsc_compare(const void *a, const void *b) {
const char *aa = *(const char**)a;
const char *bb = *(const char**)b;
size_t len_a = strlen(aa);
size_t len_b = strlen(bb);
return len_a < len_b;
}
/**
* Sort an array of strings by length
* @param arr
*/
void strsortlen(char **arr, unsigned int sort_mode) {
typedef int (*compar)(const void *, const void *);
compar fn = _strsortlen_asc_compare;
if (sort_mode != 0) {
fn = _strsortlen_dsc_compare;
}
size_t arr_size = 0;
// Determine size of array
for (size_t i = 0; arr[i] != NULL; i++) {
arr_size = i;
}
qsort(arr, arr_size, sizeof(char *), fn);
}
/**
* Search for string in an array of strings
* @param arr array of strings
* @param str string to search for
* @return yes=0, no=1, failure=-1
*/
int strstr_array(char **arr, const char *str) {
if (!arr) {
return -1;
}
for (int i = 0; arr[i] != NULL; i++) {
if (strstr(arr[i], str) != NULL) {
return 0;
}
}
return 1;
}
/**
* Remove duplicate strings from an array of strings
* @param arr
* @return success=array of unique strings, failure=NULL
*/
char **strdeldup(char **arr) {
if (!arr) {
return NULL;
}
int records;
// Determine the length of the array
for (records = 0; arr[records] != NULL; records++);
// Allocate enough memory to store the original array contents
// (It might not have duplicate values, for example)
char **result = (char **)calloc(records + 1, sizeof(char *));
if (!result) {
return NULL;
}
int rec = 0;
int i = 0;
while(i < records) {
// Search for value in results
if (strstr_array(result, arr[i]) == 0) {
// value already exists in results so ignore it
i++;
continue;
}
// Store unique value
result[rec] = (char *)calloc(strlen(arr[i]) + 1, sizeof(char));
if (!result[rec]) {
free(result);
return NULL;
}
strncpy(result[rec], arr[i], strlen(arr[i]));
i++;
rec++;
}
return result;
}
/** Remove leading whitespace from a string
* @param sptr pointer to string
* @return pointer to first non-whitespace character in string
*/
char *lstrip(char *sptr) {
char *tmp = sptr;
size_t bytes = 0;
while (isblank(*tmp)) {
bytes++;
tmp++;
}
if (tmp != sptr) {
memmove(sptr, sptr + bytes, strlen(sptr) - bytes);
memset((sptr + strlen(sptr)) - bytes, '\0', bytes);
}
return sptr;
}
/**
* Remove trailing whitespace from a string
* @param sptr string
* @return truncated string
*/
char *strip(char *sptr) {
if (!strlen(sptr)) {
return sptr;
}
strchrdel(sptr, " \r\n");
return sptr;
}
/**
* Determine if a string is empty
* @param sptr pointer to string
* @return 0=not empty, 1=empty
*/
int isempty(char *sptr) {
char *tmp = sptr;
while (*tmp) {
if (!isblank(*tmp)) {
return 0;
}
tmp++;
}
return 1;
}
/**
* Determine if a string is encapsulated by quotes
* @param sptr pointer to string
* @return 0=not quoted, 1=quoted
*/
int isquoted(char *sptr) {
const char *quotes = "'\"";
char *quote_open = strpbrk(sptr, quotes);
if (!quote_open) {
return 0;
}
char *quote_close = strpbrk(quote_open + 1, quotes);
if (!quote_close) {
return 0;
}
return 1;
}
|