Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: ui/base/template_expressions.cc

Issue 1220793010: [ui/base;css] adding string template expression replacement (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review changes Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/template_expressions.h ('k') | ui/base/template_expressions_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "ui/base/template_expressions.h"
6
7 #include "base/logging.h"
8
9 namespace ui {
10
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 }
Dan Beam 2015/07/17 01:57:18 nit: no curlies
dschuyler 2015/07/17 02:31:39 Done.
26 key_length -= key_start;
27 base::StringPiece key(format_string.begin() + key_start, key_length);
28 const auto& replacement = substitutions.find(key);
29 if (replacement != substitutions.end()) {
30 formatted.append(replacement->second);
31 i += key_length + strlen("${}");
Dan Beam 2015/07/17 01:57:18 nit: i += strlen("${" + key_length + strlen("}")
dschuyler 2015/07/17 02:31:39 Done.
32 continue;
33 } else {
34 NOTREACHED() << "TemplateExpression key not found: " << key;
35 }
36 }
37 formatted.push_back(*i);
38 ++i;
39 }
40 return formatted;
41 }
42
43 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/template_expressions.h ('k') | ui/base/template_expressions_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698