summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/libs/utils/smallstring.h11
-rw-r--r--tests/unit/unittest/smallstring-test.cpp9
2 files changed, 20 insertions, 0 deletions
diff --git a/src/libs/utils/smallstring.h b/src/libs/utils/smallstring.h
index 4cc9638109..0a38d5ddec 100644
--- a/src/libs/utils/smallstring.h
+++ b/src/libs/utils/smallstring.h
@@ -473,6 +473,17 @@ public:
replaceLargerSized(fromText, toText);
}
+ void replace(char fromCharacter, char toCharacter)
+ {
+ reserve(size());
+
+ auto operation = [=] (char currentCharacter) {
+ return currentCharacter == fromCharacter ? toCharacter : currentCharacter;
+ };
+
+ std::transform(begin(), end(), begin(), operation);
+ }
+
void replace(size_type position, size_type length, SmallStringView replacementText)
{
size_type newSize = size() - length + replacementText.size();
diff --git a/tests/unit/unittest/smallstring-test.cpp b/tests/unit/unittest/smallstring-test.cpp
index f9a8289f67..29c4063542 100644
--- a/tests/unit/unittest/smallstring-test.cpp
+++ b/tests/unit/unittest/smallstring-test.cpp
@@ -747,6 +747,15 @@ TEST(SmallString, Clear)
ASSERT_TRUE(text.isEmpty());
}
+TEST(SmallString, ReplaceWithCharacter)
+{
+ SmallString text("here is some text, here is some text, here is some text");
+
+ text.replace('s', 'x');
+
+ ASSERT_THAT(text, SmallString("here ix xome text, here ix xome text, here ix xome text"));
+}
+
TEST(SmallString, ReplaceWithEqualSizedText)
{
SmallString text("here is some text");