]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: rename strv_endswith to endswith_strv and dedup ENDSWITH_SET 30725/head
authorMike Yuan <me@yhndnzj.com>
Thu, 4 Jan 2024 08:45:54 +0000 (16:45 +0800)
committerMike Yuan <me@yhndnzj.com>
Thu, 4 Jan 2024 08:51:57 +0000 (16:51 +0800)
src/basic/strv.c
src/basic/strv.h
src/shared/discover-image.c
src/test/test-strv.c

index ff2f672c103633a7aca591cf94ff0c62dcea8759..908e9e251398a15e5e95b01622dd003c7d4b0f6c 100644 (file)
@@ -716,6 +716,16 @@ char* startswith_strv(const char *s, char * const *l) {
         return NULL;
 }
 
+char* endswith_strv(const char *s, char * const *l) {
+        STRV_FOREACH(i, l) {
+                char *found = endswith(s, *i);
+                if (found)
+                        return found;
+        }
+
+        return NULL;
+}
+
 char** strv_reverse(char **l) {
         size_t n;
 
@@ -915,13 +925,3 @@ 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;
-}
index 66fa0cd2e441b4d1fe54c049e6253d21c90818e5..f1a8bc49109540bfbbecf94d4ce9f52e997937b4 100644 (file)
@@ -164,6 +164,11 @@ char* startswith_strv(const char *s, char * const *l);
 #define STARTSWITH_SET(p, ...)                                  \
         startswith_strv(p, STRV_MAKE(__VA_ARGS__))
 
+char* endswith_strv(const char *s, char * const *l);
+
+#define ENDSWITH_SET(p, ...)                                    \
+        endswith_strv(p, STRV_MAKE(__VA_ARGS__))
+
 #define strv_from_stdarg_alloca(first)                          \
         ({                                                      \
                 char **_l;                                      \
@@ -207,18 +212,6 @@ char* startswith_strv(const char *s, char * const *l);
                 _x && strv_contains_case(STRV_MAKE(__VA_ARGS__), _x); \
         })
 
-#define ENDSWITH_SET(p, ...)                                    \
-        ({                                                      \
-                const char *_p = (p);                           \
-                char *_found = NULL;                            \
-                STRV_FOREACH(_i, STRV_MAKE(__VA_ARGS__)) {      \
-                        _found = endswith(_p, *_i);             \
-                        if (_found)                             \
-                                break;                          \
-                }                                               \
-                _found;                                         \
-        })
-
 #define _FOREACH_STRING(uniq, x, y, ...)                                \
         for (const char *x, * const*UNIQ_T(l, uniq) = STRV_MAKE_CONST(({ x = y; }), ##__VA_ARGS__); \
              x;                                                         \
@@ -257,5 +250,3 @@ 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);
index e7e3a4d71c68c83754f8d885410ee5691c6e1649..ed89580d822b8909c1f7c5cc1113f23722355d06 100644 (file)
@@ -233,7 +233,7 @@ static int extract_image_basename(
                 return r;
 
         if (format_suffixes) {
-                char *e = strv_endswith(name, format_suffixes);
+                char *e = endswith_strv(name, format_suffixes);
                 if (!e) /* Format suffix is required */
                         return -EINVAL;
 
index f4a45703d0d433e68d137dc3f07f64662961b8e6..f70e2aa862d3d1f607b8fc91975a7831aab320cb 100644 (file)
@@ -1006,12 +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")), ""));
+TEST(endswith_strv) {
+        assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("xxx", "yyy", "ldo", "zzz")), "ldo"));
+        assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("xxx", "yyy", "zzz")), NULL));
+        assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("waldo")), "waldo"));
+        assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("w", "o", "ldo")), "o"));
+        assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("knurz", "", "waldo")), ""));
 }
 
 DEFINE_TEST_MAIN(LOG_INFO);