Skip to content

Commit 2516a69

Browse files
authored
Merge pull request #200 from PiotrekB416/Fix/string-replace-multiple-longer-by-shorter
Fixed string::replace doesn't replace multiple occurrences of longer string with shorter one
2 parents 50a27fb + 12fb6ba commit 2516a69

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

Diff for: api/String.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ void String::move(String &rhs)
234234
String & String::operator = (const String &rhs)
235235
{
236236
if (this == &rhs) return *this;
237-
237+
238238
if (rhs.buffer) copy(rhs.buffer, rhs.len);
239239
else invalidate();
240-
240+
241241
return *this;
242242
}
243243

@@ -253,7 +253,7 @@ String & String::operator = (const char *cstr)
253253
{
254254
if (cstr) copy(cstr, strlen(cstr));
255255
else invalidate();
256-
256+
257257
return *this;
258258
}
259259

@@ -484,7 +484,7 @@ bool String::equalsIgnoreCase( const String &s2 ) const
484484
const char *p2 = s2.buffer;
485485
while (*p1) {
486486
if (tolower(*p1++) != tolower(*p2++)) return false;
487-
}
487+
}
488488
return true;
489489
}
490490

@@ -515,7 +515,7 @@ char String::charAt(unsigned int loc) const
515515
return operator[](loc);
516516
}
517517

518-
void String::setCharAt(unsigned int loc, char c)
518+
void String::setCharAt(unsigned int loc, char c)
519519
{
520520
if (loc < len) buffer[loc] = c;
521521
}
@@ -652,9 +652,9 @@ void String::replace(const String& find, const String& replace)
652652
}
653653
} else if (diff < 0) {
654654
unsigned int size = len; // compute size needed for result
655+
diff = 0 - diff;
655656
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
656657
readFrom = foundAt + find.len;
657-
diff = 0 - diff;
658658
size -= diff;
659659
}
660660
if (size == len) return;

Diff for: test/src/String/test_replace.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,24 @@ TEST_CASE ("Testing String::replace(String, String) substr 'find' smaller than '
6666
str.replace(arduino::String("ll"), arduino::String("111"));
6767
REQUIRE(str == "He111o Arduino!");
6868
}
69+
70+
TEST_CASE ("Testing String::replace(String, String) substr 'find' smaller than 'replace' multiple occurencies", "[String-replace-08]")
71+
{
72+
arduino::String str("Hello Arduino! Hello, Hello, Hello");
73+
str.replace(arduino::String("ll"), arduino::String("lll"));
74+
REQUIRE(str == "Helllo Arduino! Helllo, Helllo, Helllo");
75+
}
76+
77+
TEST_CASE ("Testing String::replace(String, String) substr 'find' same length as 'replace' multiple occurencies", "[String-replace-09]")
78+
{
79+
arduino::String str("Hello Arduino! Hello, Hello, Hello");
80+
str.replace(arduino::String("ll"), arduino::String("11"));
81+
REQUIRE(str == "He11o Arduino! He11o, He11o, He11o");
82+
}
83+
84+
TEST_CASE ("Testing String::replace(String, String) substr 'find' larger than 'replace' multiple occurencies", "[String-replace-10]")
85+
{
86+
arduino::String str("Helllo Arduino! Helllo, Helllo, Helllo");
87+
str.replace(arduino::String("lll"), arduino::String("ll"));
88+
REQUIRE(str == "Hello Arduino! Hello, Hello, Hello");
89+
}

0 commit comments

Comments
 (0)