From: Arnaud Ferraris Date: Tue, 14 Sep 2021 14:20:20 +0000 (+0200) Subject: basic: nulstr-util: add nulstr_get() returning the matching string X-Git-Tag: v250-rc1~665^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=533f11c5292ff68aaaaf08cff3570140bb6d1f1b;p=thirdparty%2Fsystemd.git basic: nulstr-util: add nulstr_get() returning the matching string 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 --- diff --git a/src/basic/nulstr-util.c b/src/basic/nulstr-util.c index 49fcbb0f03c..dbafc8c4c94 100644 --- a/src/basic/nulstr-util.c +++ b/src/basic/nulstr-util.c @@ -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; } diff --git a/src/basic/nulstr-util.h b/src/basic/nulstr-util.h index ee9b632a647..1d1fbc19c04 100644 --- a/src/basic/nulstr-util.h +++ b/src/basic/nulstr-util.h @@ -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); +}