string.c 2.9 KB

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