What is the fastest possible algorithm with constant space?
char * strtok_r (char *s, const char *delim, char **ptrptr) { size_t len; if (!s) { s = *ptrptr; } len = strspn (s, delim); s += len; if (*s == '\0') { return 0; } *ptrptr = strpbrk (s, delim); if (!(*ptrptr)) { *ptrptr = strchr (s, '\0'); } else { *((*ptrptr)++) = '\0'; } return s; }
char * strtok (char *s, const char *delim) { static char *ptrptr; return strtok_r (s, delim, &ptrptr); }
[code:1]char* str_chr(char* str, char c) { char* t = str; while(*t != c && *t) t++;
return *--t ? NULL : t; } char *str_tok(char *str, char *delims) { static char *pos = NULL; char *start = NULL;
if (str) /* Start a new string? */ pos = str;
if (pos) { /* Skip delimiters */ while (*pos && strchr(delims, *pos)) pos++;
if (*pos) { start = pos; /* Skip non-delimiters */ while (*pos && !strchr(delims, *pos)) pos++;
if (*pos) *pos++ = '\0'; }
}
return start; }[/code:1]
code for strtok from pnetc's libc library
char *
strtok_r (char *s, const char *delim, char **ptrptr)
{
size_t len;
if (!s)
{
s = *ptrptr;
}
len = strspn (s, delim);
s += len;
if (*s == '\0')
{
return 0;
}
*ptrptr = strpbrk (s, delim);
if (!(*ptrptr))
{
*ptrptr = strchr (s, '\0');
}
else
{
*((*ptrptr)++) = '\0';
}
return s;
}
char *
strtok (char *s, const char *delim)
{
static char *ptrptr;
return strtok_r (s, delim, &ptrptr);
}
Implement the strtok function
[code:1]char* str_chr(char* str, char c)
{
char* t = str;
while(*t != c && *t)
t++;
return *--t ? NULL : t;
}
char *str_tok(char *str, char *delims)
{
static char *pos = NULL;
char *start = NULL;
if (str) /* Start a new string? */
pos = str;
if (pos)
{
/* Skip delimiters */
while (*pos && strchr(delims, *pos))
pos++;
if (*pos) {
start = pos;
/* Skip non-delimiters */
while (*pos && !strchr(delims, *pos))
pos++;
if (*pos)
*pos++ = '\0';
}
}
return start;
}[/code:1]