xref: /freebsd/sys/libkern/strcspn.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1bbbc5bc1SKip Macy /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
4bbbc5bc1SKip Macy  * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
5bbbc5bc1SKip Macy  * All rights reserved.
6bbbc5bc1SKip Macy  *
7bbbc5bc1SKip Macy  * Redistribution and use in source and binary forms, with or without
8bbbc5bc1SKip Macy  * modification, are permitted provided that the following conditions
9bbbc5bc1SKip Macy  * are met:
10bbbc5bc1SKip Macy  * 1. Redistributions of source code must retain the above copyright
11bbbc5bc1SKip Macy  *    notice, this list of conditions and the following disclaimer.
12bbbc5bc1SKip Macy  * 2. Redistributions in binary form must reproduce the above copyright
13bbbc5bc1SKip Macy  *    notice, this list of conditions and the following disclaimer in the
14bbbc5bc1SKip Macy  *    documentation and/or other materials provided with the distribution.
15bbbc5bc1SKip Macy  *
16bbbc5bc1SKip Macy  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17bbbc5bc1SKip Macy  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bbbc5bc1SKip Macy  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bbbc5bc1SKip Macy  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20bbbc5bc1SKip Macy  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bbbc5bc1SKip Macy  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bbbc5bc1SKip Macy  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bbbc5bc1SKip Macy  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bbbc5bc1SKip Macy  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bbbc5bc1SKip Macy  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bbbc5bc1SKip Macy  * SUCH DAMAGE.
27bbbc5bc1SKip Macy  */
28bbbc5bc1SKip Macy 
29bbbc5bc1SKip Macy #include <sys/cdefs.h>
30bbbc5bc1SKip Macy #include <sys/libkern.h>
31bbbc5bc1SKip Macy #include <sys/types.h>
32bbbc5bc1SKip Macy #include <sys/limits.h>
33bbbc5bc1SKip Macy 
34bbbc5bc1SKip Macy #define	IDX(c)	((u_char)(c) / LONG_BIT)
35bbbc5bc1SKip Macy #define	BIT(c)	((u_long)1 << ((u_char)(c) % LONG_BIT))
36bbbc5bc1SKip Macy 
37bbbc5bc1SKip Macy size_t
strcspn(const char * s,const char * charset)3856d33e86SAlfonso Gregory strcspn(const char *s, const char *charset)
39bbbc5bc1SKip Macy {
40bbbc5bc1SKip Macy 	/*
41bbbc5bc1SKip Macy 	 * NB: idx and bit are temporaries whose use causes gcc 3.4.2 to
42bbbc5bc1SKip Macy 	 * generate better code.  Without them, gcc gets a little confused.
43bbbc5bc1SKip Macy 	 */
44bbbc5bc1SKip Macy 	const char *s1;
45bbbc5bc1SKip Macy 	u_long bit;
46bbbc5bc1SKip Macy 	u_long tbl[(UCHAR_MAX + 1) / LONG_BIT];
47bbbc5bc1SKip Macy 	int idx;
48bbbc5bc1SKip Macy 
49bbbc5bc1SKip Macy 	if(*s == '\0')
50bbbc5bc1SKip Macy 		return (0);
51bbbc5bc1SKip Macy 
52bbbc5bc1SKip Macy #if LONG_BIT == 64	/* always better to unroll on 64-bit architectures */
53bbbc5bc1SKip Macy 	tbl[0] = 1;
54bbbc5bc1SKip Macy 	tbl[3] = tbl[2] = tbl[1] = 0;
55bbbc5bc1SKip Macy #else
56bbbc5bc1SKip Macy 	for (tbl[0] = idx = 1; idx < sizeof(tbl) / sizeof(tbl[0]); idx++)
57bbbc5bc1SKip Macy 		tbl[idx] = 0;
58bbbc5bc1SKip Macy #endif
59bbbc5bc1SKip Macy 	for (; *charset != '\0'; charset++) {
60bbbc5bc1SKip Macy 		idx = IDX(*charset);
61bbbc5bc1SKip Macy 		bit = BIT(*charset);
62bbbc5bc1SKip Macy 		tbl[idx] |= bit;
63bbbc5bc1SKip Macy 	}
64bbbc5bc1SKip Macy 
65bbbc5bc1SKip Macy 	for(s1 = s; ; s1++) {
66bbbc5bc1SKip Macy 		idx = IDX(*s1);
67bbbc5bc1SKip Macy 		bit = BIT(*s1);
68bbbc5bc1SKip Macy 		if ((tbl[idx] & bit) != 0)
69bbbc5bc1SKip Macy 			break;
70bbbc5bc1SKip Macy 	}
71bbbc5bc1SKip Macy 	return (s1 - s);
72bbbc5bc1SKip Macy }
73