]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
string-util: add find_line[_after] functions
authorAlberto Planas <aplanas@suse.com>
Wed, 7 May 2025 09:00:10 +0000 (11:00 +0200)
committerAlberto Planas <aplanas@suse.com>
Tue, 20 May 2025 08:29:33 +0000 (10:29 +0200)
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 <aplanas@suse.com>
src/basic/string-util.c
src/basic/string-util.h

index d98d2f44d8350f23b8f943433a753c68f29c467d..de24e60a148d1900b0bf35e8832eb50cd7d2b06f 100644 (file)
@@ -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;
index 17fac1bfab5079e06bcae6398cde2803d8c064e2..b68bf291feae742cd412d0b53c495ccae2ecb9eb 100644 (file)
@@ -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);