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 { | |
12 const char kLeader[] = "$i18n{"; | |
13 const size_t kLeaderSize = arraysize(kLeader) - 1; | |
14 const char kTail[] = "}"; | |
15 const size_t kTailSize = arraysize(kTail) - 1; | |
16 } // namespace | |
17 | |
18 namespace ui { | 11 namespace ui { |
19 | 12 |
20 std::string ReplaceTemplateExpressions( | 13 std::string ReplaceTemplateExpressions( |
21 base::StringPiece format_string, | 14 base::StringPiece format_string, |
22 const TemplateReplacements& substitutions) { | 15 const std::map<base::StringPiece, std::string>& substitutions) { |
23 std::string formatted; | 16 std::string formatted; |
24 const size_t kValueLengthGuess = 16; | 17 const size_t kValueLengthGuess = 16; |
25 formatted.reserve(format_string.length() + | 18 formatted.reserve(format_string.length() + |
26 substitutions.size() * kValueLengthGuess); | 19 substitutions.size() * kValueLengthGuess); |
27 base::StringPiece::const_iterator i = format_string.begin(); | 20 base::StringPiece::const_iterator i = format_string.begin(); |
28 while (i < format_string.end()) { | 21 while (i < format_string.end()) { |
29 if (base::StringPiece(i).starts_with(kLeader)) { | 22 if (*i == '$' && i + 2 < format_string.end() && i[1] == '{' && |
30 size_t key_start = i + kLeaderSize - format_string.begin(); | 23 i[2] != '}') { |
31 size_t key_length = format_string.find(kTail, key_start); | 24 size_t key_start = i + strlen("${") - format_string.begin(); |
| 25 size_t key_length = format_string.find('}', key_start); |
32 if (key_length == base::StringPiece::npos) | 26 if (key_length == base::StringPiece::npos) |
33 NOTREACHED() << "TemplateExpression missing ending tag"; | 27 NOTREACHED() << "TemplateExpression missing ending brace '}'"; |
34 key_length -= key_start; | 28 key_length -= key_start; |
35 std::string key(format_string.begin() + key_start, key_length); | 29 base::StringPiece key(format_string.begin() + key_start, key_length); |
36 DCHECK(!key.empty()); | |
37 const auto& replacement = substitutions.find(key); | 30 const auto& replacement = substitutions.find(key); |
38 if (replacement != substitutions.end()) { | 31 if (replacement != substitutions.end()) { |
39 formatted.append(replacement->second); | 32 formatted.append(replacement->second); |
40 i += kLeaderSize + key_length + kTailSize; | 33 i += strlen("${") + key_length + strlen("}"); |
41 continue; | 34 continue; |
42 } else { | 35 } else { |
43 NOTREACHED() << "TemplateExpression key not found: " << key; | 36 NOTREACHED() << "TemplateExpression key not found: " << key; |
44 } | 37 } |
45 } | 38 } |
46 formatted.push_back(*i); | 39 formatted.push_back(*i); |
47 ++i; | 40 ++i; |
48 } | 41 } |
49 return formatted; | 42 return formatted; |
50 } | 43 } |
51 | 44 |
52 } // namespace ui | 45 } // namespace ui |
OLD | NEW |