string.c 2.9 KB

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