3 <<wcstok>>---get next token from a string
11 wchar_t *wcstok(wchar_t *__restrict <[source]>,
12 const wchar_t *__restrict <[delimiters]>,
13 wchar_t **__restrict <[lasts]>);
16 The <<wcstok>> function is the wide-character equivalent of the
17 <<strtok_r>> function (which in turn is the same as the <<strtok>>
18 function with an added argument to make it thread-safe).
20 The <<wcstok>> function is used to isolate (one at a time)
21 sequential tokens in a null-terminated wide-character string,
22 <<*<[source]>>>. A token is defined as a substring not containing
23 any wide-characters from <<*<[delimiters]>>>.
25 The first time that <<wcstok>> is called, <<*<[source]>>> should
26 be specified with the wide-character string to be searched, and
27 <<*<[lasts]>>>--but not <<lasts>>, which must be non-NULL--may be
28 random; subsequent calls, wishing to obtain further tokens from
29 the same string, should pass a null pointer for <<*<[source]>>>
30 instead but must supply <<*<[lasts]>>> unchanged from the last
31 call. The separator wide-character string, <<*<[delimiters]>>>,
32 must be supplied each time and may change between calls.
33 A pointer to placeholder <<*<[lasts]>>> must be supplied by
34 the caller, and is set each time as needed to save the state
35 by <<wcstok>>. Every call to <<wcstok>> with <<*<[source]>>>
36 == <<NULL>> must pass the value of <<*<[lasts]>>> as last set
39 The <<wcstok>> function returns a pointer to the beginning of each
40 subsequent token in the string, after replacing the separator
41 wide-character itself with a null wide-character. When no more tokens
42 remain, a null pointer is returned.
45 <<wcstok>> returns a pointer to the first wide character of a token, or
46 <<NULL>> if there is no token.
49 <<wcstok>> is thread-safe (unlike <<strtok>>, but like <<strtok_r>>).
50 <<wcstok>> writes into the string being searched.
53 <<wcstok>> is C99 and POSIX.1-2001.
55 <<wcstok>> requires no supporting OS subroutines.
60 /* wcstok for Newlib created by adapting strtok_r, 2008. */
62 * Copyright (c) 1988 Regents of the University of California.
63 * All rights reserved.
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. Neither the name of the University nor the names of its contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93 wcstok (register wchar_t *__restrict s
,
94 register const wchar_t *__restrict delim
,
95 wchar_t **__restrict lasts
)
97 register const wchar_t *spanp
;
102 if (s
== NULL
&& (s
= *lasts
) == NULL
)
106 * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
110 for (spanp
= delim
; (sc
= *spanp
++) != L
'\0';) {
111 if (c
== sc
) goto cont
;
114 if (c
== L
'\0') { /* no non-delimiter characters */
121 * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
122 * Note that delim must have one NUL; we stop if we see that, too.
128 if ((sc
= *spanp
++) == c
) {
136 } while (sc
!= L
'\0');
141 /* The remainder of this file can serve as a regression test. Compile
142 * with -D_REGRESSION_TEST. */
143 #if defined(_REGRESSION_TEST) /* [Test code: example from C99 standard */
147 /* example from C99 standard with minor additions to be a test */
152 static wchar_t str1
[] = L
"?a???b,,,#c";
153 static wchar_t str2
[] = L
"\t \t";
154 wchar_t *t
, *ptr1
, *ptr2
;
156 t
= wcstok(str1
, L
"?", &ptr1
); // t points to the token L"a"
157 if(wcscmp(t
,L
"a")) errs
++;
158 t
= wcstok(NULL
, L
",", &ptr1
); // t points to the token L"??b"
159 if(wcscmp(t
,L
"??b")) errs
++;
160 t
= wcstok(str2
, L
" \t", &ptr2
); // t is a null pointer
161 if(t
!= NULL
) errs
++;
162 t
= wcstok(NULL
, L
"#,", &ptr1
); // t points to the token L"c"
163 if(wcscmp(t
,L
"c")) errs
++;
164 t
= wcstok(NULL
, L
"?", &ptr1
); // t is a null pointer
165 if(t
!= NULL
) errs
++;
167 printf("wcstok() test ");
168 if(errs
) printf("FAILED %d test cases", errs
);
169 else printf("passed");
174 #endif /* defined(_REGRESSION_TEST) ] */