-
Notifications
You must be signed in to change notification settings - Fork 4
/
strings.c
435 lines (404 loc) · 10.1 KB
/
strings.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
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "strings.h"
/* libc includes */
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#if HAVE_LIBBSD
#include <bsd/string.h>
#else
#include <string.h>
#endif
const char* str_itoa_r(int val, char *buf)
{
#if HAVE__ITOA
return _itoa(val, buf, 10);
#elif HAVE_ITOA
return itoa(val, buf, 10);
#else
snprintf(buf, 12, "%d", val);
return buf;
#endif
}
const char *str_itoa(int n)
{
static char buf[12];
#if HAVE__ITOA
return _itoa(n, buf, 10);
#elif HAVE_ITOA
return itoa(n, buf, 10);
#else
return str_itoa_r(n, buf);
#endif
}
size_t str_strlcpy(char *dst, const char *src, size_t len)
{
#if HAVE_STRLCPY
return strlcpy(dst, src, len);
#else
register char *d = dst;
register const char *s = src;
register size_t n = len;
assert(src);
assert(dst);
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (len != 0) {
*d = '\0'; /* NUL-terminate dst */
}
return (s - src) + strlen(s); /* count does not include NUL */
}
return (s - src - 1); /* count does not include NUL */
#endif
}
size_t str_strlcat(char *dst, const char *src, size_t len)
{
#if HAVE_STRLCAT
return strlcat(dst, src, len);
#else
register char *d = dst;
register const char *s = src;
register size_t n = len;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (*d != '\0' && n-- != 0)
d++;
dlen = d - dst;
n = len - dlen;
if (n == 0)
return (dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return (dlen + (s - src)); /* count does not include NUL */
#endif
}
size_t str_slprintf(char * dst, size_t size, const char * format, ...)
{
va_list args;
int result;
va_start(args, format);
result = vsnprintf(dst, size, format, args);
va_end(args);
if (result < 0 || result >= (int)size) {
dst[size - 1] = '\0';
return size;
}
return (size_t)result;
}
void str_replace(char *buffer, size_t size, const char *tmpl, const char *var,
const char *value)
{
size_t val_len = strlen(value);
size_t var_len = strlen(var);
char *s = buffer;
while (buffer + size > s) {
char *p = strstr(tmpl, var);
size_t len;
if (p) {
len = p - tmpl;
}
else {
len = strlen(tmpl);
}
if (len < size) {
memmove(s, tmpl, len);
tmpl += len;
s += len;
size -= len;
if (p && val_len < size) {
tmpl += var_len;
memmove(s, value, val_len);
s += val_len;
size -= val_len;
}
}
if (!p) {
break;
}
}
*s = 0;
}
int str_hash(const char *s)
{
int key = 0;
assert(s);
while (*s) {
key = key * 37 + *s++;
}
return key & 0x7FFFFFFF;
}
unsigned int jenkins_hash(unsigned int a)
{
a = (a + 0x7ed55d16) + (a << 12);
a = (a ^ 0xc761c23c) ^ (a >> 19);
a = (a + 0x165667b1) + (a << 5);
a = (a + 0xd3a2646c) ^ (a << 9);
a = (a + 0xfd7046c5) + (a << 3);
a = (a ^ 0xb55a4f09) ^ (a >> 16);
return a;
}
unsigned int wang_hash(unsigned int a)
{
a = ~a + (a << 15); /* a = (a << 15) - a - 1; */
a = a ^ (a >> 12);
a = a + (a << 2);
a = a ^ (a >> 4);
a = a * 2057; /* a = (a + (a << 3)) + (a << 11); */
a = a ^ (a >> 16);
return a;
}
char *str_strdup(const char *s) {
if (s == NULL) return NULL;
#if HAVE__STRDUP
return _strdup(s);
#elif HAVE_STRDUP
return strdup(s);
#else
size_t len = strlen(s);
char *dup = malloc(len + 1);
if (dup) {
memcpy(dup, s, len + 1);
}
return dup;
#endif
}
void sbs_printf(struct sbstring *sbs, const char *format, ...)
{
size_t size = (sbs->end - sbs->begin);
if (size < sbs->size) {
va_list argp;
va_start(argp, format);
int bytes = vsnprintf(sbs->end, sbs->size - size, format, argp);
if (bytes > 0) {
if ((size_t)bytes >= sbs->size - size) {
bytes = (int)(sbs->size - size - 1);
/* terminate truncated output */
sbs->end[bytes] = '\0';
}
sbs->end += bytes;
}
va_end(argp);
}
}
void sbs_init(struct sbstring *sbs, char *buffer, size_t size)
{
assert(sbs);
assert(size > 0);
sbs->begin = buffer;
sbs->size = size;
sbs->end = buffer;
buffer[0] = '\0';
}
void sbs_adopt(struct sbstring *sbs, char *buffer, size_t size)
{
if (buffer && size > 0) {
size_t len = strlen(buffer);
assert(sbs);
assert(size > len);
sbs->begin = buffer;
sbs->size = size;
sbs->end = buffer + len;
}
else {
sbs->end = sbs->begin = NULL;
sbs->size = 0;
}
}
void sbs_strncat(struct sbstring *sbs, const char *str, size_t size)
{
assert(sbs);
if (str) {
size_t len;
len = sbs->size - (sbs->end - sbs->begin) - 1;
if (len < size) {
size = len;
}
memcpy(sbs->end, str, size);
sbs->end[size] = '\0';
sbs->end += size;
}
}
void sbs_strcat(struct sbstring *sbs, const char *str)
{
size_t len;
assert(sbs);
if (str) {
len = sbs->size - (sbs->end - sbs->begin);
str_strlcpy(sbs->end, str, len);
sbs->end += strlen(sbs->end);
}
assert(sbs->begin + sbs->size >= sbs->end);
}
void sbs_substr(sbstring *sbs, ptrdiff_t pos, size_t len)
{
if (pos > sbs->end - sbs->begin) {
/* starting past end of string, do nothing */
sbs->end = sbs->begin;
}
if (pos >= 0) {
size_t sz = sbs->end - (sbs->begin + pos);
if (len > sz) len = sz;
if (len - pos > 0) {
memmove(sbs->begin, sbs->begin + pos, len);
}
else {
memcpy(sbs->begin, sbs->begin + pos, len);
}
sbs->end = sbs->begin + len;
sbs->end[0] = '\0';
}
}
size_t sbs_length(const struct sbstring *sbs)
{
if (sbs->begin) {
assert(sbs->begin + sbs->size >= sbs->end);
return sbs->end - sbs->begin;
}
return 0;
}
char *str_unescape(char *str) {
char *read = str, *write = str;
while (*read) {
char * pos = strchr(read, '\\');
if (pos >= read) {
size_t len = (size_t)(pos - read);
memmove(write, read, len);
write += len;
read += (len + 1);
switch (read[0]) {
case 'r':
*write++ = '\r';
break;
case 'n':
*write++ = '\n';
break;
case 't':
*write++ = '\t';
break;
default:
*write++ = read[0];
}
*write = 0;
++read;
}
else {
size_t len = strlen(read);
memmove(write, read, len);
write[len] = 0;
break;
}
}
return str;
}
const char *str_escape_ex(const char *str, char *buffer, size_t size, const char *chars)
{
size_t slen = strlen(str);
const char *read = str;
unsigned char *write = (unsigned char *)buffer;
if (size < 1) {
return NULL;
}
while (slen > 0 && size > 1 && *read) {
const char *pos = strpbrk(read, chars);
size_t len = size;
if (pos >= read) {
len = pos - read;
}
if (len < size) {
unsigned char ch = *(const unsigned char *)pos;
if (len > 0) {
if (len > slen) {
len = slen;
}
memmove(write, read, len);
slen -= len;
write += len;
read += len;
size -= len;
}
switch (ch) {
case '\t':
if (size > 2) {
*write++ = '\\';
*write++ = 't';
size -= 2;
}
else size = 1;
break;
case '\n':
if (size > 2) {
*write++ = '\\';
*write++ = 'n';
size -= 2;
}
else size = 1;
break;
case '\r':
if (size > 2) {
*write++ = '\\';
*write++ = 'r';
size -= 2;
}
else size = 1;
break;
case '\"':
case '\'':
case '\\':
if (size > 2) {
*write++ = '\\';
*write++ = ch;
size -= 2;
}
break;
default:
if (size > 5) {
int n = snprintf((char *)write, size, "\\%03o", ch);
if (n > 0) {
assert(n == 5);
write += n;
size -= n;
}
else size = 1;
}
else size = 1;
}
++read;
--slen;
} else {
/* end of buffer space */
len = size - 1;
if (len > 0) {
if (len > slen) {
len = slen;
}
memmove(write, read, len);
write += len;
size -= len;
slen -= len;
break;
}
}
}
*write = '\0';
return buffer;
}
const char *str_escape(const char *str, char *buffer, size_t size) {
return str_escape_ex(str, buffer, size, "\n\t\r\'\"\\");
}