20 #include "pedigree/kernel/compiler.h" 21 #include "pedigree/kernel/processor/types.h" 22 #include "pedigree/kernel/utilities/utility.h" 26 extern void *malloc(
size_t);
27 extern void free(
void *);
30 char *strcpy(
char *dest,
const char *src);
31 char *strncpy(
char *dest,
const char *src,
size_t len);
34 char *strcat(
char *dest,
const char *src);
35 char *strncat(
char *dest,
const char *src,
size_t n);
36 char *strchr(
const char *str,
int target);
37 char *strrchr(
const char *str,
int target);
38 int vsprintf(
char *buf,
const char *fmt, va_list arg);
39 unsigned long strtoul(
const char *nptr,
char const **endptr,
int base);
45 if (c < 'a' || c >
'z')
53 if (c < 'A' || c >
'Z')
59 int max(
size_t a,
size_t b)
64 int min(
size_t a,
size_t b)
69 WEAK size_t _StringLength(
const char *src)
78 const char *orig = src;
84 return (src + n) - orig; 98 char *StringCopy(
char *dest,
const char *src)
100 char *orig_dest = dest;
112 char *StringCopyN(
char *dest,
const char *src,
size_t len)
114 char *orig_dest = dest;
115 while (len &&
LIKELY(*src))
134 int StringFormat(
char *buf,
const char *fmt, ...)
140 i = VStringFormat(buf, fmt, args);
146 WEAK int StringCompare(
const char *p1,
const char *p2)
163 WEAK int StringCompareN(
const char *p1,
const char *p2,
size_t n)
186 StringCompareNOffset(
const char *p1,
const char *p2,
size_t n,
size_t *offset)
212 *offset = orig_n - n;
217 WEAK int StringMatch(
const char *p1,
const char *p2)
232 return (*p1 == *p2) ? 0 : 1;
235 WEAK int StringMatchN(
const char *p1,
const char *p2,
size_t n)
247 for (i = 0; i < n; ++i)
259 StringMatchNOffset(
const char *p1,
const char *p2,
size_t n,
size_t *offset)
271 for (i = 0; i < n; ++i)
283 char *StringConcat(
char *dest,
const char *src)
285 char *origDest = dest;
286 while (*dest) ++dest;
297 char *StringConcatN(
char *dest,
const char *src,
size_t n)
299 char *origDest = dest;
300 while (*dest) ++dest;
301 while (src && *src && n)
314 return (c ==
' ' || c ==
'\n' || c ==
'\r' || c ==
'\t');
319 return (c >=
'A' && c <=
'Z');
324 return (c >=
'a' && c <=
'z');
329 return (c >=
'0' && c <=
'9');
334 return isupper(c) || islower(c) || isdigit(c);
338 StringToUnsignedLong(
const char *nptr,
char const **endptr,
int base)
340 register const char *s = nptr;
341 register unsigned long acc;
343 register unsigned long cutoff;
344 register int neg = 0, any, cutlim;
352 }
while (isspace(c));
360 if ((base == 0 || base == 16) && c ==
'0' && (*s ==
'x' || *s ==
'X'))
367 base = c ==
'0' ? 8 : 10;
368 cutoff = (
unsigned long) ULONG_MAX / (
unsigned long) base;
369 cutlim = (
unsigned long) ULONG_MAX % (
unsigned long) base;
370 for (acc = 0, any = 0;; c = *s++)
375 c -= isupper(c) ?
'A' - 10 :
'a' - 10;
380 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
396 *endptr = (
const char *) (any ? s - 1 : nptr);
402 #pragma GCC diagnostic push 403 #pragma GCC diagnostic ignored "-Wcast-qual" 405 char *StringFind(
const char *str,
int target)
432 char *StringReverseFind(
const char *str,
int target)
438 const char *result = NULL;
446 return (char *) result; \ 463 #pragma GCC diagnostic pop 465 int StringContains(
const char *str,
const char *search)
467 size_t alen = StringLength(str);
468 size_t blen = StringLength(search);
469 return StringContainsN(str, alen, search, blen);
472 static int isPrefix(
const char *word,
size_t wordLength,
size_t pos)
474 size_t suffixLength = wordLength - pos;
475 return StringCompareN(word, word + pos, suffixLength) == 0 ? 1 : 0;
478 static size_t suffixLength(
const char *word,
size_t wordLength,
size_t pos)
481 for (; (word[pos - i] == word[wordLength - 1 - i]) && (i < pos); i++)
487 const char *str,
size_t len,
const char *search,
size_t slen)
502 else if (len == slen)
504 return StringCompareN(str, search, slen) == 0;
509 size_t *delta2 = (
size_t *) malloc(slen *
sizeof(
size_t));
511 for (
size_t i = 0; i < 256; ++i)
517 for (
size_t i = 0; i < slen; ++i)
519 delta1[(int) search[i]] = slen - 1 - i;
523 ByteSet(delta2, 0, slen *
sizeof(
size_t));
525 ssize_t lastPrefix = slen - 1;
526 for (ssize_t i = slen - 1; i >= 0; --i)
528 if (isPrefix(search, slen, i + 1))
532 delta2[i] = lastPrefix + (slen - 1 - i);
534 for (
size_t i = 0; i < slen - 1; ++i)
536 size_t suffixLen = suffixLength(search, slen, i);
537 if (search[i - suffixLen] != search[slen - 1 - suffixLen])
539 delta2[slen - 1 - suffixLen] = slen - 1 - i + suffixLen;
543 for (
size_t i = slen - 1; i < len;)
545 ssize_t j = slen - 1;
546 while (j >= 0 && (str[i] == search[j]))
558 i += max(delta1[(
int) str[i]], delta2[j]);
565 int StringCompareCase(
566 const char *s1,
const char *s2,
int sensitive,
size_t length,
572 return StringCompareNOffset(s1, s2, length, offset);
583 *offset = StringLength(s1);
596 static size_t local = 0;
610 c = toLower(*s1) - toLower(*s2);
627 return toLower(*s1) - toLower(*s2);
630 size_t nextCharacter(
const char *s,
size_t i)
638 const uint8_t *u8buf = (
const uint8_t *) s;
639 if (
LIKELY(u8buf[i] <= 0x7F))
643 else if ((u8buf[i] & 0xC0) == 0xC0)
645 if ((u8buf[i] & 0xF8) == 0xF0)
649 else if ((u8buf[i] & 0xF0) == 0xE0)
661 size_t prevCharacter(
const char *s,
size_t i)
672 #ifndef UTILITY_LINUX 674 size_t strlen(
const char *s)
676 return StringLength(s);
679 char *strcpy(
char *dest,
const char *src)
681 return StringCopy(dest, src);
684 char *strncpy(
char *dest,
const char *src,
size_t len)
686 return StringCopyN(dest, src, len);
689 int strcmp(
const char *p1,
const char *p2)
691 return StringCompare(p1, p2);
694 int strncmp(
const char *p1,
const char *p2,
size_t n)
696 return StringCompareN(p1, p2, n);
699 char *strcat(
char *dest,
const char *src)
701 return StringConcat(dest, src);
704 char *strncat(
char *dest,
const char *src,
size_t n)
706 return StringConcatN(dest, src, n);
709 char *strchr(
const char *str,
int target)
711 return StringFind(str, target);
714 char *strrchr(
const char *str,
int target)
716 return StringReverseFind(str, target);
719 int vsprintf(
char *buf,
const char *fmt, va_list arg)
721 return VStringFormat(buf, fmt, arg);
724 unsigned long strtoul(
const char *nptr,
char const **endptr,
int base)
726 return StringToUnsignedLong(nptr, endptr, base);