Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / base / template_expressions.cc
blob43cfa53bc0256d2abb5dd5b0cefef01aa7413446
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/base/template_expressions.h"
7 #include "base/logging.h"
9 namespace ui {
11 std::string ReplaceTemplateExpressions(
12 base::StringPiece format_string,
13 const std::map<base::StringPiece, std::string>& substitutions) {
14 std::string formatted;
15 const size_t kValueLengthGuess = 16;
16 formatted.reserve(format_string.length() +
17 substitutions.size() * kValueLengthGuess);
18 base::StringPiece::const_iterator i = format_string.begin();
19 while (i < format_string.end()) {
20 if (*i == '$' && i + 1 < format_string.end() && i[1] == '{') {
21 size_t key_start = i + strlen("${") - format_string.begin();
22 size_t key_length = format_string.find('}', key_start);
23 if (key_length == base::StringPiece::npos)
24 NOTREACHED() << "TemplateExpression missing ending brace '}'";
25 key_length -= key_start;
26 base::StringPiece key(format_string.begin() + key_start, key_length);
27 const auto& replacement = substitutions.find(key);
28 if (replacement != substitutions.end()) {
29 formatted.append(replacement->second);
30 i += strlen("${") + key_length + strlen("}");
31 continue;
32 } else {
33 NOTREACHED() << "TemplateExpression key not found: " << key;
36 formatted.push_back(*i);
37 ++i;
39 return formatted;
42 } // namespace ui