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 /base/string_split.cc | |
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
Diffstat (limited to 'base/string_split.cc')
-rw-r--r-- | base/string_split.cc | 57 |
1 files changed, 57 insertions, 0 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 |