string.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include <dragonstub/dragonstub.h>
  2. char *skip_spaces(const char *str)
  3. {
  4. while (isspace(*str))
  5. ++str;
  6. return (char *)str;
  7. }
  8. unsigned int atou(const char *s)
  9. {
  10. unsigned int i = 0;
  11. while (isdigit(*s))
  12. i = i * 10 + (*s++ - '0');
  13. return i;
  14. }
  15. /* Works only for digits and letters, but small and fast */
  16. #define TOLOWER(x) ((x) | 0x20)
  17. static unsigned int simple_guess_base(const char *cp)
  18. {
  19. if (cp[0] == '0') {
  20. if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
  21. return 16;
  22. else
  23. return 8;
  24. } else {
  25. return 10;
  26. }
  27. }
  28. /**
  29. * strlen - Find the length of a string
  30. * @s: The string to be sized
  31. */
  32. size_t strlen(const char *s)
  33. {
  34. const char *sc;
  35. for (sc = s; *sc != '\0'; ++sc)
  36. /* nothing */;
  37. return sc - s;
  38. }
  39. /**
  40. * simple_strtoull - convert a string to an unsigned long long
  41. * @cp: The start of the string
  42. * @endp: A pointer to the end of the parsed string will be placed here
  43. * @base: The number base to use
  44. */
  45. unsigned long long simple_strtoull(const char *cp, char **endp,
  46. unsigned int base)
  47. {
  48. unsigned long long result = 0;
  49. if (!base)
  50. base = simple_guess_base(cp);
  51. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  52. cp += 2;
  53. while (isxdigit(*cp)) {
  54. unsigned int value;
  55. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  56. if (value >= base)
  57. break;
  58. result = result * base + value;
  59. cp++;
  60. }
  61. if (endp)
  62. *endp = (char *)cp;
  63. return result;
  64. }
  65. long simple_strtol(const char *cp, char **endp, unsigned int base)
  66. {
  67. if (*cp == '-')
  68. return -simple_strtoull(cp + 1, endp, base);
  69. return simple_strtoull(cp, endp, base);
  70. }
  71. int strcmp(const char *str1, const char *str2)
  72. {
  73. const unsigned char *s1 = (const unsigned char *)str1;
  74. const unsigned char *s2 = (const unsigned char *)str2;
  75. int delta = 0;
  76. while (*s1 || *s2) {
  77. delta = *s1 - *s2;
  78. if (delta)
  79. return delta;
  80. s1++;
  81. s2++;
  82. }
  83. return 0;
  84. }
  85. int strncmp(const char *cs, const char *ct, size_t count)
  86. {
  87. unsigned char c1, c2;
  88. while (count) {
  89. c1 = *cs++;
  90. c2 = *ct++;
  91. if (c1 != c2)
  92. return c1 < c2 ? -1 : 1;
  93. if (!c1)
  94. break;
  95. count--;
  96. }
  97. return 0;
  98. }
  99. size_t strnlen(const char *s, size_t maxlen)
  100. {
  101. const char *es = s;
  102. while (*es && maxlen) {
  103. es++;
  104. maxlen--;
  105. }
  106. return (es - s);
  107. }
  108. #define __ALIGN (sizeof(size_t))
  109. #define ONES ((size_t)-1 / UCHAR_MAX)
  110. #define HIGHS (ONES * (UCHAR_MAX / 2 + 1))
  111. #define HASZERO(x) ((x)-ONES & ~(x)&HIGHS)
  112. char *__strchrnul(const char *s, int c)
  113. {
  114. c = (unsigned char)c;
  115. if (!c)
  116. return (char *)s + strlen(s);
  117. #ifdef __GNUC__
  118. typedef size_t __attribute__((__may_alias__)) word;
  119. const word *w;
  120. for (; (uintptr_t)s % __ALIGN; s++)
  121. if (!*s || *(unsigned char *)s == c)
  122. return (char *)s;
  123. size_t k = ONES * c;
  124. for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w ^ k); w++)
  125. ;
  126. s = (void *)w;
  127. #endif
  128. for (; *s && *(unsigned char *)s != c; s++)
  129. ;
  130. return (char *)s;
  131. }
  132. char *strrchr(const char *s, int c)
  133. {
  134. return memrchr(s, c, strlen(s) + 1);
  135. }
  136. char *strchr(const char *s, int c)
  137. {
  138. char *r = __strchrnul(s, c);
  139. return *(unsigned char *)r == (unsigned char)c ? r : 0;
  140. }
  141. /**
  142. * strstr - Find the first substring in a %NUL terminated string
  143. * @s1: The string to be searched
  144. * @s2: The string to search for
  145. */
  146. char *strstr(const char *s1, const char *s2)
  147. {
  148. size_t l1, l2;
  149. l2 = strlen(s2);
  150. if (!l2)
  151. return (char *)s1;
  152. l1 = strlen(s1);
  153. while (l1 >= l2) {
  154. l1--;
  155. if (!memcmp(s1, s2, l2))
  156. return (char *)s1;
  157. s1++;
  158. }
  159. return NULL;
  160. }