1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/strings/string_split.h"
#include <stddef.h>
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/third_party/icu/icu_utf.h"
namespace base {
namespace {
// Returns either the ASCII or UTF-16 whitespace.
template<typename Str> BasicStringPiece<Str> WhitespaceForType();
#if defined(OS_WIN) && defined(BASE_STRING16_IS_STD_U16STRING)
template <>
WStringPiece WhitespaceForType<std::wstring>() {
return kWhitespaceWide;
}
#endif
template<> StringPiece16 WhitespaceForType<string16>() {
return kWhitespaceUTF16;
}
template<> StringPiece WhitespaceForType<std::string>() {
return kWhitespaceASCII;
}
// General string splitter template. Can take 8- or 16-bit input, can produce
// the corresponding string or StringPiece output.
template <typename OutputStringType, typename Str>
static std::vector<OutputStringType> SplitStringT(
BasicStringPiece<Str> str,
BasicStringPiece<Str> delimiter,
WhitespaceHandling whitespace,
SplitResult result_type) {
std::vector<OutputStringType> result;
if (str.empty())
return result;
size_t start = 0;
while (start != Str::npos) {
size_t end = str.find_first_of(delimiter, start);
BasicStringPiece<Str> piece;
if (end == Str::npos) {
piece = str.substr(start);
start = Str::npos;
} else {
piece = str.substr(start, end - start);
start = end + 1;
}
if (whitespace == TRIM_WHITESPACE)
piece = TrimString(piece, WhitespaceForType<Str>(), TRIM_ALL);
if (result_type == SPLIT_WANT_ALL || !piece.empty())
result.emplace_back(piece);
}
return result;
}
bool AppendStringKeyValue(StringPiece input,
char delimiter,
StringPairs* result) {
// Always append a new item regardless of success (it might be empty). The
// below code will copy the strings directly into the result pair.
result->resize(result->size() + 1);
auto& result_pair = result->back();
// Find the delimiter.
size_t end_key_pos = input.find_first_of(delimiter);
if (end_key_pos == std::string::npos) {
DVLOG(1) << "cannot find delimiter in: " << input;
return false; // No delimiter.
}
result_pair.first = std::string(input.substr(0, end_key_pos));
// Find the value string.
StringPiece remains = input.substr(end_key_pos, input.size() - end_key_pos);
size_t begin_value_pos = remains.find_first_not_of(delimiter);
if (begin_value_pos == StringPiece::npos) {
DVLOG(1) << "cannot parse value from input: " << input;
return false; // No value.
}
result_pair.second = std::string(
remains.substr(begin_value_pos, remains.size() - begin_value_pos));
return true;
}
template <typename OutputStringType, typename Str>
std::vector<OutputStringType> SplitStringUsingSubstrT(
BasicStringPiece<Str> input,
BasicStringPiece<Str> delimiter,
WhitespaceHandling whitespace,
SplitResult result_type) {
using Piece = BasicStringPiece<Str>;
using size_type = typename Piece::size_type;
std::vector<OutputStringType> result;
for (size_type begin_index = 0, end_index = 0; end_index != Piece::npos;
begin_index = end_index + delimiter.size()) {
end_index = input.find(delimiter, begin_index);
Piece term = end_index == Piece::npos
? input.substr(begin_index)
: input.substr(begin_index, end_index - begin_index);
if (whitespace == TRIM_WHITESPACE)
term = TrimString(term, WhitespaceForType<Str>(), TRIM_ALL);
if (result_type == SPLIT_WANT_ALL || !term.empty())
result.emplace_back(term);
}
return result;
}
} // namespace
std::vector<std::string> SplitString(StringPiece input,
StringPiece separators,
WhitespaceHandling whitespace,
SplitResult result_type) {
return SplitStringT<std::string>(input, separators, whitespace, result_type);
}
std::vector<string16> SplitString(StringPiece16 input,
StringPiece16 separators,
WhitespaceHandling whitespace,
SplitResult result_type) {
return SplitStringT<string16>(input, separators, whitespace, result_type);
}
std::vector<StringPiece> SplitStringPiece(StringPiece input,
StringPiece separators,
WhitespaceHandling whitespace,
SplitResult result_type) {
return SplitStringT<StringPiece>(input, separators, whitespace, result_type);
}
std::vector<StringPiece16> SplitStringPiece(StringPiece16 input,
StringPiece16 separators,
WhitespaceHandling whitespace,
SplitResult result_type) {
return SplitStringT<StringPiece16>(input, separators, whitespace,
result_type);
}
bool SplitStringIntoKeyValuePairs(StringPiece input,
char key_value_delimiter,
char key_value_pair_delimiter,
StringPairs* key_value_pairs) {
return SplitStringIntoKeyValuePairsUsingSubstr(
input, key_value_delimiter, StringPiece(&key_value_pair_delimiter, 1),
key_value_pairs);
}
bool SplitStringIntoKeyValuePairsUsingSubstr(
StringPiece input,
char key_value_delimiter,
StringPiece key_value_pair_delimiter,
StringPairs* key_value_pairs) {
key_value_pairs->clear();
std::vector<StringPiece> pairs = SplitStringPieceUsingSubstr(
input, key_value_pair_delimiter, TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
key_value_pairs->reserve(pairs.size());
bool success = true;
for (const StringPiece& pair : pairs) {
if (!AppendStringKeyValue(pair, key_value_delimiter, key_value_pairs)) {
// Don't return here, to allow for pairs without associated
// value or key; just record that the split failed.
success = false;
}
}
return success;
}
std::vector<string16> SplitStringUsingSubstr(StringPiece16 input,
StringPiece16 delimiter,
WhitespaceHandling whitespace,
SplitResult result_type) {
return SplitStringUsingSubstrT<string16>(input, delimiter, whitespace,
result_type);
}
std::vector<std::string> SplitStringUsingSubstr(StringPiece input,
StringPiece delimiter,
WhitespaceHandling whitespace,
SplitResult result_type) {
return SplitStringUsingSubstrT<std::string>(input, delimiter, whitespace,
result_type);
}
std::vector<StringPiece16> SplitStringPieceUsingSubstr(
StringPiece16 input,
StringPiece16 delimiter,
WhitespaceHandling whitespace,
SplitResult result_type) {
std::vector<StringPiece16> result;
return SplitStringUsingSubstrT<StringPiece16>(input, delimiter, whitespace,
result_type);
}
std::vector<StringPiece> SplitStringPieceUsingSubstr(
StringPiece input,
StringPiece delimiter,
WhitespaceHandling whitespace,
SplitResult result_type) {
return SplitStringUsingSubstrT<StringPiece>(input, delimiter, whitespace,
result_type);
}
#if defined(OS_WIN) && defined(BASE_STRING16_IS_STD_U16STRING)
std::vector<std::wstring> SplitString(WStringPiece input,
WStringPiece separators,
WhitespaceHandling whitespace,
SplitResult result_type) {
return SplitStringT<std::wstring>(input, separators, whitespace, result_type);
}
std::vector<WStringPiece> SplitStringPiece(WStringPiece input,
WStringPiece separators,
WhitespaceHandling whitespace,
SplitResult result_type) {
return SplitStringT<WStringPiece>(input, separators, whitespace, result_type);
}
std::vector<std::wstring> SplitStringUsingSubstr(WStringPiece input,
WStringPiece delimiter,
WhitespaceHandling whitespace,
SplitResult result_type) {
return SplitStringUsingSubstrT<std::wstring>(input, delimiter, whitespace,
result_type);
}
std::vector<WStringPiece> SplitStringPieceUsingSubstr(
WStringPiece input,
WStringPiece delimiter,
WhitespaceHandling whitespace,
SplitResult result_type) {
return SplitStringUsingSubstrT<WStringPiece>(input, delimiter, whitespace,
result_type);
}
#endif
} // namespace base
|