Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/template_expressions.h" | 5 #include "ui/base/template_expressions.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 | 10 |
| 11 namespace ui { | 11 namespace ui { |
| 12 | 12 |
| 13 std::string ReplaceTemplateExpressions( | 13 std::string ReplaceTemplateExpressions( |
| 14 base::StringPiece format_string, | 14 base::StringPiece format_string, |
| 15 const std::map<base::StringPiece, std::string>& substitutions) { | 15 const TemplateReplacements& substitutions) { |
| 16 const char kLeader[] = "$i18n{"; | |
| 17 const size_t kLeaderSize = arraysize(kLeader) - 1; | |
|
Dan Beam
2016/01/22 00:51:44
wait, why "- 1"? should we just go back to strlen
Dan Beam
2016/01/22 00:51:44
if these are invariant, should they be static or i
dschuyler
2016/01/22 01:58:58
arraysize allows for initializing the value static
dschuyler
2016/01/22 01:58:58
Done.
| |
| 16 std::string formatted; | 18 std::string formatted; |
| 17 const size_t kValueLengthGuess = 16; | 19 const size_t kValueLengthGuess = 16; |
| 18 formatted.reserve(format_string.length() + | 20 formatted.reserve(format_string.length() + |
| 19 substitutions.size() * kValueLengthGuess); | 21 substitutions.size() * kValueLengthGuess); |
| 20 base::StringPiece::const_iterator i = format_string.begin(); | 22 base::StringPiece::const_iterator i = format_string.begin(); |
| 21 while (i < format_string.end()) { | 23 while (i < format_string.end()) { |
| 22 if (*i == '$' && i + 2 < format_string.end() && i[1] == '{' && | 24 if (base::StringPiece(i).starts_with(kLeader)) { |
| 23 i[2] != '}') { | 25 size_t key_start = i + kLeaderSize - format_string.begin(); |
| 24 size_t key_start = i + strlen("${") - format_string.begin(); | |
| 25 size_t key_length = format_string.find('}', key_start); | 26 size_t key_length = format_string.find('}', key_start); |
| 26 if (key_length == base::StringPiece::npos) | 27 if (key_length == base::StringPiece::npos) |
| 27 NOTREACHED() << "TemplateExpression missing ending brace '}'"; | 28 NOTREACHED() << "TemplateExpression missing ending brace '}'"; |
| 28 key_length -= key_start; | 29 key_length -= key_start; |
| 29 base::StringPiece key(format_string.begin() + key_start, key_length); | 30 std::string key(format_string.begin() + key_start, key_length); |
|
Dan Beam
2016/01/22 00:51:44
why are you changing the type of |key|?
Dan Beam
2016/01/22 00:51:44
DCHECK(!key.empty());
dschuyler
2016/01/22 01:58:58
In a near-future CL this code will be run in a bac
dschuyler
2016/01/22 01:58:58
Done.
| |
| 30 const auto& replacement = substitutions.find(key); | 31 const auto& replacement = substitutions.find(key); |
| 31 if (replacement != substitutions.end()) { | 32 if (replacement != substitutions.end()) { |
| 32 formatted.append(replacement->second); | 33 formatted.append(replacement->second); |
| 33 i += strlen("${") + key_length + strlen("}"); | 34 i += kLeaderSize + key_length + strlen("}"); |
| 34 continue; | 35 continue; |
| 35 } else { | 36 } else { |
| 36 NOTREACHED() << "TemplateExpression key not found: " << key; | 37 NOTREACHED() << "TemplateExpression key not found: " << key; |
| 37 } | 38 } |
| 38 } | 39 } |
| 39 formatted.push_back(*i); | 40 formatted.push_back(*i); |
| 40 ++i; | 41 ++i; |
| 41 } | 42 } |
| 42 return formatted; | 43 return formatted; |
| 43 } | 44 } |
| 44 | 45 |
| 45 } // namespace ui | 46 } // namespace ui |
| OLD | NEW |