string.c 10 KB

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