From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by sourceware.org (Postfix) with ESMTPS id AC15B3858C54 for ; Mon, 16 Oct 2023 12:59:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AC15B3858C54 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AC15B3858C54 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::536 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697461186; cv=none; b=owk4OcLXDbYGa4jocxqrwnueWZvgbZ/slQVgjVzy8FGCLuKyurinb+M+B6rchuTy5Tuv+quoYiNhaPpSyxZk6iOppA+9cbhbVo9/DfqGUuLv0S0Tp3SHws7QkPts6tlrYPUwXqomSLQTIA0bFRlJU1oVtaxqCU2mAbcayL/7AXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697461186; c=relaxed/simple; bh=FjxfUWodkh7MQRdDdLj+6RLdwx3cugMuJ8Sm/M4lbD8=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=iHRfFRnh0Ji52/C0Vnz1yTRjwP+HrcYH0WFYqzXpb61MsUVFOjQnxvChEJqlpQwAwX/pHJnc6bDFDB4UzOpv/agin0GDyw83eupJbqZ4vXqn3TVggJG1Qn2WcPFCA7oOCintRCoBatTJjvnBzppBBzbgBpCGdJO9NT1npuOJ9wM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-5ae99bb5ccdso1016448a12.1 for ; Mon, 16 Oct 2023 05:59:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1697461183; x=1698065983; darn=sourceware.org; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=bjr7JFE3j2Bbpob05dXE6fpRRHwaOhaTCNZrvMoMe/M=; b=XwS3yykvxqqS7xfhlEFZYkwNOJReYRCAcEecStRX7JfyKBgq7oPDTjR5VHRQSnk42Y XPGNE/hjq06pdWQJSOIcQf3t5TfV+yXqDPROZKa8fv8Q9AfkFXwPzCDlQ3fLpHwuGTf0 Zx0wxtRD1MEEISDU4hNkpqwOq67bfSfpmq/5bPRU8eAe8iuivRYhNV6YB52hHht3wujO /ROiIIs4RHEe/KHaYO7gwgqQ0fQG9DHh42DipvGym0q7rf1WLWxUieJ8A3rfRupjooJj 5pTtWCqQf3RfK5rdy4HBSmYFHnyE/vrKrVxbogpZ2Fe/+zO7ytYGhNY6xLbCflqP9HHJ u82w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697461183; x=1698065983; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bjr7JFE3j2Bbpob05dXE6fpRRHwaOhaTCNZrvMoMe/M=; b=BolivRRmcBiIHuQS8b5suJIFYPxtM50MHW7d29fH2bIKJe2oiXNswr8KV7znx07C7J nmA3WUDqt2gm1cxJ2vH6fM3qk8uVzhUlWH0zQtngwKlEYr5MBm0+964YYwjpNq20z4ih y4TUr3O8YuPs4XjANo/8O8LR1IbvcxEtZDSMjYgNweHLox9JZlU9zLZOYQAd7ULYamT1 us6Xukuk/Kwdm8rPg0ZOcQeuMoKMCb9mMFNXWRm3Hd0QM2m0aOgZH19ON9dXZ2F3ssnj hvcKY9Qkglp8TInyvZD0qSINdhDE+iMWaUMnHzb+Zf99QffEuQQcW8IuMrkFEyoahOrE Gc5A== X-Gm-Message-State: AOJu0YzCzV7XfdM1AK818dWCEsXKp7VrUx6E0NfFrK20vFKOF728hw8P TRburqvslDFC5mxzAO6ms/1kOg== X-Google-Smtp-Source: AGHT+IGAd1zv3M9UVoHhSXag2jqDmML4xBKUwAIWG8NqwsgkELFVxYhwkLF06MCy6pamUCkLcyar1A== X-Received: by 2002:a05:6a20:258e:b0:159:c24f:5fa4 with SMTP id k14-20020a056a20258e00b00159c24f5fa4mr33962942pzd.1.1697461183578; Mon, 16 Oct 2023 05:59:43 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c2:d09b:6dd7:5070:b348:8a98? ([2804:1b3:a7c2:d09b:6dd7:5070:b348:8a98]) by smtp.gmail.com with ESMTPSA id w65-20020a636244000000b00584aff3060dsm4083912pgb.59.2023.10.16.05.59.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Oct 2023 05:59:42 -0700 (PDT) Message-ID: <8deea669-21b6-442b-a58a-05be075290a8@linaro.org> Date: Mon, 16 Oct 2023 09:59:40 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] strcasestr: check if ne[0] is in hs with strchr or strpbrk as does strstr Content-Language: en-US To: James Tirta Halim , wilco.dijkstra@arm.com Cc: libc-alpha@sourceware.org References: <20231014083253.138510-1-tirtajames45@gmail.com> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <20231014083253.138510-1-tirtajames45@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 14/10/23 05:32, James Tirta Halim wrote: > --- > string/strcasestr.c | 31 +++++++++++++++++++++++++++++++ > 1 file changed, 31 insertions(+) > > diff --git a/string/strcasestr.c b/string/strcasestr.c > index 2f6b4f8641..aca41211dd 100644 > --- a/string/strcasestr.c > +++ b/string/strcasestr.c > @@ -55,6 +55,30 @@ > #endif > > > +static inline char *__attribute__ ((always_inline)) > +strcasechr (const char *s, char c) > +{ > + if (isalpha(c)) { > + /* May have optimized strcspn? */ > +#if defined __sparc__ || defined __sparc || defined __x86_64__ || defined _M_X64 || defined __s390x__ || defined i386 || defined __i386__ || defined __i386 || defined _M_IX86 || defined __PPC64__ || defined __ppc64__ || defined _ARCH_PPC64 || _ARCH_PWR8 > + const char a[] = {tolower(c), toupper(c), '\0'}; We have the sysdep folder exactly to provide such macros if requires, for instance the file sysdeps/generic/string-fzi.h. So if this optimization is really worth, the best way to provide would be through a generic implementation like: static __always_inline char * strcasechr (const char *s, char c) { /* Generic implementation. */ } And then on each architecture where using strcspn is better to add a string-xxx.h override. > + s = (char *)strcspn(s, a); > +#else > + c = tolower(c); > + while (*s && tolower(*s) != c) > + ++s; > +#endif > + if (*s != '\0') > + return (char *)s; > + } else { > + s = strchr(s, c); > + if (s != NULL) > + return (char *)s; > + } > + return NULL; > +} > + > + > /* Find the first occurrence of NEEDLE in HAYSTACK, using > case-insensitive comparison. This function gives unspecified > results in multibyte locales. */ > @@ -68,6 +92,10 @@ STRCASESTR (const char *haystack, const char *needle) > if (needle[0] == '\0') > return (char *) haystack; > > + haystack = strcasechr (haystack, *needle); > + if (haystack == NULL || needle[1] == '\0') > + return (char *) haystack; > + > /* Ensure HAYSTACK length is at least as long as NEEDLE length. > Since a match may occur early on in a huge HAYSTACK, use strnlen > and read ahead a few cachelines for improved performance. */ > @@ -75,6 +103,9 @@ STRCASESTR (const char *haystack, const char *needle) > haystack_len = __strnlen (haystack, needle_len + 256); > if (haystack_len < needle_len) > return NULL; > + > + if (strncasecmp (haystack, needle, needle_len) == 0) > + return (char *) haystack; > > /* Perform the search. Abstract memory is considered to be an array > of 'unsigned char' values, not an array of 'char' values. See