xref: /freebsd/lib/libc/string/strspn.c (revision d915a14ef094c8dfc1a5aee70e135abfec01d0f1)
1f0ceb98fSDavid Schultz /*-
2*d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d915a14eSPedro 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 
2958f0484fSRodney W. Grimes #include <sys/cdefs.h>
30de5fe5d5SDavid E. O'Brien __FBSDID("$FreeBSD$");
31de5fe5d5SDavid E. O'Brien 
32f0ceb98fSDavid Schultz #include <sys/types.h>
33f0ceb98fSDavid Schultz #include <limits.h>
3458f0484fSRodney W. Grimes #include <string.h>
3558f0484fSRodney W. Grimes 
36f0ceb98fSDavid Schultz #define	IDX(c)	((u_char)(c) / LONG_BIT)
37f0ceb98fSDavid Schultz #define	BIT(c)	((u_long)1 << ((u_char)(c) % LONG_BIT))
3858f0484fSRodney W. Grimes 
39f0ceb98fSDavid Schultz size_t
40f0ceb98fSDavid Schultz strspn(const char *s, const char *charset)
41f0ceb98fSDavid Schultz {
4258f0484fSRodney W. Grimes 	/*
43f0ceb98fSDavid Schultz 	 * NB: idx and bit are temporaries whose use causes gcc 3.4.2 to
44f0ceb98fSDavid Schultz 	 * generate better code.  Without them, gcc gets a little confused.
4558f0484fSRodney W. Grimes 	 */
46f0ceb98fSDavid Schultz 	const char *s1;
47f0ceb98fSDavid Schultz 	u_long bit;
48f0ceb98fSDavid Schultz 	u_long tbl[(UCHAR_MAX + 1) / LONG_BIT];
49f0ceb98fSDavid Schultz 	int idx;
50f0ceb98fSDavid Schultz 
51f0ceb98fSDavid Schultz 	if(*s == '\0')
52f0ceb98fSDavid Schultz 		return (0);
53f0ceb98fSDavid Schultz 
54f0ceb98fSDavid Schultz #if LONG_BIT == 64	/* always better to unroll on 64-bit architectures */
55f0ceb98fSDavid Schultz 	tbl[3] = tbl[2] = tbl[1] = tbl[0] = 0;
56f0ceb98fSDavid Schultz #else
57f0ceb98fSDavid Schultz 	for (idx = 0; idx < sizeof(tbl) / sizeof(tbl[0]); idx++)
58f0ceb98fSDavid Schultz 		tbl[idx] = 0;
59f0ceb98fSDavid Schultz #endif
60f0ceb98fSDavid Schultz 	for (; *charset != '\0'; charset++) {
61f0ceb98fSDavid Schultz 		idx = IDX(*charset);
62f0ceb98fSDavid Schultz 		bit = BIT(*charset);
63f0ceb98fSDavid Schultz 		tbl[idx] |= bit;
64f0ceb98fSDavid Schultz 	}
65f0ceb98fSDavid Schultz 
66f0ceb98fSDavid Schultz 	for(s1 = s; ; s1++) {
67f0ceb98fSDavid Schultz 		idx = IDX(*s1);
68f0ceb98fSDavid Schultz 		bit = BIT(*s1);
69f0ceb98fSDavid Schultz 		if ((tbl[idx] & bit) == 0)
70f0ceb98fSDavid Schultz 			break;
71f0ceb98fSDavid Schultz 	}
72f0ceb98fSDavid Schultz 	return (s1 - s);
7358f0484fSRodney W. Grimes }
74