diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-21 23:47:54 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-21 23:47:54 +0000 |
commit | a1a61172ee9e1c59f257c4c8c49d4b9eb4c046e9 (patch) | |
tree | b9c4c407613f3682244a8d150d6c5a55f03e1344 /base | |
parent | ced35f33b676a8b5ed52ae363e0e1d9deffc2331 (diff) | |
download | chromium_src-a1a61172ee9e1c59f257c4c8c49d4b9eb4c046e9.zip chromium_src-a1a61172ee9e1c59f257c4c8c49d4b9eb4c046e9.tar.gz chromium_src-a1a61172ee9e1c59f257c4c8c49d4b9eb4c046e9.tar.bz2 |
Add a replace_all param to ReplaceSubstringsAfterOffset and update call sites.
Review URL: http://codereview.chromium.org/18603
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8413 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/string_util.cc | 28 | ||||
-rw-r--r-- | base/string_util.h | 11 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 29 |
3 files changed, 65 insertions, 3 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index f029e95..eae60d2 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -779,7 +779,8 @@ template<class StringType> void DoReplaceSubstringsAfterOffset(StringType* str, typename StringType::size_type start_offset, const StringType& find_this, - const StringType& replace_with) { + const StringType& replace_with, + bool replace_all) { if ((start_offset == StringType::npos) || (start_offset >= str->length())) return; @@ -788,21 +789,42 @@ void DoReplaceSubstringsAfterOffset(StringType* str, offs != StringType::npos; offs = str->find(find_this, offs)) { str->replace(offs, find_this.length(), replace_with); offs += replace_with.length(); + + if (!replace_all) + break; } } +void ReplaceFirstSubstringAfterOffset(std::wstring* str, + std::wstring::size_type start_offset, + const std::wstring& find_this, + const std::wstring& replace_with) { + DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with, + false); // replace first instance +} + +void ReplaceFirstSubstringAfterOffset(std::string* str, + std::string::size_type start_offset, + const std::string& find_this, + const std::string& replace_with) { + DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with, + false); // replace first instance +} + void ReplaceSubstringsAfterOffset(std::wstring* str, std::wstring::size_type start_offset, const std::wstring& find_this, const std::wstring& replace_with) { - DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with); + DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with, + true); // replace all instances } void ReplaceSubstringsAfterOffset(std::string* str, std::string::size_type start_offset, const std::string& find_this, const std::string& replace_with) { - DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with); + DoReplaceSubstringsAfterOffset(str, start_offset, find_this, replace_with, + true); // replace all instances } // Overloaded wrappers around vsnprintf and vswprintf. The buf_size parameter diff --git a/base/string_util.h b/base/string_util.h index 601d1d8..6d7ff94 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -320,6 +320,17 @@ std::wstring FormatSpeed(int64 bytes, DataUnits units, bool show_units); // Ex: FormatNumber(1234567) => 1,234,567 std::wstring FormatNumber(int64 number); +// Starting at |start_offset| (usually 0), replace the first instance of +// |find_this| with |replace_with|. +void ReplaceFirstSubstringAfterOffset(std::wstring* str, + std::wstring::size_type start_offset, + const std::wstring& find_this, + const std::wstring& replace_with); +void ReplaceFirstSubstringAfterOffset(std::string* str, + std::string::size_type start_offset, + const std::string& find_this, + const std::string& replace_with); + // Starting at |start_offset| (usually 0), look through |str| and replace all // instances of |find_this| with |replace_with|. // diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index 9a7f40f..11c727d 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -675,6 +675,35 @@ TEST(StringUtilTest, ReplaceSubstringsAfterOffset) { } } +TEST(StringUtilTest, ReplaceFirstSubstringAfterOffset) { + static const struct { + const wchar_t* str; + std::wstring::size_type start_offset; + const wchar_t* find_this; + const wchar_t* replace_with; + const wchar_t* expected; + } cases[] = { + {L"aaa", 0, L"a", L"b", L"baa"}, + {L"abb", 0, L"ab", L"a", L"ab"}, + {L"Removing some substrings inging", 0, L"ing", L"", + L"Remov some substrings inging"}, + {L"Not found", 0, L"x", L"0", L"Not found"}, + {L"Not found again", 5, L"x", L"0", L"Not found again"}, + {L" Making it much longer ", 0, L" ", L"Four score and seven years ago", + L"Four score and seven years agoMaking it much longer "}, + {L"Invalid offset", 9999, L"t", L"foobar", L"Invalid offset"}, + {L"Replace me only me once", 4, L"me ", L"", L"Replace only me once"}, + {L"abababab", 2, L"ab", L"c", L"abcabab"}, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { + std::wstring str(cases[i].str); + ReplaceFirstSubstringAfterOffset(&str, cases[i].start_offset, + cases[i].find_this, cases[i].replace_with); + EXPECT_EQ(cases[i].expected, str); + } +} + namespace { template <typename INT> |