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

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: removed string16 versions of template expansions 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..e207c1c4ccd9d6b7663a76572a1a4eb044d4a593
--- /dev/null
+++ b/ui/base/template_expressions.cc
@@ -0,0 +1,91 @@
+// 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"
+
+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);
+#if 0
+ 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 == base::StringPiece::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 == base::StringPiece::npos)
+ break;
+ // else
+ // NOTREACHED() << "Missing template expression end brace '}'";
+ base::StringPiece 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 base::StringPiece::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()) {
+ formatted.append(replacement->second);
+ } else {
+ // NOTREACHED() << "Missing template expression " << index;
+ }
+ } else if (*i == '$') {
+ while (i < format_string.end() && *i == '$') {
+ formatted.push_back('$');
+ ++i;
+ }
+ }
+ }
+ } else {
+ formatted.push_back(*i);
+ ++i;
+ }
+ }
+#endif
+ return formatted;
+}
+
+} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698