1181748Skmacy/*- 2181748Skmacy * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG> 3181748Skmacy * All rights reserved. 4181748Skmacy * 5181748Skmacy * Redistribution and use in source and binary forms, with or without 6181748Skmacy * modification, are permitted provided that the following conditions 7181748Skmacy * are met: 8181748Skmacy * 1. Redistributions of source code must retain the above copyright 9181748Skmacy * notice, this list of conditions and the following disclaimer. 10181748Skmacy * 2. Redistributions in binary form must reproduce the above copyright 11181748Skmacy * notice, this list of conditions and the following disclaimer in the 12181748Skmacy * documentation and/or other materials provided with the distribution. 13181748Skmacy * 14181748Skmacy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15181748Skmacy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16181748Skmacy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17181748Skmacy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18181748Skmacy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19181748Skmacy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20181748Skmacy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21181748Skmacy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22181748Skmacy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23181748Skmacy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24181748Skmacy * SUCH DAMAGE. 25181748Skmacy */ 26181748Skmacy 27181748Skmacy#include <sys/cdefs.h> 28181748Skmacy__FBSDID("$FreeBSD$"); 29181748Skmacy 30181748Skmacy#include <sys/libkern.h> 31181748Skmacy#include <sys/types.h> 32181748Skmacy#include <sys/limits.h> 33181748Skmacy 34181748Skmacy#define IDX(c) ((u_char)(c) / LONG_BIT) 35181748Skmacy#define BIT(c) ((u_long)1 << ((u_char)(c) % LONG_BIT)) 36181748Skmacy 37181748Skmacysize_t 38181748Skmacystrcspn(const char * __restrict s, const char * __restrict charset) 39181748Skmacy{ 40181748Skmacy /* 41181748Skmacy * NB: idx and bit are temporaries whose use causes gcc 3.4.2 to 42181748Skmacy * generate better code. Without them, gcc gets a little confused. 43181748Skmacy */ 44181748Skmacy const char *s1; 45181748Skmacy u_long bit; 46181748Skmacy u_long tbl[(UCHAR_MAX + 1) / LONG_BIT]; 47181748Skmacy int idx; 48181748Skmacy 49181748Skmacy if(*s == '\0') 50181748Skmacy return (0); 51181748Skmacy 52181748Skmacy#if LONG_BIT == 64 /* always better to unroll on 64-bit architectures */ 53181748Skmacy tbl[0] = 1; 54181748Skmacy tbl[3] = tbl[2] = tbl[1] = 0; 55181748Skmacy#else 56181748Skmacy for (tbl[0] = idx = 1; idx < sizeof(tbl) / sizeof(tbl[0]); idx++) 57181748Skmacy tbl[idx] = 0; 58181748Skmacy#endif 59181748Skmacy for (; *charset != '\0'; charset++) { 60181748Skmacy idx = IDX(*charset); 61181748Skmacy bit = BIT(*charset); 62181748Skmacy tbl[idx] |= bit; 63181748Skmacy } 64181748Skmacy 65181748Skmacy for(s1 = s; ; s1++) { 66181748Skmacy idx = IDX(*s1); 67181748Skmacy bit = BIT(*s1); 68181748Skmacy if ((tbl[idx] & bit) != 0) 69181748Skmacy break; 70181748Skmacy } 71181748Skmacy return (s1 - s); 72181748Skmacy} 73