1*0a6a1f1dSLionel Sambuc /* $NetBSD: strcspn.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: strcspn.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 /* 64bit version is in strspn.c */
41*0a6a1f1dSLionel Sambuc #if ULONG_MAX != 0xffffffffffffffffull
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambuc size_t
strcspn(const char * s,const char * charset)44*0a6a1f1dSLionel Sambuc strcspn(const char *s, const char *charset)
45*0a6a1f1dSLionel Sambuc {
46*0a6a1f1dSLionel Sambuc static const uint8_t idx[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
47*0a6a1f1dSLionel Sambuc const char *t;
48*0a6a1f1dSLionel Sambuc uint8_t set[32];
49*0a6a1f1dSLionel Sambuc #define UC(a) ((unsigned int)(unsigned char)(a))
50*0a6a1f1dSLionel Sambuc
51*0a6a1f1dSLionel Sambuc _DIAGASSERT(s != NULL);
52*0a6a1f1dSLionel Sambuc _DIAGASSERT(charset != NULL);
53*0a6a1f1dSLionel Sambuc
54*0a6a1f1dSLionel Sambuc if (charset[0] == '\0')
55*0a6a1f1dSLionel Sambuc return strlen(s);
56*0a6a1f1dSLionel Sambuc if (charset[1] == '\0') {
57*0a6a1f1dSLionel Sambuc for (t = s; *t != '\0'; ++t)
58*0a6a1f1dSLionel Sambuc if (*t == *charset)
59*0a6a1f1dSLionel Sambuc break;
60*0a6a1f1dSLionel Sambuc return t - s;
61*0a6a1f1dSLionel Sambuc }
62*0a6a1f1dSLionel Sambuc
63*0a6a1f1dSLionel Sambuc (void)memset(set, 0, sizeof(set));
64*0a6a1f1dSLionel Sambuc
65*0a6a1f1dSLionel Sambuc for (; *charset != '\0'; ++charset)
66*0a6a1f1dSLionel Sambuc set[UC(*charset) >> 3] |= idx[UC(*charset) & 7];
67*0a6a1f1dSLionel Sambuc
68*0a6a1f1dSLionel Sambuc for (t = s; *t != '\0'; ++t)
69*0a6a1f1dSLionel Sambuc if (set[UC(*t) >> 3] & idx[UC(*t) & 7])
70*0a6a1f1dSLionel Sambuc break;
71*0a6a1f1dSLionel Sambuc return t - s;
72*0a6a1f1dSLionel Sambuc }
73*0a6a1f1dSLionel Sambuc
74*0a6a1f1dSLionel Sambuc #endif
75