C Programming - Extract Token from Strings

glibc implementation of strtok for i386 platform.



static char *olds;
void *rawmemchr (const void *s, int c)
{
register unsigned long int d0;
register unsigned char *res;
__asm__ __volatile__
("cld\n\t"
"repne; scasb\n\t"
: "=D" (res), "=&c" (d0)
: "a" (c), "0" (s), "1" (0xffffffff),
"m" ( *(struct { char __x[0xfffffff]; } *)s)
: "cc");
return res - 1;
}



char *strtok (char *s, const char *delim)
{
char *token;
if (s == NULL) s = olds;

/* Scan leading delimiters. */
s += strspn (s, delim);
if (*s == '\0') {
olds = s;
return NULL;
}

/* Find the end of the token. */
token = s;
s = strpbrk (token, delim);
if (s == NULL)
/* This token finishes the string. */
olds = rawmemchr (token, '\0');
else {
/* Terminate the token and make OLDS point past it. */
*s = '\0';
olds = s + 1;
}
return token;
}



Notes:

This was extracted from glibc-2.3.6 (sysdeps/generic/strtok.c)

Labels:


About this entry