Hi Paul,

On Thu, Sep 25, 2025 at 11:06:42AM -0700, Paul Eggert wrote:
> On 2025-09-25 10:03, Alejandro Colomar wrote:
> > Did I change your opinion in any way about having the pointer variant
> > named either strprefix() or stpprefix()?  Do you have any preference for
> > any of them?
> 
> I'd have a better feeling about these if I saw how they might be used in
> Gnulib. For example, are there instances of code now that could use them but
> where str_startswith and str_endswith does not suffice?
> 
> For streq and memeq the answer was easy: there were several places where
> they simplified Gnulib callers, and that was a good argument for adding them
> to Gnulib. Is there a similar argument for strprefix and strsuffix?

There were some.  Not as many as with streq(3), of course, but some.
At home I had a draft of a patch for doing that replacement; when I
arrive home, I'll check it.

I've been looking at the gnulib source code for a quick example, and
here's one:

        diff --git i/lib/setlocale-fixes.c w/lib/setlocale-fixes.c
        index caa1863329..2b698f3d1c 100644
        --- i/lib/setlocale-fixes.c
        +++ w/lib/setlocale-fixes.c
        @@ -136,15 +136,21 @@ extract_single_name (char single_name[256+1], int 
category, const char *name)
             {
               /* A mixed locale name.  */
               const char *cat_name = cat_names[cat_to_index (category)];
        -      size_t cat_name_len = strlen (cat_name);
               const char *p = name;
               for (;;)
                 {
                   const char *q = strchr (p, ';');
        -          if (strncmp (p, cat_name, cat_name_len) == 0
        -              && p[cat_name_len] == '=')
        +
        +       // In GNU C, we could write it even more compactly:
        +       //
        +       // p = stpprefix(stpprefix(p, cat_name) ?: "", "=");
        +       // if (p != NULL)
        +
        +          p = stpprefix (p, cat_name);
        +          if (p != NULL)
        +            p = strprefix (p, "=");
        +          if (p != NULL)
                     {
        -              p += cat_name_len + 1;
                       size_t n = (q == NULL ? strlen (p) : q - p);
                       if (n >= 256+1)
                         n = 256;


Have a lovely night!
Alex

-- 
<https://www.alejandro-colomar.es>
Use port 80 (that is, <...:80/>).

Attachment: signature.asc
Description: PGP signature

Reply via email to