strv: make strv_split() accept empty string

This commit is contained in:
Yu Watanabe
2018-07-07 01:34:18 +09:00
parent 8255430754
commit 0745ce7565
2 changed files with 28 additions and 4 deletions
+4
View File
@@ -253,6 +253,10 @@ char **strv_split(const char *s, const char *separator) {
assert(s);
s += strspn(s, separator);
if (isempty(s))
return new0(char*, 1);
n = 0;
FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
n++;
+24 -4
View File
@@ -180,12 +180,31 @@ static void test_strv_split(void) {
const char str[] = "one,two,three";
l = strv_split(str, ",");
assert_se(l);
STRV_FOREACH(s, l) {
STRV_FOREACH(s, l)
assert_se(streq(*s, input_table_multiple[i++]));
}
i = 0;
strv_free(l);
l = strv_split(" one two\t three", WHITESPACE);
assert_se(l);
STRV_FOREACH(s, l)
assert_se(streq(*s, input_table_multiple[i++]));
}
static void test_strv_split_empty(void) {
_cleanup_strv_free_ char **l = NULL;
l = strv_split("", WHITESPACE);
assert_se(l);
assert_se(strv_isempty(l));
strv_free(l);
l = strv_split(" ", WHITESPACE);
assert_se(l);
assert_se(strv_isempty(l));
}
static void test_strv_split_extract(void) {
@@ -733,6 +752,7 @@ int main(int argc, char *argv[]) {
test_invalid_unquote("'x'y'g");
test_strv_split();
test_strv_split_empty();
test_strv_split_extract();
test_strv_split_newlines();
test_strv_split_nulstr();