diff options
| author | bernard.xiong <[email protected]> | 2010-03-24 07:33:00 +0000 |
|---|---|---|
| committer | bernard.xiong <[email protected]> | 2010-03-24 07:33:00 +0000 |
| commit | bc013f8a8300b901a46c3c8a6f667a398d9fb0a4 (patch) | |
| tree | 6affa52062b1915308b3864cdf4577217d97f9db | |
| parent | 1886174d46c80444e4d34b156ed87237363d8f1f (diff) | |
add strchr and strtok implementation.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@532 bbd45198-f89e-11dd-88c7-29a3b14d5316
| -rw-r--r-- | libc/minilibc/string.c | 57 | ||||
| -rw-r--r-- | libc/minilibc/string.h | 3 |
2 files changed, 60 insertions, 0 deletions
diff --git a/libc/minilibc/string.c b/libc/minilibc/string.c index 2de4f20790..179cd07b88 100644 --- a/libc/minilibc/string.c +++ b/libc/minilibc/string.c @@ -13,6 +13,7 @@ * 2010-02-15 Gary Lee add strlcpy * 2010-03-17 Bernard add strlcpy implementation to this file. * fix strlcpy declaration + * 2010-03-24 Bernard add strchr and strtok implementation. */ #include <rtthread.h> @@ -548,4 +549,60 @@ int sscanf(const char * buf, const char * fmt, ...) return i; } +size_t strspn(const char *s, const char *accept) +{ + size_t l=0; + int a=1,i, al=strlen(accept); + + while((a)&&(*s)) + { + for(a=i=0;(!a)&&(i<al);i++) + if (*s==accept[i]) a=1; + if (a) l++; + s++; + } + return l; +} + +char*strtok_r(char*s,const char*delim,char**ptrptr) +{ + char*tmp=0; + + if (s==0) s=*ptrptr; + s += strspn(s,delim); /* overread leading delimiter */ + if (*s) + { + tmp=s; + s+=strcspn(s,delim); + + if (*s) *s++=0; /* not the end ? => terminate it */ + } + *ptrptr=s; + return tmp; +} + +char *strtok(char *s, const char *delim) +{ + static char *strtok_pos; + return strtok_r(s,delim,&strtok_pos); +} + +char *strchr(const char *s1, int i) +{ + const unsigned char *s = (const unsigned char *)s1; + unsigned char c = (unsigned int)i; + + while (*s && *s != c) + { + s++; + } + + if (*s != c) + { + s = NULL; + } + + return (char *) s; +} + #endif diff --git a/libc/minilibc/string.h b/libc/minilibc/string.h index defffdcaee..19b1cbfc07 100644 --- a/libc/minilibc/string.h +++ b/libc/minilibc/string.h @@ -64,8 +64,11 @@ char *strncpy(char *dest, const char *src, size_t n); size_t strlcpy(char *dst, const char *src, size_t siz); char *strncat(char *dest, const char *src, size_t count); char *strcat(char * dest, const char * src); +char *strchr(const char *s1, int i); char *strrchr(const char *t, int c); char *strdup(const char *s); +char *strtok(char *s, const char *delim); +char*strtok_r(char*s, const char*delim, char**ptrptr); #endif |
