From e16117ca30a15f19085349703f03d12b59875642 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Wed, 6 Sep 2017 21:41:26 +0200 Subject: [PATCH] Use double pointers for haystack --- src/notification.c | 96 ++++++++++++++++++++++----------------------- src/notification.h | 2 +- test/notification.c | 9 +++-- 3 files changed, 53 insertions(+), 54 deletions(-) diff --git a/src/notification.c b/src/notification.c index e480180..68bf2eb 100644 --- a/src/notification.c +++ b/src/notification.c @@ -207,28 +207,24 @@ void notification_free(notification *n) * to point to the first char, which occurs after replacement. * */ -char *notification_replace_single_field(char *haystack, char **needle, +void notification_replace_single_field(char **haystack, char **needle, const char *replacement, enum markup_mode markup_mode) { assert(*needle[0] == '%'); // needle has to point into haystack (but not on the last char) - assert(*needle >= haystack); - assert(*needle - haystack < strlen(haystack) - 1); + assert(*needle >= *haystack); + assert(*needle - *haystack < strlen(*haystack) - 1); - char *ret; - - int pos = *needle - haystack; + int pos = *needle - *haystack; char *input = markup_transform(g_strdup(replacement), markup_mode); - ret = string_replace_at(haystack, pos, 2, input); + *haystack = string_replace_at(*haystack, pos, 2, input); // point the needle to the next char // which was originally in haystack - *needle = ret + (*needle - haystack) + strlen(input); + *needle = *haystack + pos + strlen(input); g_free(input); - - return ret; } char *notification_extract_markup_urls(char **str_ptr) { @@ -333,66 +329,66 @@ int notification_init(notification *n, int id) switch(substr[1]){ case 'a': - n->msg = notification_replace_single_field( - n->msg, - &substr, - n->appname, - MARKUP_NO); + notification_replace_single_field( + &n->msg, + &substr, + n->appname, + MARKUP_NO); break; case 's': - n->msg = notification_replace_single_field( - n->msg, - &substr, - n->summary, - n->markup); + notification_replace_single_field( + &n->msg, + &substr, + n->summary, + n->markup); break; case 'b': - n->msg = notification_replace_single_field( - n->msg, - &substr, - n->body, - n->markup); + notification_replace_single_field( + &n->msg, + &substr, + n->body, + n->markup); break; case 'I': - n->msg = notification_replace_single_field( - n->msg, - &substr, - n->icon ? basename(n->icon) : "", - MARKUP_NO); + notification_replace_single_field( + &n->msg, + &substr, + n->icon ? basename(n->icon) : "", + MARKUP_NO); break; case 'i': - n->msg = notification_replace_single_field( - n->msg, - &substr, - n->icon ? n->icon : "", - MARKUP_NO); + notification_replace_single_field( + &n->msg, + &substr, + n->icon ? n->icon : "", + MARKUP_NO); break; case 'p': if (n->progress) sprintf(pg, "[%3d%%]", n->progress - 1); - n->msg = notification_replace_single_field( - n->msg, - &substr, - n->progress ? pg : "", - MARKUP_NO); + notification_replace_single_field( + &n->msg, + &substr, + n->progress ? pg : "", + MARKUP_NO); break; case 'n': if (n->progress) sprintf(pg, "%d", n->progress - 1); - n->msg = notification_replace_single_field( - n->msg, - &substr, - n->progress ? pg : "", - MARKUP_NO); + notification_replace_single_field( + &n->msg, + &substr, + n->progress ? pg : "", + MARKUP_NO); break; case '%': - n->msg = notification_replace_single_field( - n->msg, - &substr, - "%", - MARKUP_NO); + notification_replace_single_field( + &n->msg, + &substr, + "%", + MARKUP_NO); break; case '\0': fprintf(stderr, "WARNING: format_string has trailing %% character." diff --git a/src/notification.h b/src/notification.h index dadfc3b..c0efef4 100644 --- a/src/notification.h +++ b/src/notification.h @@ -73,7 +73,7 @@ int notification_is_duplicate(const notification *a, const notification *b); void notification_run_script(notification *n); int notification_close(notification *n, int reason); void notification_print(notification *n); -char *notification_replace_single_field(char *haystack, char **needle, const char *replacement, enum markup_mode markup_mode); +void notification_replace_single_field(char **haystack, char **needle, const char *replacement, enum markup_mode markup_mode); void notification_update_text_to_render(notification *n); int notification_get_ttl(notification *n); int notification_get_age(notification *n); diff --git a/test/notification.c b/test/notification.c index d077959..e5812dd 100644 --- a/test/notification.c +++ b/test/notification.c @@ -77,17 +77,20 @@ TEST test_notification_replace_single_field(void) strcpy(str, "Markup %a preserved"); substr = strchr(str, '%'); - ASSERT_STR_EQ("Markup and & is preserved", (str = notification_replace_single_field(str, &substr, "and & is", MARKUP_FULL))); + notification_replace_single_field(&str, &substr, "and & is", MARKUP_FULL); + ASSERT_STR_EQ("Markup and & is preserved", str); ASSERT_EQ(26, substr - str); strcpy(str, "Markup %a escaped"); substr = strchr(str, '%'); - ASSERT_STR_EQ("Markup and & <i>is</i> escaped", (str = notification_replace_single_field(str, &substr, "and & is", MARKUP_NO))); + notification_replace_single_field(&str, &substr, "and & is", MARKUP_NO); + ASSERT_STR_EQ("Markup and & <i>is</i> escaped", str); ASSERT_EQ(38, substr - str); strcpy(str, "Markup %a"); substr = strchr(str, '%'); - ASSERT_STR_EQ("Markup is removed and & escaped", (str = notification_replace_single_field(str, &substr, "is removed and & escaped", MARKUP_STRIP))); + notification_replace_single_field(&str, &substr, "is removed and & escaped", MARKUP_STRIP); + ASSERT_STR_EQ("Markup is removed and & escaped", str); ASSERT_EQ(35, substr - str); g_free(str);