summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-05 01:44:17 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-05 01:44:17 +0000
commit0443f912a956df7b52edf997d05d0108be995fef (patch)
tree7701f14445c681dee145058d7090c21b2863b011 /base
parenta2fbebee62a76a9f6f75ea3231028454623c53e6 (diff)
downloadchromium_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')
-rw-r--r--base/string_util.cc39
-rw-r--r--base/string_util.h13
-rw-r--r--base/string_util_unittest.cc16
3 files changed, 68 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,
diff --git a/base/string_util.h b/base/string_util.h
index 34f9386..60f4f25 100644
--- a/base/string_util.h
+++ b/base/string_util.h
@@ -143,6 +143,19 @@ extern const char kWhitespaceASCII[];
extern const char kUtf8ByteOrderMark[];
+// Removes characters in remove_chars from anywhere in input. Returns true if
+// any characters were removed.
+// NOTE: Safe to use the same variable for both input and output.
+bool RemoveChars(const std::wstring& input,
+ const wchar_t remove_chars[],
+ std::wstring* output);
+bool RemoveChars(const string16& input,
+ const char16 remove_chars[],
+ string16* output);
+bool RemoveChars(const std::string& input,
+ const char remove_chars[],
+ std::string* output);
+
// Removes characters in trim_chars from the beginning and end of input.
// NOTE: Safe to use the same variable for both input and output.
bool TrimString(const std::wstring& input,
diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc
index 9d848a4..a3585ec 100644
--- a/base/string_util_unittest.cc
+++ b/base/string_util_unittest.cc
@@ -1468,4 +1468,20 @@ TEST(StringUtilTest, HexEncode) {
EXPECT_EQ(hex.compare("01FF02FE038081"), 0);
}
+TEST(StringUtilTest, RemoveChars) {
+ const char kRemoveChars[] = {'-', '/', '+', '*'};
+ std::string input = "A-+bc/d!*";
+ EXPECT_TRUE(RemoveChars(input, kRemoveChars, &input));
+ EXPECT_EQ("Abcd!", input);
+
+ // No characters match kRemoveChars.
+ EXPECT_FALSE(RemoveChars(input, kRemoveChars, &input));
+ EXPECT_EQ("Abcd!", input);
+
+ // Empty string.
+ input.clear();
+ EXPECT_FALSE(RemoveChars(input, kRemoveChars, &input));
+ EXPECT_EQ(std::string(), input);
+}
+
} // namaspace base