mirror of
https://git.pwmt.org/pwmt/zathura.git
synced 2025-02-05 18:54:56 +01:00
Remove replace_substring
We now have girara_replace_substring. Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
parent
df25afd5ed
commit
3285d61a99
4 changed files with 2 additions and 100 deletions
|
@ -523,8 +523,8 @@ cmd_exec(girara_session_t* session, girara_list_t* argument_list)
|
||||||
const char* path = zathura_document_get_path(zathura->document);
|
const char* path = zathura_document_get_path(zathura->document);
|
||||||
|
|
||||||
GIRARA_LIST_FOREACH(argument_list, char*, iter, value)
|
GIRARA_LIST_FOREACH(argument_list, char*, iter, value)
|
||||||
char* r = NULL;
|
char* r = girara_replace_substring(value, "$FILE", path);
|
||||||
if ((r = replace_substring(value, "$FILE", path)) != NULL) {
|
if (r != NULL) {
|
||||||
girara_list_iterator_set(iter, r);
|
girara_list_iterator_set(iter, r);
|
||||||
}
|
}
|
||||||
GIRARA_LIST_FOREACH_END(argument_list, char*, iter, value);
|
GIRARA_LIST_FOREACH_END(argument_list, char*, iter, value);
|
||||||
|
|
|
@ -10,38 +10,6 @@ START_TEST(test_file_valid_extension_null) {
|
||||||
fail_unless(file_valid_extension(NULL, "pdf") == false, NULL);
|
fail_unless(file_valid_extension(NULL, "pdf") == false, NULL);
|
||||||
} END_TEST
|
} 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()
|
Suite* suite_utils()
|
||||||
{
|
{
|
||||||
TCase* tcase = NULL;
|
TCase* tcase = NULL;
|
||||||
|
@ -52,14 +20,5 @@ Suite* suite_utils()
|
||||||
tcase_add_test(tcase, test_file_valid_extension_null);
|
tcase_add_test(tcase, test_file_valid_extension_null);
|
||||||
suite_add_tcase(suite, tcase);
|
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;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
45
utils.c
45
utils.c
|
@ -200,51 +200,6 @@ zathura_get_version_string(zathura_t* zathura, bool markup)
|
||||||
return version;
|
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)
|
GdkAtom* get_selection(zathura_t* zathura)
|
||||||
{
|
{
|
||||||
g_return_val_if_fail(zathura != NULL, NULL);
|
g_return_val_if_fail(zathura != NULL, NULL);
|
||||||
|
|
12
utils.h
12
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);
|
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.
|
* Get a pointer to the GdkAtom of the current clipboard.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue