-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspl_str.h
380 lines (324 loc) · 11.2 KB
/
spl_str.h
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
/*
===============================================================================
| spl_str.h |
| https://github.com/mrsafalpiya/spl |
| |
| The missing string manipulation library |
| |
| No warranty implied; Use at your own risk |
| See end of file for license information. |
===============================================================================
*/
/*
===============================================================================
| Version History |
===============================================================================
*
- v0.2
- Modified identifier names from 'spl_str*' to 'spl_str_*'.
- Added `does_begin_with()`, `does_begin_with_case()`, `does_end_with()`,
`does_end_with_case()`, `toupper()`, `toupper_dup()`, `tolower()`,
`tolower_dup()`.
- v0.1
*/
/*
===============================================================================
| Usage |
===============================================================================
*
* Do this:
*
* #define SPL_STR_IMPL
*
* before you include this file in *one* C or C++ file to create the
* implementation.
*/
/*
===============================================================================
| HEADER-FILE MODE |
===============================================================================
*/
#ifndef SPL_STR_H
#define SPL_STR_H
#include <stdlib.h>
#include <string.h>
/*
===============================================================================
| Options |
===============================================================================
*/
/* = SPL_STR = */
#ifndef SPL_STR_DEF
#define SPL_STR_DEF /* You may want `static` or `static inline` here */
#endif
/*
===============================================================================
| Function Declarations |
===============================================================================
*/
/*
* Remove leading and trailing whitespaces from the string.
*
* `len` is the length of the `str`. It can also be the number of bytes you want
* to clean. Pass -1 if you want the function to calculate it.
*
* Cleaning is done on the original string in destructive manner (moving and
* editing bytes within the string).
*
* NOTE: NOT to be called on a read-only memory as it WILL CAUSE SEGFAULT. In
* which case run 'spl_str_clean_dup()'.
*/
SPL_STR_DEF void
spl_str_clean(char *str, int len);
/* Same as 'spl_str_clean()' but first a duplicate of the original string is
* created. Thus this function can be called on read-only strings too. As the
* result is a dynamically allocated string, it has to be free'ed later. */
SPL_STR_DEF char *
spl_str_clean_dup(const char *str, int len);
/*
* Because 'strndup()' is not standard.
*
* `len` is the length of the `str`. It can also be the number of bytes you want
* to duplicate. Pass -1 if you want the function to calculate it.
*/
SPL_STR_DEF char *
spl_str_dup(const char *str, int len);
/*
* Check if the given string `str` begins with the given `begin_str`.
*
* `len` is the length of the `begin_str`. It can also be the number of bytes
* you want to compare. Pass -1 if you want the function to calculate it.
*
* Returns 1 if it does begin with it OR 0 if it doesn't.
*/
SPL_STR_DEF int
spl_str_does_begin_with(const char *str, const char *begin_str,
int len_begin_str);
/* Case insensitive version of 'does_begin_with()'. */
SPL_STR_DEF int
spl_str_does_begin_with_case(const char *str, const char *begin_str,
int len_begin_str);
/*
* Check if the given string `str` ends with the given `end_str`.
*
* `str_len` is the length of the `str`. `cmp_len` is the length of the
* `end_str`. It can also be the number of bytes you want to compare. Pass -1
* if you want the function to calculate it.
*
* Returns 1 if it does begin with it OR 0 if it doesn't.
*/
SPL_STR_DEF int
spl_str_does_end_with(const char *str, const char *end_str, int len_str,
int len_end_str);
/* Case insensitive version of 'does_end_with()'. */
SPL_STR_DEF int
spl_str_does_end_with_case(const char *str, const char *end_str, int len_str,
int len_end_str);
/*
* Change the whole string to uppercase.
*
* Operation is done on the original string in destructive manner (editing
* bytes within the string). Safe to pass NULL.
*
* NOTE: NOT to be called on a read-only memory as it WILL CAUSE SEGFAULT. In
* which case run 'spl_str_toupper_dup()'.
*/
SPL_STR_DEF void
spl_str_toupper(char *str, int len);
/* Same as 'spl_str_toupper()' but first a duplicate of the original string is
* created. Thus this function can be called on read-only strings too. As the
* result is a dynamically allocated string, it has to be free'ed later. */
SPL_STR_DEF char *
spl_str_toupper_dup(const char *str, int len);
/*
* Change the whole string to lowercase.
*
* Operation is done on the original string in destructive manner (editing
* bytes within the string). Safe to pass NULL.
*
* NOTE: NOT to be called on a read-only memory as it WILL CAUSE SEGFAULT. In
* which case run 'spl_str_tolower_dup()'.
*/
SPL_STR_DEF void
spl_str_tolower(char *str, int len);
/* Same as 'spl_str_tolower()' but first a duplicate of the original string is
* created. Thus this function can be called on read-only strings too. As the
* result is a dynamically allocated string, it has to be free'ed later. */
SPL_STR_DEF char *
spl_str_tolower_dup(const char *str, int len);
#endif /* SPL_STR_H */
/*
===============================================================================
| IMPLEMENTATION MODE |
===============================================================================
*/
#ifdef SPL_STR_IMPL
#include <ctype.h>
/*
===============================================================================
| Function Implementations |
===============================================================================
*/
SPL_STR_DEF void
spl_str_clean(char *str, int len)
{
if (len < 0)
len = strlen(str);
/* Remove leading whitespaces */
int lead_ws_c = 0;
char *startp = str;
while (*startp == ' ') {
lead_ws_c++;
startp++;
}
if (lead_ws_c != 0) {
memmove(str, str + lead_ws_c, (len - lead_ws_c) * sizeof(char));
str[len - lead_ws_c + 1] = '\0';
}
/* Remove trailing whitespaces */
char *endp = str + (len - lead_ws_c) - 1;
while (*endp == ' ')
endp--;
*(endp + 1) = '\0';
}
SPL_STR_DEF char *
spl_str_clean_dup(const char *str, int len)
{
if (len < 0)
len = strlen(str);
char *str_cleaned = spl_str_dup(str, len);
spl_str_clean(str_cleaned, len);
return str_cleaned;
}
SPL_STR_DEF char *
spl_str_dup(const char *str, int len)
{
if (len < 0)
len = strlen(str);
char *str_duped = malloc(len + 1);
if (!str_duped)
return NULL;
memcpy(str_duped, str, len);
str_duped[len] = '\0';
return str_duped;
}
SPL_STR_DEF int
spl_str_does_begin_with(const char *str, const char *begin_str,
int len_begin_str)
{
if (len_begin_str < 0)
len_begin_str = strlen(begin_str);
if (!strncmp(str, begin_str, len_begin_str))
return 1;
return 0;
}
SPL_STR_DEF int
spl_str_does_begin_with_case(const char *str, const char *begin_str,
int len_begin_str)
{
if (len_begin_str < 0)
len_begin_str = strlen(begin_str);
for (int i = 0; i < len_begin_str; i++) {
if (tolower(begin_str[i]) != tolower(str[i]))
return 0;
}
return 1;
}
SPL_STR_DEF int
spl_str_does_end_with(const char *str, const char *end_str, int len_str,
int len_end_str)
{
if (len_str < 0)
len_str = strlen(str);
if (len_end_str < 0)
len_end_str = strlen(end_str);
if (len_end_str > len_str)
return 0;
for (int i = 0; i < len_end_str; i++) {
if (str[len_str - 1 - i] != end_str[len_end_str - 1 - i])
return 0;
}
return 1;
}
SPL_STR_DEF int
spl_str_does_end_with_case(const char *str, const char *end_str, int len_str,
int len_end_str)
{
if (len_str < 0)
len_str = strlen(str);
if (len_end_str < 0)
len_end_str = strlen(end_str);
if (len_end_str > len_str)
return 0;
for (int i = 0; i < len_end_str; i++) {
if (tolower(str[len_str - 1 - i]) !=
tolower(end_str[len_end_str - 1 - i]))
return 0;
}
return 1;
}
SPL_STR_DEF void
spl_str_toupper(char *str, int len)
{
if (len < 0)
len = strlen(str);
for (int i = 0; i < len; i++)
str[i] = toupper(str[i]);
}
SPL_STR_DEF char *
spl_str_toupper_dup(const char *str, int len)
{
if (len < 0)
len = strlen(str);
char *str_touppered = spl_str_dup(str, len);
spl_str_toupper(str_touppered, len);
return str_touppered;
}
SPL_STR_DEF void
spl_str_tolower(char *str, int len)
{
if (len < 0)
len = strlen(str);
for (int i = 0; i < len; i++)
str[i] = tolower(str[i]);
}
SPL_STR_DEF char *
spl_str_tolower_dup(const char *str, int len)
{
if (len < 0)
len = strlen(str);
char *str_tolowered = spl_str_dup(str, len);
spl_str_tolower(str_tolowered, len);
return str_tolowered;
}
#endif /* SPL_STR_IMPL */
/*
===============================================================================
| License - Public Domain (www.unlicense.org) |
===============================================================================
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/