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

Unified Diff: core/fxcrt/fx_basic_bstring_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_bstring.cpp ('k') | core/fxcrt/fx_basic_wstring.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/fxcrt/fx_basic_bstring_unittest.cpp
diff --git a/core/fxcrt/fx_basic_bstring_unittest.cpp b/core/fxcrt/fx_basic_bstring_unittest.cpp
index 73235ddf5f040d0230682b8e39b5a149a05ec8ff..8c72b2a054292257d81691daf8b5ef20bd94c7b3 100644
--- a/core/fxcrt/fx_basic_bstring_unittest.cpp
+++ b/core/fxcrt/fx_basic_bstring_unittest.cpp
@@ -505,6 +505,37 @@ TEST(fxcrt, ByteStringTrimRight) {
EXPECT_EQ("", empty);
}
+TEST(fxcrt, ByteStringTrimRightCopies) {
+ {
+ // With a single reference, no copy takes place.
+ CFX_ByteString fred(" FRED ");
+ const FX_CHAR* old_buffer = fred.c_str();
+ fred.TrimRight();
+ EXPECT_EQ(" FRED", fred);
+ EXPECT_EQ(old_buffer, fred.c_str());
+ }
+ {
+ // With multiple references, we must copy.
+ CFX_ByteString fred(" FRED ");
+ CFX_ByteString other_fred = fred;
+ const FX_CHAR* old_buffer = fred.c_str();
+ fred.TrimRight();
+ EXPECT_EQ(" FRED", fred);
+ EXPECT_EQ(" FRED ", other_fred);
+ EXPECT_NE(old_buffer, fred.c_str());
+ }
+ {
+ // With multiple references, but no modifications, no copy.
+ CFX_ByteString fred("FRED");
+ CFX_ByteString other_fred = fred;
+ const FX_CHAR* old_buffer = fred.c_str();
+ fred.TrimRight();
+ EXPECT_EQ("FRED", fred);
+ EXPECT_EQ("FRED", other_fred);
+ EXPECT_EQ(old_buffer, fred.c_str());
+ }
+}
+
TEST(fxcrt, ByteStringTrimLeft) {
CFX_ByteString fred(" FRED ");
fred.TrimLeft();
@@ -533,6 +564,37 @@ TEST(fxcrt, ByteStringTrimLeft) {
EXPECT_EQ("", empty);
}
+TEST(fxcrt, ByteStringTrimLeftCopies) {
+ {
+ // With a single reference, no copy takes place.
+ CFX_ByteString fred(" FRED ");
+ const FX_CHAR* old_buffer = fred.c_str();
+ fred.TrimLeft();
+ EXPECT_EQ("FRED ", fred);
+ EXPECT_EQ(old_buffer, fred.c_str());
+ }
+ {
+ // With multiple references, we must copy.
+ CFX_ByteString fred(" FRED ");
+ CFX_ByteString other_fred = fred;
+ const FX_CHAR* old_buffer = fred.c_str();
+ fred.TrimLeft();
+ EXPECT_EQ("FRED ", fred);
+ EXPECT_EQ(" FRED ", other_fred);
+ EXPECT_NE(old_buffer, fred.c_str());
+ }
+ {
+ // With multiple references, but no modifications, no copy.
+ CFX_ByteString fred("FRED");
+ CFX_ByteString other_fred = fred;
+ const FX_CHAR* old_buffer = fred.c_str();
+ fred.TrimLeft();
+ EXPECT_EQ("FRED", fred);
+ EXPECT_EQ("FRED", other_fred);
+ EXPECT_EQ(old_buffer, fred.c_str());
+ }
+}
+
TEST(fxcrt, ByteStringCNotNull) {
CFX_ByteStringC string3("abc");
CFX_ByteStringC string6("abcdef");
« no previous file with comments | « core/fxcrt/fx_basic_bstring.cpp ('k') | core/fxcrt/fx_basic_wstring.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698