From db350883cc2afcbd9774d38731093c5215cc7497 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Wed, 31 Oct 2018 15:03:33 +0100 Subject: [PATCH] Improve extract_urls --- src/menu.c | 27 +++++++++------------------ src/menu.h | 7 +++++++ test/menu.c | 3 +++ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/menu.c b/src/menu.c index e427f29..6c97919 100644 --- a/src/menu.c +++ b/src/menu.c @@ -63,41 +63,32 @@ void regex_teardown(void) } } -/* - * Extract all urls from a given string. - * - * Return: a string of urls separated by \n - * - */ +/* see menu.h */ char *extract_urls(const char *to_match) { - char *urls = NULL; + if (!to_match) + return NULL; if (!regex_init()) return NULL; + char *urls = NULL; const char *p = to_match; regmatch_t m; while (1) { int nomatch = regexec(&url_regex, p, 1, &m, 0); - if (nomatch) { - return urls; - } - int start; - int finish; - if (m.rm_so == -1) { + + if (nomatch || m.rm_so == -1) break; - } - start = m.rm_so + (p - to_match); - finish = m.rm_eo + (p - to_match); + + int start = m.rm_so + (p - to_match); + int finish = m.rm_eo + (p - to_match); char *match = g_strndup(to_match + start, finish - start); - urls = string_append(urls, match, "\n"); g_free(match); - p += m.rm_eo; } return urls; diff --git a/src/menu.h b/src/menu.h index b93f78b..c58ed4d 100644 --- a/src/menu.h +++ b/src/menu.h @@ -2,7 +2,14 @@ #ifndef DUNST_MENU_H #define DUNST_MENU_H +/** + * Extract all urls from the given string. + * + * @param to_match (nullable) String to extract URLs + * @return a string of urls separated by '\n' + */ char *extract_urls(const char *to_match); + void open_browser(const char *in); void invoke_action(const char *action); void regex_teardown(void); diff --git a/test/menu.c b/test/menu.c index 7c8f373..93fd4c0 100644 --- a/test/menu.c +++ b/test/menu.c @@ -8,6 +8,9 @@ TEST test_extract_urls_from_empty_string(void) { char *urls = extract_urls(""); ASSERT_EQ_FMT(NULL, (void*)urls, "%p"); + + urls = extract_urls(NULL); + ASSERT_EQ_FMT(NULL, (void*)urls, "%p"); g_free(urls); PASS(); }