From: Alberto Planas Date: Wed, 7 May 2025 09:00:10 +0000 (+0200) Subject: string-util: add find_line[_after] functions X-Git-Tag: v258-rc1~567^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ba2d8107e2f39bd4e9a164f5baf51d7881eded30;p=thirdparty%2Fsystemd.git string-util: add find_line[_after] functions As a wrapper for `find_line_startswith`, `find_line_after` search for the exact line and return the pointer for the next line, or NULL if missing. `find_line` with search for the exact line and return the pointer to the beginning of the line. Signed-off-by: Alberto Planas --- diff --git a/src/basic/string-util.c b/src/basic/string-util.c index d98d2f44d83..de24e60a148 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -1383,6 +1383,46 @@ char* find_line_startswith(const char *haystack, const char *needle) { return p + strlen(needle); } +char* find_line(const char *haystack, const char *needle) { + char *p; + + assert(haystack); + assert(needle); + + /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the + * beginning of the line */ + + p = find_line_startswith(haystack, needle); + if (!p) + return NULL; + + if (*p == 0 || strchr(NEWLINE, *p)) + return p - strlen(needle); + + return NULL; +} + +char* find_line_after(const char *haystack, const char *needle) { + char *p; + + assert(haystack); + assert(needle); + + /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the + * next line after it */ + + p = find_line_startswith(haystack, needle); + if (!p) + return NULL; + + if (*p == 0) + return p; + if (strchr(NEWLINE, *p)) + return p + 1; + + return NULL; +} + bool version_is_valid(const char *s) { if (isempty(s)) return false; diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 17fac1bfab5..b68bf291fea 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -279,6 +279,8 @@ char* strdupcspn(const char *a, const char *reject); }) char* find_line_startswith(const char *haystack, const char *needle); +char* find_line(const char *haystack, const char *needle); +char* find_line_after(const char *haystack, const char *needle); bool version_is_valid(const char *s);