in-place CString replace when possible

This commit is contained in:
Mathias Agopian
2025-09-25 14:53:44 -07:00
committed by Mathias Agopian
parent d11a6b4467
commit 98f8c93950
2 changed files with 72 additions and 1 deletions

View File

@@ -477,6 +477,62 @@ TEST(CString, Replace) {
}
}
TEST(CString, ReplaceInPlace) {
// Shrinking replacement
{
CString str("0123456789");
const char* const original_cstr = str.c_str();
str.replace(3, 3, "ab");
EXPECT_STREQ("012ab6789", str.c_str());
EXPECT_EQ(9, str.length());
EXPECT_EQ(original_cstr, str.c_str());
}
{
CString str("0123456789");
const char* const original_cstr = str.c_str();
str.replace(0, 3, "ab");
EXPECT_STREQ("ab3456789", str.c_str());
EXPECT_EQ(9, str.length());
EXPECT_EQ(original_cstr, str.c_str());
}
{
CString str("0123456789");
const char* const original_cstr = str.c_str();
str.replace(7, 3, "ab");
EXPECT_STREQ("0123456ab", str.c_str());
EXPECT_EQ(9, str.length());
EXPECT_EQ(original_cstr, str.c_str());
}
{
CString str("0123456789");
const char* const original_cstr = str.c_str();
str.replace(0, 10, "ab");
EXPECT_STREQ("ab", str.c_str());
EXPECT_EQ(2, str.length());
EXPECT_EQ(original_cstr, str.c_str());
}
// Same size replacement
{
CString str("0123456789");
const char* const original_cstr = str.c_str();
str.replace(0, 10, "abcdefghij");
EXPECT_STREQ("abcdefghij", str.c_str());
EXPECT_EQ(10, str.length());
EXPECT_EQ(original_cstr, str.c_str());
}
// Shrink to empty
{
CString str("0123456789");
const char* const original_cstr = str.c_str();
str.replace(3, 3, "");
EXPECT_STREQ("0126789", str.c_str());
EXPECT_EQ(7, str.length());
EXPECT_EQ(original_cstr, str.c_str());
}
}
TEST(CString, ReplaceZeroLength) {
{
std::string str("foobar");