diff --git a/README.md b/README.md index 93b5343f..2f0b49a9 100644 --- a/README.md +++ b/README.md @@ -5078,12 +5078,14 @@ Replace in the string 'v' from the offset 'start' the string 'str1' by the string 'str2' once. Returns the offset of the replacement or STRING_FAILURE if no replacement 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 (string\_t v, const string\_t str1, const string\_t str2) Replace in the string 'v' all the occurences of 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[]) diff --git a/m-string.h b/m-string.h index b4f61633..09eeb5b2 100644 --- a/m-string.h +++ b/m-string.h @@ -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 str2_l = strlen(str2); assert(str1_l > 0); - assert(str2_l > 0); if (str1_l >= str2_l) { stringi_replace_all_str_1ge2(v, str1, str1_l, str2, str2_l ); } else { diff --git a/tests/test-mstring.c b/tests/test-mstring.c index 0a272d68..5178e7d2 100644 --- a/tests/test-mstring.c +++ b/tests/test-mstring.c @@ -217,6 +217,10 @@ static void test0(void) assert (i != STRING_FAILURE); 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_replace_all_str(s1, "world", "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"); 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_replace_all_str(s1, "world", "**WORLD**"); assert( string_equal_str_p(s1, "Hello **WORLD** 4 life **WORLD** is real another **WORLD**"));