1*0a6a1f1dSLionel Sambuc /* $NetBSD: strpbrk.c,v 1.1 2014/07/19 18:38:33 lneto Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2008 Joerg Sonnenberger
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc * are met:
10*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc *
16*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*0a6a1f1dSLionel Sambuc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*0a6a1f1dSLionel Sambuc * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20*0a6a1f1dSLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*0a6a1f1dSLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*0a6a1f1dSLionel Sambuc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*0a6a1f1dSLionel Sambuc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*0a6a1f1dSLionel Sambuc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*0a6a1f1dSLionel Sambuc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*0a6a1f1dSLionel Sambuc */
27*0a6a1f1dSLionel Sambuc
28*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
29*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: strpbrk.c,v 1.1 2014/07/19 18:38:33 lneto Exp $");
30*0a6a1f1dSLionel Sambuc
31*0a6a1f1dSLionel Sambuc #if !defined(_KERNEL) && !defined(_STANDALONE)
32*0a6a1f1dSLionel Sambuc #include <assert.h>
33*0a6a1f1dSLionel Sambuc #include <inttypes.h>
34*0a6a1f1dSLionel Sambuc #include <limits.h>
35*0a6a1f1dSLionel Sambuc #include <string.h>
36*0a6a1f1dSLionel Sambuc #else
37*0a6a1f1dSLionel Sambuc #include <lib/libkern/libkern.h>
38*0a6a1f1dSLionel Sambuc #endif
39*0a6a1f1dSLionel Sambuc
40*0a6a1f1dSLionel Sambuc #define FAST_STRPBRK 1
41*0a6a1f1dSLionel Sambuc #define UC(a) ((unsigned int)(unsigned char)(a))
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambuc #ifdef FAST_STRPBRK
44*0a6a1f1dSLionel Sambuc #define ADD_NEW_TO_SET(i) (set[inv[i] = idx++] = (i))
45*0a6a1f1dSLionel Sambuc #define IS_IN_SET(i) (inv[i] < idx && set[inv[i]] == (i))
46*0a6a1f1dSLionel Sambuc #define ADD_TO_SET(i) (void)(IS_IN_SET(i) || /*LINTED no effect*/ADD_NEW_TO_SET(i))
47*0a6a1f1dSLionel Sambuc #else
48*0a6a1f1dSLionel Sambuc #define IS_IN_SET(i) (set[(i) >> 3] & idx[(i) & 7])
49*0a6a1f1dSLionel Sambuc #define ADD_TO_SET(i) (void)(set[(i) >> 3] |= idx[(i) & 7])
50*0a6a1f1dSLionel Sambuc #endif
51*0a6a1f1dSLionel Sambuc
52*0a6a1f1dSLionel Sambuc char *
strpbrk(const char * s,const char * charset)53*0a6a1f1dSLionel Sambuc strpbrk(const char *s, const char *charset)
54*0a6a1f1dSLionel Sambuc {
55*0a6a1f1dSLionel Sambuc #ifdef FAST_STRPBRK
56*0a6a1f1dSLionel Sambuc uint8_t set[256], inv[256], idx = 0;
57*0a6a1f1dSLionel Sambuc #else
58*0a6a1f1dSLionel Sambuc static const size_t idx[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
59*0a6a1f1dSLionel Sambuc uint8_t set[32];
60*0a6a1f1dSLionel Sambuc
61*0a6a1f1dSLionel Sambuc (void)memset(set, 0, sizeof(set));
62*0a6a1f1dSLionel Sambuc #endif
63*0a6a1f1dSLionel Sambuc
64*0a6a1f1dSLionel Sambuc _DIAGASSERT(s != NULL);
65*0a6a1f1dSLionel Sambuc _DIAGASSERT(charset != NULL);
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc if (charset[0] == '\0')
68*0a6a1f1dSLionel Sambuc return NULL;
69*0a6a1f1dSLionel Sambuc if (charset[1] == '\0')
70*0a6a1f1dSLionel Sambuc return strchr(s, charset[0]);
71*0a6a1f1dSLionel Sambuc
72*0a6a1f1dSLionel Sambuc for (; *charset != '\0'; ++charset)
73*0a6a1f1dSLionel Sambuc ADD_TO_SET(UC(*charset));
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc for (; *s != '\0'; ++s)
76*0a6a1f1dSLionel Sambuc if (IS_IN_SET(UC(*s)))
77*0a6a1f1dSLionel Sambuc return __UNCONST(s);
78*0a6a1f1dSLionel Sambuc return NULL;
79*0a6a1f1dSLionel Sambuc }
80