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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/string_split.h ('k') | base/string_split_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/string_split.h" 5 #include "base/string_split.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/third_party/icu/icu_utf.h" 9 #include "base/third_party/icu/icu_utf.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 } 161 }
162 162
163 void SplitStringDontTrim(const std::string& str, 163 void SplitStringDontTrim(const std::string& str,
164 char c, 164 char c,
165 std::vector<std::string>* r) { 165 std::vector<std::string>* r) {
166 DCHECK(IsStringUTF8(str)); 166 DCHECK(IsStringUTF8(str));
167 DCHECK(c >= 0 && c < 0x7F); 167 DCHECK(c >= 0 && c < 0x7F);
168 SplitStringT(str, c, false, r); 168 SplitStringT(str, c, false, r);
169 } 169 }
170 170
171 template<typename STR>
172 void SplitStringAlongWhitespaceT(const STR& str, std::vector<STR>* result) {
173 const size_t length = str.length();
174 if (!length)
175 return;
176
177 bool last_was_ws = false;
178 size_t last_non_ws_start = 0;
179 for (size_t i = 0; i < length; ++i) {
180 switch (str[i]) {
181 // HTML 5 defines whitespace as: space, tab, LF, line tab, FF, or CR.
182 case L' ':
183 case L'\t':
184 case L'\xA':
185 case L'\xB':
186 case L'\xC':
187 case L'\xD':
188 if (!last_was_ws) {
189 if (i > 0) {
190 result->push_back(
191 str.substr(last_non_ws_start, i - last_non_ws_start));
192 }
193 last_was_ws = true;
194 }
195 break;
196
197 default: // Not a space character.
198 if (last_was_ws) {
199 last_was_ws = false;
200 last_non_ws_start = i;
201 }
202 break;
203 }
204 }
205 if (!last_was_ws) {
206 result->push_back(
207 str.substr(last_non_ws_start, length - last_non_ws_start));
208 }
209 }
210
211 void SplitStringAlongWhitespace(const std::wstring& str,
212 std::vector<std::wstring>* result) {
213 SplitStringAlongWhitespaceT(str, result);
214 }
215
216 #if !defined(WCHAR_T_IS_UTF16)
217 void SplitStringAlongWhitespace(const string16& str,
218 std::vector<string16>* result) {
219 SplitStringAlongWhitespaceT(str, result);
220 }
221 #endif
222
223 void SplitStringAlongWhitespace(const std::string& str,
224 std::vector<std::string>* result) {
225 SplitStringAlongWhitespaceT(str, result);
226 }
227
171 } // namespace base 228 } // namespace base
OLDNEW
« 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