update for HEAD-2003091401
[reactos.git] / lib / crtdll / mbstring / mbstok.c
1 #include <msvcrt/mbstring.h>
2
3 /*
4  * @implemented
5  */
6 unsigned char * _mbstok(unsigned char *s, unsigned char *delim)
7 {
8   const char *spanp;
9   int c, sc;
10   char *tok;
11   static char *last;
12
13
14   if (s == NULL && (s = last) == NULL)
15     return (NULL);
16
17   /*
18    * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
19    */
20  cont:
21   c = *s;
22   s = _mbsinc(s);
23
24   for (spanp = delim; (sc = *spanp) != 0; spanp = _mbsinc(spanp)) {
25     if (c == sc)
26       goto cont;
27   }
28
29   if (c == 0) {                 /* no non-delimiter characters */
30     last = NULL;
31     return (NULL);
32   }
33   tok = s - 1;
34
35   /*
36    * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
37    * Note that delim must have one NUL; we stop if we see that, too.
38    */
39   for (;;) {
40     c = *s;
41     s = _mbsinc(s);
42     spanp = delim;
43     do {
44       if ((sc = *spanp) == c) {
45         if (c == 0)
46           s = NULL;
47         else
48           s[-1] = 0;
49         last = s;
50         return (tok);
51       }
52       spanp = _mbsinc(spanp);
53     } while (sc != 0);
54   }
55   /* NOTREACHED */
56 }