| Index: base/strings/string_util.cc
|
| diff --git a/base/strings/string_util.cc b/base/strings/string_util.cc
|
| index e3dcd8542fabcc263cf26f23014669097e9c3ba0..be08f18a541957db1e45570deda14756422e5e8e 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"
|
| @@ -860,11 +861,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) {
|
| + 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) {
|
|
|