]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: nulstr-util: add nulstr_get() returning the matching string
authorArnaud Ferraris <arnaud.ferraris@collabora.com>
Tue, 14 Sep 2021 14:20:20 +0000 (16:20 +0200)
committerArnaud Ferraris <arnaud.ferraris@collabora.com>
Wed, 15 Sep 2021 14:45:58 +0000 (16:45 +0200)
Currently `nulstr_contains` returns a boolean, making it difficult to
identify which of the input strings matches the "needle".

Adding a new `nulstr_get()` function, returning a const pointer to the
matching string, eases this process and allows us to directly re-use the
result of a call to this function without additional processing or
memory allocation.

Signed-off-by: Arnaud Ferraris <arnaud.ferraris@collabora.com>
src/basic/nulstr-util.c
src/basic/nulstr-util.h

index 49fcbb0f03cefa49ff8f393a1f1681471910c1d2..dbafc8c4c94544fc0f1f553d20eba71dac6ca777 100644 (file)
@@ -3,15 +3,15 @@
 #include "nulstr-util.h"
 #include "string-util.h"
 
-bool nulstr_contains(const char *nulstr, const char *needle) {
+const char* nulstr_get(const char *nulstr, const char *needle) {
         const char *i;
 
         if (!nulstr)
-                return false;
+                return NULL;
 
         NULSTR_FOREACH(i, nulstr)
                 if (streq(i, needle))
-                        return true;
+                        return i;
 
-        return false;
+        return NULL;
 }
index ee9b632a647f0664620a83136ec219a1d77f67e7..1d1fbc19c04284d9b64327f327b5ab280e2f9f42 100644 (file)
@@ -10,4 +10,8 @@
 #define NULSTR_FOREACH_PAIR(i, j, l)                             \
         for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i))
 
-bool nulstr_contains(const char *nulstr, const char *needle);
+const char* nulstr_get(const char *nulstr, const char *needle);
+
+static inline bool nulstr_contains(const char *nulstr, const char *needle) {
+        return nulstr_get(nulstr, needle);
+}