string.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2008-08-14 Bernard the first version
  9. * 2010-02-15 Gary Lee add strlcpy
  10. * 2010-03-17 Bernard add strlcpy implementation to this file.
  11. * fix strlcpy declaration
  12. * 2010-03-24 Bernard add strchr and strtok implementation.
  13. */
  14. #include <rtthread.h>
  15. #include <stdint.h>
  16. #if !defined (RT_USING_NEWLIB) && defined (RT_USING_MINILIBC)
  17. #include "string.h"
  18. /* there is no strcpy and strcmp implementation in RT-Thread */
  19. char *strcpy(char *dest, const char *src)
  20. {
  21. return (char *)rt_strncpy(dest, src, rt_strlen(src) + 1);
  22. }
  23. char *strncpy(char *dest, const char *src, size_t siz)
  24. {
  25. return (char *)rt_strncpy(dest, src, siz);
  26. }
  27. size_t strlcpy(char *dst, const char *src, size_t siz)
  28. {
  29. register char *d = dst;
  30. register const char *s = src;
  31. register size_t n = siz;
  32. /* Copy as many bytes as will fit */
  33. if (n != 0 && --n != 0)
  34. {
  35. do
  36. {
  37. if ((*d++ = *s++) == 0) break;
  38. } while (--n != 0);
  39. }
  40. /* Not enough room in dst, add NUL and traverse rest of src */
  41. if (n == 0)
  42. {
  43. if (siz != 0) *d = '\0'; /* NUL-terminate dst */
  44. while (*s++) ;
  45. }
  46. return(s - src - 1); /* count does not include NUL */
  47. }
  48. int strcmp (const char *s1, const char *s2)
  49. {
  50. while (*s1 && *s1 == *s2)
  51. s1++, s2++;
  52. return (*s1 - *s2);
  53. }
  54. /**
  55. * strncmp - Compare two length-limited strings
  56. * @cs: One string
  57. * @ct: Another string
  58. * @count: The maximum number of bytes to compare
  59. */
  60. int strncmp(const char *cs,const char *ct, size_t count)
  61. {
  62. register signed char __res = 0;
  63. while (count) {
  64. if ((__res = *cs - *ct++) != 0 || !*cs++)
  65. break;
  66. count--;
  67. }
  68. return __res;
  69. }
  70. char *strcat(char * dest, const char * src)
  71. {
  72. char *tmp = dest;
  73. while (*dest)
  74. dest++;
  75. while ((*dest++ = *src++) != '\0')
  76. ;
  77. return tmp;
  78. }
  79. char *strncat(char *dest, const char *src, size_t count)
  80. {
  81. char *tmp = dest;
  82. if (count) {
  83. while (*dest)
  84. dest++;
  85. while ((*dest++ = *src++)) {
  86. if (--count == 0) {
  87. *dest = '\0';
  88. break;
  89. }
  90. }
  91. }
  92. return tmp;
  93. }
  94. char *strrchr(const char *t, int c)
  95. {
  96. register char ch;
  97. register const char *l=0;
  98. ch = c;
  99. for (;;)
  100. {
  101. if (*t == ch) l=t;
  102. if (!*t) return (char*)l;
  103. ++t;
  104. }
  105. return (char*)l;
  106. }
  107. int strncasecmp ( const char* s1, const char* s2, size_t len )
  108. {
  109. register unsigned int x2;
  110. register unsigned int x1;
  111. register const char* end = s1 + len;
  112. while (1)
  113. {
  114. if ((s1 >= end) )
  115. return 0;
  116. x2 = *s2 - 'A'; if ((x2 < 26u)) x2 += 32;
  117. x1 = *s1 - 'A'; if ((x1 < 26u)) x1 += 32;
  118. s1++; s2++;
  119. if (x2 != x1)
  120. break;
  121. if (x1 == (unsigned int)-'A')
  122. break;
  123. }
  124. return x1 - x2;
  125. }
  126. /* private function */
  127. #define isdigit(c) ((unsigned)((c) - '0') < 10)
  128. rt_inline int divide(int *n, int base)
  129. {
  130. rt_int32_t res;
  131. /* optimized for processor which does not support divide instructions. */
  132. if (base == 10)
  133. {
  134. res = ((int)*n) % 10U;
  135. *n = ((int)*n) / 10U;
  136. }
  137. else
  138. {
  139. res = ((int)*n) % 16U;
  140. *n = ((int)*n) / 16U;
  141. }
  142. return res;
  143. }
  144. rt_inline int skip_atoi(const char **s)
  145. {
  146. register int i=0;
  147. while (isdigit(**s)) i = i*10 + *((*s)++) - '0';
  148. return i;
  149. }
  150. unsigned char _ctype[] = {
  151. _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */
  152. _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */
  153. _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */
  154. _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */
  155. _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */
  156. _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */
  157. _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */
  158. _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */
  159. _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */
  160. _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */
  161. _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */
  162. _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */
  163. _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */
  164. _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */
  165. _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */
  166. _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */
  167. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
  168. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
  169. _S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 160-175 */
  170. _P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 176-191 */
  171. _U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U, /* 192-207 */
  172. _U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L, /* 208-223 */
  173. _L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L, /* 224-239 */
  174. _L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */
  175. #define __ismask(x) (_ctype[(int)(unsigned char)(x)])
  176. #define isalnum(c) ((__ismask(c)&(_U|_L|_D)) != 0)
  177. #define isalpha(c) ((__ismask(c)&(_U|_L)) != 0)
  178. #define iscntrl(c) ((__ismask(c)&(_C)) != 0)
  179. #define isgraph(c) ((__ismask(c)&(_P|_U|_L|_D)) != 0)
  180. #define islower(c) ((__ismask(c)&(_L)) != 0)
  181. #define isprint(c) ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
  182. #define ispunct(c) ((__ismask(c)&(_P)) != 0)
  183. #define isspace(c) ((__ismask(c)&(_S)) != 0)
  184. #define isupper(c) ((__ismask(c)&(_U)) != 0)
  185. #define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
  186. #define isascii(c) (((unsigned char)(c))<=0x7f)
  187. #define toascii(c) (((unsigned char)(c))&0x7f)
  188. static inline unsigned char __tolower(unsigned char c)
  189. {
  190. if (isupper(c))
  191. c -= 'A'-'a';
  192. return c;
  193. }
  194. static inline unsigned char __toupper(unsigned char c)
  195. {
  196. if (islower(c))
  197. c -= 'a'-'A';
  198. return c;
  199. }
  200. int tolower(int c)
  201. {
  202. return __tolower(c);
  203. }
  204. int toupper(int c)
  205. {
  206. return __toupper(c);
  207. }
  208. /**
  209. * simple_strtoul - convert a string to an unsigned long
  210. * @cp: The start of the string
  211. * @endp: A pointer to the end of the parsed string will be placed here
  212. * @base: The number base to use
  213. */
  214. unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
  215. {
  216. unsigned long result = 0,value;
  217. if (!base) {
  218. base = 10;
  219. if (*cp == '0') {
  220. base = 8;
  221. cp++;
  222. if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
  223. cp++;
  224. base = 16;
  225. }
  226. }
  227. } else if (base == 16) {
  228. if (cp[0] == '0' && toupper(cp[1]) == 'X')
  229. cp += 2;
  230. }
  231. while (isxdigit(*cp) &&
  232. (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
  233. result = result*base + value;
  234. cp++;
  235. }
  236. if (endp)
  237. *endp = (char *)cp;
  238. return result;
  239. }
  240. /**
  241. * simple_strtol - convert a string to a signed long
  242. * @cp: The start of the string
  243. * @endp: A pointer to the end of the parsed string will be placed here
  244. * @base: The number base to use
  245. */
  246. long simple_strtol(const char *cp,char **endp,unsigned int base)
  247. {
  248. if(*cp=='-')
  249. return -simple_strtoul(cp+1,endp,base);
  250. return simple_strtoul(cp,endp,base);
  251. }
  252. /**
  253. * simple_strtoull - convert a string to an unsigned long long
  254. * @cp: The start of the string
  255. * @endp: A pointer to the end of the parsed string will be placed here
  256. * @base: The number base to use
  257. */
  258. unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
  259. {
  260. unsigned long long result = 0, value;
  261. if (*cp == '0') {
  262. cp++;
  263. if ((toupper(*cp) == 'X') && isxdigit (cp[1])) {
  264. base = 16;
  265. cp++;
  266. }
  267. if (!base) {
  268. base = 8;
  269. }
  270. }
  271. if (!base) {
  272. base = 10;
  273. }
  274. while (isxdigit (*cp) && (value = isdigit (*cp)
  275. ? *cp - '0'
  276. : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
  277. result = result * base + value;
  278. cp++;
  279. }
  280. if (endp)
  281. *endp = (char *) cp;
  282. return result;
  283. }
  284. /**
  285. * simple_strtoll - convert a string to a signed long long
  286. * @cp: The start of the string
  287. * @endp: A pointer to the end of the parsed string will be placed here
  288. * @base: The number base to use
  289. */
  290. long long simple_strtoll(const char *cp,char **endp,unsigned int base)
  291. {
  292. if(*cp=='-')
  293. return -simple_strtoull(cp+1,endp,base);
  294. return simple_strtoull(cp,endp,base);
  295. }
  296. /**
  297. * vsscanf - Unformat a buffer into a list of arguments
  298. * @buf: input buffer
  299. * @fmt: format of buffer
  300. * @args: arguments
  301. */
  302. int vsscanf(const char * buf, const char * fmt, va_list args)
  303. {
  304. const char *str = buf;
  305. char *next;
  306. int num = 0;
  307. int qualifier;
  308. int base;
  309. int field_width = -1;
  310. int is_sign = 0;
  311. while(*fmt && *str) {
  312. /* skip any white space in format */
  313. /* white space in format matchs any amount of
  314. * white space, including none, in the input.
  315. */
  316. if (isspace(*fmt)) {
  317. while (isspace(*fmt))
  318. ++fmt;
  319. while (isspace(*str))
  320. ++str;
  321. }
  322. /* anything that is not a conversion must match exactly */
  323. if (*fmt != '%' && *fmt) {
  324. if (*fmt++ != *str++)
  325. break;
  326. continue;
  327. }
  328. if (!*fmt)
  329. break;
  330. ++fmt;
  331. /* skip this conversion.
  332. * advance both strings to next white space
  333. */
  334. if (*fmt == '*') {
  335. while (!isspace(*fmt) && *fmt)
  336. fmt++;
  337. while (!isspace(*str) && *str)
  338. str++;
  339. continue;
  340. }
  341. /* get field width */
  342. if (isdigit(*fmt))
  343. field_width = skip_atoi(&fmt);
  344. /* get conversion qualifier */
  345. qualifier = -1;
  346. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'Z') {
  347. qualifier = *fmt;
  348. fmt++;
  349. }
  350. base = 10;
  351. is_sign = 0;
  352. if (!*fmt || !*str)
  353. break;
  354. switch(*fmt++) {
  355. case 'c':
  356. {
  357. char *s = (char *) va_arg(args,char*);
  358. if (field_width == -1)
  359. field_width = 1;
  360. do {
  361. *s++ = *str++;
  362. } while(field_width-- > 0 && *str);
  363. num++;
  364. }
  365. continue;
  366. case 's':
  367. {
  368. char *s = (char *) va_arg(args, char *);
  369. if(field_width == -1)
  370. field_width = INT_MAX;
  371. /* first, skip leading white space in buffer */
  372. while (isspace(*str))
  373. str++;
  374. /* now copy until next white space */
  375. while (*str && !isspace(*str) && field_width--) {
  376. *s++ = *str++;
  377. }
  378. *s = '\0';
  379. num++;
  380. }
  381. continue;
  382. case 'n':
  383. /* return number of characters read so far */
  384. {
  385. int *i = (int *)va_arg(args,int*);
  386. *i = str - buf;
  387. }
  388. continue;
  389. case 'o':
  390. base = 8;
  391. break;
  392. case 'x':
  393. case 'X':
  394. base = 16;
  395. break;
  396. case 'd':
  397. case 'i':
  398. is_sign = 1;
  399. case 'u':
  400. break;
  401. case '%':
  402. /* looking for '%' in str */
  403. if (*str++ != '%')
  404. return num;
  405. continue;
  406. default:
  407. /* invalid format; stop here */
  408. return num;
  409. }
  410. /* have some sort of integer conversion.
  411. * first, skip white space in buffer.
  412. */
  413. while (isspace(*str))
  414. str++;
  415. if (!*str || !isdigit(*str))
  416. break;
  417. switch(qualifier) {
  418. case 'h':
  419. if (is_sign) {
  420. short *s = (short *) va_arg(args,short *);
  421. *s = (short) simple_strtol(str,&next,base);
  422. } else {
  423. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  424. *s = (unsigned short) simple_strtoul(str, &next, base);
  425. }
  426. break;
  427. case 'l':
  428. if (is_sign) {
  429. long *l = (long *) va_arg(args,long *);
  430. *l = simple_strtol(str,&next,base);
  431. } else {
  432. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  433. *l = simple_strtoul(str,&next,base);
  434. }
  435. break;
  436. case 'L':
  437. if (is_sign) {
  438. long long *l = (long long*) va_arg(args,long long *);
  439. *l = simple_strtoll(str,&next,base);
  440. } else {
  441. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  442. *l = simple_strtoull(str,&next,base);
  443. }
  444. break;
  445. case 'Z':
  446. {
  447. unsigned long *s = (unsigned long*) va_arg(args,unsigned long*);
  448. *s = (unsigned long) simple_strtoul(str,&next,base);
  449. }
  450. break;
  451. default:
  452. if (is_sign) {
  453. int *i = (int *) va_arg(args, int*);
  454. *i = (int) simple_strtol(str,&next,base);
  455. } else {
  456. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  457. *i = (unsigned int) simple_strtoul(str,&next,base);
  458. }
  459. break;
  460. }
  461. num++;
  462. if (!next)
  463. break;
  464. str = next;
  465. }
  466. return num;
  467. }
  468. /**
  469. * sscanf - Unformat a buffer into a list of arguments
  470. * @buf: input buffer
  471. * @fmt: formatting of buffer
  472. * @...: resulting arguments
  473. */
  474. int sscanf(const char * buf, const char * fmt, ...)
  475. {
  476. va_list args;
  477. int i;
  478. va_start(args,fmt);
  479. i = vsscanf(buf,fmt,args);
  480. va_end(args);
  481. return i;
  482. }
  483. size_t strspn(const char *s, const char *accept)
  484. {
  485. size_t l=0;
  486. int a=1,i, al=strlen(accept);
  487. while((a)&&(*s))
  488. {
  489. for(a=i=0;(!a)&&(i<al);i++)
  490. if (*s==accept[i]) a=1;
  491. if (a) l++;
  492. s++;
  493. }
  494. return l;
  495. }
  496. size_t strcspn(const char *s, const char *reject)
  497. {
  498. size_t l=0;
  499. int a=1,i,al=strlen(reject);
  500. while((a)&&(*s))
  501. {
  502. for(i=0;(a)&&(i<al);i++)
  503. if (*s==reject[i]) a=0;
  504. if (a) l++;
  505. s++;
  506. }
  507. return l;
  508. }
  509. char*strtok_r(char*s,const char*delim,char**ptrptr)
  510. {
  511. char*tmp=0;
  512. if (s==0) s=*ptrptr;
  513. s += strspn(s,delim); /* overread leading delimiter */
  514. if (*s)
  515. {
  516. tmp=s;
  517. s+=strcspn(s,delim);
  518. if (*s) *s++=0; /* not the end ? => terminate it */
  519. }
  520. *ptrptr=s;
  521. return tmp;
  522. }
  523. char *strtok(char *s, const char *delim)
  524. {
  525. static char *strtok_pos;
  526. return strtok_r(s,delim,&strtok_pos);
  527. }
  528. char *strchr(const char *s1, int i)
  529. {
  530. const unsigned char *s = (const unsigned char *)s1;
  531. unsigned char c = (unsigned int)i;
  532. while (*s && *s != c)
  533. {
  534. s++;
  535. }
  536. if (*s != c)
  537. {
  538. s = NULL;
  539. }
  540. return (char *) s;
  541. }
  542. long strtol(const char *str, char **endptr, int base)
  543. {
  544. return simple_strtol(str, endptr, base);
  545. }
  546. long long strtoll(const char *str, char **endptr, int base)
  547. {
  548. return simple_strtoll(str, endptr, base);
  549. }
  550. #endif