From f6764f41fe3b9cbcb94b042944adefa17af0de2f Mon Sep 17 00:00:00 2001 From: PiotrekB416 Date: Wed, 30 Aug 2023 16:39:11 +0200 Subject: [PATCH 1/3] Fix String::replace replace multiple occurrences of longer String by shorter one --- api/String.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/api/String.cpp b/api/String.cpp index 0a5c11fe..7e76a510 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -234,10 +234,10 @@ void String::move(String &rhs) String & String::operator = (const String &rhs) { if (this == &rhs) return *this; - + if (rhs.buffer) copy(rhs.buffer, rhs.len); else invalidate(); - + return *this; } @@ -253,7 +253,7 @@ String & String::operator = (const char *cstr) { if (cstr) copy(cstr, strlen(cstr)); else invalidate(); - + return *this; } @@ -484,7 +484,7 @@ bool String::equalsIgnoreCase( const String &s2 ) const const char *p2 = s2.buffer; while (*p1) { if (tolower(*p1++) != tolower(*p2++)) return false; - } + } return true; } @@ -515,7 +515,7 @@ char String::charAt(unsigned int loc) const return operator[](loc); } -void String::setCharAt(unsigned int loc, char c) +void String::setCharAt(unsigned int loc, char c) { if (loc < len) buffer[loc] = c; } @@ -652,9 +652,9 @@ void String::replace(const String& find, const String& replace) } } else if (diff < 0) { unsigned int size = len; // compute size needed for result + diff = 0 - diff; while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { readFrom = foundAt + find.len; - diff = 0 - diff; size -= diff; } if (size == len) return; From f782ba4bd546659f9c60ee448ea643e180535910 Mon Sep 17 00:00:00 2001 From: PiotrekB416 Date: Wed, 30 Aug 2023 16:42:00 +0200 Subject: [PATCH 2/3] Add tests --- test/src/String/test_replace.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/src/String/test_replace.cpp b/test/src/String/test_replace.cpp index 5d46fafb..d4f28bf5 100644 --- a/test/src/String/test_replace.cpp +++ b/test/src/String/test_replace.cpp @@ -66,3 +66,24 @@ TEST_CASE ("Testing String::replace(String, String) substr 'find' smaller than ' str.replace(arduino::String("ll"), arduino::String("111")); REQUIRE(str == "He111o Arduino!"); } + +TEST_CASE ("Testing String::replace(String, String) substr 'find' smaller than 'replace' multiple occurencies", "[String-replace-08]") +{ + arduino::String str("Hello Arduino! Hello, Hello, Hello"); + str.replace(arduino::String("ll"), arduino::String("lll")); + REQUIRE(str == "Helllo Arduino! Helllo, Helllo, Helllo"); +} + +TEST_CASE ("Testing String::replace(String, String) substr 'find' same length as 'replace' multiple occurencies", "[String-replace-09]") +{ + arduino::String str("Hello Arduino! Hello, Hello, Hello"); + str.replace(arduino::String("ll"), arduino::String("11")); + REQUIRE(str == "He11o Arduino! He11o, He11o, He11o"); +} + +TEST_CASE ("Testing String::replace(String, String) substr 'find' larger than 'replace' multiple occurencies", "[String-replace-10]") +{ + arduino::String str("Helllo Arduino! Helllo, Helllo, Helllo"); + str.replace(arduino::String("lll"), arduino::String("ll")); + REQUIRE(str == "Hello Arduino! Hello, Hello, Hello"); +} From 12fb6ba05e44385052a65de88f79003467bc8ce5 Mon Sep 17 00:00:00 2001 From: Piotr Bartoszewicz <89194651+PiotrekB416@users.noreply.github.com> Date: Wed, 30 Aug 2023 23:15:05 +0200 Subject: [PATCH 3/3] Update api/String.cpp Co-authored-by: per1234 --- api/String.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/String.cpp b/api/String.cpp index 7e76a510..69125aa2 100644 --- a/api/String.cpp +++ b/api/String.cpp @@ -652,7 +652,7 @@ void String::replace(const String& find, const String& replace) } } else if (diff < 0) { unsigned int size = len; // compute size needed for result - diff = 0 - diff; + diff = 0 - diff; while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { readFrom = foundAt + find.len; size -= diff;