| 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..ae0ce2856a1c643dab7adc16a4e96b6d93c62c1a
|
| --- /dev/null
|
| +++ b/ui/base/template_expressions.cc
|
| @@ -0,0 +1,56 @@
|
| +// 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"
|
| +#include "base/strings/utf_string_conversions.h"
|
| +
|
| +namespace ui {
|
| +
|
| +std::string ReplaceTemplateExpressions(
|
| + base::StringPiece format_string,
|
| + const std::map<base::StringPiece, std::string>& substitutions) {
|
| + std::string formatted;
|
| + const size_t kValueLengthGuess = 16;
|
| + formatted.reserve(format_string.length() +
|
| + substitutions.size() * kValueLengthGuess);
|
| + base::StringPiece::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()) {
|
| + 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;
|
| + }
|
| + } else if (*i == '$') {
|
| + while (i < format_string.end() && *i == '$') {
|
| + formatted.push_back('$');
|
| + ++i;
|
| + }
|
| + }
|
| + }
|
| + } else {
|
| + formatted.push_back(*i);
|
| + ++i;
|
| + }
|
| + }
|
| + return formatted;
|
| +}
|
| +
|
| +} // namespace ui
|
|
|