-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.c
385 lines (362 loc) · 8.96 KB
/
str.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
/**
* String helpers not found in <string.h> for static strings with a fixed
* buffer size.
*
* When dealing with static strings, parameters named "x_size" is to be
* interpreted as the "size of the buffer of x", that is the maximum possible
* string length including the \0 terminator.
*
* Parameters named "x_len" should be interpreted to mean "the string length of
* x", ie the number of characters in x excluding the \0 terminator.
*
* Author: Tim Sjöstrand <[email protected]>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "str.h"
/**
* An implementation of the standard strnlen, if it is not already implemented.
*/
#ifndef HAVE_STRNLEN
size_t strnlen(const char *s, size_t s_size)
{
const char *e;
size_t n;
for(e = s, n = 0; *e && n < s_size; e++, n++);
return n;
}
#endif
/**
* Insert a character into a given position in a string, and move any adjacent
* characters to accomodate the new substring.
*
* @param s The string to delete from.
* @param s_size The size of the buffer holding s.
* @param index Delete starting from this index.
* @param sub The substring to insert.
* @param sub_len The length in characters of the substring.
*
* @return The number of characters added to the string.
*
* @see str_replace_into
*/
int str_insert(char *s, size_t s_size, size_t index, const char *sub, size_t sub_len)
{
/* Sanity check: invalid arguments. */
if(s_size == 0
|| sub_len == 0
|| s == NULL
|| sub == NULL) {
return 0;
}
/* Sanity check: out of bounds. */
if(index >= s_size) {
return 0;
}
/* Sanity check: out of bounds, operate on as many chars as possible. */
if(index+sub_len+1 >= s_size) {
sub_len = s_size - index;
}
/* Sanity check: inserting null terminator? */
if(sub[sub_len - 1] == '\0') {
#ifdef DEBUG
printf("WARN @ Str: str_insert() with subtext containing null terminator\n");
#endif
if(sub_len == 0) {
return 0;
} else {
sub_len --;
}
}
/* Shift characters right. */
memmove(s+index+sub_len, s+index, s_size-(index+sub_len));
/* Insert new characters. */
memcpy(s+index, sub, sub_len);
/* Make sure string is null terminated. */
s[s_size - 1] = '\0';
/* Return number of characters added. */
return sub_len;
}
/**
* Implementation notes for str_insert():
*
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* s = | A | B | C | D | E | 0 | 0 | 0 | 0 |
* |---| | |
* | | ` index+sub_len+1 |
* | ` index+sub_len ` s_size
* ` index
*
* str_insert(s, 9, 1, "xy", 2);
*
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* s = | A | - | - | B | C | D | E | 0 | 0 |
*
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* s = | A | x | y | B | C | D | E | 0 | 0 |
*/
/**
* Insert a substring into a string, replacing any characters that were there
* previously.
*
* @see str_insert
*/
int str_replace_into(char *s, size_t s_size, size_t index, const char *sub, size_t sub_len)
{
/* Sanity check: invalid arguments. */
if(s_size == 0
|| sub_len == 0
|| s == NULL
|| sub == NULL) {
return 0;
}
/* Sanity check: out of bounds. */
if(index >= s_size) {
return 0;
}
/* Sanity check: out of bounds, operate on as many chars as possible. */
if(index + sub_len + 1 >= s_size) {
sub_len = s_size - index;
}
/* Sanity check: inserting null terminator? */
if(sub[sub_len - 1] == '\0') {
#ifdef DEBUG
printf("WARN @ Str: str_replace_into() with subtext containing null terminator\n");
#endif
if(sub_len == 0) {
return 0;
} else {
sub_len--;
}
}
/* Insert new characters. */
memcpy(s + index, sub, sub_len);
/* Make sure string is null terminated. */
s[s_size - 1] = '\0';
/* Return number of characters added. */
return sub_len;
}
/**
* Remove a set of characters from a string, and move the characters on the
* right side of the deletion to fill the gap.
*
* @param s The string to delete from.
* @param s_size The size of the buffer holding s.
* @param index Delete from this index.
* @param count The number of characters to delete.
*
* @return The number of characters removed from the string.
*/
int str_delete(char *s, size_t s_size, size_t index, size_t count)
{
/* Sanity check: invalid arguments. */
if(count < 1
|| s_size == 0
|| s == NULL) {
return 0;
}
/* Sanity check: out of bounds. */
if(index >= s_size - 1) {
return 0;
}
/* Sanity check: out of bounds, operate on as many chars as possible. */
if(index+count >= s_size) {
count = s_size-index;
}
/* Shift chars to the left. */
memmove(s+index, s+index+count, s_size-(index+count));
/* Append null chars. */
memset(s+s_size-count, '\0', count);
return count;
}
/**
* Implementation notes for str_delete():
*
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* s = | A | B | C | D | E | 0 | 0 | 0 | 0 |
* |---| | |
* | | ` index+count+1 ` s_size
* | ` index+count
* ` index
*
* str_delete(s, 9, 1, 2);
*
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* s = | A | D | E | 0 | 0 | 0 | 0 | ? | ? |
*
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* s = | A | D | E | 0 | 0 | 0 | 0 | 0 | 0 |
*
*
*/
int str_append(char *s, size_t s_size, const char *sub, size_t sub_len)
{
size_t s_null = strnlen(s, s_size);
/* Sanity check: out of bounds? */
if(s_null + sub_len >= s_size) {
sub_len = s_size - s_null;
}
memcpy(s+s_null, sub, sub_len);
return sub_len;
}
/**
* Find the next "word" in a string, where a word has a delimiter from the
* 'needles' array.
*
* @return The next word, or NULL if not found.
*/
char* str_next_word(char *s, const size_t s_size, const size_t offset, const char *needles)
{
/* Sanity check: invalid argument. */
if(s == NULL || offset > s_size) {
return NULL;
}
int needle_count = strlen(needles);
for(size_t i = offset; i < s_size; i++) {
for(int n=0; n<needle_count; n++) {
if(s[i] == needles[n] && s[i+1] != needles[n]) {
return s + i;
}
}
}
return NULL;
}
/**
* Find the prev "word" in a string, where a word has a delimiter from the
* 'needles' array.
*
* @return The prev word, or NULL if not found.
*/
char* str_prev_word(char *s, const size_t s_size, const size_t offset, const char *needles)
{
/* Sanity check: invalid argument. */
if(s == NULL || offset > s_size) {
return NULL;
}
int needle_count = strlen(needles);
for(size_t i = offset; i > 0; i--) {
for(int n=0; n<needle_count; n++) {
if(s[i] == needles[n] && s[i+1] != needles[n]) {
return s + i;
}
}
}
return NULL;
}
/**
* Searches from the back of the string after the char 'needle' the specified
* amount of times and returns a pointer to the last occurance of 'needle'.
*
* If the search fails, NULL is returned instead.
*/
char* str_search_reverse(char *s, const size_t s_size, const char needle,
int needle_count)
{
if(s == NULL) {
return NULL;
}
char *tmp = s;
for(int i=s_size; i>=0; i--) {
if(needle_count <= 0) {
break;
}
if(s[i] == needle) {
tmp = &s[i];
needle_count --;
}
}
if(needle_count != 0) {
return NULL;
} else {
return tmp;
}
}
/**
* Allocates new memory and copies a string to it. Equivalent to the
* non-standard strndup().
*/
char* str_copy(const char *s, const size_t s_len, const size_t s_size)
{
char *tmp = (char *) malloc(s_size);
if(tmp == NULL) {
return NULL;
}
memcpy(tmp, s, s_len * sizeof(char));
tmp[s_len] = '\0';
return tmp;
}
/**
* Parses a float from a string.
*
* @return -1 on error, 0 on success.
*/
int str_parse_1f(const char *s, float *dst)
{
char *end;
float f = strtof(s, &end);
if(end == NULL || s == end || (*end) != '\0') {
return -1;
}
(*dst) = f;
return 0;
}
void str_print_hex(const char *s)
{
while(*s) printf("%02x ", (unsigned int) *s++);
}
/**
* @return 1 if 'a' is equal to 'b'.
*/
int str_equals(const char *a, const char *b)
{
size_t a_len = strlen(a);
size_t b_len = strlen(b);
if(a_len != b_len) {
return 0;
}
/* a_len == b_len here. */
return strncmp(a, b, a_len) == 0;
}
/**
* @return 1 if 'a' is equal to 'b', ignoring case.
*/
int str_equals_ignore_case(const char *a, const char *b)
{
size_t a_len = strlen(a);
size_t b_len = strlen(b);
if(a_len != b_len) {
return 0;
}
/* a_len == b_len here. */
#ifndef WIN32
return strncasecmp(a, b, a_len) == 0;
#else
return _strnicmp(a, b, a_len) == 0;
#endif
}
/**
* Set target string 'dst' with a known buffer size 'dst_size' to the string
* 'src'. If src cannot fit in the buffer of dst, as many characters as
* possible will be copied.
*
* @return The number of characters excluded from the copy, if any.
*/
int str_set(char *dst, size_t dst_size, const char *src)
{
size_t src_size = strlen(src) + 1;
size_t end = src_size > dst_size ? dst_size : src_size;
memcpy(dst, src, end);
dst[end+1 < dst_size ? end+1 : dst_size] = '\0';
return src_size - end;
}
/**
* @return True if the string is NULL or has the length 0.
*/
int str_empty(const char *s, size_t s_size)
{
if(s == NULL || s_size == 0) {
return 1;
}
return strnlen(s, s_size) == 0;
}