string.c 13 KB

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