This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.c
503 lines (435 loc) · 9.82 KB
/
string.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
/**
* @file kernel/misc/string.c
* @brief Generic string functions and C standard library implementations for the kernel.
*
* @copyright
* This file is part of SiriusOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2015-2021 K. Lange
* Copyright (C) 2015 Dale Weiler
*/
#include <kernel/types.h>
#include <kernel/string.h>
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define BITOP(A, B, OP) \
((A)[(size_t)(B)/(8*sizeof *(A))] OP (size_t)1<<((size_t)(B)%(8*sizeof *(A))))
unsigned short * memsetw(unsigned short * dest, unsigned short val, int count) {
int i = 0;
for ( ; i < count; ++i ) {
dest[i] = val;
}
return dest;
}
#if 1
void * memcpy(void * restrict dest, const void * restrict src, size_t n) {
uint64_t * d_64 = dest;
const uint64_t * s_64 = src;
for (; n >= 8; n -= 8) {
*d_64++ = *s_64++;
}
uint32_t * d_32 = (void*)d_64;
const uint32_t * s_32 = (const void*)s_64;
for (; n >= 4; n -= 4) {
*d_32++ = *s_32++;
}
uint8_t * d = (void*)d_32;
const uint8_t * s = (const void*)s_32;
for (; n > 0; n--) {
*d++ = *s++;
}
return dest;
}
#else
/* FIXME why is there an x86-specific memcpy outside of the arch dir... */
void * memcpy(void * restrict dest, const void * restrict src, size_t n) {
asm volatile("rep movsb"
: : "D"(dest), "S"(src), "c"(n)
: "flags", "memory");
return dest;
}
#endif
size_t strlen(const char * s) {
const char * a = s;
const size_t * w;
for (; (uintptr_t)s % ALIGN; s++) {
if (!*s) {
return s-a;
}
}
for (w = (const void *)s; !HASZERO(*w); w++);
for (s = (const void *)w; *s; s++);
return s-a;
}
int strcmp(const char * a, const char * b) {
uint32_t i = 0;
while (1) {
if (a[i] < b[i]) {
return -1;
} else if (a[i] > b[i]) {
return 1;
} else {
if (a[i] == '\0') {
return 0;
}
++i;
}
}
}
void * memset(void * dest, int c, size_t n) {
size_t i = 0;
for ( ; i < n; ++i ) {
((char *)dest)[i] = c;
}
return dest;
}
void * memmove(void * dest, const void * src, size_t n) {
char * d = dest;
const char * s = src;
if (d==s) {
return d;
}
if (s+n <= d || d+n <= s) {
return memcpy(d, s, n);
}
if (d<s) {
if ((uintptr_t)s % sizeof(size_t) == (uintptr_t)d % sizeof(size_t)) {
while ((uintptr_t)d % sizeof(size_t)) {
if (!n--) {
return dest;
}
*d++ = *s++;
}
for (; n >= sizeof(size_t); n -= sizeof(size_t), d += sizeof(size_t), s += sizeof(size_t)) {
*(size_t *)d = *(size_t *)s;
}
}
for (; n; n--) {
*d++ = *s++;
}
} else {
if ((uintptr_t)s % sizeof(size_t) == (uintptr_t)d % sizeof(size_t)) {
while ((uintptr_t)(d+n) % sizeof(size_t)) {
if (!n--) {
return dest;
}
d[n] = s[n];
}
while (n >= sizeof(size_t)) {
n -= sizeof(size_t);
*(size_t *)(d+n) = *(size_t *)(s+n);
}
}
while (n) {
n--;
d[n] = s[n];
}
}
return dest;
}
void * memchr(const void * src, int c, size_t n) {
const unsigned char * s = src;
c = (unsigned char)c;
for (; ((uintptr_t)s & (ALIGN - 1)) && n && *s != c; s++, n--);
if (n && *s != c) {
const size_t * w;
size_t k = ONES * c;
for (w = (const void *)s; n >= sizeof(size_t) && !HASZERO(*w^k); w++, n -= sizeof(size_t));
for (s = (const void *)w; n && *s != c; s++, n--);
}
return n ? (void *)s : 0;
}
void * memrchr(const void * m, int c, size_t n) {
const unsigned char * s = m;
c = (unsigned char)c;
while (n--) {
if (s[n] == c) {
return (void*)(s+n);
}
}
return 0;
}
size_t strspn(const char * s, const char * c) {
const char * a = s;
size_t byteset[32/sizeof(size_t)] = { 0 };
if (!c[0]) {
return 0;
}
if (!c[1]) {
for (; *s == *c; s++);
return s-a;
}
for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++);
for (; *s && BITOP(byteset, *(unsigned char *)s, &); s++);
return s-a;
}
char * strchrnul(const char * s, int c) {
size_t * w;
size_t k;
c = (unsigned char)c;
if (!c) {
return (char *)s + strlen(s);
}
for (; (uintptr_t)s % ALIGN; s++) {
if (!*s || *(unsigned char *)s == c) {
return (char *)s;
}
}
k = ONES * c;
for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
for (s = (void *)w; *s && *(unsigned char *)s != c; s++);
return (char *)s;
}
char * strchr(const char * s, int c) {
char *r = strchrnul(s, c);
return *(unsigned char *)r == (unsigned char)c ? r : 0;
}
char * strrchr(const char * s, int c) {
return memrchr(s, c, strlen(s) + 1);
}
char * stpcpy(char * restrict d, const char * restrict s) {
size_t * wd;
const size_t * ws;
if ((uintptr_t)s % ALIGN == (uintptr_t)d % ALIGN) {
for (; (uintptr_t)s % ALIGN; s++, d++) {
if (!(*d = *s)) {
return d;
}
}
wd = (void *)d;
ws = (const void *)s;
for (; !HASZERO(*ws); *wd++ = *ws++);
d = (void *)wd;
s = (const void *)ws;
}
for (; (*d=*s); s++, d++);
return d;
}
char * strcpy(char * restrict dest, const char * restrict src) {
stpcpy(dest, src);
return dest;
}
size_t lfind(const char * str, const char accept) {
return (size_t)strchr(str, accept);
}
size_t rfind(const char * str, const char accept) {
return (size_t)strrchr(str, accept);
}
size_t strcspn(const char * s, const char * c) {
const char *a = s;
if (c[0] && c[1]) {
size_t byteset[32/sizeof(size_t)] = { 0 };
for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++);
for (; *s && !BITOP(byteset, *(unsigned char *)s, &); s++);
return s-a;
}
return strchrnul(s, *c)-a;
}
char * strpbrk(const char * s, const char * b) {
s += strcspn(s, b);
return *s ? (char *)s : 0;
}
char * strtok_r(char * str, const char * delim, char ** saveptr) {
char * token;
if (str == NULL) {
str = *saveptr;
}
str += strspn(str, delim);
if (*str == '\0') {
*saveptr = str;
return NULL;
}
token = str;
str = strpbrk(token, delim);
if (str == NULL) {
*saveptr = (char *)lfind(token, '\0');
} else {
*str = '\0';
*saveptr = str + 1;
}
return token;
}
static char *strstr_2b(const unsigned char * h, const unsigned char * n) {
uint16_t nw = n[0] << 8 | n[1];
uint16_t hw = h[0] << 8 | h[1];
for (h++; *h && hw != nw; hw = hw << 8 | *++h);
return *h ? (char *)h-1 : 0;
}
static char *strstr_3b(const unsigned char * h, const unsigned char * n) {
uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8;
uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8;
for (h += 2; *h && hw != nw; hw = (hw|*++h) << 8);
return *h ? (char *)h-2 : 0;
}
static char *strstr_4b(const unsigned char * h, const unsigned char * n) {
uint32_t nw = n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3];
uint32_t hw = h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3];
for (h += 3; *h && hw != nw; hw = hw << 8 | *++h);
return *h ? (char *)h-3 : 0;
}
int memcmp(const void * vl, const void * vr, size_t n) {
const unsigned char *l = vl;
const unsigned char *r = vr;
for (; n && *l == *r; n--, l++, r++);
return n ? *l-*r : 0;
}
static char *strstr_twoway(const unsigned char * h, const unsigned char * n) {
size_t mem;
size_t mem0;
size_t byteset[32 / sizeof(size_t)] = { 0 };
size_t shift[256];
size_t l;
/* Computing length of needle and fill shift table */
for (l = 0; n[l] && h[l]; l++) {
BITOP(byteset, n[l], |=);
shift[n[l]] = l+1;
}
if (n[l]) {
return 0; /* hit the end of h */
}
/* Compute maximal suffix */
size_t ip = -1;
size_t jp = 0;
size_t k = 1;
size_t p = 1;
while (jp+k<l) {
if (n[ip+k] == n[jp+k]) {
if (k == p) {
jp += p;
k = 1;
} else {
k++;
}
} else if (n[ip+k] > n[jp+k]) {
jp += k;
k = 1;
p = jp - ip;
} else {
ip = jp++;
k = p = 1;
}
}
size_t ms = ip;
size_t p0 = p;
/* And with the opposite comparison */
ip = -1;
jp = 0;
k = p = 1;
while (jp+k<l) {
if (n[ip+k] == n[jp+k]) {
if (k == p) {
jp += p;
k = 1;
} else {
k++;
}
} else if (n[ip+k] < n[jp+k]) {
jp += k;
k = 1;
p = jp - ip;
} else {
ip = jp++;
k = p = 1;
}
}
if (ip+1 > ms+1) {
ms = ip;
} else {
p = p0;
}
/* Periodic needle? */
if (memcmp(n, n+p, ms+1)) {
mem0 = 0;
p = MAX(ms, l-ms-1) + 1;
} else {
mem0 = l-p;
}
mem = 0;
/* Initialize incremental end-of-haystack pointer */
const unsigned char * z = h;
/* Search loop */
for (;;) {
/* Update incremental end-of-haystack pointer */
if ((size_t)(z-h) < l) {
/* Fast estimate for MIN(l,63) */
size_t grow = l | 63;
const unsigned char *z2 = memchr(z, 0, grow);
if (z2) {
z = z2;
if ((size_t)(z-h) < l) {
return 0;
}
} else {
z += grow;
}
}
/* Check last byte first; advance by shift on mismatch */
if (BITOP(byteset, h[l-1], &)) {
k = l-shift[h[l-1]];
if (k) {
if (mem0 && mem && k < p) k = l-p;
h += k;
mem = 0;
continue;
}
} else {
h += l;
mem = 0;
continue;
}
/* Compare right half */
for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
if (n[k]) {
h += k-ms;
mem = 0;
continue;
}
/* Compare left half */
for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
if (k <= mem) {
return (char *)h;
}
h += p;
mem = mem0;
}
}
char *strstr(const char * h, const char * n) {
/* Return immediately on empty needle */
if (!n[0]) {
return (char *)h;
}
/* Use faster algorithms for short needles */
h = strchr(h, *n);
if (!h || !n[1]) {
return (char *)h;
}
if (!h[1]) return 0;
if (!n[2]) return strstr_2b((void *)h, (void *)n);
if (!h[2]) return 0;
if (!n[3]) return strstr_3b((void *)h, (void *)n);
if (!h[3]) return 0;
if (!n[4]) return strstr_4b((void *)h, (void *)n);
/* Two-way on large needles */
return strstr_twoway((void *)h, (void *)n);
}
uint8_t startswith(const char * str, const char * accept) {
return strstr(str, accept) == str;
}
char * strdup(const char * c) {
char * out = malloc(strlen(c) + 1);
memcpy(out, c, strlen(c)+1);
return out;
}
int atoi(const char * c) {
int sign = 1;
long out = 0;
if (*c == '-') {
sign = '-';
c++;
}
while (*c) {
out *= 10;
out += (*c - '0');
c++;
}
return out * sign;
}