1144545Sdas/*- 2144545Sdas * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG> 3144545Sdas * All rights reserved. 41573Srgrimes * 51573Srgrimes * Redistribution and use in source and binary forms, with or without 61573Srgrimes * modification, are permitted provided that the following conditions 71573Srgrimes * are met: 81573Srgrimes * 1. Redistributions of source code must retain the above copyright 91573Srgrimes * notice, this list of conditions and the following disclaimer. 101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111573Srgrimes * notice, this list of conditions and the following disclaimer in the 121573Srgrimes * documentation and/or other materials provided with the distribution. 131573Srgrimes * 14144545Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 151573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 161573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17144545Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 181573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 191573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 201573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 211573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 221573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 231573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 241573Srgrimes * SUCH DAMAGE. 251573Srgrimes */ 261573Srgrimes 2786170Sobrien#include <sys/cdefs.h> 2886170Sobrien__FBSDID("$FreeBSD$"); 291573Srgrimes 30144545Sdas#include <sys/types.h> 31144545Sdas#include <limits.h> 321573Srgrimes#include <string.h> 331573Srgrimes 34144545Sdas#define IDX(c) ((u_char)(c) / LONG_BIT) 35144545Sdas#define BIT(c) ((u_long)1 << ((u_char)(c) % LONG_BIT)) 36144545Sdas 371573Srgrimessize_t 38144545Sdasstrspn(const char *s, const char *charset) 391573Srgrimes{ 401573Srgrimes /* 41144545Sdas * NB: idx and bit are temporaries whose use causes gcc 3.4.2 to 42144545Sdas * generate better code. Without them, gcc gets a little confused. 431573Srgrimes */ 44144545Sdas const char *s1; 45144545Sdas u_long bit; 46144545Sdas u_long tbl[(UCHAR_MAX + 1) / LONG_BIT]; 47144545Sdas int idx; 48144545Sdas 49144545Sdas if(*s == '\0') 50144545Sdas return (0); 51144545Sdas 52144545Sdas#if LONG_BIT == 64 /* always better to unroll on 64-bit architectures */ 53144545Sdas tbl[3] = tbl[2] = tbl[1] = tbl[0] = 0; 54144545Sdas#else 55144545Sdas for (idx = 0; idx < sizeof(tbl) / sizeof(tbl[0]); idx++) 56144545Sdas tbl[idx] = 0; 57144545Sdas#endif 58144545Sdas for (; *charset != '\0'; charset++) { 59144545Sdas idx = IDX(*charset); 60144545Sdas bit = BIT(*charset); 61144545Sdas tbl[idx] |= bit; 62144545Sdas } 63144545Sdas 64144545Sdas for(s1 = s; ; s1++) { 65144545Sdas idx = IDX(*s1); 66144545Sdas bit = BIT(*s1); 67144545Sdas if ((tbl[idx] & bit) == 0) 68144545Sdas break; 69144545Sdas } 70144545Sdas return (s1 - s); 711573Srgrimes} 72