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

Unified Diff: base/strings/string_split.cc

Issue 184233010: Cleanup SplitStringIntoKeyaluePairs implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased. Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/strings/string_split.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/strings/string_split.cc
diff --git a/base/strings/string_split.cc b/base/strings/string_split.cc
index 210789cd22d0df9b524d40aaeca002ad6e6fb705..7ef4760c58f1500d3b807cbcd96bb526db292d45 100644
--- a/base/strings/string_split.cc
+++ b/base/strings/string_split.cc
@@ -11,11 +11,13 @@
namespace base {
-template<typename STR>
-static void SplitStringT(const STR& str,
- const typename STR::value_type s,
- bool trim_whitespace,
- std::vector<STR>* r) {
+namespace {
+
+template <typename STR>
+void SplitStringT(const STR& str,
+ const typename STR::value_type s,
+ bool trim_whitespace,
+ std::vector<STR>* r) {
r->clear();
size_t last = 0;
size_t c = str.size();
@@ -33,87 +35,34 @@ static void SplitStringT(const STR& str,
}
}
-void SplitString(const string16& str,
- char16 c,
- std::vector<string16>* r) {
- DCHECK(CBU16_IS_SINGLE(c));
- SplitStringT(str, c, true, r);
-}
-
-void SplitString(const std::string& str,
- char c,
- std::vector<std::string>* r) {
-#if CHAR_MIN < 0
- DCHECK(c >= 0);
-#endif
- DCHECK(c < 0x7F);
- SplitStringT(str, c, true, r);
-}
-
-bool SplitStringIntoKeyValues(
- const std::string& line,
- char key_value_delimiter,
- std::string* key, std::vector<std::string>* values) {
+bool SplitStringIntoKeyValue(const std::string& line,
+ char key_value_delimiter,
+ std::string* key,
+ std::string* value) {
key->clear();
- values->clear();
+ value->clear();
- // Find the key string.
+ // Find the delimiter.
size_t end_key_pos = line.find_first_of(key_value_delimiter);
if (end_key_pos == std::string::npos) {
- DVLOG(1) << "cannot parse key from line: " << line;
- return false; // no key
+ DVLOG(1) << "cannot find delimiter in: " << line;
+ return false; // no delimiter
}
key->assign(line, 0, end_key_pos);
- // Find the values string.
+ // Find the value string.
std::string remains(line, end_key_pos, line.size() - end_key_pos);
- size_t begin_values_pos = remains.find_first_not_of(key_value_delimiter);
- if (begin_values_pos == std::string::npos) {
+ 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
}
- std::string values_string(remains, begin_values_pos,
- remains.size() - begin_values_pos);
-
- // Construct the values vector.
- values->push_back(values_string);
+ value->assign(remains, begin_value_pos, remains.size() - begin_value_pos);
return true;
}
-bool SplitStringIntoKeyValuePairs(const std::string& line,
- 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);
-
- bool success = true;
- for (size_t i = 0; i < pairs.size(); ++i) {
- // Empty pair. SplitStringIntoKeyValues is more strict about an empty pair
- // line, so continue with the next pair.
- if (pairs[i].empty())
- continue;
-
- std::string key;
- std::vector<std::string> value;
- if (!SplitStringIntoKeyValues(pairs[i],
- key_value_delimiter,
- &key, &value)) {
- // Don't return here, to allow for keys without associated
- // values; just record that our split failed.
- success = false;
- }
- DCHECK_LE(value.size(), 1U);
- key_value_pairs->push_back(
- make_pair(key, value.empty() ? std::string() : value[0]));
- }
- return success;
-}
-
template <typename STR>
-static void SplitStringUsingSubstrT(const STR& str,
+void SplitStringUsingSubstrT(const STR& str,
const STR& s,
std::vector<STR>* r) {
r->clear();
@@ -135,36 +84,6 @@ static void SplitStringUsingSubstrT(const STR& str,
}
}
-void SplitStringUsingSubstr(const string16& str,
- const string16& s,
- std::vector<string16>* r) {
- SplitStringUsingSubstrT(str, s, r);
-}
-
-void SplitStringUsingSubstr(const std::string& str,
- const std::string& s,
- std::vector<std::string>* r) {
- SplitStringUsingSubstrT(str, s, r);
-}
-
-void SplitStringDontTrim(const string16& str,
- char16 c,
- std::vector<string16>* r) {
- DCHECK(CBU16_IS_SINGLE(c));
- SplitStringT(str, c, false, r);
-}
-
-void SplitStringDontTrim(const std::string& str,
- char c,
- std::vector<std::string>* r) {
- DCHECK(IsStringUTF8(str));
-#if CHAR_MIN < 0
- DCHECK(c >= 0);
-#endif
- DCHECK(c < 0x7F);
- SplitStringT(str, c, false, r);
-}
-
template<typename STR>
void SplitStringAlongWhitespaceT(const STR& str, std::vector<STR>* result) {
result->clear();
@@ -206,6 +125,82 @@ void SplitStringAlongWhitespaceT(const STR& str, std::vector<STR>* result) {
}
}
+} // namespace
+
+void SplitString(const string16& str,
+ char16 c,
+ std::vector<string16>* r) {
+ DCHECK(CBU16_IS_SINGLE(c));
+ SplitStringT(str, c, true, r);
+}
+
+void SplitString(const std::string& str,
+ char c,
+ std::vector<std::string>* r) {
+#if CHAR_MIN < 0
+ DCHECK(c >= 0);
+#endif
+ DCHECK(c < 0x7F);
+ SplitStringT(str, c, true, r);
+}
+
+bool SplitStringIntoKeyValuePairs(const std::string& line,
+ 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);
+
+ 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)) {
+ // 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(const std::string& str,
+ const std::string& s,
+ std::vector<std::string>* r) {
+ SplitStringUsingSubstrT(str, s, r);
+}
+
+void SplitStringDontTrim(const string16& str,
+ char16 c,
+ std::vector<string16>* r) {
+ DCHECK(CBU16_IS_SINGLE(c));
+ SplitStringT(str, c, false, r);
+}
+
+void SplitStringDontTrim(const std::string& str,
+ char c,
+ std::vector<std::string>* r) {
+ DCHECK(IsStringUTF8(str));
+#if CHAR_MIN < 0
+ DCHECK(c >= 0);
+#endif
+ DCHECK(c < 0x7F);
+ SplitStringT(str, c, false, r);
+}
+
void SplitStringAlongWhitespace(const string16& str,
std::vector<string16>* result) {
SplitStringAlongWhitespaceT(str, result);
« no previous file with comments | « base/strings/string_split.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698