OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/strings/string_split.h" | 5 #include "base/strings/string_split.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/third_party/icu/icu_utf.h" | 9 #include "base/third_party/icu/icu_utf.h" |
10 | 10 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 if (begin_value_pos == StringPiece::npos) { | 121 if (begin_value_pos == StringPiece::npos) { |
122 DVLOG(1) << "cannot parse value from input: " << input; | 122 DVLOG(1) << "cannot parse value from input: " << input; |
123 return false; // No value. | 123 return false; // No value. |
124 } | 124 } |
125 remains.substr(begin_value_pos, remains.size() - begin_value_pos) | 125 remains.substr(begin_value_pos, remains.size() - begin_value_pos) |
126 .CopyToString(&result_pair.second); | 126 .CopyToString(&result_pair.second); |
127 | 127 |
128 return true; | 128 return true; |
129 } | 129 } |
130 | 130 |
131 template <typename Str> | 131 template <typename Str, typename OutputStringType> |
132 void SplitStringUsingSubstrT(BasicStringPiece<Str> input, | 132 void SplitStringUsingSubstrT(BasicStringPiece<Str> input, |
133 BasicStringPiece<Str> delimiter, | 133 BasicStringPiece<Str> delimiter, |
134 std::vector<Str>* result) { | 134 WhitespaceHandling whitespace, |
| 135 SplitResult result_type, |
| 136 std::vector<OutputStringType>* result) { |
135 using Piece = BasicStringPiece<Str>; | 137 using Piece = BasicStringPiece<Str>; |
136 using size_type = typename Piece::size_type; | 138 using size_type = typename Piece::size_type; |
137 | 139 |
138 result->clear(); | 140 result->clear(); |
139 size_type begin_index = 0; | 141 for (size_type begin_index = 0, end_index = 0; end_index != Piece::npos; |
140 while (true) { | 142 begin_index = end_index + delimiter.size()) { |
141 size_type end_index = input.find(delimiter, begin_index); | 143 end_index = input.find(delimiter, begin_index); |
142 if (end_index == Piece::npos) { | 144 Piece term = end_index == Piece::npos |
143 // No delimiter, use the rest of the string. | 145 ? input.substr(begin_index) |
144 Piece term = TrimString(input.substr(begin_index), | 146 : input.substr(begin_index, end_index - begin_index); |
145 WhitespaceForType<Str>(), TRIM_ALL); | 147 |
146 result->push_back(term.as_string()); | 148 if (whitespace == TRIM_WHITESPACE) |
147 return; | 149 term = TrimString(term, WhitespaceForType<Str>(), TRIM_ALL); |
148 } | 150 |
149 Piece term = TrimString(input.substr(begin_index, end_index - begin_index), | 151 if (result_type == SPLIT_WANT_ALL || !term.empty()) |
150 WhitespaceForType<Str>(), TRIM_ALL); | 152 result->push_back(PieceToOutputType<Str, OutputStringType>(term)); |
151 result->push_back(term.as_string()); | |
152 begin_index = end_index + delimiter.size(); | |
153 } | 153 } |
154 } | 154 } |
155 | 155 |
156 } // namespace | 156 } // namespace |
157 | 157 |
158 std::vector<std::string> SplitString(StringPiece input, | 158 std::vector<std::string> SplitString(StringPiece input, |
159 StringPiece separators, | 159 StringPiece separators, |
160 WhitespaceHandling whitespace, | 160 WhitespaceHandling whitespace, |
161 SplitResult result_type) { | 161 SplitResult result_type) { |
162 if (separators.size() == 1) { | 162 if (separators.size() == 1) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 // value or key; just record that the split failed. | 221 // value or key; just record that the split failed. |
222 success = false; | 222 success = false; |
223 } | 223 } |
224 } | 224 } |
225 return success; | 225 return success; |
226 } | 226 } |
227 | 227 |
228 void SplitStringUsingSubstr(StringPiece16 input, | 228 void SplitStringUsingSubstr(StringPiece16 input, |
229 StringPiece16 delimiter, | 229 StringPiece16 delimiter, |
230 std::vector<string16>* result) { | 230 std::vector<string16>* result) { |
231 SplitStringUsingSubstrT(input, delimiter, result); | 231 SplitStringUsingSubstrT(input, delimiter, TRIM_WHITESPACE, SPLIT_WANT_ALL, |
| 232 result); |
232 } | 233 } |
233 | 234 |
234 void SplitStringUsingSubstr(StringPiece input, | 235 void SplitStringUsingSubstr(StringPiece input, |
235 StringPiece delimiter, | 236 StringPiece delimiter, |
236 std::vector<std::string>* result) { | 237 std::vector<std::string>* result) { |
237 SplitStringUsingSubstrT(input, delimiter, result); | 238 SplitStringUsingSubstrT(input, delimiter, TRIM_WHITESPACE, SPLIT_WANT_ALL, |
| 239 result); |
| 240 } |
| 241 |
| 242 std::vector<StringPiece16> SplitStringPieceUsingSubstr( |
| 243 StringPiece16 input, |
| 244 StringPiece16 delimiter, |
| 245 WhitespaceHandling whitespace, |
| 246 SplitResult result_type) { |
| 247 std::vector<StringPiece16> result; |
| 248 SplitStringUsingSubstrT(input, delimiter, whitespace, result_type, &result); |
| 249 return result; |
| 250 } |
| 251 |
| 252 std::vector<StringPiece> SplitStringPieceUsingSubstr( |
| 253 StringPiece input, |
| 254 StringPiece delimiter, |
| 255 WhitespaceHandling whitespace, |
| 256 SplitResult result_type) { |
| 257 std::vector<StringPiece> result; |
| 258 SplitStringUsingSubstrT(input, delimiter, whitespace, result_type, &result); |
| 259 return result; |
238 } | 260 } |
239 | 261 |
240 } // namespace base | 262 } // namespace base |
OLD | NEW |