1144545Sdas/*- 2144545Sdas * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG> 3144545Sdas * All rights reserved. 4141665Sglebius * 5141665Sglebius * Redistribution and use in source and binary forms, with or without 6141665Sglebius * modification, are permitted provided that the following conditions 7141665Sglebius * are met: 8141665Sglebius * 1. Redistributions of source code must retain the above copyright 9141665Sglebius * notice, this list of conditions and the following disclaimer. 10141665Sglebius * 2. Redistributions in binary form must reproduce the above copyright 11141665Sglebius * notice, this list of conditions and the following disclaimer in the 12141665Sglebius * documentation and/or other materials provided with the distribution. 13141665Sglebius * 14144545Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15141665Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16141665Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17144545Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18141665Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19141665Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20141665Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21141665Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22141665Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23141665Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24141665Sglebius * SUCH DAMAGE. 25141665Sglebius */ 26141665Sglebius 27141665Sglebius#include <sys/cdefs.h> 28141665Sglebius__FBSDID("$FreeBSD$"); 29141665Sglebius 30141665Sglebius#include <sys/libkern.h> 31144545Sdas#include <sys/limits.h> 32144545Sdas#include <sys/types.h> 33141665Sglebius 34144545Sdas#define IDX(c) ((u_char)(c) / LONG_BIT) 35144545Sdas#define BIT(c) ((u_long)1 << ((u_char)(c) % LONG_BIT)) 36144545Sdas 37141665Sglebiussize_t 38144545Sdasstrspn(const char *s, const char *charset) 39141665Sglebius{ 40141665Sglebius /* 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. 43141665Sglebius */ 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); 71141665Sglebius} 72