Remove string_replace()

string_replace_at does almost the same and string_replace is only used
once, where additionally string_replace_at would be the better choice.
This commit is contained in:
Benedikt Heine 2018-11-10 18:02:34 +01:00
parent b805273fb9
commit d8d457f500
3 changed files with 1 additions and 37 deletions

View File

@ -44,16 +44,6 @@ char *string_replace_at(char *buf, int pos, int len, const char *repl)
return tmp;
}
char *string_replace(const char *needle, const char *replacement, char *haystack)
{
char *start;
start = strstr(haystack, needle);
if (!start)
return haystack;
return string_replace_at(haystack, (start - haystack), strlen(needle), replacement);
}
char *string_replace_all(const char *needle, const char *replacement, char *haystack)
{
char *start;
@ -134,7 +124,7 @@ char *string_to_path(char *string)
if (string && STRN_EQ(string, "~/", 2)) {
char *home = g_strconcat(getenv("HOME"), "/", NULL);
string = string_replace("~/", home, string);
string = string_replace_at(string, 0, 2, home);
g_free(home);
}

View File

@ -25,9 +25,6 @@ char *string_replace_all(const char *needle, const char *replacement, char *hays
/* replace <len> characters with <repl> at position <pos> of the string <buf> */
char *string_replace_at(char *buf, int pos, int len, const char *repl);
/* replace needle with replacement in haystack */
char *string_replace(const char *needle, const char *replacement, char *haystack);
char *string_append(char *a, const char *b, const char *sep);
/* strip content between two delimiter characters (inplace) */

View File

@ -48,28 +48,6 @@ TEST test_string_replace_all(void)
PASS();
}
TEST test_string_replace(void)
{
char *text = malloc(128 * sizeof(char));
strcpy(text, "aaaaa");
ASSERT_STR_EQ("baaaa", (text = string_replace("a", "b", text)) );
strcpy(text, "");
ASSERT_STR_EQ((text = string_replace("a", "b", text)), "");
strcpy(text, "Nothing to replace");
ASSERT_STR_EQ((text = string_replace("z", "a", text)), "Nothing to replace");
strcpy(text, "Reverse this");
ASSERT_STR_EQ("Reverse sith", (text = string_replace("this", "sith", text)));
strcpy(text, "abcdabc");
ASSERT_STR_EQ("xyzabcdabc", (text = string_replace("a", "xyza", text)));
free(text);
PASS();
}
TEST test_string_append(void)
{
char *exp;
@ -202,7 +180,6 @@ SUITE(suite_utils)
{
RUN_TEST(test_string_replace_char);
RUN_TEST(test_string_replace_all);
RUN_TEST(test_string_replace);
RUN_TEST(test_string_append);
RUN_TEST(test_string_strip_quotes);
RUN_TEST(test_string_strip_delimited);