forked from qt-creator/qt-creator
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:
@@ -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();
|
||||
|
@@ -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");
|
||||
|
Reference in New Issue
Block a user