char * strtok ( char * str, const char * delimiters );
Split string into tokens
A sequence of calls to this function split
str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of
delimiters.
On a first call, the function expects a C string as argument for
str,
whose first character is used as the starting location to scan for
tokens. In subsequent calls, the function expects a null pointer and
uses the position right after the end of last token as the new starting
location for scanning.
第一次调用时第一个参数使用的是string的指针
第二次及后续的调用使用的是null作为参数
strtok实现过程中,使用某个静态变量来记录分析的str的位置。
陷阱:strtok 对参数进行了修改,在delimeter的位置换成了 \0 。
忽略了这一点,调了半天的bug.
- char * __cdecl strtok(char *s1, const char *delimit)
-
{
-
static char *lastToken = NULL; /* UNSAFE SHARED */
-
char *tmp;
-
-
/* Skip leading delimiters if new string. */
-
if ( s1 == NULL ) {
-
s1 = lastToken;
-
if (s1 == NULL) /* End of story? */
-
return NULL;
-
} else {
-
s1 += strspn(s1, delimit);
-
}
-
-
/* Find end of segment */
-
tmp = strpbrk(s1, delimit);
-
if (tmp) {
-
/* Found another delimiter, split string and save state. */
-
*tmp = '\0';
-
lastToken = tmp + 1;
-
} else {
-
/* Last segment, remember that. */
-
lastToken = NULL;
-
}
-
-
return s1;
-
}
- #include <sys/cdefs.h>
-
#include <string.h>
-
-
/*
-
* Span the string s2 (skip characters that are in s2).
-
*/
-
size_t
-
strspn(s1, s2)
-
const char *s1;
-
register const char *s2;
-
{
-
register const char *p = s1, *spanp;
-
register char c, sc;
-
-
/*
-
* Skip any characters in s2, excluding the terminating \0.
-
*/
-
cont:
-
c = *p++;
-
for (spanp = s2; (sc = *spanp++) != 0;)
-
if (sc == c)
-
goto cont;
-
return (p - 1 - s1);
-
}
- #include <sys/cdefs.h>
-
#include <string.h>
-
-
/*
-
* Find the first occurrence in s1 of a character in s2 (excluding NUL).
-
*/
-
char *
-
strpbrk(s1, s2)
-
register const char *s1, *s2;
-
{
-
register const char *scanp;
-
register int c, sc;
-
-
while ((c = *s1++) != 0) {
-
for (scanp = s2; (sc = *scanp++) != 0;)
-
if (sc == c)
-
return ((char *)(s1 - 1));
-
}
-
return (NULL);
-
}
阅读(643) | 评论(0) | 转发(0) |