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 ui { | |
| 10 | |
| 11 std::string ReplaceTemplateExpressions( | |
| 12 base::StringPiece format_string, | |
| 13 const std::map<base::StringPiece, std::string>& substitutions) { | |
| 14 std::string formatted; | |
| 15 const size_t kValueLengthGuess = 16; | |
| 16 formatted.reserve(format_string.length() + | |
| 17 substitutions.size() * kValueLengthGuess); | |
| 18 base::StringPiece::const_iterator i = format_string.begin(); | |
| 19 while (i < format_string.end()) { | |
| 20 if (*i == '$' && i < format_string.end() && i[1] == '{') { | |
|
Nico
2015/07/16 20:34:00
you want `i + 1 < format_string.end()` here I thin
dschuyler
2015/07/16 20:57:14
There already is a test case that would go past th
| |
| 21 size_t key_start = i + 2 - format_string.begin(); | |
|
Nico
2015/07/16 20:34:00
s/2/strlen("${")
dschuyler
2015/07/16 20:57:14
Done.
| |
| 22 size_t key_length = format_string.find('}', key_start) - key_start; | |
|
Nico
2015/07/16 20:34:00
find() returns npos on not found, which becomes a
dschuyler
2015/07/16 20:57:14
Done.
| |
| 23 base::StringPiece key(format_string.begin() + key_start, key_length); | |
| 24 const auto& replacement = substitutions.find(key); | |
| 25 if (replacement != substitutions.end()) { | |
| 26 formatted.append(replacement->second); | |
| 27 i += key_length + 3; | |
|
Nico
2015/07/16 20:34:00
s/3/strlen("${}")
dschuyler
2015/07/16 20:57:14
Done.
| |
| 28 continue; | |
| 29 } else { | |
| 30 NOTREACHED() << "TemplateExpression key not found: " << key; | |
| 31 } | |
| 32 } | |
| 33 formatted.push_back(*i); | |
| 34 ++i; | |
| 35 } | |
| 36 return formatted; | |
| 37 } | |
| 38 | |
| 39 } // namespace ui | |
| OLD | NEW |