string.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. int strcmp(const char *str1, const char *str2)
  33. {
  34. const unsigned char *s1 = (const unsigned char *)str1;
  35. const unsigned char *s2 = (const unsigned char *)str2;
  36. int delta = 0;
  37. while (*s1 || *s2) {
  38. delta = *s1 - *s2;
  39. if (delta)
  40. return delta;
  41. s1++;
  42. s2++;
  43. }
  44. return 0;
  45. }
  46. int strncmp(const char *cs, const char *ct, size_t count)
  47. {
  48. unsigned char c1, c2;
  49. while (count) {
  50. c1 = *cs++;
  51. c2 = *ct++;
  52. if (c1 != c2)
  53. return c1 < c2 ? -1 : 1;
  54. if (!c1)
  55. break;
  56. count--;
  57. }
  58. return 0;
  59. }
  60. size_t strnlen(const char *s, size_t maxlen)
  61. {
  62. const char *es = s;
  63. while (*es && maxlen) {
  64. es++;
  65. maxlen--;
  66. }
  67. return (es - s);
  68. }
  69. unsigned int atou(const char *s)
  70. {
  71. unsigned int i = 0;
  72. while (isdigit(*s))
  73. i = i * 10 + (*s++ - '0');
  74. return i;
  75. }
  76. /* Works only for digits and letters, but small and fast */
  77. #define TOLOWER(x) ((x) | 0x20)
  78. static unsigned int simple_guess_base(const char *cp)
  79. {
  80. if (cp[0] == '0') {
  81. if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
  82. return 16;
  83. else
  84. return 8;
  85. } else {
  86. return 10;
  87. }
  88. }
  89. /**
  90. * simple_strtoull - convert a string to an unsigned long long
  91. * @cp: The start of the string
  92. * @endp: A pointer to the end of the parsed string will be placed here
  93. * @base: The number base to use
  94. */
  95. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  96. {
  97. unsigned long long result = 0;
  98. if (!base)
  99. base = simple_guess_base(cp);
  100. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  101. cp += 2;
  102. while (isxdigit(*cp)) {
  103. unsigned int value;
  104. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  105. if (value >= base)
  106. break;
  107. result = result * base + value;
  108. cp++;
  109. }
  110. if (endp)
  111. *endp = (char *)cp;
  112. return result;
  113. }
  114. long simple_strtol(const char *cp, char **endp, unsigned int base)
  115. {
  116. if (*cp == '-')
  117. return -simple_strtoull(cp + 1, endp, base);
  118. return simple_strtoull(cp, endp, base);
  119. }
  120. /**
  121. * strlen - Find the length of a string
  122. * @s: The string to be sized
  123. */
  124. size_t strlen(const char *s)
  125. {
  126. const char *sc;
  127. for (sc = s; *sc != '\0'; ++sc)
  128. /* nothing */;
  129. return sc - s;
  130. }
  131. /**
  132. * strstr - Find the first substring in a %NUL terminated string
  133. * @s1: The string to be searched
  134. * @s2: The string to search for
  135. */
  136. char *strstr(const char *s1, const char *s2)
  137. {
  138. size_t l1, l2;
  139. l2 = strlen(s2);
  140. if (!l2)
  141. return (char *)s1;
  142. l1 = strlen(s1);
  143. while (l1 >= l2) {
  144. l1--;
  145. if (!memcmp(s1, s2, l2))
  146. return (char *)s1;
  147. s1++;
  148. }
  149. return NULL;
  150. }
  151. /**
  152. * strchr - Find the first occurrence of the character c in the string s.
  153. * @s: the string to be searched
  154. * @c: the character to search for
  155. */
  156. char *strchr(const char *s, int c)
  157. {
  158. while (*s != (char)c)
  159. if (*s++ == '\0')
  160. return NULL;
  161. return (char *)s;
  162. }