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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user