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 #include "base/values.h" | |
| 10 #include "net/base/escape.h" | 11 #include "net/base/escape.h" |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 const char kLeader[] = "$i18n"; | 14 const char kLeader[] = "$i18n"; |
| 14 const size_t kLeaderSize = arraysize(kLeader) - 1; | 15 const size_t kLeaderSize = arraysize(kLeader) - 1; |
| 15 const char kKeyOpen = '{'; | 16 const char kKeyOpen = '{'; |
| 16 const char kKeyClose = '}'; | 17 const char kKeyClose = '}'; |
| 17 } // namespace | 18 } // namespace |
| 18 | 19 |
| 19 namespace ui { | 20 namespace ui { |
| 20 | 21 |
| 22 void TemplateReplacementsFromDictionaryValue( | |
| 23 const base::DictionaryValue& dictionary, | |
| 24 TemplateReplacements* replacements) { | |
| 25 for (base::DictionaryValue::Iterator it(dictionary); !it.IsAtEnd(); | |
| 26 it.Advance()) { | |
| 27 if (it.value().IsType(base::Value::Type::STRING)) { | |
|
Dan Beam
2017/01/04 22:41:51
might be worth writing a small test for this (but
dschuyler
2017/01/04 23:18:29
Acknowledged.
| |
| 28 std::string str_value; | |
| 29 if (it.value().GetAsString(&str_value)) | |
| 30 (*replacements)[it.key()] = str_value; | |
| 31 } | |
| 32 } | |
| 33 } | |
| 34 | |
| 21 std::string ReplaceTemplateExpressions( | 35 std::string ReplaceTemplateExpressions( |
| 22 base::StringPiece source, | 36 base::StringPiece source, |
| 23 const TemplateReplacements& replacements) { | 37 const TemplateReplacements& replacements) { |
| 24 std::string formatted; | 38 std::string formatted; |
| 25 const size_t kValueLengthGuess = 16; | 39 const size_t kValueLengthGuess = 16; |
| 26 formatted.reserve(source.length() + replacements.size() * kValueLengthGuess); | 40 formatted.reserve(source.length() + replacements.size() * kValueLengthGuess); |
| 27 // Two position markers are used as cursors through the |source|. | 41 // Two position markers are used as cursors through the |source|. |
| 28 // The |current_pos| will follow behind |next_pos|. | 42 // The |current_pos| will follow behind |next_pos|. |
| 29 size_t current_pos = 0; | 43 size_t current_pos = 0; |
| 30 while (true) { | 44 while (true) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 } | 82 } |
| 69 | 83 |
| 70 formatted.append(replacement); | 84 formatted.append(replacement); |
| 71 | 85 |
| 72 current_pos = key_end + sizeof(kKeyClose); | 86 current_pos = key_end + sizeof(kKeyClose); |
| 73 } | 87 } |
| 74 return formatted; | 88 return formatted; |
| 75 } | 89 } |
| 76 | 90 |
| 77 } // namespace ui | 91 } // namespace ui |
| OLD | NEW |