Utils: Add replace for characters

It is much more performant to have a overload of char instead of providing
a string all the time.

Change-Id: I1a4ed82bf056f6af0c1f91c236b3fc30afa7f5d8
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2017-02-02 11:56:24 +01:00
parent 63e7e83e22
commit bdfb466cd2
2 changed files with 20 additions and 0 deletions

View File

@@ -473,6 +473,17 @@ public:
replaceLargerSized(fromText, toText); 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) void replace(size_type position, size_type length, SmallStringView replacementText)
{ {
size_type newSize = size() - length + replacementText.size(); size_type newSize = size() - length + replacementText.size();

View File

@@ -747,6 +747,15 @@ TEST(SmallString, Clear)
ASSERT_TRUE(text.isEmpty()); 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) TEST(SmallString, ReplaceWithEqualSizedText)
{ {
SmallString text("here is some text"); SmallString text("here is some text");