dryang / rpms / systemd

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