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..1672fb6d3b6f6cfe881beafa46daab3cfd24ff4c |
| --- /dev/null |
| +++ b/ui/base/template_expressions.cc |
| @@ -0,0 +1,108 @@ |
| +// 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> |
| +OutStringType DoReplaceTemplateExpressions( |
| + const FormatStringType& format_string, |
| + const std::map<FormatStringType, OutStringType>& substitutions) { |
| + OutStringType formatted; |
| + const size_t kValueLengthGuess = 16; |
| + formatted.reserve(format_string.length() + |
| + substitutions.size() * kValueLengthGuess); |
| +#if 1 |
|
dschuyler
2015/07/14 16:45:36
Both the #if section and the #else section work pr
Nico
2015/07/14 21:44:18
I feel the first block is nicer, seems to be at a
dschuyler
2015/07/15 00:11:04
I'm ok with either version. I have a leaning towa
|
| + size_t copy_begin = 0; |
| + size_t format_string_length = format_string.length(); |
| + while (copy_begin < format_string_length) { |
| + size_t copy_end = format_string.find('$', copy_begin); |
| + formatted.append(format_string.data(), copy_begin, copy_end - copy_begin); |
| + if (copy_end == FormatStringType::npos) |
| + break; |
| + copy_begin = copy_end + 1; |
| + if (format_string[copy_begin] == '{') { |
| + ++copy_begin; |
| + size_t key_end = format_string.find('}', copy_begin); |
| + if (copy_end == FormatStringType::npos) |
| + break; |
| + FormatStringType key = |
| + format_string.substr(copy_begin, key_end - copy_begin); |
| + const auto& replacement = substitutions.find(key); |
| + copy_begin = key_end + 1; |
| + if (replacement != substitutions.end()) |
| + formatted.append(replacement->second); |
| + // else |
| + // NOTREACHED() << "Missing template expression " << key; |
|
dschuyler
2015/07/14 16:45:36
What is a good way to handle this case so that it
Nico
2015/07/14 21:44:18
Right Thing in what sense?
dschuyler
2015/07/15 00:11:04
Some of the unit test try things that will fail to
|
| + } else if (format_string[copy_begin] == '$') { |
| + copy_end = format_string.find_first_not_of('$', copy_begin); |
| + formatted.append(format_string.data(), copy_begin, copy_end - copy_begin); |
| + copy_begin = copy_end; |
| + } |
| + } |
| +#else |
| + typename FormatStringType::const_iterator i = format_string.begin(); |
| + while (i < format_string.end()) { |
| + if (*i == '$') { |
| + ++i; |
| + if (i < format_string.end()) { |
| + if (*i == '{') { |
| + ++i; |
| + OutStringType index; |
| + while (i < format_string.end()) { |
| + if (*i == '}') { |
| + ++i; |
| + break; |
| + } |
| + index.push_back(*i); |
| + ++i; |
| + } |
| + const auto& replacement = substitutions.find(index); |
| + if (replacement != substitutions.end()) { |
| + formatted.append(replacement->second); |
| + } else { |
| + // NOTREACHED() << "Missing template expression " << index; |
|
dschuyler
2015/07/14 16:45:36
What is a good way to handle this case so that it
|
| + } |
| + } else if (*i == '$') { |
| + while (i < format_string.end() && *i == '$') { |
| + formatted.push_back('$'); |
| + ++i; |
| + } |
| + } |
| + } |
| + } else { |
| + formatted.push_back(*i); |
| + ++i; |
| + } |
| + } |
| +#endif |
| + return formatted; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace ui { |
| + |
| +std::string ReplaceTemplateExpressions( |
| + base::StringPiece format_string, |
| + const std::map<base::StringPiece, std::string>& substitutions) { |
| + return DoReplaceTemplateExpressions(format_string, substitutions); |
| +} |
| + |
| +base::string16 ReplaceTemplateExpressions( |
| + const base::string16& format_string, |
| + const std::map<base::string16, base::string16>& substitutions) { |
| + return DoReplaceTemplateExpressions(format_string, substitutions); |
| +} |
| + |
| +std::string ReplaceTemplateExpressions( |
| + const std::string& format_string, |
| + const std::map<std::string, std::string>& substitutions) { |
| + return DoReplaceTemplateExpressions(format_string, substitutions); |
| +} |
| + |
| +} // namespace ui |