diff options
Diffstat (limited to 'string/strtok_r.c')
-rw-r--r-- | string/strtok_r.c | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/string/strtok_r.c b/string/strtok_r.c index f351304..2d251f9 100644 --- a/string/strtok_r.c +++ b/string/strtok_r.c @@ -22,14 +22,10 @@ #include <string.h> -#undef strtok_r -#undef __strtok_r - #ifndef _LIBC /* Get specification. */ # include "strtok_r.h" # define __strtok_r strtok_r -# define __rawmemchr strchr #endif /* Parse S into tokens separated by characters in DELIM. @@ -45,11 +41,17 @@ char * __strtok_r (char *s, const char *delim, char **save_ptr) { - char *token; + char *end; if (s == NULL) s = *save_ptr; + if (*s == '\0') + { + *save_ptr = s; + return NULL; + } + /* Scan leading delimiters. */ s += strspn (s, delim); if (*s == '\0') @@ -59,18 +61,17 @@ __strtok_r (char *s, const char *delim, char **save_ptr) } /* Find the end of the token. */ - token = s; - s = strpbrk (token, delim); - if (s == NULL) - /* This token finishes the string. */ - *save_ptr = __rawmemchr (token, '\0'); - else + end = s + strcspn (s, delim); + if (*end == '\0') { - /* Terminate the token and make *SAVE_PTR point past it. */ - *s = '\0'; - *save_ptr = s + 1; + *save_ptr = end; + return s; } - return token; + + /* Terminate the token and make *SAVE_PTR point past it. */ + *end = '\0'; + *save_ptr = end + 1; + return s; } #ifdef weak_alias libc_hidden_def (__strtok_r) |