string.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "ctype.h"
  15. #include "string.h"
  16. /*
  17. * Undef these macros so that the functions that we provide
  18. * here will have the correct names regardless of how string.h
  19. * may have chosen to #define them.
  20. */
  21. #undef memcpy
  22. #undef memset
  23. #undef memcmp
  24. int memcmp(const void *s1, const void *s2, size_t len)
  25. {
  26. u8 diff;
  27. asm("repe; cmpsb; setnz %0"
  28. : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
  29. return diff;
  30. }
  31. int strcmp(const char *str1, const char *str2)
  32. {
  33. const unsigned char *s1 = (const unsigned char *)str1;
  34. const unsigned char *s2 = (const unsigned char *)str2;
  35. int delta = 0;
  36. while (*s1 || *s2) {
  37. delta = *s1 - *s2;
  38. if (delta)
  39. return delta;
  40. s1++;
  41. s2++;
  42. }
  43. return 0;
  44. }
  45. int strncmp(const char *cs, const char *ct, size_t count)
  46. {
  47. unsigned char c1, c2;
  48. while (count) {
  49. c1 = *cs++;
  50. c2 = *ct++;
  51. if (c1 != c2)
  52. return c1 < c2 ? -1 : 1;
  53. if (!c1)
  54. break;
  55. count--;
  56. }
  57. return 0;
  58. }
  59. size_t strnlen(const char *s, size_t maxlen)
  60. {
  61. const char *es = s;
  62. while (*es && maxlen) {
  63. es++;
  64. maxlen--;
  65. }
  66. return (es - s);
  67. }
  68. unsigned int atou(const char *s)
  69. {
  70. unsigned int i = 0;
  71. while (isdigit(*s))
  72. i = i * 10 + (*s++ - '0');
  73. return i;
  74. }
  75. /* Works only for digits and letters, but small and fast */
  76. #define TOLOWER(x) ((x) | 0x20)
  77. static unsigned int simple_guess_base(const char *cp)
  78. {
  79. if (cp[0] == '0') {
  80. if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
  81. return 16;
  82. else
  83. return 8;
  84. } else {
  85. return 10;
  86. }
  87. }
  88. /**
  89. * simple_strtoull - convert a string to an unsigned long long
  90. * @cp: The start of the string
  91. * @endp: A pointer to the end of the parsed string will be placed here
  92. * @base: The number base to use
  93. */
  94. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  95. {
  96. unsigned long long result = 0;
  97. if (!base)
  98. base = simple_guess_base(cp);
  99. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  100. cp += 2;
  101. while (isxdigit(*cp)) {
  102. unsigned int value;
  103. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  104. if (value >= base)
  105. break;
  106. result = result * base + value;
  107. cp++;
  108. }
  109. if (endp)
  110. *endp = (char *)cp;
  111. return result;
  112. }
  113. /**
  114. * strlen - Find the length of a string
  115. * @s: The string to be sized
  116. */
  117. size_t strlen(const char *s)
  118. {
  119. const char *sc;
  120. for (sc = s; *sc != '\0'; ++sc)
  121. /* nothing */;
  122. return sc - s;
  123. }
  124. /**
  125. * strstr - Find the first substring in a %NUL terminated string
  126. * @s1: The string to be searched
  127. * @s2: The string to search for
  128. */
  129. char *strstr(const char *s1, const char *s2)
  130. {
  131. size_t l1, l2;
  132. l2 = strlen(s2);
  133. if (!l2)
  134. return (char *)s1;
  135. l1 = strlen(s1);
  136. while (l1 >= l2) {
  137. l1--;
  138. if (!memcmp(s1, s2, l2))
  139. return (char *)s1;
  140. s1++;
  141. }
  142. return NULL;
  143. }