Index: base/strings/string_util.cc |
diff --git a/base/strings/string_util.cc b/base/strings/string_util.cc |
index cb668ed7fff4f60f76291a0a19d478fb9faaacd5..c342557433ab32fe0ad7a5815d9ed20bc9becc4b 100644 |
--- a/base/strings/string_util.cc |
+++ b/base/strings/string_util.cc |
@@ -64,6 +64,19 @@ static bool CompareParameter(const ReplacementOffset& elem1, |
return elem1.parameter < elem2.parameter; |
} |
+// Overloaded function to append one string onto the end of another. Having a |
+// separate overload for |source| as both stringpiece and string allows for more |
danakj
2017/02/22 15:10:45
StringPiece
Matt Giuca
2017/02/22 22:39:36
I was trying to consistently use lowercase "string
danakj
2017/02/22 22:40:30
SP16 implied imo. lowercase breaks case-sensitive
Matt Giuca
2017/02/23 04:48:06
Done.
|
+// efficient usage from functions templated to work with either type (avoiding a |
+// redundant call to the BasicStringPiece constructor in both cases). |
+template <typename STR> |
+inline void AppendToString(STR* target, const STR& source) { |
+ target->append(source); |
+} |
+template <typename STR> |
danakj
2017/02/22 15:10:45
style: blank line between functions
Matt Giuca
2017/02/23 04:48:06
Done.
|
+inline void AppendToString(STR* target, const BasicStringPiece<STR>& source) { |
+ source.AppendToString(target); |
+} |
+ |
// Assuming that a pointer is the size of a "machine word", then |
// uintptr_t is an integer type that is also a machine word. |
typedef uintptr_t MachineWord; |
@@ -853,19 +866,27 @@ char16* WriteInto(string16* str, size_t length_with_null) { |
return WriteIntoT(str, length_with_null); |
} |
-template<typename STR> |
-static STR JoinStringT(const std::vector<STR>& parts, |
- BasicStringPiece<STR> sep) { |
- if (parts.empty()) |
+// Generic version for all JoinString overloads. |LIST| must be a sequence |
+// (std::vector or std::initializer_list) of strings/stringpieces (std::string, |
+// string16, StringPiece or StringPiece16). |STR| is either std::string or |
+// string16. |
+template <typename LIST, typename STR> |
danakj
2017/02/22 15:10:45
It's not normal in chromium for template types to
Matt Giuca
2017/02/23 04:48:06
Done.
|
+static STR JoinStringT(const LIST& parts, BasicStringPiece<STR> sep) { |
+ auto iter = parts.begin(); |
+ // Early-exit to avoid bad access to the first element. |
+ if (iter == parts.end()) |
return STR(); |
- STR result(parts[0]); |
- auto iter = parts.begin(); |
+ // Begin constructing the result from the first element. |
+ STR result(iter->data(), iter->size()); |
++iter; |
for (; iter != parts.end(); ++iter) { |
sep.AppendToString(&result); |
- result += *iter; |
+ // Using the overloaded AppendToString allows this template function to work |
+ // on both strings and stringpieces without creating an intermediate |
danakj
2017/02/22 15:10:46
StringPieces
Matt Giuca
2017/02/23 04:48:06
Done.
|
+ // stringpiece object. |
danakj
2017/02/22 15:10:45
StringPiece
Matt Giuca
2017/02/23 04:48:06
Done.
|
+ AppendToString(&result, *iter); |
} |
return result; |
@@ -881,6 +902,26 @@ string16 JoinString(const std::vector<string16>& parts, |
return JoinStringT(parts, separator); |
} |
+std::string JoinString(const std::vector<StringPiece>& parts, |
+ StringPiece separator) { |
+ return JoinStringT(parts, separator); |
+} |
+ |
+string16 JoinString(const std::vector<StringPiece16>& parts, |
+ StringPiece16 separator) { |
+ return JoinStringT(parts, separator); |
+} |
+ |
+std::string JoinString(const std::initializer_list<StringPiece>& parts, |
+ StringPiece separator) { |
+ return JoinStringT(parts, separator); |
+} |
+ |
+string16 JoinString(const std::initializer_list<StringPiece16>& parts, |
+ StringPiece16 separator) { |
+ return JoinStringT(parts, separator); |
+} |
+ |
template<class FormatStringType, class OutStringType> |
OutStringType DoReplaceStringPlaceholders( |
const FormatStringType& format_string, |