string-util: add new helper for finding line starting with specific string in a text buffer

We have implemented this manually a couple of times, and always wrong.
Hence let's implement this correctly for once and use everywhere.
This commit is contained in:
Lennart Poettering
2023-03-24 18:30:20 +01:00
parent 32e07cff96
commit 7b82d95f8d
3 changed files with 54 additions and 0 deletions
+23
View File
@@ -1260,3 +1260,26 @@ char *strdupcspn(const char *a, const char *reject) {
return strndup(a, strcspn(a, reject));
}
char *find_line_startswith(const char *haystack, const char *needle) {
char *p;
assert(haystack);
assert(needle);
/* Finds the first line in 'haystack' that starts with the specified string. Returns a pointer to the
* first character after it */
p = strstr(haystack, needle);
if (!p)
return NULL;
if (p > haystack)
while (p[-1] != '\n') {
p = strstr(p + 1, needle);
if (!p)
return NULL;
}
return p + strlen(needle);
}