string-util: add strnspn()

Like strspn(), but reads at most 'n' bytes from the input. strspn()
is bounded only by a non-matching byte or a NUL, so it over-reads a
buffer that is all matching bytes and not NUL terminated within 'n'.
This commit is contained in:
Syed Mohammed Nayyar
2026-07-16 15:53:37 +05:30
parent e61a0705db
commit 0af6caff42
3 changed files with 38 additions and 0 deletions
+17
View File
@@ -1389,6 +1389,23 @@ size_t strspn_from_end(const char *str, const char *accept) {
return n;
}
size_t strnspn(const char *str, const char *accept, size_t n) {
size_t i;
/* Like strspn(), but reads at most 'n' bytes from 'str'. Returns the length of the initial
* run of 'str' (capped at 'n') that consists solely of bytes found in 'accept'. Stops at a
* NUL byte too. Unlike strspn() this is safe on a buffer that is not NUL terminated within
* 'n' bytes. */
assert(str || n == 0);
assert(accept);
for (i = 0; i < n && str[i] != '\0' && strchr(accept, str[i]); i++)
;
return i;
}
char* strdupspn(const char *a, const char *accept) {
if (isempty(a) || isempty(accept))
return strdup("");