Chromium Code Reviews| Index: ui/base/template_expressions.cc |
| diff --git a/ui/base/template_expressions.cc b/ui/base/template_expressions.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8d664de9c87ed482c6f24781f28d00fe9ea7d7cd |
| --- /dev/null |
| +++ b/ui/base/template_expressions.cc |
| @@ -0,0 +1,64 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/base/template_expressions.h" |
| + |
| +#include "base/logging.h" |
| + |
| +namespace { |
| + |
| +template <class FormatStringType, class OutStringType> |
|
Nico
2015/07/09 19:57:45
This is only used with one string type, no need fo
dschuyler
2015/07/14 00:56:17
The intention was to have more than one. I've add
|
| +OutStringType DoReplaceTemplateExpressions( |
| + const FormatStringType& format_string, |
| + const std::map<std::string, OutStringType>& substitutions) { |
| + OutStringType formatted; |
| + const size_t kValueLengthGuess = 16; |
| + formatted.reserve(format_string.length() + |
| + substitutions.size() * kValueLengthGuess); |
| + typename FormatStringType::const_iterator i = format_string.begin(); |
| + while (i < format_string.end()) { |
| + if (*i == '$') { |
| + ++i; |
| + if (i < format_string.end()) { |
| + if (*i == '{') { |
| + ++i; |
| + std::string index; |
| + while (i < format_string.end()) { |
|
Nico
2015/07/09 19:57:45
nit: I feel this can probably be simplified by usi
dschuyler
2015/07/14 00:56:17
I've mocked up an alternate version that leans mor
dschuyler
2015/07/15 21:40:44
Since I went back to the other version of the loop
|
| + if (*i == '}') { |
| + ++i; |
| + break; |
| + } |
| + index.push_back(*i); |
| + ++i; |
| + } |
| + const auto& replacement = substitutions.find(index); |
| + if (replacement != substitutions.end()) { |
| + formatted.append(replacement->second); |
| + } |
|
Nico
2015/07/09 19:57:46
should it be an error if you say ${foo} and foo do
dschuyler
2015/07/14 00:56:17
Done.
|
| + } else if (*i == '$') { |
| + while (i < format_string.end() && *i == '$') { |
| + formatted.push_back('$'); |
| + ++i; |
| + } |
| + } |
| + } |
| + } else { |
| + formatted.push_back(*i); |
| + ++i; |
| + } |
| + } |
| + return formatted; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace ui { |
| + |
| +std::string ReplaceTemplateExpressions( |
| + const std::string& format_string, |
| + const std::map<std::string, std::string>& substitutions) { |
| + return DoReplaceTemplateExpressions(format_string, substitutions); |
| +} |
| + |
| +} // namespace ui |