summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Boccassi <bluca@debian.org>2023-03-27 12:05:28 +0100
committerLuca Boccassi <bluca@debian.org>2023-03-28 10:36:01 +0100
commit2ed74695b310207deca33fcc95a890b0fdb9e622 (patch)
tree7ce170b3e80bdd953c9771ea4c9cb5b4f8a0e19b
parent6255bbe2624caa1e656c4f8a1b88721b62c2e16e (diff)
downloadsystemd-2ed74695b310207deca33fcc95a890b0fdb9e622.tar.gz
strv: add helper to find value in key/value pairs from list of keys
-rw-r--r--src/basic/strv.c11
-rw-r--r--src/basic/strv.h3
-rw-r--r--src/test/test-strv.c12
3 files changed, 26 insertions, 0 deletions
diff --git a/src/basic/strv.c b/src/basic/strv.c
index 5fcf3620a6..822dadeeb4 100644
--- a/src/basic/strv.c
+++ b/src/basic/strv.c
@@ -7,6 +7,7 @@
#include <stdlib.h>
#include "alloc-util.h"
+#include "env-util.h"
#include "escape.h"
#include "extract-word.h"
#include "fileio.h"
@@ -63,6 +64,16 @@ char* strv_find_startswith(char * const *l, const char *name) {
return NULL;
}
+char* strv_find_first_field(char * const *needles, char * const *haystack) {
+ STRV_FOREACH(k, needles) {
+ char *value = strv_env_pairs_get((char **)haystack, *k);
+ if (value)
+ return value;
+ }
+
+ return NULL;
+}
+
char** strv_free(char **l) {
STRV_FOREACH(k, l)
free(*k);
diff --git a/src/basic/strv.h b/src/basic/strv.h
index 419cda1ee3..b4d3f121f9 100644
--- a/src/basic/strv.h
+++ b/src/basic/strv.h
@@ -17,6 +17,9 @@ char* strv_find(char * const *l, const char *name) _pure_;
char* strv_find_case(char * const *l, const char *name) _pure_;
char* strv_find_prefix(char * const *l, const char *name) _pure_;
char* strv_find_startswith(char * const *l, const char *name) _pure_;
+/* Given two vectors, the first a list of keys and the second a list of key-value pairs, returns the value
+ * of the first key from the first vector that is found in the second vector. */
+char* strv_find_first_field(char * const *needles, char * const *haystack) _pure_;
#define strv_contains(l, s) (!!strv_find((l), (s)))
#define strv_contains_case(l, s) (!!strv_find_case((l), (s)))
diff --git a/src/test/test-strv.c b/src/test/test-strv.c
index 0f08dd4615..a39a2d8122 100644
--- a/src/test/test-strv.c
+++ b/src/test/test-strv.c
@@ -994,4 +994,16 @@ TEST(strv_copy_n) {
assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e")));
}
+TEST(strv_find_first_field) {
+ char **haystack = STRV_MAKE("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
+
+ assert_se(strv_find_first_field(NULL, NULL) == NULL);
+ assert_se(strv_find_first_field(NULL, haystack) == NULL);
+ assert_se(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "b"), NULL) == NULL);
+ assert_se(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "b"), haystack) == NULL);
+ assert_se(streq_ptr(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "a", "c"), haystack), "b"));
+ assert_se(streq_ptr(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "c", "a"), haystack), "d"));
+ assert_se(streq_ptr(strv_find_first_field(STRV_MAKE("i", "k", "l", "m", "d", "c", "a", "b"), haystack), "j"));
+}
+
DEFINE_TEST_MAIN(LOG_INFO);