Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Unified Diff: base/string_split.cc

Issue 5004002: base: Move StringSplitAlongWhitespace to string_split.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: unittests Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/string_split.h ('k') | base/string_split_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/string_split.cc
diff --git a/base/string_split.cc b/base/string_split.cc
index bc7955d74134407e8342e8345cf9e33c3d80cb10..44b5d067615c8d4cd1b1112d8cc2642a6ae76d6b 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
« no previous file with comments | « base/string_split.h ('k') | base/string_split_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698