xref: /dflybsd-src/lib/libc/string/wcstok.c (revision 0d5acd7467c4e95f792ef49fceb3ab8e917ce86b)
1716024cdSPeter Avalos /*-
2716024cdSPeter Avalos  * Copyright (c) 1998 Softweyr LLC.  All rights reserved.
3716024cdSPeter Avalos  *
4716024cdSPeter Avalos  * strtok_r, from Berkeley strtok
5716024cdSPeter Avalos  * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
6716024cdSPeter Avalos  *
7716024cdSPeter Avalos  * Copyright (c) 1988, 1993
8716024cdSPeter Avalos  *	The Regents of the University of California.  All rights reserved.
9716024cdSPeter Avalos  *
10716024cdSPeter Avalos  * Redistribution and use in source and binary forms, with or without
11716024cdSPeter Avalos  * modification, are permitted provided that the following conditions
12716024cdSPeter Avalos  * are met:
13716024cdSPeter Avalos  * 1. Redistributions of source code must retain the above copyright
14716024cdSPeter Avalos  *    notices, this list of conditions and the following disclaimer.
15716024cdSPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
16716024cdSPeter Avalos  *    notices, this list of conditions and the following disclaimer in the
17716024cdSPeter Avalos  *    documentation and/or other materials provided with the distribution.
18dc71b7abSJustin C. Sherrill  * 3. Neither the name of the University nor the names of its contributors
19716024cdSPeter Avalos  *    may be used to endorse or promote products derived from this software
20716024cdSPeter Avalos  *    without specific prior written permission.
21716024cdSPeter Avalos  *
22716024cdSPeter Avalos  * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
23716024cdSPeter Avalos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24716024cdSPeter Avalos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25716024cdSPeter Avalos  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE
26716024cdSPeter Avalos  * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27716024cdSPeter Avalos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28716024cdSPeter Avalos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29716024cdSPeter Avalos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30716024cdSPeter Avalos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31716024cdSPeter Avalos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32716024cdSPeter Avalos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33716024cdSPeter Avalos  *
34*0d5acd74SJohn Marino  * $FreeBSD: head/lib/libc/string/wcstok.c 251069 2013-05-28 20:57:40Z emaste $
35716024cdSPeter Avalos  */
36716024cdSPeter Avalos 
37716024cdSPeter Avalos #include <wchar.h>
38716024cdSPeter Avalos 
39716024cdSPeter Avalos wchar_t *
wcstok(wchar_t * __restrict s,const wchar_t * __restrict delim,wchar_t ** __restrict last)40716024cdSPeter Avalos wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
41716024cdSPeter Avalos     wchar_t ** __restrict last)
42716024cdSPeter Avalos {
43716024cdSPeter Avalos 	const wchar_t *spanp;
44716024cdSPeter Avalos 	wchar_t *tok;
45716024cdSPeter Avalos 	wchar_t c, sc;
46716024cdSPeter Avalos 
47716024cdSPeter Avalos 	if (s == NULL && (s = *last) == NULL)
48716024cdSPeter Avalos 		return (NULL);
49716024cdSPeter Avalos 
50716024cdSPeter Avalos 	/*
51716024cdSPeter Avalos 	 * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
52716024cdSPeter Avalos 	 */
53716024cdSPeter Avalos cont:
54716024cdSPeter Avalos 	c = *s++;
55716024cdSPeter Avalos 	for (spanp = delim; (sc = *spanp++) != L'\0';) {
56716024cdSPeter Avalos 		if (c == sc)
57716024cdSPeter Avalos 			goto cont;
58716024cdSPeter Avalos 	}
59716024cdSPeter Avalos 
60716024cdSPeter Avalos 	if (c == L'\0') {	/* no non-delimiter characters */
61716024cdSPeter Avalos 		*last = NULL;
62716024cdSPeter Avalos 		return (NULL);
63716024cdSPeter Avalos 	}
64716024cdSPeter Avalos 	tok = s - 1;
65716024cdSPeter Avalos 
66716024cdSPeter Avalos 	/*
67716024cdSPeter Avalos 	 * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
68716024cdSPeter Avalos 	 * Note that delim must have one NUL; we stop if we see that, too.
69716024cdSPeter Avalos 	 */
70716024cdSPeter Avalos 	for (;;) {
71716024cdSPeter Avalos 		c = *s++;
72716024cdSPeter Avalos 		spanp = delim;
73716024cdSPeter Avalos 		do {
74716024cdSPeter Avalos 			if ((sc = *spanp++) == c) {
75716024cdSPeter Avalos 				if (c == L'\0')
76716024cdSPeter Avalos 					s = NULL;
77716024cdSPeter Avalos 				else
78716024cdSPeter Avalos 					s[-1] = L'\0';
79716024cdSPeter Avalos 				*last = s;
80716024cdSPeter Avalos 				return (tok);
81716024cdSPeter Avalos 			}
82716024cdSPeter Avalos 		} while (sc != L'\0');
83716024cdSPeter Avalos 	}
84716024cdSPeter Avalos 	/* NOTREACHED */
85716024cdSPeter Avalos }
86