From 8b11113be14a748391814a4e49756a6fae8dada8 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Mon, 25 Sep 2017 03:54:37 +0200 Subject: [PATCH 1/4] Implement test_string_append --- test/utils.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/utils.c b/test/utils.c index 84dc344..bccdd60 100644 --- a/test/utils.c +++ b/test/utils.c @@ -72,7 +72,29 @@ TEST test_string_replace(void) TEST test_string_append(void) { - SKIP(); //TODO: Implement this + char *exp; + + ASSERT_STR_EQ("text_sep_bit", (exp = string_append(g_strdup("text"), "bit", "_sep_"))); + g_free(exp); + ASSERT_STR_EQ("textbit", (exp = string_append(g_strdup("text"), "bit", NULL))); + g_free(exp); + ASSERT_STR_EQ("textbit", (exp = string_append(g_strdup("text"), "bit", ""))); + g_free(exp); + + ASSERT_STR_EQ("text", (exp = string_append(g_strdup("text"), "", NULL))); + g_free(exp); + + ASSERT_STR_EQ("b", (exp = string_append(g_strdup(""), "b", NULL))); + g_free(exp); + ASSERT_STR_EQ("b", (exp = string_append(NULL, "b", "_sep_"))); + g_free(exp); + + ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), "", NULL))); + g_free(exp); + + ASSERT_EQ(NULL, (exp = string_append(NULL, NULL, "_sep_"))); + g_free(exp); + PASS(); } From a1781a451b3002c95b9444f7a191daca4bfab72b Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Mon, 25 Sep 2017 03:55:19 +0200 Subject: [PATCH 2/4] Check for b value if null in string_append This avoids "a", NULL, "sep" to come to "asep" instead of "a". --- src/utils.c | 2 ++ test/utils.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/utils.c b/src/utils.c index d3e5787..094a748 100644 --- a/src/utils.c +++ b/src/utils.c @@ -78,6 +78,8 @@ char *string_append(char *a, const char *b, const char *sep) { if (!a) return g_strdup(b); + if (!b) + return a; char *new; if (!sep) diff --git a/test/utils.c b/test/utils.c index bccdd60..6fcee97 100644 --- a/test/utils.c +++ b/test/utils.c @@ -91,6 +91,8 @@ TEST test_string_append(void) ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), "", NULL))); g_free(exp); + ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), NULL, "_sep_"))); + g_free(exp); ASSERT_EQ(NULL, (exp = string_append(NULL, NULL, "_sep_"))); g_free(exp); From a6301ff46486e7e6fbbbbde7a4375ec627c9a543 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Mon, 25 Sep 2017 03:58:38 +0200 Subject: [PATCH 3/4] Do not concatenate empty strings with separator --- src/utils.c | 6 ++++-- test/utils.c | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/utils.c b/src/utils.c index 094a748..d838c53 100644 --- a/src/utils.c +++ b/src/utils.c @@ -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) { - if (!a) + if (!a || *a == '\0') { + g_free(a); return g_strdup(b); - if (!b) + } + if (!b || *b == '\0') return a; char *new; diff --git a/test/utils.c b/test/utils.c index 6fcee97..f0b7969 100644 --- a/test/utils.c +++ b/test/utils.c @@ -83,6 +83,8 @@ TEST test_string_append(void) ASSERT_STR_EQ("text", (exp = string_append(g_strdup("text"), "", NULL))); 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))); g_free(exp); @@ -94,6 +96,8 @@ TEST test_string_append(void) ASSERT_STR_EQ("a", (exp = string_append(g_strdup("a"), NULL, "_sep_"))); g_free(exp); + ASSERT_STR_EQ("", (exp = string_append(g_strdup(""), "", "_sep_"))); + g_free(exp); ASSERT_EQ(NULL, (exp = string_append(NULL, NULL, "_sep_"))); g_free(exp); From 6e654eb751166aacf31d121b7af4e64a37a2ed90 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Fri, 6 Oct 2017 13:05:09 +0200 Subject: [PATCH 4/4] Improve usage of string_append --- src/notification.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/notification.c b/src/notification.c index 68bf2eb..3f7d9ae 100644 --- a/src/notification.c +++ b/src/notification.c @@ -522,14 +522,8 @@ int notification_init(notification *n, int id) char *tmp = g_strconcat(n->summary, " ", n->body, NULL); char *tmp_urls = extract_urls(tmp); - if (tmp_urls != NULL) { - if (n->urls != NULL) { - n->urls = string_append(n->urls, tmp_urls, "\n"); - g_free(tmp_urls); - } else { - n->urls = tmp_urls; - } - } + n->urls = string_append(n->urls, tmp_urls, "\n"); + g_free(tmp_urls); if (n->actions) { n->actions->dmenu_str = NULL;