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

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: 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 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"
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 #if 0
20 size_t copy_begin = 0;
21 size_t format_string_length = format_string.length();
22 while (copy_begin < format_string_length) {
23 size_t copy_end = format_string.find('$', copy_begin);
24 if (copy_end == base::StringPiece::npos) {
25 formatted.append(format_string.data(), copy_begin,
26 format_string.length() - copy_begin);
27 break;
28 }
29 formatted.append(format_string.data(), copy_begin, copy_end - copy_begin);
30 copy_begin = copy_end + 1;
31 if (format_string[copy_begin] == '{') {
32 ++copy_begin;
33 size_t key_end = format_string.find('}', copy_begin);
34 if (copy_end == base::StringPiece::npos)
35 break;
36 // else
37 // NOTREACHED() << "Missing template expression end brace '}'";
38 base::StringPiece key =
39 format_string.substr(copy_begin, key_end - copy_begin);
40 const auto& replacement = substitutions.find(key);
41 copy_begin = key_end + 1;
42 if (replacement != substitutions.end())
43 formatted.append(replacement->second);
44 // else
45 // NOTREACHED() << "Missing template expression " << key;
46 } else if (format_string[copy_begin] == '$') {
47 copy_end = format_string.find_first_not_of('$', copy_begin);
48 formatted.append(format_string.data(), copy_begin, copy_end - copy_begin);
49 copy_begin = copy_end;
50 }
51 }
52 #else
53 typename base::StringPiece::const_iterator i = format_string.begin();
54 while (i < format_string.end()) {
55 if (*i == '$') {
56 ++i;
57 if (i < format_string.end()) {
58 if (*i == '{') {
59 ++i;
60 std::string index;
61 while (i < format_string.end()) {
62 if (*i == '}') {
63 ++i;
64 break;
65 }
66 index.push_back(*i);
67 ++i;
68 }
69 const auto& replacement = substitutions.find(index);
70 if (replacement != substitutions.end()) {
71 formatted.append(replacement->second);
72 } else {
73 // NOTREACHED() << "Missing template expression " << index;
74 }
75 } else if (*i == '$') {
76 while (i < format_string.end() && *i == '$') {
77 formatted.push_back('$');
78 ++i;
79 }
80 }
81 }
82 } else {
83 formatted.push_back(*i);
84 ++i;
85 }
86 }
87 #endif
88 return formatted;
89 }
90
91 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698