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