]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: introduce test_strv_split_and_extend()
authorFrantisek Sumsal <frantisek@sumsal.cz>
Wed, 29 Sep 2021 10:46:11 +0000 (12:46 +0200)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Wed, 29 Sep 2021 11:32:22 +0000 (13:32 +0200)
src/basic/strv.c
src/basic/strv.h
src/test/test-strv.c

index 3adf3c5c7e0043d8f7b4610c5c2f917e450c14e9..005f30a016b9c295a992f8af583b1b068c4111ac 100644 (file)
@@ -300,6 +300,24 @@ int strv_split_full(char ***t, const char *s, const char *separators, ExtractFla
         return (int) n;
 }
 
+int strv_split_and_extend_full(char ***t, const char *s, const char *separators, bool filter_duplicates, ExtractFlags flags) {
+        _cleanup_strv_free_ char **l = NULL;
+        int r;
+
+        assert(t);
+        assert(s);
+
+        r = strv_split_full(&l, s, separators, flags);
+        if (r < 0)
+                return r;
+
+        r = strv_extend_strv(t, l, filter_duplicates);
+        if (r < 0)
+                return r;
+
+        return (int) strv_length(*t);
+}
+
 int strv_split_colon_pairs(char ***t, const char *s) {
         _cleanup_strv_free_ char **l = NULL;
         size_t n = 0;
index e7654c0c0ff6f09417c4c135e0220016502a5df1..a56ef94139976263da24734321023fed9eaff0a6 100644 (file)
@@ -83,6 +83,9 @@ static inline char **strv_split(const char *s, const char *separators) {
         return ret;
 }
 
+int strv_split_and_extend_full(char ***t, const char *s, const char *separators, bool filter_duplicates, ExtractFlags flags);
+#define strv_split_and_extend(t, s, sep, dup) strv_split_and_extend_full(t, s, sep, dup, 0)
+
 int strv_split_newlines_full(char ***ret, const char *s, ExtractFlags flags);
 static inline char **strv_split_newlines(const char *s) {
         char **ret;
index 88ff35fc5a198e58d50111d8992471deb5fe5e15..51ad45b5ac381e2932643016cd38367a35941f93 100644 (file)
@@ -390,6 +390,28 @@ static void test_strv_split_full(void) {
         assert_se(streq_ptr(l[5], NULL));
 }
 
+static void test_strv_split_and_extend_full(void) {
+        _cleanup_strv_free_ char **l = NULL;
+        const char *str1 = ":foo\\:bar:";
+        const char *str2 = "waldo::::::baz";
+        int r;
+
+        log_info("/* %s */", __func__);
+
+        r = strv_split_and_extend(&l, "", ":", false);
+        assert_se(r == (int) strv_length(l));
+        r = strv_split_and_extend_full(&l, str1, ":", false, EXTRACT_DONT_COALESCE_SEPARATORS);
+        assert_se(r == (int) strv_length(l));
+        assert_se(streq_ptr(l[0], ""));
+        assert_se(streq_ptr(l[1], "foo:bar"));
+        assert_se(streq_ptr(l[2], ""));
+        r = strv_split_and_extend_full(&l, str2, ":", false, 0);
+        assert_se(r == (int) strv_length(l));
+        assert_se(streq_ptr(l[3], "waldo"));
+        assert_se(streq_ptr(l[4], "baz"));
+        assert_se(streq_ptr(l[5], NULL));
+}
+
 static void test_strv_split_colon_pairs(void) {
         _cleanup_strv_free_ char **l = NULL;
         const char *str = "one:two three four:five six seven:eight\\:nine ten\\:eleven\\\\",
@@ -1028,6 +1050,7 @@ int main(int argc, char *argv[]) {
         test_strv_split();
         test_strv_split_empty();
         test_strv_split_full();
+        test_strv_split_and_extend_full();
         test_strv_split_colon_pairs();
         test_strv_split_newlines();
         test_strv_split_newlines_full();