Re: [hackers] [dmenu] follow-up fix: add -D_GNU_SOURCE for strcasestr for some systems || Hiltjo Posthuma

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Mon, 7 Feb 2022 15:18:32 +0100

On Mon, Feb 07, 2022 at 02:31:24PM +0100, Quentin Rameau wrote:
> Hola,
>
> Here are my two cents,
>
> > On Mon, Feb 07, 2022 at 10:41:58AM +0100, Laslo Hunhold wrote:
> > > On Mon, 7 Feb 2022 10:36:46 +0100 (CET)
> > > git_AT_suckless.org wrote:
> > >
> > > Dear Hiltjo,
> > >
> > > > follow-up fix: add -D_GNU_SOURCE for strcasestr for some systems
> > >
> > > wouldn't it be better to avoid GNU-extensions in code?
> > >
> > > With best regards
> > >
> > > Laslo
> > >
> >
> > I kindof expected a reply like this. In general I don't disagree.
> >
> > This function is available on many systems for decades.
> >
> > On some systems like OpenBSD the -D_GNU_SOURCE is not needed.
>
> I still think it's kind of problematic to add a non-portable function
> here.
>
> What we do in other places is to provide a fallback implementation of
> such functions, so maybe we should do that here too.
>
> Also, without questioning your actual push, Hiltjo, maybe that's not the
> best way to fix the performance issue and we could invest more time to
> think about this.
>
>

I'm also OK with adding a better performant version of cistrstr in dmenu using
the code of strcasestr. I think the current version is too naive.

I tested it with a file with 500k lines and it was noticably faster.

What about this version? It is the strcasestr code from the sacc gopher client.
It is still noticably faster:


diff --git a/dmenu.c b/dmenu.c
index 98507d9..9b9a046 100644
--- a/dmenu.c
+++ b/dmenu.c
_AT_@ -102,14 +102,22 @@ cleanup(void)
         XCloseDisplay(dpy);
 }
 
-static char *
-cistrstr(const char *s, const char *sub)
+char *
+cistrstr(const char *h, const char *n)
 {
- size_t len;
+ size_t i;
+
+ if (!n[0])
+ return (char *)h;
+
+ for (; *h; ++h) {
+ for (i = 0; n[i] && tolower((unsigned char)n[i]) ==
+ tolower((unsigned char)h[i]); ++i)
+ ;
+ if (n[i] == '\0')
+ return (char *)h;
+ }
 
- for (len = strlen(sub); *s; s++)
- if (!strncasecmp(s, sub, len))
- return (char *)s;
         return NULL;
 }
 

-- 
Kind regards,
Hiltjo
Received on Mon Feb 07 2022 - 15:18:32 CET

This archive was generated by hypermail 2.3.0 : Mon Feb 07 2022 - 15:24:32 CET