Do not concatenate empty strings with separator

This commit is contained in:
Benedikt Heine 2017-09-25 03:58:38 +02:00
parent a1781a451b
commit a6301ff464
2 changed files with 8 additions and 2 deletions

View File

@ -76,9 +76,11 @@ char *string_replace_all(const char *needle, const char *replacement,
char *string_append(char *a, const char *b, const char *sep) char *string_append(char *a, const char *b, const char *sep)
{ {
if (!a) if (!a || *a == '\0') {
g_free(a);
return g_strdup(b); return g_strdup(b);
if (!b) }
if (!b || *b == '\0')
return a; return a;
char *new; char *new;

View File

@ -83,6 +83,8 @@ TEST test_string_append(void)
ASSERT_STR_EQ("text", (exp = string_append(g_strdup("text"), "", NULL))); ASSERT_STR_EQ("text", (exp = string_append(g_strdup("text"), "", NULL)));
g_free(exp); g_free(exp);
ASSERT_STR_EQ("text", (exp = string_append(g_strdup("text"), "", "_sep_")));
g_free(exp);
ASSERT_STR_EQ("b", (exp = string_append(g_strdup(""), "b", NULL))); ASSERT_STR_EQ("b", (exp = string_append(g_strdup(""), "b", NULL)));
g_free(exp); g_free(exp);
@ -94,6 +96,8 @@ TEST test_string_append(void)
ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), NULL, "_sep_"))); ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), NULL, "_sep_")));
g_free(exp); g_free(exp);
ASSERT_STR_EQ("", (exp = string_append(g_strdup(""), "", "_sep_")));
g_free(exp);
ASSERT_EQ(NULL, (exp = string_append(NULL, NULL, "_sep_"))); ASSERT_EQ(NULL, (exp = string_append(NULL, NULL, "_sep_")));
g_free(exp); g_free(exp);