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

Side by Side Diff: base/strings/string_util_unittest.cc

Issue 2979393002: [string_util] fix bug in ReplaceSubstringsAfterOffset() (Closed)
Patch Set: str_length Created 3 years, 4 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
« base/strings/string_util.cc ('K') | « base/strings/string_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium 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 "base/strings/string_util.h" 5 #include "base/strings/string_util.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #include <stdarg.h> 8 #include <stdarg.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 #include <stdint.h> 10 #include <stdint.h>
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 {100LL*1024*1024*1024, "100 GB"}, 578 {100LL*1024*1024*1024, "100 GB"},
579 }; 579 };
580 580
581 for (size_t i = 0; i < arraysize(cases); ++i) { 581 for (size_t i = 0; i < arraysize(cases); ++i) {
582 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), 582 EXPECT_EQ(ASCIIToUTF16(cases[i].expected),
583 FormatBytesUnlocalized(cases[i].bytes)); 583 FormatBytesUnlocalized(cases[i].bytes));
584 } 584 }
585 } 585 }
586 TEST(StringUtilTest, ReplaceSubstringsAfterOffset) { 586 TEST(StringUtilTest, ReplaceSubstringsAfterOffset) {
587 static const struct { 587 static const struct {
588 const char* str; 588 StringPiece str;
589 string16::size_type start_offset; 589 size_t start_offset;
590 const char* find_this; 590 StringPiece find_this;
591 const char* replace_with; 591 StringPiece replace_with;
592 const char* expected; 592 StringPiece expected;
593 } cases[] = { 593 } cases[] = {
594 {"aaa", 0, "a", "b", "bbb"}, 594 {"aaa", 0, "a", "b", "bbb"},
595 {"abb", 0, "ab", "a", "ab"}, 595 {"aaa", 0, "aa", "b", "ba"},
596 {"Removing some substrings inging", 0, "ing", "", "Remov some substrs "}, 596 {"aaa", 0, "aa", "bbb", "bbba"},
597 {"Not found", 0, "x", "0", "Not found"}, 597 {"aaaaa", 0, "aa", "b", "bba"},
598 {"Not found again", 5, "x", "0", "Not found again"}, 598 {"ababaaababa", 0, "aba", "", "baaba"},
599 {" Making it much longer ", 0, " ", "Four score and seven years ago", 599 {"ababaaababa", 0, "aba", "_", "_baa_ba"},
600 "Four score and seven years agoMakingFour score and seven years agoit" 600 {"ababaaababa", 0, "aba", "__", "__baa__ba"},
601 "Four score and seven years agomuchFour score and seven years agolonger" 601 {"ababaaababa", 0, "aba", "___", "___baa___ba"},
602 "Four score and seven years ago"}, 602 {"ababaaababa", 0, "aba", "____", "____baa____ba"},
603 {"Invalid offset", 9999, "t", "foobar", "Invalid offset"}, 603 {"ababaaababa", 0, "aba", "_____", "_____baa_____ba"},
604 {"Replace me only me once", 9, "me ", "", "Replace me only once"}, 604 {"abb", 0, "ab", "a", "ab"},
605 {"abababab", 2, "ab", "c", "abccc"}, 605 {"Removing some substrings inging", 0, "ing", "", "Remov some substrs "},
606 {"Not found", 0, "x", "0", "Not found"},
607 {"Not found again", 5, "x", "0", "Not found again"},
608 {" Making it much longer ", 0, " ", "Four score and seven years ago",
609 "Four score and seven years agoMakingFour score and seven years agoit"
610 "Four score and seven years agomuchFour score and seven years agolonger"
611 "Four score and seven years ago"},
612 {" Making it much much much much shorter ", 0,
613 "Making it much much much much shorter", "", " "},
614 {"so much much much much much very much much much shorter", 0, "much ",
615 "", "so very shorter"},
616 {"Invalid offset", 9999, "t", "foobar", "Invalid offset"},
617 {"Replace me only me once", 9, "me ", "", "Replace me only once"},
618 {"abababab", 2, "ab", "c", "abccc"},
606 }; 619 };
607 620
608 for (size_t i = 0; i < arraysize(cases); i++) { 621 // base::string16 variant
609 string16 str = ASCIIToUTF16(cases[i].str); 622 for (const auto& scenario : cases) {
610 ReplaceSubstringsAfterOffset(&str, cases[i].start_offset, 623 string16 str = ASCIIToUTF16(scenario.str);
611 ASCIIToUTF16(cases[i].find_this), 624 ReplaceSubstringsAfterOffset(&str, scenario.start_offset,
612 ASCIIToUTF16(cases[i].replace_with)); 625 ASCIIToUTF16(scenario.find_this),
613 EXPECT_EQ(ASCIIToUTF16(cases[i].expected), str); 626 ASCIIToUTF16(scenario.replace_with));
627 EXPECT_EQ(ASCIIToUTF16(scenario.expected), str);
628 }
629
630 // std::string with insufficient capacity: expansion must realloc the buffer.
631 for (const auto& scenario : cases) {
632 std::string str = scenario.str.as_string();
633 str.shrink_to_fit(); // This is nonbinding, but it's the best we've got.
634 ReplaceSubstringsAfterOffset(&str, scenario.start_offset,
635 scenario.find_this, scenario.replace_with);
636 EXPECT_EQ(scenario.expected, str);
637 }
638
639 // std::string with ample capacity: should be possible to grow in-place.
640 for (const auto& scenario : cases) {
641 std::string str = scenario.str.as_string();
642 str.reserve(std::max(scenario.str.length(), scenario.expected.length()) *
643 2);
644
645 ReplaceSubstringsAfterOffset(&str, scenario.start_offset,
646 scenario.find_this, scenario.replace_with);
647 EXPECT_EQ(scenario.expected, str);
614 } 648 }
615 } 649 }
616 650
617 TEST(StringUtilTest, ReplaceFirstSubstringAfterOffset) { 651 TEST(StringUtilTest, ReplaceFirstSubstringAfterOffset) {
618 static const struct { 652 static const struct {
619 const char* str; 653 const char* str;
620 string16::size_type start_offset; 654 string16::size_type start_offset;
621 const char* find_this; 655 const char* find_this;
622 const char* replace_with; 656 const char* replace_with;
623 const char* expected; 657 const char* expected;
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after
1231 const std::string live = kLive; 1265 const std::string live = kLive;
1232 std::string dead = live; 1266 std::string dead = live;
1233 strncpy(WriteInto(&dead, 5), kDead, 4); 1267 strncpy(WriteInto(&dead, 5), kDead, 4);
1234 EXPECT_EQ(kDead, dead); 1268 EXPECT_EQ(kDead, dead);
1235 EXPECT_EQ(4u, dead.size()); 1269 EXPECT_EQ(4u, dead.size());
1236 EXPECT_EQ(kLive, live); 1270 EXPECT_EQ(kLive, live);
1237 EXPECT_EQ(4u, live.size()); 1271 EXPECT_EQ(4u, live.size());
1238 } 1272 }
1239 1273
1240 } // namespace base 1274 } // namespace base
OLDNEW
« base/strings/string_util.cc ('K') | « base/strings/string_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698