]> git.sur5r.net Git - cc65/blob - libsrc/common/strtok.c
Merge remote-tracking branch 'upstream/master'
[cc65] / libsrc / common / strtok.c
1 /*
2 ** strtok.c
3 **
4 ** Ullrich von Bassewitz, 11.12.1998
5 */
6
7
8
9 #include <string.h>
10
11
12
13 /*****************************************************************************/
14 /*                                   Data                                    */
15 /*****************************************************************************/
16
17
18
19 /* Memory location that holds the last input */
20 static char* Last = 0;
21
22
23
24 /*****************************************************************************/
25 /*                                   Code                                    */
26 /*****************************************************************************/
27
28
29
30 char* __fastcall__ strtok (register char* s1, const char* s2)
31 {
32     char c;
33     char* start;
34
35     /* Use the stored location if called with a NULL pointer */
36     if (s1 == 0) {
37         s1 = Last;
38     }
39
40     /* If s1 is empty, there are no more tokens. Return 0 in this case. */
41     if (*s1 == '\0') {
42         return 0;
43     }
44
45     /* Search the address of the first element in s1 that equals none
46     ** of the characters in s2.
47     */
48     while ((c = *s1) && strchr (s2, c) != 0) {
49         ++s1;
50     }
51     if (c == '\0') {
52         /* No more tokens found */
53         Last = s1;
54         return 0;
55     }
56
57     /* Remember the start of the token */
58     start = s1;
59
60     /* Search for the end of the token */
61     while ((c = *s1) && strchr (s2, c) == 0) {
62         ++s1;
63     }
64     if (c == '\0') {
65         /* Last element */
66         Last = s1;
67     } else {
68         *s1 = '\0';
69         Last = s1 + 1;
70     }
71
72     /* Return the start of the token */
73     return start;
74 }
75
76
77