From: Lennart Poettering Date: Wed, 15 Nov 2023 15:55:22 +0000 (+0100) Subject: strv: add new strv_endswith() helper X-Git-Tag: v256-rc1~1319^2~11 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=cb599f881a804eb3c9e3991e1e293d08bc7d9430;p=thirdparty%2Fsystemd.git strv: add new strv_endswith() helper --- diff --git a/src/basic/strv.c b/src/basic/strv.c index c2109d35bc2..43a4f569bd2 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -905,3 +905,13 @@ int _string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const } DEFINE_HASH_OPS_FULL(string_strv_hash_ops, char, string_hash_func, string_compare_func, free, char*, strv_free); + +char* strv_endswith(const char *s, char **l) { + STRV_FOREACH(i, l) { + char *e = endswith(s, *i); + if (e) + return (char*) e; + } + + return NULL; +} diff --git a/src/basic/strv.h b/src/basic/strv.h index fec26167339..18df0f23f24 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -252,3 +252,5 @@ int _string_strv_hashmap_put(Hashmap **h, const char *key, const char *value HA int _string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS); #define string_strv_hashmap_put(h, k, v) _string_strv_hashmap_put(h, k, v HASHMAP_DEBUG_SRC_ARGS) #define string_strv_ordered_hashmap_put(h, k, v) _string_strv_ordered_hashmap_put(h, k, v HASHMAP_DEBUG_SRC_ARGS) + +char* strv_endswith(const char *s, char **l); diff --git a/src/test/test-strv.c b/src/test/test-strv.c index cfd662b329d..f4a45703d0d 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -1006,4 +1006,12 @@ TEST(strv_find_first_field) { assert_se(streq_ptr(strv_find_first_field(STRV_MAKE("i", "k", "l", "m", "d", "c", "a", "b"), haystack), "j")); } +TEST(strv_endswith) { + assert_se(streq_ptr(strv_endswith("waldo", STRV_MAKE("xxx", "yyy", "ldo", "zzz")), "ldo")); + assert_se(streq_ptr(strv_endswith("waldo", STRV_MAKE("xxx", "yyy", "zzz")), NULL)); + assert_se(streq_ptr(strv_endswith("waldo", STRV_MAKE("waldo")), "waldo")); + assert_se(streq_ptr(strv_endswith("waldo", STRV_MAKE("w", "o", "ldo")), "o")); + assert_se(streq_ptr(strv_endswith("waldo", STRV_MAKE("knurz", "", "waldo")), "")); +} + DEFINE_TEST_MAIN(LOG_INFO);