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

Side by Side 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 unified diff | 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 »
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_basic.h" 5 #include "core/fxcrt/include/fx_basic.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, WideStringOperatorSubscript) { 9 TEST(fxcrt, WideStringOperatorSubscript) {
10 // CFX_WideString includes the NUL terminator for non-empty strings. 10 // CFX_WideString includes the NUL terminator for non-empty strings.
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 465
466 CFX_WideString empty; 466 CFX_WideString empty;
467 empty.TrimRight(L"ERP"); 467 empty.TrimRight(L"ERP");
468 EXPECT_EQ(L"", empty); 468 EXPECT_EQ(L"", empty);
469 empty.TrimRight(L'E'); 469 empty.TrimRight(L'E');
470 EXPECT_EQ(L"", empty); 470 EXPECT_EQ(L"", empty);
471 empty.TrimRight(); 471 empty.TrimRight();
472 EXPECT_EQ(L"", empty); 472 EXPECT_EQ(L"", empty);
473 } 473 }
474 474
475 TEST(fxcrt, WideStringTrimRightCopies) {
476 {
477 // With a single reference, no copy takes place.
478 CFX_WideString fred(L" FRED ");
479 const FX_WCHAR* old_buffer = fred.c_str();
480 fred.TrimRight();
481 EXPECT_EQ(L" FRED", fred);
482 EXPECT_EQ(old_buffer, fred.c_str());
483 }
484 {
485 // With multiple references, we must copy.
486 CFX_WideString fred(L" FRED ");
487 CFX_WideString other_fred = fred;
488 const FX_WCHAR* old_buffer = fred.c_str();
489 fred.TrimRight();
490 EXPECT_EQ(L" FRED", fred);
491 EXPECT_EQ(L" FRED ", other_fred);
492 EXPECT_NE(old_buffer, fred.c_str());
493 }
494 {
495 // With multiple references, but no modifications, no copy.
496 CFX_WideString fred(L"FRED");
497 CFX_WideString other_fred = fred;
498 const FX_WCHAR* old_buffer = fred.c_str();
499 fred.TrimRight();
500 EXPECT_EQ(L"FRED", fred);
501 EXPECT_EQ(L"FRED", other_fred);
502 EXPECT_EQ(old_buffer, fred.c_str());
503 }
504 }
505
475 TEST(fxcrt, WideStringTrimLeft) { 506 TEST(fxcrt, WideStringTrimLeft) {
476 CFX_WideString fred(L" FRED "); 507 CFX_WideString fred(L" FRED ");
477 fred.TrimLeft(); 508 fred.TrimLeft();
478 EXPECT_EQ(L"FRED ", fred); 509 EXPECT_EQ(L"FRED ", fred);
479 fred.TrimLeft(L'E'); 510 fred.TrimLeft(L'E');
480 EXPECT_EQ(L"FRED ", fred); 511 EXPECT_EQ(L"FRED ", fred);
481 fred.TrimLeft(L'F'); 512 fred.TrimLeft(L'F');
482 EXPECT_EQ(L"RED ", fred); 513 EXPECT_EQ(L"RED ", fred);
483 fred.TrimLeft(L"ERP"); 514 fred.TrimLeft(L"ERP");
484 EXPECT_EQ(L"D ", fred); 515 EXPECT_EQ(L"D ", fred);
485 516
486 CFX_WideString blank(L" "); 517 CFX_WideString blank(L" ");
487 blank.TrimLeft(L"ERP"); 518 blank.TrimLeft(L"ERP");
488 EXPECT_EQ(L" ", blank); 519 EXPECT_EQ(L" ", blank);
489 blank.TrimLeft(L'E'); 520 blank.TrimLeft(L'E');
490 EXPECT_EQ(L" ", blank); 521 EXPECT_EQ(L" ", blank);
491 blank.TrimLeft(); 522 blank.TrimLeft();
492 EXPECT_EQ(L"", blank); 523 EXPECT_EQ(L"", blank);
493 524
494 CFX_WideString empty; 525 CFX_WideString empty;
495 empty.TrimLeft(L"ERP"); 526 empty.TrimLeft(L"ERP");
496 EXPECT_EQ(L"", empty); 527 EXPECT_EQ(L"", empty);
497 empty.TrimLeft(L'E'); 528 empty.TrimLeft(L'E');
498 EXPECT_EQ(L"", empty); 529 EXPECT_EQ(L"", empty);
499 empty.TrimLeft(); 530 empty.TrimLeft();
500 EXPECT_EQ(L"", empty); 531 EXPECT_EQ(L"", empty);
501 } 532 }
502 533
534 TEST(fxcrt, WideStringTrimLeftCopies) {
535 {
536 // With a single reference, no copy takes place.
537 CFX_WideString fred(L" FRED ");
538 const FX_WCHAR* old_buffer = fred.c_str();
539 fred.TrimLeft();
540 EXPECT_EQ(L"FRED ", fred);
541 EXPECT_EQ(old_buffer, fred.c_str());
542 }
543 {
544 // With multiple references, we must copy.
545 CFX_WideString fred(L" FRED ");
546 CFX_WideString other_fred = fred;
547 const FX_WCHAR* old_buffer = fred.c_str();
548 fred.TrimLeft();
549 EXPECT_EQ(L"FRED ", fred);
550 EXPECT_EQ(L" FRED ", other_fred);
551 EXPECT_NE(old_buffer, fred.c_str());
552 }
553 {
554 // With multiple references, but no modifications, no copy.
555 CFX_WideString fred(L"FRED");
556 CFX_WideString other_fred = fred;
557 const FX_WCHAR* old_buffer = fred.c_str();
558 fred.TrimLeft();
559 EXPECT_EQ(L"FRED", fred);
560 EXPECT_EQ(L"FRED", other_fred);
561 EXPECT_EQ(old_buffer, fred.c_str());
562 }
563 }
564
503 TEST(fxcrt, WideStringUTF16LE_Encode) { 565 TEST(fxcrt, WideStringUTF16LE_Encode) {
504 struct UTF16LEEncodeCase { 566 struct UTF16LEEncodeCase {
505 CFX_WideString ws; 567 CFX_WideString ws;
506 CFX_ByteString bs; 568 CFX_ByteString bs;
507 } utf16le_encode_cases[] = { 569 } utf16le_encode_cases[] = {
508 {L"", CFX_ByteString("\0\0", 2)}, 570 {L"", CFX_ByteString("\0\0", 2)},
509 {L"abc", CFX_ByteString("a\0b\0c\0\0\0", 8)}, 571 {L"abc", CFX_ByteString("a\0b\0c\0\0\0", 8)},
510 {L"abcdef", CFX_ByteString("a\0b\0c\0d\0e\0f\0\0\0", 14)}, 572 {L"abcdef", CFX_ByteString("a\0b\0c\0d\0e\0f\0\0\0", 14)},
511 {L"abc\0def", CFX_ByteString("a\0b\0c\0\0\0", 8)}, 573 {L"abc\0def", CFX_ByteString("a\0b\0c\0\0\0", 8)},
512 {L"\xaabb\xccdd", CFX_ByteString("\xbb\xaa\xdd\xcc\0\0", 6)}, 574 {L"\xaabb\xccdd", CFX_ByteString("\xbb\xaa\xdd\xcc\0\0", 6)},
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 } 801 }
740 } 802 }
741 803
742 TEST(fxcrt, EmptyWideString) { 804 TEST(fxcrt, EmptyWideString) {
743 CFX_WideString empty_str; 805 CFX_WideString empty_str;
744 EXPECT_TRUE(empty_str.IsEmpty()); 806 EXPECT_TRUE(empty_str.IsEmpty());
745 EXPECT_EQ(0, empty_str.GetLength()); 807 EXPECT_EQ(0, empty_str.GetLength());
746 const FX_WCHAR* cstr = empty_str.c_str(); 808 const FX_WCHAR* cstr = empty_str.c_str();
747 EXPECT_EQ(0, FXSYS_wcslen(cstr)); 809 EXPECT_EQ(0, FXSYS_wcslen(cstr));
748 } 810 }
OLDNEW
« 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