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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: ui/base/template_expressions.cc
diff --git a/ui/base/template_expressions.cc b/ui/base/template_expressions.cc
new file mode 100644
index 0000000000000000000000000000000000000000..43cfa53bc0256d2abb5dd5b0cefef01aa7413446
--- /dev/null
+++ b/ui/base/template_expressions.cc
@@ -0,0 +1,42 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/base/template_expressions.h"
+
+#include "base/logging.h"
+
+namespace ui {
+
+std::string ReplaceTemplateExpressions(
+ base::StringPiece format_string,
+ const std::map<base::StringPiece, std::string>& 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 + 1 < format_string.end() && i[1] == '{') {
+ size_t key_start = i + strlen("${") - format_string.begin();
+ size_t key_length = format_string.find('}', key_start);
+ if (key_length == base::StringPiece::npos)
+ NOTREACHED() << "TemplateExpression missing ending brace '}'";
+ key_length -= key_start;
+ base::StringPiece key(format_string.begin() + key_start, key_length);
+ const auto& replacement = substitutions.find(key);
+ if (replacement != substitutions.end()) {
+ formatted.append(replacement->second);
+ i += strlen("${") + key_length + strlen("}");
+ continue;
+ } else {
+ NOTREACHED() << "TemplateExpression key not found: " << key;
+ }
+ }
+ formatted.push_back(*i);
+ ++i;
+ }
+ return formatted;
+}
+
+} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698