Chromium Code Reviews| Index: base/strings/string_util.cc |
| diff --git a/base/strings/string_util.cc b/base/strings/string_util.cc |
| index e2b7311636f166338d1893a9b2932a193765d5d0..e537d4ec188db140f43686334b241bea28fe2239 100644 |
| --- a/base/strings/string_util.cc |
| +++ b/base/strings/string_util.cc |
| @@ -16,6 +16,7 @@ |
| #include <wctype.h> |
| #include <algorithm> |
| +#include <map> |
| #include <vector> |
| #include "base/basictypes.h" |
| @@ -869,11 +870,55 @@ string16 JoinString(const std::vector<string16>& parts, |
| return JoinStringT(parts, separator); |
| } |
| -template<class FormatStringType, class OutStringType> |
| -OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string, |
| - const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) { |
| - size_t substitutions = subst.size(); |
| +template <class FormatStringType, class OutStringType> |
| +OutStringType DoReplaceStringPlaceholders( |
| + const FormatStringType& format_string, |
| + const std::map<std::string, OutStringType>& subst) { |
| + OutStringType formatted; |
| + const size_t kValueLengthGuess = 16; |
| + formatted.reserve(format_string.length() + subst.size() * kValueLengthGuess); |
| + for (typename FormatStringType::const_iterator i = format_string.begin(); |
| + i != format_string.end(); ++i) { |
|
Dan Beam
2015/07/07 02:23:31
please remove auto-increment, IMO
dschuyler
2015/07/07 23:05:30
Done.
|
| + if (*i == '$') { |
| + if (i + 1 != format_string.end()) { |
| + ++i; |
| + if (*i == '{') { |
| + std::string index; |
| + ++i; |
| + while (i != format_string.end() && *i != '}') { |
| + index.push_back(*i); |
| + ++i; |
| + } |
| + const auto& replacement = subst.find(index); |
| + if (replacement != subst.end()) |
| + formatted.append(replacement->second); |
| + } else if (*i == '$') { |
| + while (i != format_string.end() && *i == '$') { |
| + formatted.push_back('$'); |
| + ++i; |
| + } |
| + --i; |
| + } |
| + } |
| + } else { |
| + formatted.push_back(*i); |
| + } |
| + } |
| + return formatted; |
| +} |
| +std::string ReplaceStringPlaceholders( |
| + const std::string& format_string, |
| + const std::map<std::string, std::string>& subst) { |
| + return DoReplaceStringPlaceholders(format_string, subst); |
| +} |
| + |
| +template <class FormatStringType, class OutStringType> |
| +OutStringType DoReplaceStringPlaceholders( |
| + const FormatStringType& format_string, |
| + const std::vector<OutStringType>& subst, |
| + std::vector<size_t>* offsets) { |
| + size_t substitutions = subst.size(); |
| size_t sub_length = 0; |
| for (typename std::vector<OutStringType>::const_iterator iter = subst.begin(); |
| iter != subst.end(); ++iter) { |