Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
| 9 namespace { | |
| 10 | |
| 11 template <class FormatStringType, class OutStringType> | |
|
Nico
2015/07/09 19:57:45
This is only used with one string type, no need fo
dschuyler
2015/07/14 00:56:17
The intention was to have more than one. I've add
| |
| 12 OutStringType DoReplaceTemplateExpressions( | |
| 13 const FormatStringType& format_string, | |
| 14 const std::map<std::string, OutStringType>& substitutions) { | |
| 15 OutStringType formatted; | |
| 16 const size_t kValueLengthGuess = 16; | |
| 17 formatted.reserve(format_string.length() + | |
| 18 substitutions.size() * kValueLengthGuess); | |
| 19 typename FormatStringType::const_iterator i = format_string.begin(); | |
| 20 while (i < format_string.end()) { | |
| 21 if (*i == '$') { | |
| 22 ++i; | |
| 23 if (i < format_string.end()) { | |
| 24 if (*i == '{') { | |
| 25 ++i; | |
| 26 std::string index; | |
| 27 while (i < format_string.end()) { | |
|
Nico
2015/07/09 19:57:45
nit: I feel this can probably be simplified by usi
dschuyler
2015/07/14 00:56:17
I've mocked up an alternate version that leans mor
dschuyler
2015/07/15 21:40:44
Since I went back to the other version of the loop
| |
| 28 if (*i == '}') { | |
| 29 ++i; | |
| 30 break; | |
| 31 } | |
| 32 index.push_back(*i); | |
| 33 ++i; | |
| 34 } | |
| 35 const auto& replacement = substitutions.find(index); | |
| 36 if (replacement != substitutions.end()) { | |
| 37 formatted.append(replacement->second); | |
| 38 } | |
|
Nico
2015/07/09 19:57:46
should it be an error if you say ${foo} and foo do
dschuyler
2015/07/14 00:56:17
Done.
| |
| 39 } else if (*i == '$') { | |
| 40 while (i < format_string.end() && *i == '$') { | |
| 41 formatted.push_back('$'); | |
| 42 ++i; | |
| 43 } | |
| 44 } | |
| 45 } | |
| 46 } else { | |
| 47 formatted.push_back(*i); | |
| 48 ++i; | |
| 49 } | |
| 50 } | |
| 51 return formatted; | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 namespace ui { | |
| 57 | |
| 58 std::string ReplaceTemplateExpressions( | |
| 59 const std::string& format_string, | |
| 60 const std::map<std::string, std::string>& substitutions) { | |
| 61 return DoReplaceTemplateExpressions(format_string, substitutions); | |
| 62 } | |
| 63 | |
| 64 } // namespace ui | |
| OLD | NEW |