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

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: Removing changes to base/strings 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..8d664de9c87ed482c6f24781f28d00fe9ea7d7cd
--- /dev/null
+++ b/ui/base/template_expressions.cc
@@ -0,0 +1,64 @@
+// 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 {
+
+template <class FormatStringType, class OutStringType>
+OutStringType DoReplaceTemplateExpressions(
+ const FormatStringType& format_string,
+ const std::map<std::string, OutStringType>& substitutions) {
+ OutStringType formatted;
+ const size_t kValueLengthGuess = 16;
+ formatted.reserve(format_string.length() +
+ substitutions.size() * kValueLengthGuess);
+ typename FormatStringType::const_iterator i = format_string.begin();
+ while (i < format_string.end()) {
+ if (*i == '$') {
+ ++i;
+ if (i < format_string.end()) {
+ if (*i == '{') {
+ ++i;
+ std::string index;
+ while (i < format_string.end()) {
+ if (*i == '}') {
+ ++i;
+ break;
+ }
+ index.push_back(*i);
+ ++i;
+ }
+ const auto& replacement = substitutions.find(index);
+ if (replacement != substitutions.end()) {
Dan Beam 2015/07/09 19:26:01 hmmmm, this should probably crash or something, IM
dschuyler 2015/07/14 00:56:17 Done.
+ formatted.append(replacement->second);
+ }
Dan Beam 2015/07/09 19:26:01 nit: no curlies
dschuyler 2015/07/14 00:56:17 Done.
+ } else if (*i == '$') {
+ while (i < format_string.end() && *i == '$') {
+ formatted.push_back('$');
+ ++i;
+ }
+ }
+ }
+ } else {
+ formatted.push_back(*i);
+ ++i;
+ }
+ }
+ return formatted;
+}
+
+} // namespace
+
+namespace ui {
+
+std::string ReplaceTemplateExpressions(
+ const std::string& format_string,
+ const std::map<std::string, std::string>& substitutions) {
+ return DoReplaceTemplateExpressions(format_string, substitutions);
+}
+
+} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698