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

Side by Side Diff: base/strings/string_split.cc

Issue 1549063003: Don't unnecessarily copy strings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 12 months 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
OLDNEW
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
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 std::vector<OutputStringType>* result) {
135 using Piece = BasicStringPiece<Str>; 135 using Piece = BasicStringPiece<Str>;
136 using size_type = typename Piece::size_type; 136 using size_type = typename Piece::size_type;
137 137
138 result->clear(); 138 result->clear();
139 size_type begin_index = 0; 139 size_type begin_index = 0;
140 while (true) { 140 while (true) {
141 size_type end_index = input.find(delimiter, begin_index); 141 size_type end_index = input.find(delimiter, begin_index);
142 if (end_index == Piece::npos) { 142 if (end_index == Piece::npos) {
143 // No delimiter, use the rest of the string. 143 // No delimiter, use the rest of the string.
144 Piece term = TrimString(input.substr(begin_index), 144 Piece term = TrimString(input.substr(begin_index),
145 WhitespaceForType<Str>(), TRIM_ALL); 145 WhitespaceForType<Str>(), TRIM_ALL);
146 result->push_back(term.as_string()); 146 result->push_back(PieceToOutputType<Str, OutputStringType>(term));
147 return; 147 return;
148 } 148 }
149 Piece term = TrimString(input.substr(begin_index, end_index - begin_index), 149 Piece term = TrimString(input.substr(begin_index, end_index - begin_index),
150 WhitespaceForType<Str>(), TRIM_ALL); 150 WhitespaceForType<Str>(), TRIM_ALL);
151 result->push_back(term.as_string()); 151 result->push_back(PieceToOutputType<Str, OutputStringType>(term));
152 begin_index = end_index + delimiter.size(); 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) {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 std::vector<string16>* result) { 230 std::vector<string16>* result) {
231 SplitStringUsingSubstrT(input, delimiter, result); 231 SplitStringUsingSubstrT(input, delimiter, result);
232 } 232 }
233 233
234 void SplitStringUsingSubstr(StringPiece input, 234 void SplitStringUsingSubstr(StringPiece input,
235 StringPiece delimiter, 235 StringPiece delimiter,
236 std::vector<std::string>* result) { 236 std::vector<std::string>* result) {
237 SplitStringUsingSubstrT(input, delimiter, result); 237 SplitStringUsingSubstrT(input, delimiter, result);
238 } 238 }
239 239
240 std::vector<StringPiece16> SplitStringPieceUsingSubstr(
241 StringPiece16 input,
242 StringPiece16 delimiter) {
mef 2015/12/28 17:29:55 Would it make sense to take a pointer to an output
brettw 2015/12/29 04:07:14 Return value optimization should take care of this
mef 2015/12/30 19:36:58 Acknowledged.
243 std::vector<StringPiece16> result;
244 SplitStringUsingSubstrT(input, delimiter, &result);
245 return result;
246 }
247
248 std::vector<StringPiece> SplitStringPieceUsingSubstr(StringPiece input,
249 StringPiece delimiter) {
250 std::vector<StringPiece> result;
251 SplitStringUsingSubstrT(input, delimiter, &result);
252 return result;
253 }
254
240 } // namespace base 255 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698