kstring.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-03-10 Meco Man the first version
  9. */
  10. #include <rtthread.h>
  11. #ifdef RT_KLIBC_USING_STDLIB
  12. #include <string.h>
  13. #endif /* RT_KLIBC_USING_STDLIB */
  14. #define DBG_TAG "kernel.string"
  15. #ifdef RT_DEBUG_DEVICE
  16. #define DBG_LVL DBG_LOG
  17. #else
  18. #define DBG_LVL DBG_WARNING
  19. #endif /* defined (RT_DEBUG_DEVICE) */
  20. #include <rtdbg.h>
  21. /**
  22. * @brief This function will set the content of memory to specified value.
  23. *
  24. * @param s is the address of source memory, point to the memory block to be filled.
  25. *
  26. * @param c is the value to be set. The value is passed in int form, but the function
  27. * uses the unsigned character form of the value when filling the memory block.
  28. *
  29. * @param count number of bytes to be set.
  30. *
  31. * @return The address of source memory.
  32. */
  33. rt_weak void *rt_memset(void *s, int c, rt_ubase_t count)
  34. {
  35. #if defined(RT_KLIBC_USING_STDLIB_MEMORY)
  36. return memset(s, c, count);
  37. #elif defined(RT_KLIBC_USING_TINY_SIZE)
  38. char *xs = (char *)s;
  39. while (count--)
  40. *xs++ = c;
  41. return s;
  42. #else
  43. #define LBLOCKSIZE (sizeof(rt_ubase_t))
  44. #define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))
  45. #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
  46. unsigned int i = 0;
  47. char *m = (char *)s;
  48. unsigned long buffer = 0;
  49. unsigned long *aligned_addr = RT_NULL;
  50. unsigned char d = (unsigned int)c & (unsigned char)(-1); /* To avoid sign extension, copy C to an
  51. unsigned variable. (unsigned)((char)(-1))=0xFF for 8bit and =0xFFFF for 16bit: word independent */
  52. RT_ASSERT(LBLOCKSIZE == 2 || LBLOCKSIZE == 4 || LBLOCKSIZE == 8);
  53. if (!TOO_SMALL(count) && !UNALIGNED(s))
  54. {
  55. /* If we get this far, we know that count is large and s is word-aligned. */
  56. aligned_addr = (unsigned long *)s;
  57. /* Store d into each char sized location in buffer so that
  58. * we can set large blocks quickly.
  59. */
  60. for (i = 0; i < LBLOCKSIZE; i++)
  61. {
  62. *(((unsigned char *)&buffer)+i) = d;
  63. }
  64. while (count >= LBLOCKSIZE * 4)
  65. {
  66. *aligned_addr++ = buffer;
  67. *aligned_addr++ = buffer;
  68. *aligned_addr++ = buffer;
  69. *aligned_addr++ = buffer;
  70. count -= 4 * LBLOCKSIZE;
  71. }
  72. while (count >= LBLOCKSIZE)
  73. {
  74. *aligned_addr++ = buffer;
  75. count -= LBLOCKSIZE;
  76. }
  77. /* Pick up the remainder with a bytewise loop. */
  78. m = (char *)aligned_addr;
  79. }
  80. while (count--)
  81. {
  82. *m++ = (char)d;
  83. }
  84. return s;
  85. #undef LBLOCKSIZE
  86. #undef UNALIGNED
  87. #undef TOO_SMALL
  88. #endif /* RT_KLIBC_USING_STDLIB_MEMORY */
  89. }
  90. RTM_EXPORT(rt_memset);
  91. /**
  92. * @brief This function will copy memory content from source address to destination address.
  93. *
  94. * @param dst is the address of destination memory, points to the copied content.
  95. *
  96. * @param src is the address of source memory, pointing to the data source to be copied.
  97. *
  98. * @param count is the copied length.
  99. *
  100. * @return The address of destination memory
  101. */
  102. rt_weak void *rt_memcpy(void *dst, const void *src, rt_ubase_t count)
  103. {
  104. #if defined(RT_KLIBC_USING_STDLIB_MEMORY)
  105. return memcpy(dst, src, count);
  106. #elif defined(RT_KLIBC_USING_TINY_SIZE)
  107. char *tmp = (char *)dst, *s = (char *)src;
  108. rt_ubase_t len = 0;
  109. if (tmp <= s || tmp > (s + count))
  110. {
  111. while (count--)
  112. *tmp ++ = *s ++;
  113. }
  114. else
  115. {
  116. for (len = count; len > 0; len --)
  117. tmp[len - 1] = s[len - 1];
  118. }
  119. return dst;
  120. #else
  121. #define UNALIGNED(X, Y) \
  122. (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
  123. #define BIGBLOCKSIZE (sizeof (long) << 2)
  124. #define LITTLEBLOCKSIZE (sizeof (long))
  125. #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
  126. char *dst_ptr = (char *)dst;
  127. char *src_ptr = (char *)src;
  128. long *aligned_dst = RT_NULL;
  129. long *aligned_src = RT_NULL;
  130. rt_ubase_t len = count;
  131. /* If the size is small, or either SRC or DST is unaligned,
  132. then punt into the byte copy loop. This should be rare. */
  133. if (!TOO_SMALL(len) && !UNALIGNED(src_ptr, dst_ptr))
  134. {
  135. aligned_dst = (long *)dst_ptr;
  136. aligned_src = (long *)src_ptr;
  137. /* Copy 4X long words at a time if possible. */
  138. while (len >= BIGBLOCKSIZE)
  139. {
  140. *aligned_dst++ = *aligned_src++;
  141. *aligned_dst++ = *aligned_src++;
  142. *aligned_dst++ = *aligned_src++;
  143. *aligned_dst++ = *aligned_src++;
  144. len -= BIGBLOCKSIZE;
  145. }
  146. /* Copy one long word at a time if possible. */
  147. while (len >= LITTLEBLOCKSIZE)
  148. {
  149. *aligned_dst++ = *aligned_src++;
  150. len -= LITTLEBLOCKSIZE;
  151. }
  152. /* Pick up any residual with a byte copier. */
  153. dst_ptr = (char *)aligned_dst;
  154. src_ptr = (char *)aligned_src;
  155. }
  156. while (len--)
  157. *dst_ptr++ = *src_ptr++;
  158. return dst;
  159. #undef UNALIGNED
  160. #undef BIGBLOCKSIZE
  161. #undef LITTLEBLOCKSIZE
  162. #undef TOO_SMALL
  163. #endif /* RT_KLIBC_USING_STDLIB_MEMORY */
  164. }
  165. RTM_EXPORT(rt_memcpy);
  166. /**
  167. * @brief This function will move memory content from source address to destination
  168. * address. If the destination memory does not overlap with the source memory,
  169. * the function is the same as memcpy().
  170. *
  171. * @param dest is the address of destination memory, points to the copied content.
  172. *
  173. * @param src is the address of source memory, point to the data source to be copied.
  174. *
  175. * @param n is the copied length.
  176. *
  177. * @return The address of destination memory.
  178. */
  179. void *rt_memmove(void *dest, const void *src, rt_size_t n)
  180. {
  181. #ifdef RT_KLIBC_USING_STDLIB_MEMORY
  182. return memmove(dest, src, n);
  183. #else
  184. char *tmp = (char *)dest, *s = (char *)src;
  185. if (s < tmp && tmp < s + n)
  186. {
  187. tmp += n;
  188. s += n;
  189. while (n--)
  190. *(--tmp) = *(--s);
  191. }
  192. else
  193. {
  194. while (n--)
  195. *tmp++ = *s++;
  196. }
  197. return dest;
  198. #endif /* RT_KLIBC_USING_STDLIB_MEMORY */
  199. }
  200. RTM_EXPORT(rt_memmove);
  201. /**
  202. * @brief This function will compare two areas of memory.
  203. *
  204. * @param cs is a block of memory.
  205. *
  206. * @param ct is another block of memory.
  207. *
  208. * @param count is the size of the area.
  209. *
  210. * @return Compare the results:
  211. * If the result < 0, cs is smaller than ct.
  212. * If the result > 0, cs is greater than ct.
  213. * If the result = 0, cs is equal to ct.
  214. */
  215. rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_size_t count)
  216. {
  217. #ifdef RT_KLIBC_USING_STDLIB_MEMORY
  218. return memcmp(cs, ct, count);
  219. #else
  220. const unsigned char *su1 = RT_NULL, *su2 = RT_NULL;
  221. int res = 0;
  222. for (su1 = (const unsigned char *)cs, su2 = (const unsigned char *)ct; 0 < count; ++su1, ++su2, count--)
  223. if ((res = *su1 - *su2) != 0)
  224. break;
  225. return res;
  226. #endif /* RT_KLIBC_USING_STDLIB_MEMORY */
  227. }
  228. RTM_EXPORT(rt_memcmp);
  229. /**
  230. * @brief This function will return the first occurrence of a string, without the
  231. * terminator '\0'.
  232. *
  233. * @param s1 is the source string.
  234. *
  235. * @param s2 is the find string.
  236. *
  237. * @return The first occurrence of a s2 in s1, or RT_NULL if no found.
  238. */
  239. char *rt_strstr(const char *s1, const char *s2)
  240. {
  241. #ifdef RT_KLIBC_USING_STDLIB
  242. return strstr(s1, s2);
  243. #else
  244. int l1 = 0, l2 = 0;
  245. l2 = rt_strlen(s2);
  246. if (!l2)
  247. {
  248. return (char *)s1;
  249. }
  250. l1 = rt_strlen(s1);
  251. while (l1 >= l2)
  252. {
  253. l1 --;
  254. if (!rt_memcmp(s1, s2, l2))
  255. {
  256. return (char *)s1;
  257. }
  258. s1 ++;
  259. }
  260. return RT_NULL;
  261. #endif /* RT_KLIBC_USING_STDLIB */
  262. }
  263. RTM_EXPORT(rt_strstr);
  264. /**
  265. * @brief This function will compare two strings while ignoring differences in case
  266. *
  267. * @param a is the string to be compared.
  268. *
  269. * @param b is the string to be compared.
  270. *
  271. * @return Compare the results:
  272. * If the result < 0, a is smaller than a.
  273. * If the result > 0, a is greater than a.
  274. * If the result = 0, a is equal to a.
  275. */
  276. rt_int32_t rt_strcasecmp(const char *a, const char *b)
  277. {
  278. int ca = 0, cb = 0;
  279. do
  280. {
  281. ca = *a++ & 0xff;
  282. cb = *b++ & 0xff;
  283. if (ca >= 'A' && ca <= 'Z')
  284. ca += 'a' - 'A';
  285. if (cb >= 'A' && cb <= 'Z')
  286. cb += 'a' - 'A';
  287. }
  288. while (ca == cb && ca != '\0');
  289. return ca - cb;
  290. }
  291. RTM_EXPORT(rt_strcasecmp);
  292. /**
  293. * @brief This function will copy string no more than n bytes.
  294. *
  295. * @param dst points to the address used to store the copied content.
  296. *
  297. * @param src is the string to be copied.
  298. *
  299. * @param n is the maximum copied length.
  300. *
  301. * @return The address where the copied content is stored.
  302. */
  303. char *rt_strncpy(char *dst, const char *src, rt_size_t n)
  304. {
  305. #ifdef RT_KLIBC_USING_STDLIB
  306. return strncpy(dst, src, n);
  307. #else
  308. if (n != 0)
  309. {
  310. char *d = dst;
  311. const char *s = src;
  312. do
  313. {
  314. if ((*d++ = *s++) == 0)
  315. {
  316. /* NUL pad the remaining n-1 bytes */
  317. while (--n != 0)
  318. {
  319. *d++ = 0;
  320. }
  321. break;
  322. }
  323. } while (--n != 0);
  324. }
  325. return (dst);
  326. #endif /* RT_KLIBC_USING_STDLIB */
  327. }
  328. RTM_EXPORT(rt_strncpy);
  329. /**
  330. * @brief This function will copy string.
  331. *
  332. * @param dst points to the address used to store the copied content.
  333. *
  334. * @param src is the string to be copied.
  335. *
  336. * @return The address where the copied content is stored.
  337. */
  338. char *rt_strcpy(char *dst, const char *src)
  339. {
  340. #ifdef RT_KLIBC_USING_STDLIB
  341. return strcpy(dst, src);
  342. #else
  343. char *dest = dst;
  344. while (*src != '\0')
  345. {
  346. *dst = *src;
  347. dst++;
  348. src++;
  349. }
  350. *dst = '\0';
  351. return dest;
  352. #endif /* RT_KLIBC_USING_STDLIB */
  353. }
  354. RTM_EXPORT(rt_strcpy);
  355. /**
  356. * @brief This function will compare two strings with specified maximum length.
  357. *
  358. * @param cs is the string to be compared.
  359. *
  360. * @param ct is the string to be compared.
  361. *
  362. * @param count is the maximum compare length.
  363. *
  364. * @return Compare the results:
  365. * If the result < 0, cs is smaller than ct.
  366. * If the result > 0, cs is greater than ct.
  367. * If the result = 0, cs is equal to ct.
  368. */
  369. rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_size_t count)
  370. {
  371. #ifdef RT_KLIBC_USING_STDLIB
  372. return strncmp(cs, ct, count);
  373. #else
  374. signed char res = 0;
  375. while (count)
  376. {
  377. if ((res = *cs - *ct++) != 0 || !*cs++)
  378. {
  379. break;
  380. }
  381. count --;
  382. }
  383. return res;
  384. #endif /* RT_KLIBC_USING_STDLIB */
  385. }
  386. RTM_EXPORT(rt_strncmp);
  387. /**
  388. * @brief This function will compare two strings without specified length.
  389. *
  390. * @param cs is the string to be compared.
  391. *
  392. * @param ct is the string to be compared.
  393. *
  394. * @return Compare the results:
  395. * If the result < 0, cs is smaller than ct.
  396. * If the result > 0, cs is greater than ct.
  397. * If the result = 0, cs is equal to ct.
  398. */
  399. rt_int32_t rt_strcmp(const char *cs, const char *ct)
  400. {
  401. #ifdef RT_KLIBC_USING_STDLIB
  402. return strcmp(cs, ct);
  403. #else
  404. while (*cs && *cs == *ct)
  405. {
  406. cs++;
  407. ct++;
  408. }
  409. return (*cs - *ct);
  410. #endif /* RT_KLIBC_USING_STDLIB */
  411. }
  412. RTM_EXPORT(rt_strcmp);
  413. /**
  414. * @brief This function will return the length of a string, which terminate will
  415. * null character.
  416. *
  417. * @param s is the string
  418. *
  419. * @return The length of string.
  420. */
  421. rt_size_t rt_strlen(const char *s)
  422. {
  423. #ifdef RT_KLIBC_USING_STDLIB
  424. return strlen(s);
  425. #else
  426. const char *sc = RT_NULL;
  427. for (sc = s; *sc != '\0'; ++sc);
  428. return sc - s;
  429. #endif /* RT_KLIBC_USING_STDLIB */
  430. }
  431. RTM_EXPORT(rt_strlen);
  432. /**
  433. * @brief The strnlen() function returns the number of characters in the
  434. * string pointed to by s, excluding the terminating null byte ('\0'),
  435. * but at most maxlen. In doing this, strnlen() looks only at the
  436. * first maxlen characters in the string pointed to by s and never
  437. * beyond s+maxlen.
  438. *
  439. * @param s is the string.
  440. *
  441. * @param maxlen is the max size.
  442. *
  443. * @return The length of string.
  444. */
  445. rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen)
  446. {
  447. const char *sc;
  448. for (sc = s; *sc != '\0' && (rt_ubase_t)(sc - s) < maxlen; ++sc);
  449. return sc - s;
  450. }
  451. RTM_EXPORT(rt_strnlen);
  452. #ifdef RT_USING_HEAP
  453. /**
  454. * @brief This function will duplicate a string.
  455. *
  456. * @param s is the string to be duplicated.
  457. *
  458. * @return The string address of the copy.
  459. */
  460. char *rt_strdup(const char *s)
  461. {
  462. rt_size_t len = rt_strlen(s) + 1;
  463. char *tmp = (char *)rt_malloc(len);
  464. if (!tmp)
  465. {
  466. return RT_NULL;
  467. }
  468. rt_memcpy(tmp, s, len);
  469. return tmp;
  470. }
  471. RTM_EXPORT(rt_strdup);
  472. #endif /* RT_USING_HEAP */