Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2762)

Unified Diff: core/fxcrt/fx_basic_wstring_unittest.cpp

Issue 1877553002: Avoid copying in TrimRight() and TrimLeft() if possible. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: More accurate comment in fx_string.h Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « core/fxcrt/fx_basic_wstring.cpp ('k') | core/fxcrt/include/fx_string.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/fxcrt/fx_basic_wstring_unittest.cpp
diff --git a/core/fxcrt/fx_basic_wstring_unittest.cpp b/core/fxcrt/fx_basic_wstring_unittest.cpp
index cfc190f72ae75384f98d5dc0f43b8af8c3657622..bc38141a95ca9f317d07b4850e28a8877f0c3360 100644
--- a/core/fxcrt/fx_basic_wstring_unittest.cpp
+++ b/core/fxcrt/fx_basic_wstring_unittest.cpp
@@ -472,6 +472,37 @@ TEST(fxcrt, WideStringTrimRight) {
EXPECT_EQ(L"", empty);
}
+TEST(fxcrt, WideStringTrimRightCopies) {
+ {
+ // With a single reference, no copy takes place.
+ CFX_WideString fred(L" FRED ");
+ const FX_WCHAR* old_buffer = fred.c_str();
+ fred.TrimRight();
+ EXPECT_EQ(L" FRED", fred);
+ EXPECT_EQ(old_buffer, fred.c_str());
+ }
+ {
+ // With multiple references, we must copy.
+ CFX_WideString fred(L" FRED ");
+ CFX_WideString other_fred = fred;
+ const FX_WCHAR* old_buffer = fred.c_str();
+ fred.TrimRight();
+ EXPECT_EQ(L" FRED", fred);
+ EXPECT_EQ(L" FRED ", other_fred);
+ EXPECT_NE(old_buffer, fred.c_str());
+ }
+ {
+ // With multiple references, but no modifications, no copy.
+ CFX_WideString fred(L"FRED");
+ CFX_WideString other_fred = fred;
+ const FX_WCHAR* old_buffer = fred.c_str();
+ fred.TrimRight();
+ EXPECT_EQ(L"FRED", fred);
+ EXPECT_EQ(L"FRED", other_fred);
+ EXPECT_EQ(old_buffer, fred.c_str());
+ }
+}
+
TEST(fxcrt, WideStringTrimLeft) {
CFX_WideString fred(L" FRED ");
fred.TrimLeft();
@@ -500,6 +531,37 @@ TEST(fxcrt, WideStringTrimLeft) {
EXPECT_EQ(L"", empty);
}
+TEST(fxcrt, WideStringTrimLeftCopies) {
+ {
+ // With a single reference, no copy takes place.
+ CFX_WideString fred(L" FRED ");
+ const FX_WCHAR* old_buffer = fred.c_str();
+ fred.TrimLeft();
+ EXPECT_EQ(L"FRED ", fred);
+ EXPECT_EQ(old_buffer, fred.c_str());
+ }
+ {
+ // With multiple references, we must copy.
+ CFX_WideString fred(L" FRED ");
+ CFX_WideString other_fred = fred;
+ const FX_WCHAR* old_buffer = fred.c_str();
+ fred.TrimLeft();
+ EXPECT_EQ(L"FRED ", fred);
+ EXPECT_EQ(L" FRED ", other_fred);
+ EXPECT_NE(old_buffer, fred.c_str());
+ }
+ {
+ // With multiple references, but no modifications, no copy.
+ CFX_WideString fred(L"FRED");
+ CFX_WideString other_fred = fred;
+ const FX_WCHAR* old_buffer = fred.c_str();
+ fred.TrimLeft();
+ EXPECT_EQ(L"FRED", fred);
+ EXPECT_EQ(L"FRED", other_fred);
+ EXPECT_EQ(old_buffer, fred.c_str());
+ }
+}
+
TEST(fxcrt, WideStringUTF16LE_Encode) {
struct UTF16LEEncodeCase {
CFX_WideString ws;
« no previous file with comments | « core/fxcrt/fx_basic_wstring.cpp ('k') | core/fxcrt/include/fx_string.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698