158f0484fSRodney W. Grimes /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni *
4f0ceb98fSDavid Schultz * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
5f0ceb98fSDavid Schultz * All rights reserved.
658f0484fSRodney W. Grimes *
758f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes * are met:
1058f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution.
1558f0484fSRodney W. Grimes *
16f0ceb98fSDavid Schultz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1758f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1858f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f0ceb98fSDavid Schultz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2058f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2158f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2258f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2358f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2458f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2558f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2658f0484fSRodney W. Grimes * SUCH DAMAGE.
2758f0484fSRodney W. Grimes */
2858f0484fSRodney W. Grimes
29f0ceb98fSDavid Schultz #include <sys/types.h>
30f0ceb98fSDavid Schultz #include <limits.h>
3158f0484fSRodney W. Grimes #include <string.h>
3258f0484fSRodney W. Grimes
33f0ceb98fSDavid Schultz #define IDX(c) ((u_char)(c) / LONG_BIT)
34f0ceb98fSDavid Schultz #define BIT(c) ((u_long)1 << ((u_char)(c) % LONG_BIT))
3558f0484fSRodney W. Grimes
36f0ceb98fSDavid Schultz size_t
strcspn(const char * s,const char * charset)37f0ceb98fSDavid Schultz strcspn(const char *s, const char *charset)
38f0ceb98fSDavid Schultz {
3958f0484fSRodney W. Grimes /*
40f0ceb98fSDavid Schultz * NB: idx and bit are temporaries whose use causes gcc 3.4.2 to
41f0ceb98fSDavid Schultz * generate better code. Without them, gcc gets a little confused.
4258f0484fSRodney W. Grimes */
43f0ceb98fSDavid Schultz const char *s1;
44f0ceb98fSDavid Schultz u_long bit;
45f0ceb98fSDavid Schultz u_long tbl[(UCHAR_MAX + 1) / LONG_BIT];
46f0ceb98fSDavid Schultz int idx;
47f0ceb98fSDavid Schultz
48f0ceb98fSDavid Schultz if(*s == '\0')
49f0ceb98fSDavid Schultz return (0);
50f0ceb98fSDavid Schultz
51f0ceb98fSDavid Schultz #if LONG_BIT == 64 /* always better to unroll on 64-bit architectures */
52f0ceb98fSDavid Schultz tbl[0] = 1;
53f0ceb98fSDavid Schultz tbl[3] = tbl[2] = tbl[1] = 0;
54f0ceb98fSDavid Schultz #else
55f0ceb98fSDavid Schultz for (tbl[0] = idx = 1; idx < sizeof(tbl) / sizeof(tbl[0]); idx++)
56f0ceb98fSDavid Schultz tbl[idx] = 0;
57f0ceb98fSDavid Schultz #endif
58f0ceb98fSDavid Schultz for (; *charset != '\0'; charset++) {
59f0ceb98fSDavid Schultz idx = IDX(*charset);
60f0ceb98fSDavid Schultz bit = BIT(*charset);
61f0ceb98fSDavid Schultz tbl[idx] |= bit;
6258f0484fSRodney W. Grimes }
63f0ceb98fSDavid Schultz
64f0ceb98fSDavid Schultz for(s1 = s; ; s1++) {
65f0ceb98fSDavid Schultz idx = IDX(*s1);
66f0ceb98fSDavid Schultz bit = BIT(*s1);
67f0ceb98fSDavid Schultz if ((tbl[idx] & bit) != 0)
68f0ceb98fSDavid Schultz break;
69f0ceb98fSDavid Schultz }
70f0ceb98fSDavid Schultz return (s1 - s);
7158f0484fSRodney W. Grimes }
72