kstring.c 12 KB

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