Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fixed string::replace doesn't replace multiple occurrences of longer string with shorter one#200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletionsapi/String.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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;
}

Expand All@@ -253,7 +253,7 @@ String & String::operator = (const char *cstr)
{
if (cstr) copy(cstr, strlen(cstr));
else invalidate();

return *this;
}

Expand DownExpand Up@@ -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;
}

Expand DownExpand Up@@ -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;
}
Expand DownExpand Up@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Ah, this line hurts 😉 . (Could it not beabs(diff)? However, you did not commit this crime, simply moved the line to the right position 👍 .

while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
readFrom = foundAt + find.len;
diff = 0 - diff;
size -= diff;
}
if (size == len) return;
Expand Down
21 changes: 21 additions & 0 deletionstest/src/String/test_replace.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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");
}

[8]ページ先頭

©2009-2025 Movatter.jp