summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorbrettw <brettw@chromium.org>2015-08-13 15:10:03 -0700
committerCommit bot <commit-bot@chromium.org>2015-08-13 22:10:30 +0000
commitce0fbef478ffe95f41e76bf6e85ff6b0072002ba (patch)
tree414662ba3815d8553e999c02f2f11023ab464b2d /base
parent66d74d497c608b8b3349597d2c82dea030c0c8be (diff)
downloadchromium_src-ce0fbef478ffe95f41e76bf6e85ff6b0072002ba.zip
chromium_src-ce0fbef478ffe95f41e76bf6e85ff6b0072002ba.tar.gz
chromium_src-ce0fbef478ffe95f41e76bf6e85ff6b0072002ba.tar.bz2
Updates to StringSplit implementation
Remove the deprecated StringSplit implementation. The header declarations and all external uses were already removed, but the actual implementation was not removed. It was still used by SplitStringIntoKeyValuePairs which is now updated to use the new version. Both the key/value splitter and the substring variants have been updated to avoid many intermediate copies. Updates the name of a trim whitespace variant in string_util to match the header. This is currently unused but was provided for symmetry before. Review URL: https://codereview.chromium.org/1291103002 Cr-Commit-Position: refs/heads/master@{#343286}
Diffstat (limited to 'base')
-rw-r--r--base/strings/string_split.cc140
-rw-r--r--base/strings/string_split.h14
-rw-r--r--base/strings/string_util.cc4
3 files changed, 62 insertions, 96 deletions
diff --git a/base/strings/string_split.cc b/base/strings/string_split.cc
index b01ca66..4253e2f 100644
--- a/base/strings/string_split.cc
+++ b/base/strings/string_split.cc
@@ -99,52 +99,57 @@ static std::vector<OutputStringType> SplitStringT(
return result;
}
-bool SplitStringIntoKeyValue(const std::string& line,
- char key_value_delimiter,
- std::string* key,
- std::string* value) {
- key->clear();
- value->clear();
+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 = line.find_first_of(key_value_delimiter);
+ size_t end_key_pos = input.find_first_of(delimiter);
if (end_key_pos == std::string::npos) {
- DVLOG(1) << "cannot find delimiter in: " << line;
- return false; // no delimiter
+ DVLOG(1) << "cannot find delimiter in: " << input;
+ return false; // No delimiter.
}
- key->assign(line, 0, end_key_pos);
+ input.substr(0, end_key_pos).CopyToString(&result_pair.first);
// Find the value string.
- std::string remains(line, end_key_pos, line.size() - end_key_pos);
- size_t begin_value_pos = remains.find_first_not_of(key_value_delimiter);
- if (begin_value_pos == std::string::npos) {
- DVLOG(1) << "cannot parse value from line: " << line;
- return false; // no value
+ 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.
}
- value->assign(remains, begin_value_pos, remains.size() - begin_value_pos);
+ remains.substr(begin_value_pos, remains.size() - begin_value_pos)
+ .CopyToString(&result_pair.second);
+
return true;
}
-template <typename STR>
-void SplitStringUsingSubstrT(const STR& str,
- const STR& s,
- std::vector<STR>* r) {
- r->clear();
- typename STR::size_type begin_index = 0;
+template <typename Str>
+void SplitStringUsingSubstrT(BasicStringPiece<Str> input,
+ BasicStringPiece<Str> delimiter,
+ std::vector<Str>* result) {
+ using Piece = BasicStringPiece<Str>;
+ using size_type = typename Piece::size_type;
+
+ result->clear();
+ size_type begin_index = 0;
while (true) {
- const typename STR::size_type end_index = str.find(s, begin_index);
- if (end_index == STR::npos) {
- const STR term = str.substr(begin_index);
- STR tmp;
- TrimWhitespace(term, TRIM_ALL, &tmp);
- r->push_back(tmp);
+ size_type end_index = input.find(delimiter, begin_index);
+ if (end_index == Piece::npos) {
+ // No delimiter, use the rest of the string.
+ Piece term = TrimString(input.substr(begin_index),
+ WhitespaceForType<Str>(), TRIM_ALL);
+ result->push_back(term.as_string());
return;
}
- const STR term = str.substr(begin_index, end_index - begin_index);
- STR tmp;
- TrimWhitespace(term, TRIM_ALL, &tmp);
- r->push_back(tmp);
- begin_index = end_index + s.size();
+ Piece term = TrimString(input.substr(begin_index, end_index - begin_index),
+ WhitespaceForType<Str>(), TRIM_ALL);
+ result->push_back(term.as_string());
+ begin_index = end_index + delimiter.size();
}
}
@@ -198,77 +203,38 @@ std::vector<StringPiece16> SplitStringPiece(StringPiece16 input,
input, separators, whitespace, result_type);
}
-void SplitString(const string16& str,
- char16 c,
- std::vector<string16>* result) {
- DCHECK(CBU16_IS_SINGLE(c));
- *result = SplitStringT<string16, string16, char16>(
- str, c, TRIM_WHITESPACE, SPLIT_WANT_ALL);
-
- // Backward-compat hack: The old SplitString implementation would keep
- // empty substrings, for example:
- // "a,,b" -> ["a", "", "b"]
- // "a, ,b" -> ["a", "", "b"]
- // which the current code also does. But the old one would discard them when
- // the only result was that empty string:
- // " " -> []
- // In the latter case, our new code will give [""]
- if (result->size() == 1 && (*result)[0].empty())
- result->clear();
-}
-
-void SplitString(const std::string& str,
- char c,
- std::vector<std::string>* result) {
-#if CHAR_MIN < 0
- DCHECK_GE(c, 0);
-#endif
- DCHECK_LT(c, 0x7F);
- *result = SplitStringT<std::string, std::string, char>(
- str, c, TRIM_WHITESPACE, SPLIT_WANT_ALL);
-
- // Backward-compat hack, see above.
- if (result->size() == 1 && (*result)[0].empty())
- result->clear();
-}
-
-bool SplitStringIntoKeyValuePairs(const std::string& line,
+bool SplitStringIntoKeyValuePairs(StringPiece input,
char key_value_delimiter,
char key_value_pair_delimiter,
StringPairs* key_value_pairs) {
key_value_pairs->clear();
- std::vector<std::string> pairs;
- SplitString(line, key_value_pair_delimiter, &pairs);
+ std::vector<StringPiece> pairs = SplitStringPiece(
+ input, std::string(1, key_value_pair_delimiter),
+ TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
+ key_value_pairs->reserve(pairs.size());
bool success = true;
- for (size_t i = 0; i < pairs.size(); ++i) {
- // Don't add empty pairs into the result.
- if (pairs[i].empty())
- continue;
-
- std::string key;
- std::string value;
- if (!SplitStringIntoKeyValue(pairs[i], key_value_delimiter, &key, &value)) {
+ 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;
}
- key_value_pairs->push_back(make_pair(key, value));
}
return success;
}
-void SplitStringUsingSubstr(const string16& str,
- const string16& s,
- std::vector<string16>* r) {
- SplitStringUsingSubstrT(str, s, r);
+void SplitStringUsingSubstr(StringPiece16 input,
+ StringPiece16 delimiter,
+ std::vector<string16>* result) {
+ SplitStringUsingSubstrT(input, delimiter, result);
}
-void SplitStringUsingSubstr(const std::string& str,
- const std::string& s,
- std::vector<std::string>* r) {
- SplitStringUsingSubstrT(str, s, r);
+void SplitStringUsingSubstr(StringPiece input,
+ StringPiece delimiter,
+ std::vector<std::string>* result) {
+ SplitStringUsingSubstrT(input, delimiter, result);
}
} // namespace base
diff --git a/base/strings/string_split.h b/base/strings/string_split.h
index 3eed974..2a0c795 100644
--- a/base/strings/string_split.h
+++ b/base/strings/string_split.h
@@ -83,7 +83,7 @@ using StringPairs = std::vector<std::pair<std::string, std::string>>;
// removes whitespace leading each key and trailing each value. Returns true
// only if each pair has a non-empty key and value. |key_value_pairs| will
// include ("","") pairs for entries without |key_value_delimiter|.
-BASE_EXPORT bool SplitStringIntoKeyValuePairs(const std::string& line,
+BASE_EXPORT bool SplitStringIntoKeyValuePairs(StringPiece input,
char key_value_delimiter,
char key_value_pair_delimiter,
StringPairs* key_value_pairs);
@@ -94,12 +94,12 @@ BASE_EXPORT bool SplitStringIntoKeyValuePairs(const std::string& line,
// TODO(brettw) this should probably be changed and expanded to provide a
// mirror of the SplitString[Piece] API above, just with the different
// delimiter handling.
-BASE_EXPORT void SplitStringUsingSubstr(const string16& str,
- const string16& s,
- std::vector<string16>* r);
-BASE_EXPORT void SplitStringUsingSubstr(const std::string& str,
- const std::string& s,
- std::vector<std::string>* r);
+BASE_EXPORT void SplitStringUsingSubstr(StringPiece16 input,
+ StringPiece16 delimiter,
+ std::vector<string16>* result);
+BASE_EXPORT void SplitStringUsingSubstr(StringPiece input,
+ StringPiece delimiter,
+ std::vector<std::string>* result);
} // namespace base
diff --git a/base/strings/string_util.cc b/base/strings/string_util.cc
index a389dc3..d7a1d54 100644
--- a/base/strings/string_util.cc
+++ b/base/strings/string_util.cc
@@ -400,8 +400,8 @@ TrimPositions TrimWhitespace(const string16& input,
return TrimStringT(input, StringPiece16(kWhitespaceUTF16), positions, output);
}
-StringPiece16 TrimWhitespaceASCII(StringPiece16 input,
- TrimPositions positions) {
+StringPiece16 TrimWhitespace(StringPiece16 input,
+ TrimPositions positions) {
return TrimStringPieceT(input, StringPiece16(kWhitespaceUTF16), positions);
}