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

Unified 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: fix for $i18n comment 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/base/template_expressions.cc
diff --git a/ui/base/template_expressions.cc b/ui/base/template_expressions.cc
index 1d63e0c34eac113f386f5a80d5817faa092a5270..40adddc98de9d3c2394bf1829de314dfc992c3e5 100644
--- a/ui/base/template_expressions.cc
+++ b/ui/base/template_expressions.cc
@@ -8,29 +8,36 @@
#include "base/logging.h"
+namespace {
+const char kLeader[] = "$i18n{";
+const size_t kLeaderSize = arraysize(kLeader) - 1;
+const char kTail[] = "}";
+const size_t kTailSize = arraysize(kTail) - 1;
+} // namespace
+
namespace ui {
std::string ReplaceTemplateExpressions(
base::StringPiece format_string,
- const std::map<base::StringPiece, std::string>& substitutions) {
+ const TemplateReplacements& substitutions) {
std::string formatted;
const size_t kValueLengthGuess = 16;
formatted.reserve(format_string.length() +
substitutions.size() * kValueLengthGuess);
base::StringPiece::const_iterator i = format_string.begin();
while (i < format_string.end()) {
- if (*i == '$' && i + 2 < format_string.end() && i[1] == '{' &&
- i[2] != '}') {
- size_t key_start = i + strlen("${") - format_string.begin();
- size_t key_length = format_string.find('}', key_start);
+ if (base::StringPiece(i).starts_with(kLeader)) {
+ size_t key_start = i + kLeaderSize - format_string.begin();
+ size_t key_length = format_string.find(kTail, key_start);
if (key_length == base::StringPiece::npos)
- NOTREACHED() << "TemplateExpression missing ending brace '}'";
+ NOTREACHED() << "TemplateExpression missing ending tag";
key_length -= key_start;
- base::StringPiece key(format_string.begin() + key_start, key_length);
+ std::string key(format_string.begin() + key_start, key_length);
+ DCHECK(!key.empty());
const auto& replacement = substitutions.find(key);
if (replacement != substitutions.end()) {
formatted.append(replacement->second);
- i += strlen("${") + key_length + strlen("}");
+ i += kLeaderSize + key_length + kTailSize;
continue;
} else {
NOTREACHED() << "TemplateExpression key not found: " << key;
« 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