From patchwork Sat Oct 14 08:32:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Tirta Halim X-Patchwork-Id: 77772 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2B7D33854143 for ; Sat, 14 Oct 2023 08:36:32 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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 3E0EA3858C2B for ; Sat, 14 Oct 2023 08:36:14 +0000 (GMT) ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3E0EA3858C2B 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=1697272576; cv=none; b=bRcl/gD4Wi6BFloTDkhSwnqk7vP5FLP7NHEjt6dtHk8ZE+AX9VMbBsMhYMwbZ9N+XJEbHZZsY9C4wZYr9NdFE92BLwwTS2IRb/YjonWtOyfaXF7IA+Wdf0JBXsfItXuBcZz0/yfsiVryon7UWNpB+EhdKxbNQMkHwOeSvh27+YU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697272576; c=relaxed/simple; bh=ltcp+VzZmZBCmA0dghknowoWNo7rf+Gd0sT35v98cAM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=kigSeqaGMJmKrZyTJ7iEA+8EzVssRkiIJsPsNEfOKGT3bf9UNBKq/U8JLCXtfqFW85KrXbxwYcRIcwLD1NJ4HgwF6ftErSgB7exwIth+S+lhJVcNWX6j7mJPXNyH8TZLb0AGKqtUanZt9B5I/YAevJ+ZamsDUpEmBNM+o85RTHM= ARC-Authentication-Results: i=1; server2.sourceware.org DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3E0EA3858C2B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-5ac88d2cfaaso906047a12.2 for ; Sat, 14 Oct 2023 01:36:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697272573; x=1697877373; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=o9WQIWonXKyGVCf+oroaK6DrImVPuuWgub/DhChhVeI=; b=Dj05/MkwA4tWWoMsdEZ5F6uGql7aoEqaWblrr1G2jl8yQsbdV+DINLogBbyMawwwAw 5bWFk/BfSWROX8o/0RipxRuZU2Z2eW2TCvn2nii4XWQ+qj/k3W5iPqafWMPHH8fNGsJ0 7OUeT40oq5kY2D1Uywq0DCqdD+PUuFFIHCv14HrKpkJR9HOs6I/wcOk8lwvvg8Mu1Pq6 SA/ODlji7Fw04MsOY5LvjJ/XMly3MY7+LUqhbyNJCZR0sQc290tzDXaSY48uqmyp7bkv xJeno2uey5vaAkxo+jloS+iySzU2f4tf/aTemN5NrLhUARmU5l1N0lv93UPn7vpdOfdd X+sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697272573; x=1697877373; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=o9WQIWonXKyGVCf+oroaK6DrImVPuuWgub/DhChhVeI=; b=J6U4pmviHUoLOojFAJ1JYz5U30nv4tRyqi9DOuMz83B7UerXwu9m2HSmS/QDGfjSg6 tE8RdhOBTeX8C8sJKE6JotShfI7hmKwXASRbqpUpZTGV+s30YOAWAdnJY6WszfI/k4zX axHp97OM9lXAL8j8PruNpquP5itI0T6UUrZl9J4bN39bwNgx8yLOhO5Ua2F5L8j+G/Af oMXmRpSHYZOEcR4NIpBNjHuNODn9usAP4FI7boabp4pwJiGLiWcd9+ABAzBO990i6Pw6 um4SqrbObAbUGpthsNdJzXgAb30Cep0f/irHHJGV9DnkFH4etKDBEEL7CCezdLairxFB k6aw== X-Gm-Message-State: AOJu0YwuF31SXRD6m8K0Nxar4Cx8W9ICC7uyS4KXx5hqhWpLYcSd76uZ 6I/oIn6ry4BQWZtkPBm7sHI= X-Google-Smtp-Source: AGHT+IHNgHCWVOim/Yry/ann5pbS9Ob4xZs2ARk2xKa/Zd27u++LQrfTXpI6NQavUFkdMIl649wKlg== X-Received: by 2002:a17:902:c94e:b0:1c7:245a:7fea with SMTP id i14-20020a170902c94e00b001c7245a7feamr38808827pla.58.1697272572646; Sat, 14 Oct 2023 01:36:12 -0700 (PDT) Received: from localhost.localdomain ([2001:448a:20a0:422b:fa4f:515b:1eb7:6778]) by smtp.gmail.com with ESMTPSA id c7-20020a170903234700b001bb892a7a67sm5077891plh.1.2023.10.14.01.36.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Oct 2023 01:36:12 -0700 (PDT) From: James Tirta Halim To: wilco.dijkstra@arm.com Cc: libc-alpha@sourceware.org, tirtajames45@gmail.com Subject: [PATCH] strcasestr: check if ne[0] is in hs with strchr or strpbrk as does strstr Date: Sat, 14 Oct 2023 15:32:53 +0700 Message-ID: <20231014083253.138510-1-tirtajames45@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org --- 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'}; + 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