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

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: 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
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 ui { 11 namespace ui {
12 12
13 std::string ReplaceTemplateExpressions( 13 std::string ReplaceTemplateExpressions(
14 base::StringPiece format_string, 14 base::StringPiece format_string,
15 const std::map<base::StringPiece, std::string>& substitutions) { 15 const ReplaceTemplateMap& substitutions) {
16 const char* leader = "$i18n{";
Dan Beam 2016/01/20 22:34:38 const char kLeader[] = "$i18n{"; const size_t kLea
dschuyler 2016/01/21 18:43:53 Done.
16 std::string formatted; 17 std::string formatted;
17 const size_t kValueLengthGuess = 16; 18 const size_t kValueLengthGuess = 16;
18 formatted.reserve(format_string.length() + 19 formatted.reserve(format_string.length() +
19 substitutions.size() * kValueLengthGuess); 20 substitutions.size() * kValueLengthGuess);
20 base::StringPiece::const_iterator i = format_string.begin(); 21 base::StringPiece::const_iterator i = format_string.begin();
21 while (i < format_string.end()) { 22 while (i < format_string.end()) {
22 if (*i == '$' && i + 2 < format_string.end() && i[1] == '{' && 23 if (base::StringPiece(i).starts_with(leader)) {
23 i[2] != '}') { 24 size_t key_start = i + strlen(leader) - format_string.begin();
Dan Beam 2016/01/20 22:34:38 can we cache this strlen()?
dschuyler 2016/01/21 18:43:53 Done.
24 size_t key_start = i + strlen("${") - format_string.begin();
25 size_t key_length = format_string.find('}', key_start); 25 size_t key_length = format_string.find('}', key_start);
26 if (key_length == base::StringPiece::npos) 26 if (key_length == base::StringPiece::npos)
27 NOTREACHED() << "TemplateExpression missing ending brace '}'"; 27 NOTREACHED() << "TemplateExpression missing ending brace '}'";
28 key_length -= key_start; 28 key_length -= key_start;
29 base::StringPiece key(format_string.begin() + key_start, key_length); 29 std::string key(format_string.begin() + key_start, key_length);
30 const auto& replacement = substitutions.find(key); 30 const auto& replacement = substitutions.find(key);
31 if (replacement != substitutions.end()) { 31 if (replacement != substitutions.end()) {
32 formatted.append(replacement->second); 32 formatted.append(replacement->second);
33 i += strlen("${") + key_length + strlen("}"); 33 i += strlen(leader) + key_length + strlen("}");
Dan Beam 2016/01/20 22:34:38 and re-use the cached strlen here as well?
dschuyler 2016/01/21 18:43:53 Done.
34 continue; 34 continue;
35 } else { 35 } else {
36 NOTREACHED() << "TemplateExpression key not found: " << key; 36 NOTREACHED() << "TemplateExpression key not found: " << key;
37 } 37 }
38 } 38 }
39 formatted.push_back(*i); 39 formatted.push_back(*i);
40 ++i; 40 ++i;
41 } 41 }
42 return formatted; 42 return formatted;
43 } 43 }
44 44
45 } // namespace ui 45 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698