diff options
-rw-r--r-- | base/string_util.cc | 19 | ||||
-rw-r--r-- | base/string_util.h | 2 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 30 |
3 files changed, 47 insertions, 4 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index 1cdbd7b..2998d2b 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -420,9 +420,10 @@ TrimPositions TrimWhitespace(const std::string& input, return TrimWhitespaceASCII(input, positions, output); } -std::wstring CollapseWhitespace(const std::wstring& text, - bool trim_sequences_with_line_breaks) { - std::wstring result; +template<typename STR> +STR CollapseWhitespaceT(const STR& text, + bool trim_sequences_with_line_breaks) { + STR result; result.resize(text.size()); // Set flags to pretend we're already in a trimmed whitespace sequence, so we @@ -431,7 +432,7 @@ std::wstring CollapseWhitespace(const std::wstring& text, bool already_trimmed = true; int chars_written = 0; - for (std::wstring::const_iterator i(text.begin()); i != text.end(); ++i) { + for (typename STR::const_iterator i(text.begin()); i != text.end(); ++i) { if (IsWhitespace(*i)) { if (!in_whitespace) { // Reduce all whitespace sequences to a single space. @@ -461,6 +462,16 @@ std::wstring CollapseWhitespace(const std::wstring& text, return result; } +std::wstring CollapseWhitespace(const std::wstring& text, + bool trim_sequences_with_line_breaks) { + return CollapseWhitespaceT(text, trim_sequences_with_line_breaks); +} + +std::string CollapseWhitespaceASCII(const std::string& text, + bool trim_sequences_with_line_breaks) { + return CollapseWhitespaceT(text, trim_sequences_with_line_breaks); +} + std::string WideToASCII(const std::wstring& wide) { DCHECK(IsStringASCII(wide)); return std::string(wide.begin(), wide.end()); diff --git a/base/string_util.h b/base/string_util.h index b082078..44b6d03 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -171,6 +171,8 @@ TrimPositions TrimWhitespace(const std::string& input, // (3) All other whitespace sequences are converted to single spaces. std::wstring CollapseWhitespace(const std::wstring& text, bool trim_sequences_with_line_breaks); +std::string CollapseWhitespaceASCII(const std::string& text, + bool trim_sequences_with_line_breaks); // These convert between ASCII (7-bit) and Wide/UTF16 strings. std::string WideToASCII(const std::wstring& wide); diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index a2f4f1d..1087aea 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -157,6 +157,36 @@ TEST(StringUtilTest, CollapseWhitespace) { } } +static const struct collapse_case_ascii { + const char* input; + const bool trim; + const char* output; +} collapse_cases_ascii[] = { + {" Google Video ", false, "Google Video"}, + {"Google Video", false, "Google Video"}, + {"", false, ""}, + {" ", false, ""}, + {"\t\rTest String\n", false, "Test String"}, + {" Test \n \t String ", false, "Test String"}, + {" Test String", false, "Test String"}, + {"Test String ", false, "Test String"}, + {"Test String", false, "Test String"}, + {"", true, ""}, + {"\n", true, ""}, + {" \r ", true, ""}, + {"\nFoo", true, "Foo"}, + {"\r Foo ", true, "Foo"}, + {" Foo bar ", true, "Foo bar"}, + {" \tFoo bar \n", true, "Foo bar"}, + {" a \r b\n c \r\n d \t\re \t f \n ", true, "abcde f"}, +}; + +TEST(StringUtilTest, CollapseWhitespaceASCII) { + for (size_t i = 0; i < arraysize(collapse_cases_ascii); ++i) { + const collapse_case_ascii& value = collapse_cases_ascii[i]; + EXPECT_EQ(value.output, CollapseWhitespaceASCII(value.input, value.trim)); + } +} TEST(StringUtilTest, IsStringUTF8) { EXPECT_TRUE(IsStringUTF8("abc")); |