summaryrefslogtreecommitdiff
path: root/core/fxcrt/fx_basic_wstring_unittest.cpp
diff options
context:
space:
mode:
authortsepez <tsepez@chromium.org>2016-04-11 18:01:13 -0700
committerCommit bot <commit-bot@chromium.org>2016-04-11 18:01:13 -0700
commite09c1e4db92e28a332f55aa3c80ceb44f4b74287 (patch)
tree2ba0b13dadf379f7673317fdd2909db21cce9412 /core/fxcrt/fx_basic_wstring_unittest.cpp
parent24a48881c5407651a58cfd6547ac9b6a9823a63e (diff)
downloadpdfium-e09c1e4db92e28a332f55aa3c80ceb44f4b74287.tar.xz
Make CFX_{Byte,Wide}String::Remove() no-touch if possible
Don't try to copy the string until we are sure we need to change it. Review URL: https://codereview.chromium.org/1877993002
Diffstat (limited to 'core/fxcrt/fx_basic_wstring_unittest.cpp')
-rw-r--r--core/fxcrt/fx_basic_wstring_unittest.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/core/fxcrt/fx_basic_wstring_unittest.cpp b/core/fxcrt/fx_basic_wstring_unittest.cpp
index bc38141a95..708556af1d 100644
--- a/core/fxcrt/fx_basic_wstring_unittest.cpp
+++ b/core/fxcrt/fx_basic_wstring_unittest.cpp
@@ -294,6 +294,35 @@ TEST(fxcrt, WideStringRemove) {
EXPECT_EQ(L"", empty);
}
+TEST(fxcrt, WideStringRemoveCopies) {
+ CFX_WideString freed(L"FREED");
+ const FX_WCHAR* old_buffer = freed.c_str();
+
+ // No change with single reference - no copy.
+ freed.Remove(L'Q');
+ EXPECT_EQ(L"FREED", freed);
+ EXPECT_EQ(old_buffer, freed.c_str());
+
+ // Change with single reference - no copy.
+ freed.Remove(L'E');
+ EXPECT_EQ(L"FRD", freed);
+ EXPECT_EQ(old_buffer, freed.c_str());
+
+ // No change with multiple references - no copy.
+ CFX_WideString shared(freed);
+ freed.Remove(L'Q');
+ EXPECT_EQ(L"FRD", freed);
+ EXPECT_EQ(old_buffer, freed.c_str());
+ EXPECT_EQ(old_buffer, shared.c_str());
+
+ // Change with multiple references -- must copy.
+ freed.Remove(L'D');
+ EXPECT_EQ(L"FR", freed);
+ EXPECT_NE(old_buffer, freed.c_str());
+ EXPECT_EQ(L"FRD", shared);
+ EXPECT_EQ(old_buffer, shared.c_str());
+}
+
TEST(fxcrt, WideStringReplace) {
CFX_WideString fred(L"FRED");
fred.Replace(L"FR", L"BL");