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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/fxcrt/include/fx_string.h" 5 #include "core/fxcrt/include/fx_string.h"
6 #include "testing/fx_string_testhelpers.h" 6 #include "testing/fx_string_testhelpers.h"
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 8
9 TEST(fxcrt, ByteStringOperatorSubscript) { 9 TEST(fxcrt, ByteStringOperatorSubscript) {
10 // CFX_ByteString includes the NUL terminator for non-empty strings. 10 // CFX_ByteString includes the NUL terminator for non-empty strings.
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 498
499 CFX_ByteString empty; 499 CFX_ByteString empty;
500 empty.TrimRight("ERP"); 500 empty.TrimRight("ERP");
501 EXPECT_EQ("", empty); 501 EXPECT_EQ("", empty);
502 empty.TrimRight('E'); 502 empty.TrimRight('E');
503 EXPECT_EQ("", empty); 503 EXPECT_EQ("", empty);
504 empty.TrimRight(); 504 empty.TrimRight();
505 EXPECT_EQ("", empty); 505 EXPECT_EQ("", empty);
506 } 506 }
507 507
508 TEST(fxcrt, ByteStringTrimRightCopies) {
509 {
510 // With a single reference, no copy takes place.
511 CFX_ByteString fred(" FRED ");
512 const FX_CHAR* old_buffer = fred.c_str();
513 fred.TrimRight();
514 EXPECT_EQ(" FRED", fred);
515 EXPECT_EQ(old_buffer, fred.c_str());
516 }
517 {
518 // With multiple references, we must copy.
519 CFX_ByteString fred(" FRED ");
520 CFX_ByteString other_fred = fred;
521 const FX_CHAR* old_buffer = fred.c_str();
522 fred.TrimRight();
523 EXPECT_EQ(" FRED", fred);
524 EXPECT_EQ(" FRED ", other_fred);
525 EXPECT_NE(old_buffer, fred.c_str());
526 }
527 {
528 // With multiple references, but no modifications, no copy.
529 CFX_ByteString fred("FRED");
530 CFX_ByteString other_fred = fred;
531 const FX_CHAR* old_buffer = fred.c_str();
532 fred.TrimRight();
533 EXPECT_EQ("FRED", fred);
534 EXPECT_EQ("FRED", other_fred);
535 EXPECT_EQ(old_buffer, fred.c_str());
536 }
537 }
538
508 TEST(fxcrt, ByteStringTrimLeft) { 539 TEST(fxcrt, ByteStringTrimLeft) {
509 CFX_ByteString fred(" FRED "); 540 CFX_ByteString fred(" FRED ");
510 fred.TrimLeft(); 541 fred.TrimLeft();
511 EXPECT_EQ("FRED ", fred); 542 EXPECT_EQ("FRED ", fred);
512 fred.TrimLeft('E'); 543 fred.TrimLeft('E');
513 EXPECT_EQ("FRED ", fred); 544 EXPECT_EQ("FRED ", fred);
514 fred.TrimLeft('F'); 545 fred.TrimLeft('F');
515 EXPECT_EQ("RED ", fred); 546 EXPECT_EQ("RED ", fred);
516 fred.TrimLeft("ERP"); 547 fred.TrimLeft("ERP");
517 EXPECT_EQ("D ", fred); 548 EXPECT_EQ("D ", fred);
518 549
519 CFX_ByteString blank(" "); 550 CFX_ByteString blank(" ");
520 blank.TrimLeft("ERP"); 551 blank.TrimLeft("ERP");
521 EXPECT_EQ(" ", blank); 552 EXPECT_EQ(" ", blank);
522 blank.TrimLeft('E'); 553 blank.TrimLeft('E');
523 EXPECT_EQ(" ", blank); 554 EXPECT_EQ(" ", blank);
524 blank.TrimLeft(); 555 blank.TrimLeft();
525 EXPECT_EQ("", blank); 556 EXPECT_EQ("", blank);
526 557
527 CFX_ByteString empty; 558 CFX_ByteString empty;
528 empty.TrimLeft("ERP"); 559 empty.TrimLeft("ERP");
529 EXPECT_EQ("", empty); 560 EXPECT_EQ("", empty);
530 empty.TrimLeft('E'); 561 empty.TrimLeft('E');
531 EXPECT_EQ("", empty); 562 EXPECT_EQ("", empty);
532 empty.TrimLeft(); 563 empty.TrimLeft();
533 EXPECT_EQ("", empty); 564 EXPECT_EQ("", empty);
534 } 565 }
535 566
567 TEST(fxcrt, ByteStringTrimLeftCopies) {
568 {
569 // With a single reference, no copy takes place.
570 CFX_ByteString fred(" FRED ");
571 const FX_CHAR* old_buffer = fred.c_str();
572 fred.TrimLeft();
573 EXPECT_EQ("FRED ", fred);
574 EXPECT_EQ(old_buffer, fred.c_str());
575 }
576 {
577 // With multiple references, we must copy.
578 CFX_ByteString fred(" FRED ");
579 CFX_ByteString other_fred = fred;
580 const FX_CHAR* old_buffer = fred.c_str();
581 fred.TrimLeft();
582 EXPECT_EQ("FRED ", fred);
583 EXPECT_EQ(" FRED ", other_fred);
584 EXPECT_NE(old_buffer, fred.c_str());
585 }
586 {
587 // With multiple references, but no modifications, no copy.
588 CFX_ByteString fred("FRED");
589 CFX_ByteString other_fred = fred;
590 const FX_CHAR* old_buffer = fred.c_str();
591 fred.TrimLeft();
592 EXPECT_EQ("FRED", fred);
593 EXPECT_EQ("FRED", other_fred);
594 EXPECT_EQ(old_buffer, fred.c_str());
595 }
596 }
597
536 TEST(fxcrt, ByteStringCNotNull) { 598 TEST(fxcrt, ByteStringCNotNull) {
537 CFX_ByteStringC string3("abc"); 599 CFX_ByteStringC string3("abc");
538 CFX_ByteStringC string6("abcdef"); 600 CFX_ByteStringC string6("abcdef");
539 CFX_ByteStringC alternate_string3("abcdef", 3); 601 CFX_ByteStringC alternate_string3("abcdef", 3);
540 CFX_ByteStringC embedded_nul_string7("abc\0def", 7); 602 CFX_ByteStringC embedded_nul_string7("abc\0def", 7);
541 CFX_ByteStringC illegal_string7("abcdef", 7); 603 CFX_ByteStringC illegal_string7("abcdef", 7);
542 604
543 EXPECT_EQ(3, string3.GetLength()); 605 EXPECT_EQ(3, string3.GetLength());
544 EXPECT_EQ(6, string6.GetLength()); 606 EXPECT_EQ(6, string6.GetLength());
545 EXPECT_EQ(3, alternate_string3.GetLength()); 607 EXPECT_EQ(3, alternate_string3.GetLength());
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 } 960 }
899 } 961 }
900 962
901 TEST(fxcrt, EmptyByteString) { 963 TEST(fxcrt, EmptyByteString) {
902 CFX_ByteString empty_str; 964 CFX_ByteString empty_str;
903 EXPECT_TRUE(empty_str.IsEmpty()); 965 EXPECT_TRUE(empty_str.IsEmpty());
904 EXPECT_EQ(0, empty_str.GetLength()); 966 EXPECT_EQ(0, empty_str.GetLength());
905 const FX_CHAR* cstr = empty_str.c_str(); 967 const FX_CHAR* cstr = empty_str.c_str();
906 EXPECT_EQ(0, FXSYS_strlen(cstr)); 968 EXPECT_EQ(0, FXSYS_strlen(cstr));
907 } 969 }
OLDNEW
« 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