From 1c8829fff4506434d44be94d90b49fe01d72e9f1 Mon Sep 17 00:00:00 2001 From: Joerg Sonnenberger Date: Thu, 31 Mar 2005 13:17:22 +0000 Subject: [PATCH] Make it O(strlen(s) + strlen(charset)) like strcspn. Submitted-by: Andreas Hauser --- lib/libc/string/strspn.c | 93 +++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/lib/libc/string/strspn.c b/lib/libc/string/strspn.c index d0d35ccc1e..eac35de004 100644 --- a/lib/libc/string/strspn.c +++ b/lib/libc/string/strspn.c @@ -1,61 +1,68 @@ -/* - * Copyright (c) 1989, 1993 - * The Regents of the University of California. All rights reserved. - * +/*- + * Copyright (c) 2005 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Andreas Hauser + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: + * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)strspn.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strspn.c,v 1.1.1.1.14.1 2001/07/09 23:30:07 obrien Exp $ - * $DragonFly: src/lib/libc/string/strspn.c,v 1.3 2004/10/25 19:38:02 drhodus Exp $ + * $DragonFly: src/lib/libc/string/strspn.c,v 1.4 2005/03/31 13:17:22 joerg Exp $ */ -#include +#include +#include +#include #include /* - * Span the string s2 (skip characters that are in s2). + * Longest initial segment of string *str, containing any chars from *chars. */ size_t -strspn(s1, s2) - const char *s1; - const char *s2; +strspn(const char *str, const char *chars) { - const char *p = s1, *spanp; - char c, sc; + const char *s; + bool charset[UCHAR_MAX]; + + _DIAGASSERT(str != NULL); + _DIAGASSERT(chars != NULL); + + if(*str == '\0') + return 0; + if(*chars == '\0') + return strlen(str); + + memset(charset, false, sizeof(charset)); + for( ; *chars != '\0'; chars++) + charset[*(const unsigned char *)chars] = true; + + for(s = str; charset[*(const unsigned char *)s] == true; s++) + /* NOTHING */ + ; - /* - * Skip any characters in s2, excluding the terminating \0. - */ -cont: - c = *p++; - for (spanp = s2; (sc = *spanp++) != 0;) - if (sc == c) - goto cont; - return (p - 1 - s1); + return (s - str); } -- 2.41.0