1 /*
2 FUNCTION
3 	<<wcstok>>---get next token from a string
4 
5 INDEX
6 	wcstok
7 
8 
9 SYNOPSIS
10 	#include <wchar.h>
11       	wchar_t *wcstok(wchar_t *__restrict <[source]>,
12       			const wchar_t *__restrict <[delimiters]>,
13 			wchar_t **__restrict <[lasts]>);
14 
15 DESCRIPTION
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).
19 
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]>>>.
24 
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
37 	by <<wcstok>>.
38 
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.
43 
44 RETURNS
45 	<<wcstok>> returns a pointer to the first wide character of a token, or
46 	<<NULL>> if there is no token.
47 
48 NOTES
49 	<<wcstok>> is thread-safe (unlike <<strtok>>, but like <<strtok_r>>).
50 	<<wcstok>> writes into the string being searched.
51 
52 PORTABILITY
53 <<wcstok>> is C99 and POSIX.1-2001.
54 
55 <<wcstok>> requires no supporting OS subroutines.
56 
57 QUICKREF
58 	strtok ansi pure
59 */
60 /* wcstok for Newlib created by adapting strtok_r, 2008.  */
61 /*
62  * Copyright (c) 1988 Regents of the University of California.
63  * All rights reserved.
64  *
65  * Redistribution and use in source and binary forms, with or without
66  * modification, are permitted provided that the following conditions
67  * are met:
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.
76  *
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
87  * SUCH DAMAGE.
88  */
89 
90 #include <wchar.h>
91 
92 wchar_t *
wcstok(register wchar_t * __restrict s,register const wchar_t * __restrict delim,wchar_t ** __restrict lasts)93 wcstok (register wchar_t *__restrict s,
94 	register const wchar_t *__restrict delim,
95 	wchar_t **__restrict lasts)
96 {
97 	register const wchar_t *spanp;
98 	register int c, sc;
99 	wchar_t *tok;
100 
101 
102 	if (s == NULL && (s = *lasts) == NULL)
103 		return (NULL);
104 
105 	/*
106 	 * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
107 	 */
108 cont:
109 	c = *s++;
110 	for (spanp = delim; (sc = *spanp++) != L'\0';) {
111 		if (c == sc)  goto cont;
112 	}
113 
114 	if (c == L'\0') {		/* no non-delimiter characters */
115 		*lasts = NULL;
116 		return (NULL);
117 	}
118 	tok = s - 1;
119 
120 	/*
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.
123 	 */
124 	for (;;) {
125 		c = *s++;
126 		spanp = delim;
127 		do {
128 			if ((sc = *spanp++) == c) {
129 				if (c == L'\0')
130 					s = NULL;
131 				else
132 					s[-1] = L'\0';
133 				*lasts = s;
134 				return (tok);
135 			}
136 		} while (sc != L'\0');
137 	}
138 	/* NOTREACHED */
139 }
140 
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 */
144 #include <stdio.h>
145 #include <wchar.h>
146 
147 /* example from C99 standard with minor additions to be a test */
148 int
main(void)149 main(void)
150 {
151 int  errs=0;
152 static wchar_t str1[] = L"?a???b,,,#c";
153 static wchar_t str2[] = L"\t \t";
154 wchar_t *t, *ptr1, *ptr2;
155 
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++;
166 
167 printf("wcstok() test ");
168 if(errs)  printf("FAILED %d test cases", errs);
169   else    printf("passed");
170 printf(".\n");
171 
172 return(errs);
173 }
174 #endif /* defined(_REGRESSION_TEST) ] */
175