cstring.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-01-12 Meco Man The first version.
  9. */
  10. #include "posix/string.h"
  11. #include <ctype.h>
  12. #include <rtthread.h>
  13. #include <stdlib.h>
  14. /**
  15. * @brief erases the data in the n bytes of the memory starting at the
  16. * location pointed to by s, by writing zeros (bytes containing '\0') to that area.
  17. *
  18. * @note The bzero() function is deprecated (marked as LEGACY in POSIX. 1-2001).
  19. */
  20. #ifndef RT_USING_PICOLIBC
  21. void bzero(void* s, size_t n)
  22. {
  23. rt_memset(s, 0, n);
  24. }
  25. #endif
  26. void bcopy(const void* src, void* dest, size_t n)
  27. {
  28. rt_memcpy(dest, src, n);
  29. }
  30. int bcmp(const void* s1, const void* s2, size_t n)
  31. {
  32. return rt_memcmp(s1, s2, n);
  33. }
  34. void explicit_bzero(void* s, size_t n)
  35. {
  36. volatile char* vs = (volatile char*)s;
  37. while (n)
  38. {
  39. *vs++ = 0;
  40. n--;
  41. }
  42. }
  43. char* index(const char* s, int c)
  44. {
  45. return strchr(s, c);
  46. }
  47. char* rindex(const char* s, int c)
  48. {
  49. return strrchr(s, c);
  50. }
  51. int ffs(int i)
  52. {
  53. int bit;
  54. if (0 == i)
  55. return 0;
  56. for (bit = 1; !(i & 1); ++bit)
  57. i >>= 1;
  58. return bit;
  59. }
  60. int ffsl(long i)
  61. {
  62. int bit;
  63. if (0 == i)
  64. return 0;
  65. for (bit = 1; !(i & 1); ++bit)
  66. i >>= 1;
  67. return bit;
  68. }
  69. int ffsll(long long i)
  70. {
  71. int bit;
  72. if (0 == i)
  73. return 0;
  74. for (bit = 1; !(i & 1); ++bit)
  75. i >>= 1;
  76. return bit;
  77. }
  78. /**
  79. * @brief The memchr() function scans the initial n bytes of the memory area pointed to
  80. * by s for the first instance of c. Both c and the bytes of the memory area
  81. * pointed to by s are interpreted as unsigned char.
  82. *
  83. * @note This function is GNU extension, available since glibc 2.1.91.
  84. */
  85. void* memrchr(const void* ptr, int ch, size_t pos)
  86. {
  87. char* end = (char*)ptr + pos - 1;
  88. while (end != ptr)
  89. {
  90. if (*end == ch)
  91. return end;
  92. end--;
  93. }
  94. return (*end == ch) ? (end) : (NULL);
  95. }
  96. size_t strnlen(const char *s, size_t maxlen)
  97. {
  98. const char *sc;
  99. for (sc = s; maxlen != 0 && *sc != '\0'; maxlen--, ++sc);
  100. return sc - s;
  101. }
  102. char* strchrnul(const char* s, int c)
  103. {
  104. while (*s != '\0' && *s != c)
  105. s++;
  106. return (char*)s;
  107. }
  108. int strcasecmp(const char* s1, const char* s2)
  109. {
  110. const unsigned char* u1 = (const unsigned char*)s1;
  111. const unsigned char* u2 = (const unsigned char*)s2;
  112. int result;
  113. while ((result = tolower(*u1) - tolower(*u2)) == 0 && *u1 != 0)
  114. {
  115. u1++;
  116. u2++;
  117. }
  118. return result;
  119. }
  120. int strncasecmp(const char* s1, const char* s2, size_t n)
  121. {
  122. const unsigned char* u1 = (const unsigned char*)s1;
  123. const unsigned char* u2 = (const unsigned char*)s2;
  124. int result;
  125. for (; n != 0; n--)
  126. {
  127. result = tolower(*u1) - tolower(*u2);
  128. if (result)
  129. return result;
  130. if (*u1 == 0)
  131. return 0;
  132. u1++;
  133. u2++;
  134. }
  135. return 0;
  136. }
  137. char *strdup(const char *s)
  138. {
  139. char *news = (char *)malloc(strlen(s) + 1);
  140. if (news)
  141. {
  142. strcpy(news, s);
  143. }
  144. return news;
  145. }
  146. char *strndup(const char *s, size_t size)
  147. {
  148. size_t nsize = strnlen(s, size);
  149. char *news = (char *)malloc(nsize + 1);
  150. if (news)
  151. {
  152. rt_memcpy(news, s, nsize);
  153. news[nsize] = '\0';
  154. }
  155. return news;
  156. }
  157. rt_weak char *strtok_r(char *str, const char *delim, char **saveptr)
  158. {
  159. char *pbegin;
  160. char *pend = NULL;
  161. if (str)
  162. {
  163. pbegin = str;
  164. }
  165. else if (saveptr && *saveptr)
  166. {
  167. pbegin = *saveptr;
  168. }
  169. else
  170. {
  171. return NULL;
  172. }
  173. for (;*pbegin && strchr(delim, *pbegin) != NULL; pbegin++);
  174. if (!*pbegin)
  175. {
  176. return NULL;
  177. }
  178. for (pend = pbegin + 1; *pend && strchr(delim, *pend) == NULL; pend++);
  179. if (*pend)
  180. {
  181. *pend++ = '\0';
  182. }
  183. if (saveptr)
  184. {
  185. *saveptr = pend;
  186. }
  187. return pbegin;
  188. }