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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/base/template_expressions.h"
6
7 #include "base/logging.h"
8 #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.
9
10 namespace ui {
11
12 std::string ReplaceTemplateExpressions(
13 base::StringPiece format_string,
14 const std::map<base::StringPiece, std::string>& substitutions) {
15 std::string formatted;
16 const size_t kValueLengthGuess = 16;
17 formatted.reserve(format_string.length() +
18 substitutions.size() * kValueLengthGuess);
19 base::StringPiece::const_iterator i = format_string.begin();
20 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
21 if (*i == '$' && i < format_string.end() && i[1] == '{') {
22 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.
23 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.
24 const auto& replacement = substitutions.find(
25 base::StringPiece(format_string.begin() + offset, length));
26 if (replacement != substitutions.end()) {
27 formatted.append(replacement->second);
28 i += length + 3;
29 continue;
30 }
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.
31 }
32 formatted.push_back(*i);
33 ++i;
34 }
35 return formatted;
36 }
37
38 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698