public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] strcasestr: check if ne[0] is in hs with strchr or strpbrk as does strstr
@ 2023-09-06 17:42 James Tirta Halim
  0 siblings, 0 replies; 7+ messages in thread
From: James Tirta Halim @ 2023-09-06 17:42 UTC (permalink / raw)
  To: libc-alpha; +Cc: James Tirta Halim

---
 string/strcasestr.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/string/strcasestr.c b/string/strcasestr.c
index 2f6b4f8641..295cbf364d 100644
--- a/string/strcasestr.c
+++ b/string/strcasestr.c
@@ -67,6 +67,14 @@ STRCASESTR (const char *haystack, const char *needle)
   /* Handle empty NEEDLE special case.  */
   if (needle[0] == '\0')
     return (char *) haystack;
+  if (isalpha(*needle)) {
+    const char a[] = { tolower(*needle), toupper(*needle), '\0'};
+    haystack = strpbrk(haystack, a);
+  } else {
+    haystack = strchr(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
-- 
2.42.0


^ permalink raw reply	[flat|nested] 7+ messages in thread
* [PATCH] strcasestr: check if ne[0] is in hs with strchr or strpbrk as does strstr
@ 2023-09-07 22:14 Wilco Dijkstra
  2023-10-14  8:32 ` James Tirta Halim
  0 siblings, 1 reply; 7+ messages in thread
From: Wilco Dijkstra @ 2023-09-07 22:14 UTC (permalink / raw)
  To: tirtajames45; +Cc: 'GNU C Library'

Hi James,

This looks correct to me, but what about performance?

+  if (isalpha(*needle)) {
+    const char a[] = { tolower(*needle), toupper(*needle), '\0'};
+    haystack = strpbrk(haystack, a);

strpbrk has a high startup overhead and is slow overall. A basic
while loop checking tolower (haystack[0]) will be faster here.

+  } else {
+    haystack = strchr(haystack, *needle);
+  }
+  if (haystack == NULL || needle[1] == '\0')
+    return (char *)haystack;

This should help a bit in some cases, but searching for the first
character match improves performance the most if you check for
a full match before the expensive initialization of the main algorithm
(similar to what strstr does).

Note that using strchr on a large haystack may actually result in a
slowdown given that the matching algorithms are faster than strchr
on typical inputs (due to being superlinear).

Cheers,
Wilco

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-10-17  9:58 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-06 17:42 [PATCH] strcasestr: check if ne[0] is in hs with strchr or strpbrk as does strstr James Tirta Halim
2023-09-07 22:14 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

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).