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

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

Issue 1585923011: [Template Expressions] changing ${ leader to $i18n{ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review changes Created 4 years, 11 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/base/template_expressions.h" 5 #include "ui/base/template_expressions.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 10
11 namespace {
12 const char kLeader[] = "$i18n{";
13 const size_t kLeaderSize = arraysize(kLeader) - 1;
14 const char kTail[] = "}";
15 const size_t kTailSize = arraysize(kTail) - 1;
Dan Beam 2016/01/23 03:31:55 nit: don't indent inside of namespaces
dschuyler 2016/01/25 22:19:11 Done.
16 } // namespace
17
11 namespace ui { 18 namespace ui {
12 19
13 std::string ReplaceTemplateExpressions( 20 std::string ReplaceTemplateExpressions(
14 base::StringPiece format_string, 21 base::StringPiece format_string,
15 const std::map<base::StringPiece, std::string>& substitutions) { 22 const TemplateReplacements& substitutions) {
16 std::string formatted; 23 std::string formatted;
17 const size_t kValueLengthGuess = 16; 24 const size_t kValueLengthGuess = 16;
18 formatted.reserve(format_string.length() + 25 formatted.reserve(format_string.length() +
19 substitutions.size() * kValueLengthGuess); 26 substitutions.size() * kValueLengthGuess);
20 base::StringPiece::const_iterator i = format_string.begin(); 27 base::StringPiece::const_iterator i = format_string.begin();
21 while (i < format_string.end()) { 28 while (i < format_string.end()) {
22 if (*i == '$' && i + 2 < format_string.end() && i[1] == '{' && 29 if (base::StringPiece(i).starts_with(kLeader)) {
23 i[2] != '}') { 30 size_t key_start = i + kLeaderSize - format_string.begin();
24 size_t key_start = i + strlen("${") - format_string.begin(); 31 size_t key_length = format_string.find(kTail, key_start);
25 size_t key_length = format_string.find('}', key_start);
26 if (key_length == base::StringPiece::npos) 32 if (key_length == base::StringPiece::npos)
27 NOTREACHED() << "TemplateExpression missing ending brace '}'"; 33 NOTREACHED() << "TemplateExpression missing ending tag";
28 key_length -= key_start; 34 key_length -= key_start;
29 base::StringPiece key(format_string.begin() + key_start, key_length); 35 std::string key(format_string.begin() + key_start, key_length);
36 DCHECK(!key.empty());
30 const auto& replacement = substitutions.find(key); 37 const auto& replacement = substitutions.find(key);
31 if (replacement != substitutions.end()) { 38 if (replacement != substitutions.end()) {
32 formatted.append(replacement->second); 39 formatted.append(replacement->second);
33 i += strlen("${") + key_length + strlen("}"); 40 i += kLeaderSize + key_length + kTailSize;
34 continue; 41 continue;
35 } else { 42 } else {
36 NOTREACHED() << "TemplateExpression key not found: " << key; 43 NOTREACHED() << "TemplateExpression key not found: " << key;
37 } 44 }
38 } 45 }
39 formatted.push_back(*i); 46 formatted.push_back(*i);
40 ++i; 47 ++i;
41 } 48 }
42 return formatted; 49 return formatted;
43 } 50 }
44 51
45 } // namespace ui 52 } // 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