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

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: better use of StringPiece in template expressions 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..3664db49e3b7a0fcbefc2e13394b919487c75450
--- /dev/null
+++ b/ui/base/template_expressions.cc
@@ -0,0 +1,38 @@
+// 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"
+#include "base/strings/utf_string_conversions.h"
Dan Beam 2015/07/16 00:21:44 ^ needed?
dschuyler 2015/07/16 00:33:18 Nope, thanks. Done.
+
+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()) {
Dan Beam 2015/07/15 21:56:09 why not format_string.find("${")?
dschuyler 2015/07/16 00:12:29 imo, it makes the code more complicated that it is
+ if (*i == '$' && i < format_string.end() && i[1] == '{') {
+ size_t offset = i + 2 - format_string.begin();
Dan Beam 2015/07/16 00:21:44 template_start or start
dschuyler 2015/07/16 00:33:18 Done.
+ size_t length = format_string.find('}', offset) - offset;
Dan Beam 2015/07/16 00:21:44 expression_key_size or key_size
dschuyler 2015/07/16 00:33:18 Acknowledged.
+ const auto& replacement = substitutions.find(
+ base::StringPiece(format_string.begin() + offset, length));
+ if (replacement != substitutions.end()) {
+ formatted.append(replacement->second);
+ i += length + 3;
+ continue;
+ }
Dan Beam 2015/07/16 00:21:44 where did we land on } else { NOTREACHED(); }
dschuyler 2015/07/16 00:33:18 The current code will leave ${unfound} in the resu
dschuyler 2015/07/16 02:08:42 I've added a NOTREACHED for unfound keys.
+ }
+ formatted.push_back(*i);
+ ++i;
+ }
+ return formatted;
+}
+
+} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698