string.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. */
  15. #include <rtthread.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 rt_strncpy(dest, src, rt_strlen(src) + 1);
  22. }
  23. char *strncpy(char *dest, const char *src, rt_ubase_t n)
  24. {
  25. return rt_strncpy(dest, src, n);
  26. }
  27. char *strlcpy(char *dest, const char *src, rt_ubase_t n)
  28. {
  29. return rt_strlcpy(dest, src, n);
  30. }
  31. int strcmp (const char *s1, const char *s2)
  32. {
  33. while (*s1 && *s1 == *s2)
  34. s1++, s2++;
  35. return (*s1 - *s2);
  36. }
  37. char* strcat(register char* s,register const char* t)
  38. {
  39. char *dest = s;
  40. s += strlen(s);
  41. for (;;)
  42. {
  43. if (!(*s = *t)) break;
  44. ++s;
  45. ++t;
  46. }
  47. return dest;
  48. }
  49. char *strncat(char *s, const char *t, size_t n)
  50. {
  51. char *dest = s;
  52. register char *max;
  53. s += rt_strlen(s);
  54. if ((max=s+n)==s)
  55. goto fini;
  56. for (;;)
  57. {
  58. if (!(*s = *t)) break;
  59. if (++s==max) break;
  60. ++t;
  61. }
  62. *s=0;
  63. fini:
  64. return dest;
  65. }
  66. char *strrchr(const char *t, int c)
  67. {
  68. register char ch;
  69. register const char *l=0;
  70. ch = c;
  71. for (;;)
  72. {
  73. if (*t == ch) l=t;
  74. if (!*t) return (char*)l;
  75. ++t;
  76. }
  77. return (char*)l;
  78. }
  79. int strncasecmp ( const char* s1, const char* s2, size_t len )
  80. {
  81. register unsigned int x2;
  82. register unsigned int x1;
  83. register const char* end = s1 + len;
  84. while (1)
  85. {
  86. if ((s1 >= end) )
  87. return 0;
  88. x2 = *s2 - 'A'; if ((x2 < 26u)) x2 += 32;
  89. x1 = *s1 - 'A'; if ((x1 < 26u)) x1 += 32;
  90. s1++; s2++;
  91. if ((x2 != x1))
  92. break;
  93. if ((x1 == (unsigned int)-'A'))
  94. break;
  95. }
  96. return x1 - x2;
  97. }
  98. /* private function */
  99. #define isdigit(c) ((unsigned)((c) - '0') < 10)
  100. rt_inline int divide(int *n, int base)
  101. {
  102. rt_int32_t res;
  103. /* optimized for processor which does not support divide instructions. */
  104. if (base == 10)
  105. {
  106. res = ((int)*n) % 10U;
  107. *n = ((int)*n) / 10U;
  108. }
  109. else
  110. {
  111. res = ((int)*n) % 16U;
  112. *n = ((int)*n) / 16U;
  113. }
  114. return res;
  115. }
  116. rt_inline int skip_atoi(const char **s)
  117. {
  118. register int i=0;
  119. while (isdigit(**s)) i = i*10 + *((*s)++) - '0';
  120. return i;
  121. }
  122. unsigned char _ctype[] = {
  123. _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */
  124. _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */
  125. _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */
  126. _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */
  127. _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */
  128. _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */
  129. _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */
  130. _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */
  131. _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */
  132. _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */
  133. _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */
  134. _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */
  135. _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */
  136. _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */
  137. _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */
  138. _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */
  139. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
  140. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
  141. _S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 160-175 */
  142. _P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 176-191 */
  143. _U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U, /* 192-207 */
  144. _U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L, /* 208-223 */
  145. _L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L, /* 224-239 */
  146. _L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */
  147. #define __ismask(x) (_ctype[(int)(unsigned char)(x)])
  148. #define isalnum(c) ((__ismask(c)&(_U|_L|_D)) != 0)
  149. #define isalpha(c) ((__ismask(c)&(_U|_L)) != 0)
  150. #define iscntrl(c) ((__ismask(c)&(_C)) != 0)
  151. #define isgraph(c) ((__ismask(c)&(_P|_U|_L|_D)) != 0)
  152. #define islower(c) ((__ismask(c)&(_L)) != 0)
  153. #define isprint(c) ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
  154. #define ispunct(c) ((__ismask(c)&(_P)) != 0)
  155. #define isspace(c) ((__ismask(c)&(_S)) != 0)
  156. #define isupper(c) ((__ismask(c)&(_U)) != 0)
  157. #define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
  158. #define isascii(c) (((unsigned char)(c))<=0x7f)
  159. #define toascii(c) (((unsigned char)(c))&0x7f)
  160. static inline unsigned char __tolower(unsigned char c)
  161. {
  162. if (isupper(c))
  163. c -= 'A'-'a';
  164. return c;
  165. }
  166. static inline unsigned char __toupper(unsigned char c)
  167. {
  168. if (islower(c))
  169. c -= 'a'-'A';
  170. return c;
  171. }
  172. int tolower(int c)
  173. {
  174. return __tolower(c);
  175. }
  176. int toupper(int c)
  177. {
  178. return __toupper(c);
  179. }
  180. /**
  181. * simple_strtoul - convert a string to an unsigned long
  182. * @cp: The start of the string
  183. * @endp: A pointer to the end of the parsed string will be placed here
  184. * @base: The number base to use
  185. */
  186. unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
  187. {
  188. unsigned long result = 0,value;
  189. if (!base) {
  190. base = 10;
  191. if (*cp == '0') {
  192. base = 8;
  193. cp++;
  194. if ((*cp == 'x') && isxdigit(cp[1])) {
  195. cp++;
  196. base = 16;
  197. }
  198. }
  199. }
  200. while (isxdigit(*cp) &&
  201. (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
  202. result = result*base + value;
  203. cp++;
  204. }
  205. if (endp)
  206. *endp = (char *)cp;
  207. return result;
  208. }
  209. /**
  210. * simple_strtol - convert a string to a signed long
  211. * @cp: The start of the string
  212. * @endp: A pointer to the end of the parsed string will be placed here
  213. * @base: The number base to use
  214. */
  215. long simple_strtol(const char *cp,char **endp,unsigned int base)
  216. {
  217. if(*cp=='-')
  218. return -simple_strtoul(cp+1,endp,base);
  219. return simple_strtoul(cp,endp,base);
  220. }
  221. /**
  222. * simple_strtoull - convert a string to an unsigned long long
  223. * @cp: The start of the string
  224. * @endp: A pointer to the end of the parsed string will be placed here
  225. * @base: The number base to use
  226. */
  227. unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
  228. {
  229. unsigned long long result = 0,value;
  230. if (!base) {
  231. base = 10;
  232. if (*cp == '0') {
  233. base = 8;
  234. cp++;
  235. if ((*cp == 'x') && isxdigit(cp[1])) {
  236. cp++;
  237. base = 16;
  238. }
  239. }
  240. }
  241. while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
  242. ? toupper(*cp) : *cp)-'A'+10) < base) {
  243. result = result*base + value;
  244. cp++;
  245. }
  246. if (endp)
  247. *endp = (char *)cp;
  248. return result;
  249. }
  250. /**
  251. * simple_strtoll - convert a string to a signed long long
  252. * @cp: The start of the string
  253. * @endp: A pointer to the end of the parsed string will be placed here
  254. * @base: The number base to use
  255. */
  256. long long simple_strtoll(const char *cp,char **endp,unsigned int base)
  257. {
  258. if(*cp=='-')
  259. return -simple_strtoull(cp+1,endp,base);
  260. return simple_strtoull(cp,endp,base);
  261. }
  262. /**
  263. * vsscanf - Unformat a buffer into a list of arguments
  264. * @buf: input buffer
  265. * @fmt: format of buffer
  266. * @args: arguments
  267. */
  268. int vsscanf(const char * buf, const char * fmt, va_list args)
  269. {
  270. const char *str = buf;
  271. char *next;
  272. int num = 0;
  273. int qualifier;
  274. int base;
  275. int field_width = -1;
  276. int is_sign = 0;
  277. while(*fmt && *str) {
  278. /* skip any white space in format */
  279. /* white space in format matchs any amount of
  280. * white space, including none, in the input.
  281. */
  282. if (isspace(*fmt)) {
  283. while (isspace(*fmt))
  284. ++fmt;
  285. while (isspace(*str))
  286. ++str;
  287. }
  288. /* anything that is not a conversion must match exactly */
  289. if (*fmt != '%' && *fmt) {
  290. if (*fmt++ != *str++)
  291. break;
  292. continue;
  293. }
  294. if (!*fmt)
  295. break;
  296. ++fmt;
  297. /* skip this conversion.
  298. * advance both strings to next white space
  299. */
  300. if (*fmt == '*') {
  301. while (!isspace(*fmt) && *fmt)
  302. fmt++;
  303. while (!isspace(*str) && *str)
  304. str++;
  305. continue;
  306. }
  307. /* get field width */
  308. if (isdigit(*fmt))
  309. field_width = skip_atoi(&fmt);
  310. /* get conversion qualifier */
  311. qualifier = -1;
  312. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'Z') {
  313. qualifier = *fmt;
  314. fmt++;
  315. }
  316. base = 10;
  317. is_sign = 0;
  318. if (!*fmt || !*str)
  319. break;
  320. switch(*fmt++) {
  321. case 'c':
  322. {
  323. char *s = (char *) va_arg(args,char*);
  324. if (field_width == -1)
  325. field_width = 1;
  326. do {
  327. *s++ = *str++;
  328. } while(field_width-- > 0 && *str);
  329. num++;
  330. }
  331. continue;
  332. case 's':
  333. {
  334. char *s = (char *) va_arg(args, char *);
  335. if(field_width == -1)
  336. field_width = INT_MAX;
  337. /* first, skip leading white space in buffer */
  338. while (isspace(*str))
  339. str++;
  340. /* now copy until next white space */
  341. while (*str && !isspace(*str) && field_width--) {
  342. *s++ = *str++;
  343. }
  344. *s = '\0';
  345. num++;
  346. }
  347. continue;
  348. case 'n':
  349. /* return number of characters read so far */
  350. {
  351. int *i = (int *)va_arg(args,int*);
  352. *i = str - buf;
  353. }
  354. continue;
  355. case 'o':
  356. base = 8;
  357. break;
  358. case 'x':
  359. case 'X':
  360. base = 16;
  361. break;
  362. case 'd':
  363. case 'i':
  364. is_sign = 1;
  365. case 'u':
  366. break;
  367. case '%':
  368. /* looking for '%' in str */
  369. if (*str++ != '%')
  370. return num;
  371. continue;
  372. default:
  373. /* invalid format; stop here */
  374. return num;
  375. }
  376. /* have some sort of integer conversion.
  377. * first, skip white space in buffer.
  378. */
  379. while (isspace(*str))
  380. str++;
  381. if (!*str || !isdigit(*str))
  382. break;
  383. switch(qualifier) {
  384. case 'h':
  385. if (is_sign) {
  386. short *s = (short *) va_arg(args,short *);
  387. *s = (short) simple_strtol(str,&next,base);
  388. } else {
  389. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  390. *s = (unsigned short) simple_strtoul(str, &next, base);
  391. }
  392. break;
  393. case 'l':
  394. if (is_sign) {
  395. long *l = (long *) va_arg(args,long *);
  396. *l = simple_strtol(str,&next,base);
  397. } else {
  398. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  399. *l = simple_strtoul(str,&next,base);
  400. }
  401. break;
  402. case 'L':
  403. if (is_sign) {
  404. long long *l = (long long*) va_arg(args,long long *);
  405. *l = simple_strtoll(str,&next,base);
  406. } else {
  407. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  408. *l = simple_strtoull(str,&next,base);
  409. }
  410. break;
  411. case 'Z':
  412. {
  413. unsigned long *s = (unsigned long*) va_arg(args,unsigned long*);
  414. *s = (unsigned long) simple_strtoul(str,&next,base);
  415. }
  416. break;
  417. default:
  418. if (is_sign) {
  419. int *i = (int *) va_arg(args, int*);
  420. *i = (int) simple_strtol(str,&next,base);
  421. } else {
  422. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  423. *i = (unsigned int) simple_strtoul(str,&next,base);
  424. }
  425. break;
  426. }
  427. num++;
  428. if (!next)
  429. break;
  430. str = next;
  431. }
  432. return num;
  433. }
  434. /**
  435. * sscanf - Unformat a buffer into a list of arguments
  436. * @buf: input buffer
  437. * @fmt: formatting of buffer
  438. * @...: resulting arguments
  439. */
  440. int sscanf(const char * buf, const char * fmt, ...)
  441. {
  442. va_list args;
  443. int i;
  444. va_start(args,fmt);
  445. i = vsscanf(buf,fmt,args);
  446. va_end(args);
  447. return i;
  448. }
  449. #endif