1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-18 06:03:42 +03:00

basic: introduce test_strv_split_and_extend()

This commit is contained in:
Frantisek Sumsal 2021-09-29 12:46:11 +02:00
parent bbc1bb0742
commit 3c318caa6f
3 changed files with 44 additions and 0 deletions

View File

@ -300,6 +300,24 @@ int strv_split_full(char ***t, const char *s, const char *separators, ExtractFla
return (int) n; 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) { int strv_split_colon_pairs(char ***t, const char *s) {
_cleanup_strv_free_ char **l = NULL; _cleanup_strv_free_ char **l = NULL;
size_t n = 0; size_t n = 0;

View File

@ -83,6 +83,9 @@ static inline char **strv_split(const char *s, const char *separators) {
return ret; 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); int strv_split_newlines_full(char ***ret, const char *s, ExtractFlags flags);
static inline char **strv_split_newlines(const char *s) { static inline char **strv_split_newlines(const char *s) {
char **ret; char **ret;

View File

@ -390,6 +390,28 @@ static void test_strv_split_full(void) {
assert_se(streq_ptr(l[5], NULL)); 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) { static void test_strv_split_colon_pairs(void) {
_cleanup_strv_free_ char **l = NULL; _cleanup_strv_free_ char **l = NULL;
const char *str = "one:two three four:five six seven:eight\\:nine ten\\:eleven\\\\", 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();
test_strv_split_empty(); test_strv_split_empty();
test_strv_split_full(); test_strv_split_full();
test_strv_split_and_extend_full();
test_strv_split_colon_pairs(); test_strv_split_colon_pairs();
test_strv_split_newlines(); test_strv_split_newlines();
test_strv_split_newlines_full(); test_strv_split_newlines_full();