basic/strv: fix strv_join for first empty argument
Empty strings were ignored in strv_join, but only if they were at the beginning
of the string. Empty strings after at least one non-empty item were treated
normally.
Previously:
{"x"} → "x"
{"x", ""} → "x"
{"x", "", ""} → "x::"
{""} → ""
{"", ""} → ""
{"", "", ""} → ""
{"", "x"} → "x"
{"", "x", ""} → "x:"
Now:
{"x"} → "x"
{"x", ""} → "x"
{"x", "", ""} → "x::"
{""} → ""
{"", ""} → ":"
{"", "", ""} → "::"
{"", "x"} → ":x"
{"", "x", ""} → "❌"
This commit is contained in:
parent
06e948901a
commit
afe773b01a
|
@ -371,7 +371,7 @@ char *strv_join(char **l, const char *separator) {
|
|||
|
||||
n = 0;
|
||||
STRV_FOREACH(s, l) {
|
||||
if (n != 0)
|
||||
if (s != l)
|
||||
n += k;
|
||||
n += strlen(*s);
|
||||
}
|
||||
|
@ -382,7 +382,7 @@ char *strv_join(char **l, const char *separator) {
|
|||
|
||||
e = r;
|
||||
STRV_FOREACH(s, l) {
|
||||
if (e != r)
|
||||
if (s != l)
|
||||
e = stpcpy(e, separator);
|
||||
|
||||
e = stpcpy(e, *s);
|
||||
|
|
|
@ -70,6 +70,18 @@ static const char* const input_table_none[] = {
|
|||
NULL,
|
||||
};
|
||||
|
||||
static const char* const input_table_two_empties[] = {
|
||||
"",
|
||||
"",
|
||||
NULL,
|
||||
};
|
||||
|
||||
static const char* const input_table_one_empty[] = {
|
||||
"",
|
||||
NULL,
|
||||
};
|
||||
|
||||
|
||||
static const char* const input_table_quotes[] = {
|
||||
"\"",
|
||||
"'",
|
||||
|
@ -130,7 +142,7 @@ static void test_strv_find_startswith(void) {
|
|||
}
|
||||
|
||||
static void test_strv_join(void) {
|
||||
_cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL;
|
||||
_cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL, *v = NULL, *w = NULL;
|
||||
|
||||
p = strv_join((char **)input_table_multiple, ", ");
|
||||
assert_se(p);
|
||||
|
@ -151,6 +163,14 @@ static void test_strv_join(void) {
|
|||
t = strv_join((char **)input_table_none, ", ");
|
||||
assert_se(t);
|
||||
assert_se(streq(t, ""));
|
||||
|
||||
v = strv_join((char **)input_table_two_empties, ", ");
|
||||
assert_se(v);
|
||||
assert_se(streq(v, ", "));
|
||||
|
||||
w = strv_join((char **)input_table_one_empty, ", ");
|
||||
assert_se(w);
|
||||
assert_se(streq(w, ""));
|
||||
}
|
||||
|
||||
static void test_strv_quote_unquote(const char* const *split, const char *quoted) {
|
||||
|
@ -653,6 +673,8 @@ int main(int argc, char *argv[]) {
|
|||
test_strv_quote_unquote(input_table_multiple, "\"one\" \"two\" \"three\"");
|
||||
test_strv_quote_unquote(input_table_one, "\"one\"");
|
||||
test_strv_quote_unquote(input_table_none, "");
|
||||
test_strv_quote_unquote(input_table_one_empty, "\"\"");
|
||||
test_strv_quote_unquote(input_table_two_empties, "\"\" \"\"");
|
||||
test_strv_quote_unquote(input_table_quotes, QUOTES_STRING);
|
||||
test_strv_quote_unquote(input_table_spaces, SPACES_STRING);
|
||||
|
||||
|
|
Loading…
Reference in a new issue