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:
parent
b805273fb9
commit
d8d457f500
12
src/utils.c
12
src/utils.c
@ -44,16 +44,6 @@ char *string_replace_at(char *buf, int pos, int len, const char *repl)
|
|||||||
return tmp;
|
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 *string_replace_all(const char *needle, const char *replacement, char *haystack)
|
||||||
{
|
{
|
||||||
char *start;
|
char *start;
|
||||||
@ -134,7 +124,7 @@ char *string_to_path(char *string)
|
|||||||
if (string && STRN_EQ(string, "~/", 2)) {
|
if (string && STRN_EQ(string, "~/", 2)) {
|
||||||
char *home = g_strconcat(getenv("HOME"), "/", NULL);
|
char *home = g_strconcat(getenv("HOME"), "/", NULL);
|
||||||
|
|
||||||
string = string_replace("~/", home, string);
|
string = string_replace_at(string, 0, 2, home);
|
||||||
|
|
||||||
g_free(home);
|
g_free(home);
|
||||||
}
|
}
|
||||||
|
@ -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> */
|
/* 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);
|
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);
|
char *string_append(char *a, const char *b, const char *sep);
|
||||||
|
|
||||||
/* strip content between two delimiter characters (inplace) */
|
/* strip content between two delimiter characters (inplace) */
|
||||||
|
23
test/utils.c
23
test/utils.c
@ -48,28 +48,6 @@ TEST test_string_replace_all(void)
|
|||||||
PASS();
|
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)
|
TEST test_string_append(void)
|
||||||
{
|
{
|
||||||
char *exp;
|
char *exp;
|
||||||
@ -202,7 +180,6 @@ SUITE(suite_utils)
|
|||||||
{
|
{
|
||||||
RUN_TEST(test_string_replace_char);
|
RUN_TEST(test_string_replace_char);
|
||||||
RUN_TEST(test_string_replace_all);
|
RUN_TEST(test_string_replace_all);
|
||||||
RUN_TEST(test_string_replace);
|
|
||||||
RUN_TEST(test_string_append);
|
RUN_TEST(test_string_append);
|
||||||
RUN_TEST(test_string_strip_quotes);
|
RUN_TEST(test_string_strip_quotes);
|
||||||
RUN_TEST(test_string_strip_delimited);
|
RUN_TEST(test_string_strip_delimited);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user