PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/rtt-1.0.2/components/libc/minilibc/string.c

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