diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-05 01:44:17 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-05 01:44:17 +0000 |
commit | 0443f912a956df7b52edf997d05d0108be995fef (patch) | |
tree | 7701f14445c681dee145058d7090c21b2863b011 /base/string_util.cc | |
parent | a2fbebee62a76a9f6f75ea3231028454623c53e6 (diff) | |
download | chromium_src-0443f912a956df7b52edf997d05d0108be995fef.zip chromium_src-0443f912a956df7b52edf997d05d0108be995fef.tar.gz chromium_src-0443f912a956df7b52edf997d05d0108be995fef.tar.bz2 |
Implement RemoveChars, a function to remove a list of characters from a string, and use it in PhoneNumber to remove extra phone number characters.
BUG=none
TEST=StringUtilTest.RemoveChars
Review URL: http://codereview.chromium.org/572015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@38162 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util.cc')
-rw-r--r-- | base/string_util.cc | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index bf69b0c..5990ef4 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -391,6 +391,45 @@ const char kWhitespaceASCII[] = { const char kUtf8ByteOrderMark[] = "\xEF\xBB\xBF"; template<typename STR> +bool RemoveCharsT(const STR& input, + const typename STR::value_type remove_chars[], + STR* output) { + bool removed = false; + size_t found; + + *output = input; + + found = output->find_first_of(remove_chars); + while (found != STR::npos) { + removed = true; + output->replace(found, 1, STR()); + found = output->find_first_of(remove_chars, found); + } + + return removed; +} + +bool RemoveChars(const std::wstring& input, + const wchar_t remove_chars[], + std::wstring* output) { + return RemoveCharsT(input, remove_chars, output); +} + +#if !defined(WCHAR_T_IS_UTF16) +bool RemoveChars(const string16& input, + const char16 remove_chars[], + string16* output) { + return RemoveCharsT(input, remove_chars, output); +} +#endif + +bool RemoveChars(const std::string& input, + const char remove_chars[], + std::string* output) { + return RemoveCharsT(input, remove_chars, output); +} + +template<typename STR> TrimPositions TrimStringT(const STR& input, const typename STR::value_type trim_chars[], TrimPositions positions, |