diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-15 22:03:42 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-15 22:03:42 +0000 |
commit | b87c4a775b5b625dc2034b70e1232862d5d7faac (patch) | |
tree | 5e4939490ae5d5d049bc9c4ea5ccc1e70c0fb041 | |
parent | df5f308da4fb0526f2af1fc8db24280ffcafcd6e (diff) | |
download | chromium_src-b87c4a775b5b625dc2034b70e1232862d5d7faac.zip chromium_src-b87c4a775b5b625dc2034b70e1232862d5d7faac.tar.gz chromium_src-b87c4a775b5b625dc2034b70e1232862d5d7faac.tar.bz2 |
base: Move StringSplitAlongWhitespace to string_split.h
BUG=None
TEST=trybots
Review URL: http://codereview.chromium.org/5004002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66181 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/string_split.cc | 57 | ||||
-rw-r--r-- | base/string_split.h | 15 | ||||
-rw-r--r-- | base/string_split_unittest.cc | 32 | ||||
-rw-r--r-- | base/string_util.cc | 57 | ||||
-rw-r--r-- | base/string_util.h | 16 | ||||
-rw-r--r-- | base/string_util_unittest.cc | 32 | ||||
-rw-r--r-- | chrome/browser/bookmarks/bookmark_model_unittest.cc | 3 | ||||
-rw-r--r-- | chrome/browser/history/top_sites_database.cc | 5 | ||||
-rw-r--r-- | chrome/browser/themes/browser_theme_provider.cc | 3 | ||||
-rw-r--r-- | net/tools/hresolv/hresolv.cc | 2 | ||||
-rw-r--r-- | webkit/glue/dom_operations.cc | 2 |
11 files changed, 113 insertions, 111 deletions
diff --git a/base/string_split.cc b/base/string_split.cc index bc7955d..44b5d06 100644 --- a/base/string_split.cc +++ b/base/string_split.cc @@ -168,4 +168,61 @@ void SplitStringDontTrim(const std::string& str, SplitStringT(str, c, false, r); } +template<typename STR> +void SplitStringAlongWhitespaceT(const STR& str, std::vector<STR>* result) { + const size_t length = str.length(); + if (!length) + return; + + bool last_was_ws = false; + size_t last_non_ws_start = 0; + for (size_t i = 0; i < length; ++i) { + switch (str[i]) { + // HTML 5 defines whitespace as: space, tab, LF, line tab, FF, or CR. + case L' ': + case L'\t': + case L'\xA': + case L'\xB': + case L'\xC': + case L'\xD': + if (!last_was_ws) { + if (i > 0) { + result->push_back( + str.substr(last_non_ws_start, i - last_non_ws_start)); + } + last_was_ws = true; + } + break; + + default: // Not a space character. + if (last_was_ws) { + last_was_ws = false; + last_non_ws_start = i; + } + break; + } + } + if (!last_was_ws) { + result->push_back( + str.substr(last_non_ws_start, length - last_non_ws_start)); + } +} + +void SplitStringAlongWhitespace(const std::wstring& str, + std::vector<std::wstring>* result) { + SplitStringAlongWhitespaceT(str, result); +} + +#if !defined(WCHAR_T_IS_UTF16) +void SplitStringAlongWhitespace(const string16& str, + std::vector<string16>* result) { + SplitStringAlongWhitespaceT(str, result); +} +#endif + +void SplitStringAlongWhitespace(const std::string& str, + std::vector<std::string>* result) { + SplitStringAlongWhitespaceT(str, result); +} + } // namespace base diff --git a/base/string_split.h b/base/string_split.h index 6af1511..9a9030a 100644 --- a/base/string_split.h +++ b/base/string_split.h @@ -69,6 +69,21 @@ void SplitStringDontTrim(const std::string& str, char c, std::vector<std::string>* r); +// WARNING: this uses whitespace as defined by the HTML5 spec. If you need +// a function similar to this but want to trim all types of whitespace, then +// factor this out into a function that takes a string containing the characters +// that are treated as whitespace. +// +// Splits the string along whitespace (where whitespace is the five space +// characters defined by HTML 5). Each contiguous block of non-whitespace +// characters is added to result. +void SplitStringAlongWhitespace(const std::wstring& str, + std::vector<std::wstring>* result); +void SplitStringAlongWhitespace(const string16& str, + std::vector<string16>* result); +void SplitStringAlongWhitespace(const std::string& str, + std::vector<std::string>* result); + } // namespace base #endif // BASE_STRING_SPLIT_H diff --git a/base/string_split_unittest.cc b/base/string_split_unittest.cc index afdc526..749d566 100644 --- a/base/string_split_unittest.cc +++ b/base/string_split_unittest.cc @@ -262,4 +262,36 @@ TEST(StringSplitTest, StringSplitDontTrim) { r.clear(); } +TEST(StringSplitTest, SplitStringAlongWhitespace) { + struct TestData { + const std::wstring input; + const size_t expected_result_count; + const std::wstring output1; + const std::wstring output2; + } data[] = { + { L"a", 1, L"a", L"" }, + { L" ", 0, L"", L"" }, + { L" a", 1, L"a", L"" }, + { L" ab ", 1, L"ab", L"" }, + { L" ab c", 2, L"ab", L"c" }, + { L" ab c ", 2, L"ab", L"c" }, + { L" ab cd", 2, L"ab", L"cd" }, + { L" ab cd ", 2, L"ab", L"cd" }, + { L" \ta\t", 1, L"a", L"" }, + { L" b\ta\t", 2, L"b", L"a" }, + { L" b\tat", 2, L"b", L"at" }, + { L"b\tat", 2, L"b", L"at" }, + { L"b\t at", 2, L"b", L"at" }, + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { + std::vector<std::wstring> results; + SplitStringAlongWhitespace(data[i].input, &results); + ASSERT_EQ(data[i].expected_result_count, results.size()); + if (data[i].expected_result_count > 0) + ASSERT_EQ(data[i].output1, results[0]); + if (data[i].expected_result_count > 1) + ASSERT_EQ(data[i].output2, results[1]); + } +} + } // namespace base diff --git a/base/string_util.cc b/base/string_util.cc index 125c713..d7b6729 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -822,63 +822,6 @@ string16 JoinString(const std::vector<string16>& parts, char16 sep) { return JoinStringT(parts, sep); } -template<typename STR> -void SplitStringAlongWhitespaceT(const STR& str, std::vector<STR>* result) { - const size_t length = str.length(); - if (!length) - return; - - bool last_was_ws = false; - size_t last_non_ws_start = 0; - for (size_t i = 0; i < length; ++i) { - switch (str[i]) { - // HTML 5 defines whitespace as: space, tab, LF, line tab, FF, or CR. - case L' ': - case L'\t': - case L'\xA': - case L'\xB': - case L'\xC': - case L'\xD': - if (!last_was_ws) { - if (i > 0) { - result->push_back( - str.substr(last_non_ws_start, i - last_non_ws_start)); - } - last_was_ws = true; - } - break; - - default: // Not a space character. - if (last_was_ws) { - last_was_ws = false; - last_non_ws_start = i; - } - break; - } - } - if (!last_was_ws) { - result->push_back( - str.substr(last_non_ws_start, length - last_non_ws_start)); - } -} - -void SplitStringAlongWhitespace(const std::wstring& str, - std::vector<std::wstring>* result) { - SplitStringAlongWhitespaceT(str, result); -} - -#if !defined(WCHAR_T_IS_UTF16) -void SplitStringAlongWhitespace(const string16& str, - std::vector<string16>* result) { - SplitStringAlongWhitespaceT(str, result); -} -#endif - -void SplitStringAlongWhitespace(const std::string& str, - std::vector<std::string>* result) { - SplitStringAlongWhitespaceT(str, result); -} - template<class FormatStringType, class OutStringType> OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string, const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) { diff --git a/base/string_util.h b/base/string_util.h index c238e4a..5466403 100644 --- a/base/string_util.h +++ b/base/string_util.h @@ -486,7 +486,6 @@ inline typename string_type::value_type* WriteInto(string_type* str, //----------------------------------------------------------------------------- - // Splits a string into its fields delimited by any of the characters in // |delimiters|. Each field is added to the |tokens| vector. Returns the // number of tokens found. @@ -507,21 +506,6 @@ size_t Tokenize(const base::StringPiece& str, string16 JoinString(const std::vector<string16>& parts, char16 s); std::string JoinString(const std::vector<std::string>& parts, char s); -// WARNING: this uses whitespace as defined by the HTML5 spec. If you need -// a function similar to this but want to trim all types of whitespace, then -// factor this out into a function that takes a string containing the characters -// that are treated as whitespace. -// -// Splits the string along whitespace (where whitespace is the five space -// characters defined by HTML 5). Each contiguous block of non-whitespace -// characters is added to result. -void SplitStringAlongWhitespace(const std::wstring& str, - std::vector<std::wstring>* result); -void SplitStringAlongWhitespace(const string16& str, - std::vector<string16>* result); -void SplitStringAlongWhitespace(const std::string& str, - std::vector<std::string>* result); - // Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively. // Additionally, any number of consecutive '$' characters is replaced by that // number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index 0f7d79d..b7639bb 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -925,38 +925,6 @@ TEST(StringUtilTest, ReplaceStringPlaceholdersConsecutiveDollarSigns) { "$1 $$2 $$$3"); } -TEST(StringUtilTest, SplitStringAlongWhitespace) { - struct TestData { - const std::wstring input; - const size_t expected_result_count; - const std::wstring output1; - const std::wstring output2; - } data[] = { - { L"a", 1, L"a", L"" }, - { L" ", 0, L"", L"" }, - { L" a", 1, L"a", L"" }, - { L" ab ", 1, L"ab", L"" }, - { L" ab c", 2, L"ab", L"c" }, - { L" ab c ", 2, L"ab", L"c" }, - { L" ab cd", 2, L"ab", L"cd" }, - { L" ab cd ", 2, L"ab", L"cd" }, - { L" \ta\t", 1, L"a", L"" }, - { L" b\ta\t", 2, L"b", L"a" }, - { L" b\tat", 2, L"b", L"at" }, - { L"b\tat", 2, L"b", L"at" }, - { L"b\t at", 2, L"b", L"at" }, - }; - for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { - std::vector<std::wstring> results; - SplitStringAlongWhitespace(data[i].input, &results); - ASSERT_EQ(data[i].expected_result_count, results.size()); - if (data[i].expected_result_count > 0) - ASSERT_EQ(data[i].output1, results[0]); - if (data[i].expected_result_count > 1) - ASSERT_EQ(data[i].output2, results[1]); - } -} - TEST(StringUtilTest, MatchPatternTest) { EXPECT_TRUE(MatchPattern("www.google.com", "*.com")); EXPECT_TRUE(MatchPattern("www.google.com", "*")); diff --git a/chrome/browser/bookmarks/bookmark_model_unittest.cc b/chrome/browser/bookmarks/bookmark_model_unittest.cc index 29605ab..6d6017a 100644 --- a/chrome/browser/bookmarks/bookmark_model_unittest.cc +++ b/chrome/browser/bookmarks/bookmark_model_unittest.cc @@ -12,6 +12,7 @@ #include "base/path_service.h" #include "base/string16.h" #include "base/string_number_conversions.h" +#include "base/string_split.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/browser/bookmarks/bookmark_codec.h" @@ -621,7 +622,7 @@ static void PopulateNodeFromString(const std::string& description, TestNode* parent) { std::vector<std::string> elements; size_t index = 0; - SplitStringAlongWhitespace(description, &elements); + base::SplitStringAlongWhitespace(description, &elements); PopulateNodeImpl(elements, &index, parent); } diff --git a/chrome/browser/history/top_sites_database.cc b/chrome/browser/history/top_sites_database.cc index 8bfbcaf..4639494 100644 --- a/chrome/browser/history/top_sites_database.cc +++ b/chrome/browser/history/top_sites_database.cc @@ -5,6 +5,7 @@ #include "app/sql/connection.h" #include "app/sql/transaction.h" #include "base/file_util.h" +#include "base/string_split.h" #include "base/string_util.h" #include "chrome/browser/diagnostics/sqlite_diagnostics.h" #include "chrome/browser/history/history_types.h" @@ -134,8 +135,8 @@ std::string TopSitesDatabase::GetRedirects(const MostVisitedURL& url) { void TopSitesDatabase::SetRedirects(const std::string& redirects, MostVisitedURL* url) { std::vector<std::string> redirects_vector; - SplitStringAlongWhitespace(redirects, &redirects_vector); - for (size_t i = 0; i < redirects_vector.size(); i++) + base::SplitStringAlongWhitespace(redirects, &redirects_vector); + for (size_t i = 0; i < redirects_vector.size(); ++i) url->redirects.push_back(GURL(redirects_vector[i])); } diff --git a/chrome/browser/themes/browser_theme_provider.cc b/chrome/browser/themes/browser_theme_provider.cc index ae40eef..c31a341 100644 --- a/chrome/browser/themes/browser_theme_provider.cc +++ b/chrome/browser/themes/browser_theme_provider.cc @@ -5,6 +5,7 @@ #include "chrome/browser/themes/browser_theme_provider.h" #include "app/resource_bundle.h" +#include "base/string_split.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/browser/extensions/extensions_service.h" @@ -361,7 +362,7 @@ std::string BrowserThemeProvider::AlignmentToString(int alignment) { // static int BrowserThemeProvider::StringToAlignment(const std::string& alignment) { std::vector<std::wstring> split; - SplitStringAlongWhitespace(UTF8ToWide(alignment), &split); + base::SplitStringAlongWhitespace(UTF8ToWide(alignment), &split); int alignment_mask = 0; for (std::vector<std::wstring>::iterator alignments(split.begin()); diff --git a/net/tools/hresolv/hresolv.cc b/net/tools/hresolv/hresolv.cc index da5c4b0..314ff32 100644 --- a/net/tools/hresolv/hresolv.cc +++ b/net/tools/hresolv/hresolv.cc @@ -390,7 +390,7 @@ bool ReadHostsAndTimesFromFile(const FilePath& path, it != line_end; ++it) { std::vector<std::string> tokens; - SplitStringAlongWhitespace(*it, &tokens); + base::SplitStringAlongWhitespace(*it, &tokens); switch (tokens.size()) { case 0: // Unexpected, but keep going. diff --git a/webkit/glue/dom_operations.cc b/webkit/glue/dom_operations.cc index 2d0fb68..bc546e2 100644 --- a/webkit/glue/dom_operations.cc +++ b/webkit/glue/dom_operations.cc @@ -278,7 +278,7 @@ bool ParseIconSizes(const string16& text, bool* is_any) { *is_any = false; std::vector<string16> size_strings; - SplitStringAlongWhitespace(text, &size_strings); + base::SplitStringAlongWhitespace(text, &size_strings); for (size_t i = 0; i < size_strings.size(); ++i) { if (EqualsASCII(size_strings[i], "any")) { *is_any = true; |