1111072Sjake/*-
2111072Sjake * SPDX-License-Identifier: BSD-2-Clause
3111072Sjake *
4111072Sjake * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
5111072Sjake * All rights reserved.
6111072Sjake *
7111072Sjake * Redistribution and use in source and binary forms, with or without
8111072Sjake * modification, are permitted provided that the following conditions
9111072Sjake * are met:
10111072Sjake * 1. Redistributions of source code must retain the above copyright
11111072Sjake *    notice, this list of conditions and the following disclaimer.
12111072Sjake * 2. Redistributions in binary form must reproduce the above copyright
13111072Sjake *    notice, this list of conditions and the following disclaimer in the
14111072Sjake *    documentation and/or other materials provided with the distribution.
15111072Sjake *
16111072Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17111072Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18111072Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19111072Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20111072Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21111072Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22111072Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23111072Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24111072Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25111072Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26111072Sjake * SUCH DAMAGE.
27143129Smarius */
28143129Smarius
29143129Smarius#include <sys/types.h>
30111072Sjake#include <limits.h>
31111072Sjake#include <string.h>
32111072Sjake
33111072Sjake#define	IDX(c)	((u_char)(c) / LONG_BIT)
34111072Sjake#define	BIT(c)	((u_long)1 << ((u_char)(c) % LONG_BIT))
35111123Sjake
36111072Sjakesize_t
37133589Smariusstrcspn(const char *s, const char *charset)
38111072Sjake{
39111072Sjake	/*
40111072Sjake	 * NB: idx and bit are temporaries whose use causes gcc 3.4.2 to
41111072Sjake	 * generate better code.  Without them, gcc gets a little confused.
42111072Sjake	 */
43111072Sjake	const char *s1;
44111072Sjake	u_long bit;
45111072Sjake	u_long tbl[(UCHAR_MAX + 1) / LONG_BIT];
46111072Sjake	int idx;
47111072Sjake
48111072Sjake	if(*s == '\0')
49111072Sjake		return (0);
50111123Sjake
51111123Sjake#if LONG_BIT == 64	/* always better to unroll on 64-bit architectures */
52111123Sjake	tbl[0] = 1;
53111123Sjake	tbl[3] = tbl[2] = tbl[1] = 0;
54111123Sjake#else
55111123Sjake	for (tbl[0] = idx = 1; idx < sizeof(tbl) / sizeof(tbl[0]); idx++)
56111123Sjake		tbl[idx] = 0;
57111123Sjake#endif
58111072Sjake	for (; *charset != '\0'; charset++) {
59133589Smarius		idx = IDX(*charset);
60133589Smarius		bit = BIT(*charset);
61111072Sjake		tbl[idx] |= bit;
62111072Sjake	}
63111072Sjake
64111072Sjake	for(s1 = s; ; s1++) {
65111072Sjake		idx = IDX(*s1);
66111072Sjake		bit = BIT(*s1);
67111123Sjake		if ((tbl[idx] & bit) != 0)
68111123Sjake			break;
69111072Sjake	}
70111072Sjake	return (s1 - s);
71111072Sjake}
72111072Sjake