Add support for empty replaced string

This commit is contained in:
Patrick Pelissier committed 2020-11-22 23:29:38 +01:00
1 parent 2e4404ca7e
commit b343d356c3
3 files changed
+10 -1

No files matched your search

+2
View File
@@ -5078,12 +5078,14 @@ Replace in the string 'v' from the offset 'start'
the string 'str1' by the string 'str2' once. the string 'str1' by the string 'str2' once.
Returns the offset of the replacement or STRING_FAILURE if no replacement Returns the offset of the replacement or STRING_FAILURE if no replacement
was performed. was performed.
str1 shall be a non empty string.
##### size\_t string\_replace\_all\_str (string\_t v, const char str1[], const char str2[]) ##### size\_t string\_replace\_all\_str (string\_t v, const char str1[], const char str2[])
##### size\_t string\_replace\_all (string\_t v, const string\_t str1, const string\_t str2) ##### size\_t string\_replace\_all (string\_t v, const string\_t str1, const string\_t str2)
Replace in the string 'v' all the occurences of Replace in the string 'v' all the occurences of
the string 'str1' by the string 'str2'. the string 'str1' by the string 'str2'.
str1 shall be a non empty string.
##### void string\_replace\_at (string\_t v, size\_t pos, size\_t len, const char str2[]) ##### void string\_replace\_at (string\_t v, size\_t pos, size\_t len, const char str2[])
-1
View File
@@ -887,7 +887,6 @@ string_replace_all_str (string_t v, const char str1[], const char str2[])
size_t str1_l = strlen(str1); size_t str1_l = strlen(str1);
size_t str2_l = strlen(str2); size_t str2_l = strlen(str2);
assert(str1_l > 0); assert(str1_l > 0);
assert(str2_l > 0);
if (str1_l >= str2_l) { if (str1_l >= str2_l) {
stringi_replace_all_str_1ge2(v, str1, str1_l, str2, str2_l ); stringi_replace_all_str_1ge2(v, str1, str1_l, str2, str2_l );
} else { } else {
+8
View File
@@ -217,6 +217,10 @@ static void test0(void)
assert (i != STRING_FAILURE); assert (i != STRING_FAILURE);
assert (string_cmp_str (s1, "Hello, Juliette!") == 0); assert (string_cmp_str (s1, "Hello, Juliette!") == 0);
i = string_replace_str (s1, "Juliette", "");
assert (i != STRING_FAILURE);
assert (string_cmp_str (s1, "Hello, !") == 0);
string_set_str(s1, "Hello world 4 life world is real another world"); string_set_str(s1, "Hello world 4 life world is real another world");
string_replace_all_str(s1, "world", "earth"); string_replace_all_str(s1, "world", "earth");
assert( string_equal_str_p(s1, "Hello earth 4 life earth is real another earth")); assert( string_equal_str_p(s1, "Hello earth 4 life earth is real another earth"));
@@ -229,6 +233,10 @@ static void test0(void)
string_replace_all_str(s1, "world", "Wrld"); string_replace_all_str(s1, "world", "Wrld");
assert( string_equal_str_p(s1, "Wrld Hello Wrld 4 life Wrld is real another planet")); assert( string_equal_str_p(s1, "Wrld Hello Wrld 4 life Wrld is real another planet"));
string_set_str(s1, "Hello world 4 life world is real another world");
string_replace_all_str(s1, "world", "");
assert( string_equal_str_p(s1, "Hello 4 life is real another "));
string_set_str(s1, "Hello world 4 life world is real another world"); string_set_str(s1, "Hello world 4 life world is real another world");
string_replace_all_str(s1, "world", "**WORLD**"); string_replace_all_str(s1, "world", "**WORLD**");
assert( string_equal_str_p(s1, "Hello **WORLD** 4 life **WORLD** is real another **WORLD**")); assert( string_equal_str_p(s1, "Hello **WORLD** 4 life **WORLD** is real another **WORLD**"));