diff --git a/commands.c b/commands.c index a8882c0..00326ba 100644 --- a/commands.c +++ b/commands.c @@ -523,8 +523,8 @@ cmd_exec(girara_session_t* session, girara_list_t* argument_list) const char* path = zathura_document_get_path(zathura->document); GIRARA_LIST_FOREACH(argument_list, char*, iter, value) - char* r = NULL; - if ((r = replace_substring(value, "$FILE", path)) != NULL) { + char* r = girara_replace_substring(value, "$FILE", path); + if (r != NULL) { girara_list_iterator_set(iter, r); } GIRARA_LIST_FOREACH_END(argument_list, char*, iter, value); diff --git a/tests/test_utils.c b/tests/test_utils.c index 541707e..9a098fe 100644 --- a/tests/test_utils.c +++ b/tests/test_utils.c @@ -10,38 +10,6 @@ START_TEST(test_file_valid_extension_null) { fail_unless(file_valid_extension(NULL, "pdf") == false, NULL); } END_TEST -START_TEST(test_strings_replace_substrings_invalid) { - fail_unless(replace_substring(NULL, NULL, NULL) == NULL); - fail_unless(replace_substring("", NULL, NULL) == NULL); - fail_unless(replace_substring("", "", NULL) == NULL); -} END_TEST - -START_TEST(test_strings_replace_substrings_nothing_to_replace) { - fail_unless(replace_substring("test", "n", "y") == NULL); -} END_TEST - -START_TEST(test_strings_replace_substrings_1) { - char* result = replace_substring("test", "e", "f"); - fail_unless(result != NULL); - fail_unless(strncmp(result, "tfst", 5) == 0); - g_free(result); -} END_TEST - -START_TEST(test_strings_replace_substrings_2) { - char* result = replace_substring("test", "es", "f"); - fail_unless(result != NULL); - fail_unless(strncmp(result, "tft", 4) == 0); - g_free(result); -} END_TEST - -START_TEST(test_strings_replace_substrings_3) { - char* result = replace_substring("test", "e", "fg"); - fail_unless(result != NULL); - fail_unless(strncmp(result, "tfgst", 6) == 0); - g_free(result); -} END_TEST - - Suite* suite_utils() { TCase* tcase = NULL; @@ -52,14 +20,5 @@ Suite* suite_utils() tcase_add_test(tcase, test_file_valid_extension_null); suite_add_tcase(suite, tcase); - /* strings */ - tcase = tcase_create("strings"); - tcase_add_test(tcase, test_strings_replace_substrings_invalid); - tcase_add_test(tcase, test_strings_replace_substrings_nothing_to_replace); - tcase_add_test(tcase, test_strings_replace_substrings_1); - tcase_add_test(tcase, test_strings_replace_substrings_2); - tcase_add_test(tcase, test_strings_replace_substrings_3); - suite_add_tcase(suite, tcase); - return suite; } diff --git a/utils.c b/utils.c index b9c00f9..79ff6bc 100644 --- a/utils.c +++ b/utils.c @@ -200,51 +200,6 @@ zathura_get_version_string(zathura_t* zathura, bool markup) return version; } -char* -replace_substring(const char* string, const char* old, const char* new) -{ - if (string == NULL || old == NULL || new == NULL) { - return NULL; - } - - size_t old_len = strlen(old); - size_t new_len = strlen(new); - - /* count occurrences */ - size_t count = 0; - size_t i = 0; - - for (i = 0; string[i] != '\0'; i++) { - if (strstr(&string[i], old) == &string[i]) { - i += (old_len - 1); - count++; - } - } - - if (count == 0) { - return NULL; - } - - char* ret = g_try_malloc0(sizeof(char) * (i - count * old_len + count * new_len + 1)); - if (ret == NULL) { - return NULL; - } - - /* replace */ - i = 0; - while (*string != '\0') { - if (strstr(string, old) == string) { - strncpy(&ret[i], new, new_len); - i += new_len; - string += old_len; - } else { - ret[i++] = *string++; - } - } - - return ret; -} - GdkAtom* get_selection(zathura_t* zathura) { g_return_val_if_fail(zathura != NULL, NULL); diff --git a/utils.h b/utils.h index c061384..c09b71b 100644 --- a/utils.h +++ b/utils.h @@ -85,18 +85,6 @@ void document_draw_search_results(zathura_t* zathura, bool value); */ char* zathura_get_version_string(zathura_t* zathura, bool markup); -/** - * Replaces all occurences of \ref old in \ref string with \ref new and returns - * a new allocated string - * - * @param string The original string - * @param old String to replace - * @param new Replacement string - * - * @return new allocated string - */ -char* replace_substring(const char* string, const char* old, const char* new); - /** * Get a pointer to the GdkAtom of the current clipboard. *