string.c 3.0 KB

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