Re: [PATCH v2 1/2] string: Add strprefix(), and deprecate str_has_prefix()

From: Andy Shevchenko

Date: Mon Nov 09 2026 - 17:40:27 EST


On Sun, Nov 08, 2026 at 12:54:33PM +0100, Alejandro Colomar wrote:
> > Date: 2026-11-08 12:49:08+0100
> > From: Alejandro Colomar <alx@xxxxxxxxxx>
> >
> > Hi Maciej,
> >
> > > Date: 2026-11-08 11:05:51+0000
> > > From: "Maciej W. Rozycki" <macro@xxxxxxxxxxx>
> > >
> > > On Sat, 7 Nov 2026, Alejandro Colomar wrote:
> > >
> > > > Hi Maciej,
> > > >
> > > > > Date: 2026-11-07 02:44:23+0000
> > > > > From: "Maciej W. Rozycki" <macro@xxxxxxxxxxx>
> > > > >
> > > > > On Thu, 5 Nov 2026, Alejandro Colomar wrote:
> > > > >
> > > > > > Do you think strafterprefix() would be better? Do you have any other
> > > > > > suggestions?
> > > > >
> > > > > FWIW strsuffix() seems an obvious choice to me. After all what the API
> > > > > does is request the string's suffix beyond the specified leading part.
> > > >
> > > > Actually, strsuffix() is something else:
> > > >
> > > > char *
> > > > strsuffix_(const char *str, const char *suffix)
> > > > {
> > > > ptrdiff_t off;
> > > >
> > > > off = strlen(str) - strlen(suffix);
> > > > if (off < 0)
> > > > return NULL;
> > > >
> > > > if (!streq(str + off, suffix))
> > > > return NULL;
> > > >
> > > > return str + off;
> > > > }
> > > >
> > > > It finds the start of a suffix in a string. That is:
> > > >
> > > > strcmp(strsuffix("foobar", "bar"), "bar") == 0;
> > >
> > > Hmm, I'd have called that strskipto(). If that's not possible for some
> > > reason (I can't see it used in Linux), then strskip() or strskipover()
> > > could be used for this API.
>
> Another issue with the name strskipto() is that the name doesn't make it
> clear whether the second argument is a string, or a list of characters.
>
> That is, would strskipto() something similar to strpbrk(3)?
> It's not clear from the name. From strprefix() and strsuffix(), the
> meaning of the second argument is clear from the function name.

On this bikeshedding thread I'm with Alejandro.

> > strsuffix() exists in many projects, and in all of them it seems to have
> > these semantics (or very similar ones).
> >
> > Just look at a Debian code search of those names.
> >
> > The strprefix() and strsuffix() pair of functions are widely
> > implemented, and while the exact details of the return value differ
> > slightly (some return just a boolean, others return a pointer that can
> > be used as a boolean too), the overall semantics of them are quite
> > consistent. strsuffix() has less drift than strprefix().
> >
> > If we added strsuffix() here with the semantics commonly attributed to
> > strprefix(), and then had strskipto() with the semantics commonly
> > attributed to strsuffix(), I'd expect programmers to eventually get
> > confused and use the wrong one.

--
With Best Regards,
Andy Shevchenko