string.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  6. * This file is part of the Linux kernel, and is made available under
  7. * the terms of the GNU General Public License version 2.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * Very basic string functions
  12. */
  13. #include <linux/types.h>
  14. #include <asm/asm.h>
  15. #include "ctype.h"
  16. #include "string.h"
  17. /*
  18. * Undef these macros so that the functions that we provide
  19. * here will have the correct names regardless of how string.h
  20. * may have chosen to #define them.
  21. */
  22. #undef memcpy
  23. #undef memset
  24. #undef memcmp
  25. int memcmp(const void *s1, const void *s2, size_t len)
  26. {
  27. bool diff;
  28. asm("repe; cmpsb" CC_SET(nz)
  29. : CC_OUT(nz) (diff), "+D" (s1), "+S" (s2), "+c" (len));
  30. return diff;
  31. }
  32. /*
  33. * Clang may lower `memcmp == 0` to `bcmp == 0`.
  34. */
  35. int bcmp(const void *s1, const void *s2, size_t len)
  36. {
  37. return memcmp(s1, s2, len);
  38. }
  39. int strcmp(const char *str1, const char *str2)
  40. {
  41. const unsigned char *s1 = (const unsigned char *)str1;
  42. const unsigned char *s2 = (const unsigned char *)str2;
  43. int delta = 0;
  44. while (*s1 || *s2) {
  45. delta = *s1 - *s2;
  46. if (delta)
  47. return delta;
  48. s1++;
  49. s2++;
  50. }
  51. return 0;
  52. }
  53. int strncmp(const char *cs, const char *ct, size_t count)
  54. {
  55. unsigned char c1, c2;
  56. while (count) {
  57. c1 = *cs++;
  58. c2 = *ct++;
  59. if (c1 != c2)
  60. return c1 < c2 ? -1 : 1;
  61. if (!c1)
  62. break;
  63. count--;
  64. }
  65. return 0;
  66. }
  67. size_t strnlen(const char *s, size_t maxlen)
  68. {
  69. const char *es = s;
  70. while (*es && maxlen) {
  71. es++;
  72. maxlen--;
  73. }
  74. return (es - s);
  75. }
  76. unsigned int atou(const char *s)
  77. {
  78. unsigned int i = 0;
  79. while (isdigit(*s))
  80. i = i * 10 + (*s++ - '0');
  81. return i;
  82. }
  83. /* Works only for digits and letters, but small and fast */
  84. #define TOLOWER(x) ((x) | 0x20)
  85. static unsigned int simple_guess_base(const char *cp)
  86. {
  87. if (cp[0] == '0') {
  88. if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
  89. return 16;
  90. else
  91. return 8;
  92. } else {
  93. return 10;
  94. }
  95. }
  96. /**
  97. * simple_strtoull - convert a string to an unsigned long long
  98. * @cp: The start of the string
  99. * @endp: A pointer to the end of the parsed string will be placed here
  100. * @base: The number base to use
  101. */
  102. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  103. {
  104. unsigned long long result = 0;
  105. if (!base)
  106. base = simple_guess_base(cp);
  107. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  108. cp += 2;
  109. while (isxdigit(*cp)) {
  110. unsigned int value;
  111. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  112. if (value >= base)
  113. break;
  114. result = result * base + value;
  115. cp++;
  116. }
  117. if (endp)
  118. *endp = (char *)cp;
  119. return result;
  120. }
  121. long simple_strtol(const char *cp, char **endp, unsigned int base)
  122. {
  123. if (*cp == '-')
  124. return -simple_strtoull(cp + 1, endp, base);
  125. return simple_strtoull(cp, endp, base);
  126. }
  127. /**
  128. * strlen - Find the length of a string
  129. * @s: The string to be sized
  130. */
  131. size_t strlen(const char *s)
  132. {
  133. const char *sc;
  134. for (sc = s; *sc != '\0'; ++sc)
  135. /* nothing */;
  136. return sc - s;
  137. }
  138. /**
  139. * strstr - Find the first substring in a %NUL terminated string
  140. * @s1: The string to be searched
  141. * @s2: The string to search for
  142. */
  143. char *strstr(const char *s1, const char *s2)
  144. {
  145. size_t l1, l2;
  146. l2 = strlen(s2);
  147. if (!l2)
  148. return (char *)s1;
  149. l1 = strlen(s1);
  150. while (l1 >= l2) {
  151. l1--;
  152. if (!memcmp(s1, s2, l2))
  153. return (char *)s1;
  154. s1++;
  155. }
  156. return NULL;
  157. }
  158. /**
  159. * strchr - Find the first occurrence of the character c in the string s.
  160. * @s: the string to be searched
  161. * @c: the character to search for
  162. */
  163. char *strchr(const char *s, int c)
  164. {
  165. while (*s != (char)c)
  166. if (*s++ == '\0')
  167. return NULL;
  168. return (char *)s;
  169. }