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