dcavalca / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone
21255d
From 50b103a982dfd6f1b2bf98bbc98a8063fa153e89 Mon Sep 17 00:00:00 2001
21255d
From: Lennart Poettering <lennart@poettering.net>
21255d
Date: Fri, 23 Nov 2018 16:27:15 +0100
21255d
Subject: [PATCH] strv: add new macro STARTSWITH_SET()
21255d
21255d
This is to startswith() what PATH_STARTSWITH_SET() is to
21255d
path_startswith().
21255d
21255d
Or in other words, checks if the specified string has any of the listed
21255d
prefixes, and if so, returns the remainder of the string.
21255d
21255d
(cherry picked from commit 52f1552073047195d51901f7e5a5a4fa3189034e)
21255d
21255d
Related: #1848373
21255d
---
21255d
 src/basic/strv.h     | 12 ++++++++++++
21255d
 src/test/test-strv.c | 15 +++++++++++++++
21255d
 2 files changed, 27 insertions(+)
21255d
21255d
diff --git a/src/basic/strv.h b/src/basic/strv.h
21255d
index 51d03db940..c1e4c973b6 100644
21255d
--- a/src/basic/strv.h
21255d
+++ b/src/basic/strv.h
21255d
@@ -136,6 +136,18 @@ void strv_print(char **l);
21255d
                 _x && strv_contains(STRV_MAKE(__VA_ARGS__), _x); \
21255d
         })
21255d
 
21255d
+#define STARTSWITH_SET(p, ...)                                  \
21255d
+        ({                                                      \
21255d
+                const char *_p = (p);                           \
21255d
+                char  *_found = NULL, **_i;                     \
21255d
+                STRV_FOREACH(_i, STRV_MAKE(__VA_ARGS__)) {      \
21255d
+                        _found = startswith(_p, *_i);           \
21255d
+                        if (_found)                             \
21255d
+                                break;                          \
21255d
+                }                                               \
21255d
+                _found;                                         \
21255d
+        })
21255d
+
21255d
 #define FOREACH_STRING(x, ...)                               \
21255d
         for (char **_l = ({                                  \
21255d
                 char **_ll = STRV_MAKE(__VA_ARGS__);         \
21255d
diff --git a/src/test/test-strv.c b/src/test/test-strv.c
21255d
index 1c192239a2..79d999d3ed 100644
21255d
--- a/src/test/test-strv.c
21255d
+++ b/src/test/test-strv.c
21255d
@@ -56,6 +56,20 @@ static void test_strptr_in_set(void) {
21255d
         assert_se(!STRPTR_IN_SET(NULL, NULL));
21255d
 }
21255d
 
21255d
+static void test_startswith_set(void) {
21255d
+        assert_se(!STARTSWITH_SET("foo", "bar", "baz", "waldo"));
21255d
+        assert_se(!STARTSWITH_SET("foo", "bar"));
21255d
+
21255d
+        assert_se(STARTSWITH_SET("abc", "a", "ab", "abc"));
21255d
+        assert_se(STARTSWITH_SET("abc", "ax", "ab", "abc"));
21255d
+        assert_se(STARTSWITH_SET("abc", "ax", "abx", "abc"));
21255d
+        assert_se(!STARTSWITH_SET("abc", "ax", "abx", "abcx"));
21255d
+
21255d
+        assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "foo", "zzz"), "bar"));
21255d
+        assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "", "zzz"), "foobar"));
21255d
+        assert_se(streq_ptr(STARTSWITH_SET("", "hhh", "kkk", "zzz", ""), ""));
21255d
+}
21255d
+
21255d
 static const char* const input_table_multiple[] = {
21255d
         "one",
21255d
         "two",
21255d
@@ -700,6 +714,7 @@ int main(int argc, char *argv[]) {
21255d
         test_specifier_printf();
21255d
         test_str_in_set();
21255d
         test_strptr_in_set();
21255d
+        test_startswith_set();
21255d
         test_strv_foreach();
21255d
         test_strv_foreach_backwards();
21255d
         test_strv_foreach_pair();