xref: /freebsd/lib/libc/string/wcstok.c (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998 Softweyr LLC.  All rights reserved.
5  *
6  * strtok_r, from Berkeley strtok
7  * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
8  *
9  * Copyright (c) 1988, 1993
10  *	The Regents of the University of California.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notices, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notices, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE
28  * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <wchar.h>
41 
42 wchar_t *
43 wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
44     wchar_t ** __restrict last)
45 {
46 	const wchar_t *spanp;
47 	wchar_t *tok;
48 	wchar_t c, sc;
49 
50 	if (s == NULL && (s = *last) == NULL)
51 		return (NULL);
52 
53 	/*
54 	 * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
55 	 */
56 cont:
57 	c = *s++;
58 	for (spanp = delim; (sc = *spanp++) != L'\0';) {
59 		if (c == sc)
60 			goto cont;
61 	}
62 
63 	if (c == L'\0') {	/* no non-delimiter characters */
64 		*last = NULL;
65 		return (NULL);
66 	}
67 	tok = s - 1;
68 
69 	/*
70 	 * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
71 	 * Note that delim must have one NUL; we stop if we see that, too.
72 	 */
73 	for (;;) {
74 		c = *s++;
75 		spanp = delim;
76 		do {
77 			if ((sc = *spanp++) == c) {
78 				if (c == L'\0')
79 					s = NULL;
80 				else
81 					s[-1] = L'\0';
82 				*last = s;
83 				return (tok);
84 			}
85 		} while (sc != L'\0');
86 	}
87 	/* NOTREACHED */
88 }
89