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

From: Alejandro Colomar

Date: Sun Nov 08 2026 - 06:49:53 EST


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.

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.


Have a lovely day!
Alex

>
> FWIW,
>
> Maciej

--
<https://www.alejandro-colomar.es>

Attachment: signature.asc
Description: PGP signature