xref: /netbsd-src/lib/libc/string/wcspbrk.c (revision d22a7f54f9780106e0b11d879b3aaa5a1c7c676e)
1*d22a7f54Sjoerg /*	$NetBSD: wcspbrk.c,v 1.5 2011/11/24 18:44:25 joerg Exp $	*/
2383f218aSitojun 
3383f218aSitojun /*-
4383f218aSitojun  * Copyright (c) 1999 Citrus Project,
5*d22a7f54Sjoerg  * Copyright (c) 2011 Joerg Sonnenberger,
6383f218aSitojun  * All rights reserved.
7383f218aSitojun  *
8383f218aSitojun  * Redistribution and use in source and binary forms, with or without
9383f218aSitojun  * modification, are permitted provided that the following conditions
10383f218aSitojun  * are met:
11383f218aSitojun  * 1. Redistributions of source code must retain the above copyright
12383f218aSitojun  *    notice, this list of conditions and the following disclaimer.
13383f218aSitojun  * 2. Redistributions in binary form must reproduce the above copyright
14383f218aSitojun  *    notice, this list of conditions and the following disclaimer in the
15383f218aSitojun  *    documentation and/or other materials provided with the distribution.
16383f218aSitojun  *
17383f218aSitojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18383f218aSitojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19383f218aSitojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20383f218aSitojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21383f218aSitojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22383f218aSitojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23383f218aSitojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24383f218aSitojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25383f218aSitojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26383f218aSitojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27383f218aSitojun  * SUCH DAMAGE.
28383f218aSitojun  *
29383f218aSitojun  *	citrus Id: wcspbrk.c,v 1.2 2000/12/21 05:07:25 itojun Exp
30383f218aSitojun  */
31383f218aSitojun 
32383f218aSitojun #include <sys/cdefs.h>
33*d22a7f54Sjoerg __RCSID("$NetBSD: wcspbrk.c,v 1.5 2011/11/24 18:44:25 joerg Exp $");
34383f218aSitojun 
355ba790cbSlukem #include <assert.h>
36*d22a7f54Sjoerg #include <inttypes.h>
37*d22a7f54Sjoerg #include <string.h>
38383f218aSitojun #include <wchar.h>
39383f218aSitojun 
40*d22a7f54Sjoerg #include "wcscspn_bloom.h"
41*d22a7f54Sjoerg 
42383f218aSitojun wchar_t *
wcspbrk(const wchar_t * s,const wchar_t * set)437e173c18Sjoerg wcspbrk(const wchar_t *s, const wchar_t *set)
44383f218aSitojun {
45*d22a7f54Sjoerg 	size_t bloom[BLOOM_ARRAY_SIZE];
46383f218aSitojun 	const wchar_t *p;
47383f218aSitojun 	const wchar_t *q;
48383f218aSitojun 
495ba790cbSlukem 	_DIAGASSERT(s != NULL);
505ba790cbSlukem 	_DIAGASSERT(set != NULL);
515ba790cbSlukem 
52*d22a7f54Sjoerg 	if (set[0] == '\0')
53*d22a7f54Sjoerg 		return NULL;
54*d22a7f54Sjoerg 	if (set[1] == '\0')
55*d22a7f54Sjoerg 		return wcschr(s, set[0]);
56*d22a7f54Sjoerg 
57*d22a7f54Sjoerg 	wcsspn_bloom_init(bloom, set);
58*d22a7f54Sjoerg 
597e173c18Sjoerg 	for (p = s; *p; ++p) {
60*d22a7f54Sjoerg 		if (!wcsspn_in_bloom(bloom, *p))
61*d22a7f54Sjoerg 			continue;
62*d22a7f54Sjoerg 
63*d22a7f54Sjoerg 		q = set;
64*d22a7f54Sjoerg 		do {
6503256c6eSchristos 			if (*p == *q)
6603256c6eSchristos 				return __UNCONST(p);
67*d22a7f54Sjoerg 		} while (*++q);
68383f218aSitojun 	}
69383f218aSitojun 	return NULL;
70383f218aSitojun }
71