On Thu, Sep 25, 2025 at 08:29:47PM +0200, Alejandro Colomar wrote:
> 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;
And here's a case for the suffix pointer API:
diff --git i/lib/parse-datetime.y w/lib/parse-datetime.y
index 6c52cd2c4c..bf2a53561d 100644
--- i/lib/parse-datetime.y
+++ w/lib/parse-datetime.y
@@ -1390,13 +1390,14 @@ lookup_word (parser_control const *pc, char
*word)
return tp;
/* Strip off any plural and try the units table again. */
- if (word[wordlen - 1] == 'S')
+ char *sfx = stpsuffix(word, "S");
+ if (sfx)
{
- word[wordlen - 1] = '\0';
+ strcpy(sfx, "");
for (tp = time_units_table; tp->name; tp++)
if (streq (word, tp->name))
return tp;
- word[wordlen - 1] = 'S'; /* For "this" in relative_time_table.
*/
+ strcpy(sfx, "S"); /* For "this" in relative_time_table. */
}
for (tp = relative_time_table; tp->name; tp++)
>
>
> Have a lovely night!
> Alex
>
> --
> <https://www.alejandro-colomar.es>
> Use port 80 (that is, <...:80/>).
--
<https://www.alejandro-colomar.es>
Use port 80 (that is, <...:80/>).
signature.asc
Description: PGP signature
