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

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: Alternate template expression replacement, using find 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 {
10
11 template <class FormatStringType, class OutStringType>
12 OutStringType DoReplaceTemplateExpressions(
13 const FormatStringType& format_string,
14 const std::map<FormatStringType, OutStringType>& substitutions) {
15 OutStringType formatted;
16 const size_t kValueLengthGuess = 16;
17 formatted.reserve(format_string.length() +
18 substitutions.size() * kValueLengthGuess);
19 #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
20 size_t copy_begin = 0;
21 size_t format_string_length = format_string.length();
22 while (copy_begin < format_string_length) {
23 size_t copy_end = format_string.find('$', copy_begin);
24 formatted.append(format_string.data(), copy_begin, copy_end - copy_begin);
25 if (copy_end == FormatStringType::npos)
26 break;
27 copy_begin = copy_end + 1;
28 if (format_string[copy_begin] == '{') {
29 ++copy_begin;
30 size_t key_end = format_string.find('}', copy_begin);
31 if (copy_end == FormatStringType::npos)
32 break;
33 FormatStringType key =
34 format_string.substr(copy_begin, key_end - copy_begin);
35 const auto& replacement = substitutions.find(key);
36 copy_begin = key_end + 1;
37 if (replacement != substitutions.end())
38 formatted.append(replacement->second);
39 // else
40 // 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
41 } else if (format_string[copy_begin] == '$') {
42 copy_end = format_string.find_first_not_of('$', copy_begin);
43 formatted.append(format_string.data(), copy_begin, copy_end - copy_begin);
44 copy_begin = copy_end;
45 }
46 }
47 #else
48 typename FormatStringType::const_iterator i = format_string.begin();
49 while (i < format_string.end()) {
50 if (*i == '$') {
51 ++i;
52 if (i < format_string.end()) {
53 if (*i == '{') {
54 ++i;
55 OutStringType index;
56 while (i < format_string.end()) {
57 if (*i == '}') {
58 ++i;
59 break;
60 }
61 index.push_back(*i);
62 ++i;
63 }
64 const auto& replacement = substitutions.find(index);
65 if (replacement != substitutions.end()) {
66 formatted.append(replacement->second);
67 } else {
68 // NOTREACHED() << "Missing template expression " << index;
dschuyler 2015/07/14 16:45:36 What is a good way to handle this case so that it
69 }
70 } else if (*i == '$') {
71 while (i < format_string.end() && *i == '$') {
72 formatted.push_back('$');
73 ++i;
74 }
75 }
76 }
77 } else {
78 formatted.push_back(*i);
79 ++i;
80 }
81 }
82 #endif
83 return formatted;
84 }
85
86 } // namespace
87
88 namespace ui {
89
90 std::string ReplaceTemplateExpressions(
91 base::StringPiece format_string,
92 const std::map<base::StringPiece, std::string>& substitutions) {
93 return DoReplaceTemplateExpressions(format_string, substitutions);
94 }
95
96 base::string16 ReplaceTemplateExpressions(
97 const base::string16& format_string,
98 const std::map<base::string16, base::string16>& substitutions) {
99 return DoReplaceTemplateExpressions(format_string, substitutions);
100 }
101
102 std::string ReplaceTemplateExpressions(
103 const std::string& format_string,
104 const std::map<std::string, std::string>& substitutions) {
105 return DoReplaceTemplateExpressions(format_string, substitutions);
106 }
107
108 } // 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