strv: add helper to find value in key/value pairs from list of keys

This commit is contained in:
Luca Boccassi
2023-03-28 10:36:01 +01:00
parent 6255bbe262
commit 2ed74695b3
3 changed files with 26 additions and 0 deletions
+11
View File
@@ -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);
+3
View File
@@ -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)))