diff --git a/src/basic/strv.c b/src/basic/strv.c index 3adf3c5c7e..005f30a016 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -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; diff --git a/src/basic/strv.h b/src/basic/strv.h index e7654c0c0f..a56ef94139 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -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; diff --git a/src/test/test-strv.c b/src/test/test-strv.c index 88ff35fc5a..51ad45b5ac 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -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();