Hi,
Similar to strnul(3) returning a pointer, this stpspn(3) is a macro that
returns a pointer instead of a length.
The canonical implementation I have in shadow-utils is
#define stpspn(s, accept) \
({ \
__auto_type s_ = s; \
\
s_ + strspn(s_, accept); \
})
but for gnulib I've implemented it similar to strnul(3). This macro
covers most uses of strspn(3), and makes it less error-prone. See
<https://lists.mutt.org/pipermail/mutt-dev/Week-of-Mon-20260216/002040.html>
for a discussion in mutt(1) where an OpenBSD contributor made a mistake
with strcspn(3) which would have been precluded by an stpcspn() macro.
I've only implemented stpspn() to wrap strspn(3), but if you merge this,
I'd certainly continue with an stpcspn() wrapper for strcspn(3).
The name is stPspn(), because it's similar to the stp*() functions such
as stpcpy(3) and mempcpy(3), in that it returns an offset pointer
compared to the str*() version.
Let me know what you think.
In replies to this cover letter, you'll find a patch that implements
stpspn(), and another patch that uses it in gnulib, to show how
beneficial it is.
I'll send them in an attachment when you want to apply them as patches,
to make it easy for you; but for a first discussion, I thought inline
might be more useful (to quote-reply).
Have a lovely day!
Alex
Alejandro Colomar (2):
Add stpspn()
Use stpspn()
lib/canonicalize.c | 2 +-
lib/chdir-long.c | 3 +-
lib/idcache.c | 4 +--
lib/string.in.h | 40 +++++++++++++++++++++
lib/strtok_r.c | 2 +-
tests/test-hash.c | 2 +-
tests/test-strerror_r.c | 2 +-
tests/unigbrk/test-uc-grapheme-breaks.c | 8 ++---
tests/unigbrk/test-uc-is-grapheme-break.c | 8 ++---
tests/unilbrk/test-uc-possible-linebreaks.c | 8 ++---
tests/uniname/test-uninames.c | 4 +--
tests/uniwbrk/test-uc-wordbreaks.c | 8 ++---
12 files changed, 65 insertions(+), 26 deletions(-)
--
2.51.0