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

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: Fix for incognito window 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..b0c5194aa3fd32d51ccb7c075e49456c0a420452
--- /dev/null
+++ b/ui/base/template_expressions.cc
@@ -0,0 +1,113 @@
+// 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<FormatStringType, OutStringType>& substitutions) {
+ OutStringType formatted;
+ const size_t kValueLengthGuess = 16;
+ formatted.reserve(format_string.length() +
+ substitutions.size() * kValueLengthGuess);
+#if 1
Dan Beam 2015/07/14 17:14:35 I understand you're asking for feedback, but I wou
dschuyler 2015/07/14 21:05:30 Acknowledged.
+ size_t copy_begin = 0;
+ size_t format_string_length = format_string.length();
+ while (copy_begin < format_string_length) {
+ size_t copy_end = format_string.find('$', copy_begin);
+ if (copy_end == FormatStringType::npos) {
+ formatted.append(format_string.data(), copy_begin,
+ format_string.length() - copy_begin);
+ break;
+ }
+ formatted.append(format_string.data(), copy_begin, copy_end - copy_begin);
+ copy_begin = copy_end + 1;
+ if (format_string[copy_begin] == '{') {
+ ++copy_begin;
+ size_t key_end = format_string.find('}', copy_begin);
+ if (copy_end == FormatStringType::npos)
+ break;
+ // else
+ // NOTREACHED() << "Missing template expression end brace '}'";
+ FormatStringType key =
+ format_string.substr(copy_begin, key_end - copy_begin);
+ const auto& replacement = substitutions.find(key);
+ copy_begin = key_end + 1;
+ if (replacement != substitutions.end())
+ formatted.append(replacement->second);
+ // else
+ // NOTREACHED() << "Missing template expression " << key;
+ } else if (format_string[copy_begin] == '$') {
+ copy_end = format_string.find_first_not_of('$', copy_begin);
+ formatted.append(format_string.data(), copy_begin, copy_end - copy_begin);
+ copy_begin = copy_end;
+ }
+ }
+#else
+ typename FormatStringType::const_iterator i = format_string.begin();
+ while (i < format_string.end()) {
+ if (*i == '$') {
+ ++i;
+ if (i < format_string.end()) {
+ if (*i == '{') {
+ ++i;
+ OutStringType 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()) {
+ formatted.append(replacement->second);
+ } else {
+ // NOTREACHED() << "Missing template expression " << index;
Dan Beam 2015/07/14 17:14:35 I think this means it'd only blow up in debug. do
dschuyler 2015/07/14 21:05:30 Who should we ask?
+ }
+ } else if (*i == '$') {
+ while (i < format_string.end() && *i == '$') {
+ formatted.push_back('$');
+ ++i;
+ }
+ }
+ }
+ } else {
+ formatted.push_back(*i);
+ ++i;
+ }
+ }
Dan Beam 2015/07/14 17:14:35 I find this second one more readable, I think Nico
dschuyler 2015/07/14 21:05:30 Acknowledged.
+#endif
+ return formatted;
+}
+
+} // namespace
+
+namespace ui {
+
+std::string ReplaceTemplateExpressions(
+ base::StringPiece format_string,
+ const std::map<base::StringPiece, std::string>& substitutions) {
+ return DoReplaceTemplateExpressions(format_string, substitutions);
+}
+
+base::string16 ReplaceTemplateExpressions(
+ const base::string16& format_string,
+ const std::map<base::string16, base::string16>& substitutions) {
+ return DoReplaceTemplateExpressions(format_string, substitutions);
+}
+
+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