From: James Tirta Halim <tirtajames45@gmail.com>
To: wilco.dijkstra@arm.com
Cc: libc-alpha@sourceware.org, tirtajames45@gmail.com
Subject: [PATCH 1/2] strcasestr: check if ne[0] is in hs with strchr or strpbrk as does strstr
Date: Sat, 14 Oct 2023 15:56:42 +0700 [thread overview]
Message-ID: <20231014085642.153858-1-tirtajames45@gmail.com> (raw)
In-Reply-To: <PAWPR08MB89829F6BE1B835956E61AEE983EEA@PAWPR08MB8982.eurprd08.prod.outlook.com>
---
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 || defined _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);
+ haystack_len = __strnlen (haystack, needle_len);
if (haystack_len < needle_len)
return NULL;
+
+ if (strncasecmp (haystack, needle, needle_len) == 0)
+ return (char *) haystack;
+ haystack_len += __strnlen(haystack + haystack_len, 256);
/* Perform the search. Abstract memory is considered to be an array
of 'unsigned char' values, not an array of 'char' values. See
--
2.42.0
next prev parent reply other threads:[~2023-10-14 8:59 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-07 22:14 [PATCH] " Wilco Dijkstra
2023-10-14 8:32 ` James Tirta Halim
2023-10-16 12:59 ` Adhemerval Zanella Netto
2023-10-16 13:52 ` Wilco Dijkstra
2023-10-16 16:56 ` Noah Goldstein
2023-10-17 9:57 ` Wilco Dijkstra
2023-10-14 8:56 ` James Tirta Halim [this message]
2023-11-28 14:01 ` [PATCH] strcasestr: try to find non-alpha char in NEEDLE James Tirta Halim
2023-12-04 14:44 ` Carlos O'Donell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231014085642.153858-1-tirtajames45@gmail.com \
--to=tirtajames45@gmail.com \
--cc=libc-alpha@sourceware.org \
--cc=wilco.dijkstra@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).