string0.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. #include <string.h>
  28. #define likely(x) __builtin_expect((x), 1)
  29. void *memcpy(void *dst, const void *src, size_t n) {
  30. if (likely(!(((uintptr_t)dst) & 3) && !(((uintptr_t)src) & 3))) {
  31. // pointers aligned
  32. uint32_t *d = dst;
  33. const uint32_t *s = src;
  34. // copy words first
  35. for (size_t i = (n >> 2); i; i--) {
  36. *d++ = *s++;
  37. }
  38. if (n & 2) {
  39. // copy half-word
  40. *(uint16_t*)d = *(const uint16_t*)s;
  41. d = (uint32_t*)((uint16_t*)d + 1);
  42. s = (const uint32_t*)((const uint16_t*)s + 1);
  43. }
  44. if (n & 1) {
  45. // copy byte
  46. *((uint8_t*)d) = *((const uint8_t*)s);
  47. }
  48. } else {
  49. // unaligned access, copy bytes
  50. uint8_t *d = dst;
  51. const uint8_t *s = src;
  52. for (; n; n--) {
  53. *d++ = *s++;
  54. }
  55. }
  56. return dst;
  57. }
  58. void *memmove(void *dest, const void *src, size_t n) {
  59. if (src < dest && (uint8_t*)dest < (const uint8_t*)src + n) {
  60. // need to copy backwards
  61. uint8_t *d = (uint8_t*)dest + n - 1;
  62. const uint8_t *s = (const uint8_t*)src + n - 1;
  63. for (; n > 0; n--) {
  64. *d-- = *s--;
  65. }
  66. return dest;
  67. } else {
  68. // can use normal memcpy
  69. return memcpy(dest, src, n);
  70. }
  71. }
  72. void *memset(void *s, int c, size_t n) {
  73. if (c == 0 && ((uintptr_t)s & 3) == 0) {
  74. // aligned store of 0
  75. uint32_t *s32 = s;
  76. for (size_t i = n >> 2; i > 0; i--) {
  77. *s32++ = 0;
  78. }
  79. if (n & 2) {
  80. *((uint16_t*)s32) = 0;
  81. s32 = (uint32_t*)((uint16_t*)s32 + 1);
  82. }
  83. if (n & 1) {
  84. *((uint8_t*)s32) = 0;
  85. }
  86. } else {
  87. uint8_t *s2 = s;
  88. for (; n > 0; n--) {
  89. *s2++ = c;
  90. }
  91. }
  92. return s;
  93. }
  94. int memcmp(const void *s1, const void *s2, size_t n) {
  95. const uint8_t *s1_8 = s1;
  96. const uint8_t *s2_8 = s2;
  97. while (n--) {
  98. char c1 = *s1_8++;
  99. char c2 = *s2_8++;
  100. if (c1 < c2) return -1;
  101. else if (c1 > c2) return 1;
  102. }
  103. return 0;
  104. }
  105. void *memchr(const void *s, int c, size_t n) {
  106. if (n != 0) {
  107. const unsigned char *p = s;
  108. do {
  109. if (*p++ == c)
  110. return ((void *)(p - 1));
  111. } while (--n != 0);
  112. }
  113. return 0;
  114. }
  115. size_t strlen(const char *str) {
  116. int len = 0;
  117. for (const char *s = str; *s; s++) {
  118. len += 1;
  119. }
  120. return len;
  121. }
  122. int strcmp(const char *s1, const char *s2) {
  123. while (*s1 && *s2) {
  124. char c1 = *s1++; // XXX UTF8 get char, next char
  125. char c2 = *s2++; // XXX UTF8 get char, next char
  126. if (c1 < c2) return -1;
  127. else if (c1 > c2) return 1;
  128. }
  129. if (*s2) return -1;
  130. else if (*s1) return 1;
  131. else return 0;
  132. }
  133. int strncmp(const char *s1, const char *s2, size_t n) {
  134. while (*s1 && *s2 && n > 0) {
  135. char c1 = *s1++; // XXX UTF8 get char, next char
  136. char c2 = *s2++; // XXX UTF8 get char, next char
  137. n--;
  138. if (c1 < c2) return -1;
  139. else if (c1 > c2) return 1;
  140. }
  141. if (n == 0) return 0;
  142. else if (*s2) return -1;
  143. else if (*s1) return 1;
  144. else return 0;
  145. }
  146. char *strcpy(char *dest, const char *src) {
  147. char *d = dest;
  148. while (*src) {
  149. *d++ = *src++;
  150. }
  151. *d = '\0';
  152. return dest;
  153. }
  154. // needed because gcc optimises strcpy + strcat to this
  155. char *stpcpy(char *dest, const char *src) {
  156. while (*src) {
  157. *dest++ = *src++;
  158. }
  159. *dest = '\0';
  160. return dest;
  161. }
  162. char *strcat(char *dest, const char *src) {
  163. char *d = dest;
  164. while (*d) {
  165. d++;
  166. }
  167. while (*src) {
  168. *d++ = *src++;
  169. }
  170. *d = '\0';
  171. return dest;
  172. }
  173. // Public Domain implementation of strchr from:
  174. // http://en.wikibooks.org/wiki/C_Programming/Strings#The_strchr_function
  175. char *strchr(const char *s, int c)
  176. {
  177. /* Scan s for the character. When this loop is finished,
  178. s will either point to the end of the string or the
  179. character we were looking for. */
  180. while (*s != '\0' && *s != (char)c)
  181. s++;
  182. return ((*s == c) ? (char *) s : 0);
  183. }
  184. // Public Domain implementation of strstr from:
  185. // http://en.wikibooks.org/wiki/C_Programming/Strings#The_strstr_function
  186. char *strstr(const char *haystack, const char *needle)
  187. {
  188. size_t needlelen;
  189. /* Check for the null needle case. */
  190. if (*needle == '\0')
  191. return (char *) haystack;
  192. needlelen = strlen(needle);
  193. for (; (haystack = strchr(haystack, *needle)) != 0; haystack++)
  194. if (strncmp(haystack, needle, needlelen) == 0)
  195. return (char *) haystack;
  196. return 0;
  197. }