string-util: add strstrafter()

strstrafter() is like strstr() but returns a pointer to the first
character *after* the found substring, not on the substring itself.
Quite often this is what we actually want.

Inspired by #27267 I think it makes sense to add a helper for this,
to avoid the potentially fragile manual pointer increment afterwards.
This commit is contained in:
Lennart Poettering
2023-04-14 12:48:14 +02:00
parent bb7b1da8fe
commit d791013ff5
7 changed files with 42 additions and 16 deletions

View File

@@ -29,6 +29,18 @@ static inline char* strstr_ptr(const char *haystack, const char *needle) {
return strstr(haystack, needle);
}
static inline char *strstrafter(const char *haystack, const char *needle) {
char *p;
/* Returns NULL if not found, or pointer to first character after needle if found */
p = strstr_ptr(haystack, needle);
if (!p)
return NULL;
return p + strlen(needle);
}
static inline const char* strnull(const char *s) {
return s ?: "(null)";
}