Hi Bruno, Paul,

On Fri, Sep 19, 2025 at 04:23:32PM +0200, Alejandro Colomar wrote:
> > The major reason that speaks for the name 'str_startswith', as used
> > in Gnulib, is:
> >   - The term starts-with is already used for this purpose in
> >     10 out of 15 programming languages. Find attached a summary from
> >     the prior knowledge summarization engine.
> >   - The prefix 'str' shows that it's for 'char *' strings and allows
> >     a similar function with prefix 'wcs' for 'wchar_t *' wide strings.
> 
> The second point isn't exclusive of str_startswith.  stRprefix() has the
> same property.
> 
> The etymology for the name stPprefix() comes from the stp* (POSIX; e.g.,
> stpcpy(3)) and memp* (GNU, BSD; e.g. mempcpy(3)) families of functions,
> which are string functions that return an offset pointer.
> 
> Since these return an offset pointer, the stp* prefix is quite
> appropriate for stpprefix() and stpsuffix().
> 
> About names from other languages, I don't necessarily like that
> reasoning.  We almost had _Countof() named _Lengthof() because committee
> members wanted that name from other languages which have it as length.
> That would have been harmful, as it would have mixed sizes and lengths
> in string-handling code, promoting off-by-one bugs.  In general, I'd
> take other languages with a pinch of salt.
> 
> Consistency with libc is important too.  If I see str_startswith(), I
> would guess it's a projec-specific API.  If I see strprefix(), I guess
> it's a libc API.  That's because the reserved prefix is str* followed by
> lowercase.  And even without the reservation, we have no such names in
> string.h, so instinct also plays a role.
> 
> > Therefore, I think we should stay with that for Gnulib. No 'strprefix'
> > or such.
> > 
> > Then, let's look for a readable and pronounceable name for the variant
> > that returns a pointer.
> > 
> >   - I tried thinking in terms of parsing, i.e. "str_parse_prefix",
> >     but what about the one with suffix then? "str_backwardparse_suffix"?
> >     Backward parsing is rarely seen in code.
> 
> That induces confusion.  Does the function reverse the string as if by
> rev(1) before searching?
> 
> >   - How about the names 'str_prefix_end' and 'str_suffix_start'?
> >     It's descriptive.
> >     It's pronounceable.
> >     The names imply that they return a pointer.
> >     The names are not very long, compared to what we already find in ISO C:
> > 
> >       str_prefix_end
> >       str_suffix_start
> >       fegetexceptflag
> >       fe_dec_getround
> >       fmaximum_mag_num
> >       decodebind128
> >       atomic_compare_exchange
> >       atomic_flag_test_and_set
> >       atomic_flag_test_and_set_explicit
> >       stdc_first_leading_zero_ull
> >       memset_explicit
> 
> However, we also need to compare them to <string.h>.  They're part of
> the strchr(3) and strstr(3) family of functions, as they have similar
> semantics (although they also are related to streq() in some sense).
> Having consistency with their family is important, IMO.
> 
> >     They are not used at all [1][2] in existing code, so not a hindrance
> >     for WG14.
> > 
> > Bruno
> > 
> > [1] https://codesearch.debian.net/search?q=str_prefix_end&literal=1
> > [2] https://codesearch.debian.net/search?q=str_suffix_start&literal=1
> 
> On Fri, Sep 19, 2025 at 03:14:52PM +0200, Bruno Haible wrote:
> Since strprefix() and strsuffix() are essentially part of that same
> family, consistency is an important factor.
> 
> One benefit of these dual-purpose functions is that you learn one
> function only, but have both funtionalities.  If there were two
> variants, and the one returning an offset pointer would be stp*, then
> the similar names would let programmers just remember that stp* is the
> one with an offset pointer, and the other one is the basic one.  But
> entirely different names are cognitively problematic.
> 
> Here I agree with Paul, and prefer a single API.
> 
> I am not sure if I would have preferred libc to have separated the
> strchr(3) and strstr(3) family of functions into bool-returning str*
> functions are offset-pointer-returning as stp* variants.  But given the
> status quo, I'd slightly prefer following it.
> 
> Projects that don't like <string.h> conventions can write their own
> wrappers that perform bool conversions.


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?


Have a lovely day!
Alex

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

Attachment: signature.asc
Description: PGP signature

Reply via email to