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> | |
| 12 OutStringType DoReplaceTemplateExpressions( | |
| 13 const FormatStringType& format_string, | |
| 14 const std::map<FormatStringType, OutStringType>& substitutions) { | |
| 15 OutStringType formatted; | |
| 16 const size_t kValueLengthGuess = 16; | |
| 17 formatted.reserve(format_string.length() + | |
| 18 substitutions.size() * kValueLengthGuess); | |
| 19 #if 1 | |
|
Dan Beam
2015/07/14 17:14:35
I understand you're asking for feedback, but I wou
dschuyler
2015/07/14 21:05:30
Acknowledged.
| |
| 20 size_t copy_begin = 0; | |
| 21 size_t format_string_length = format_string.length(); | |
| 22 while (copy_begin < format_string_length) { | |
| 23 size_t copy_end = format_string.find('$', copy_begin); | |
| 24 if (copy_end == FormatStringType::npos) { | |
| 25 formatted.append(format_string.data(), copy_begin, | |
| 26 format_string.length() - copy_begin); | |
| 27 break; | |
| 28 } | |
| 29 formatted.append(format_string.data(), copy_begin, copy_end - copy_begin); | |
| 30 copy_begin = copy_end + 1; | |
| 31 if (format_string[copy_begin] == '{') { | |
| 32 ++copy_begin; | |
| 33 size_t key_end = format_string.find('}', copy_begin); | |
| 34 if (copy_end == FormatStringType::npos) | |
| 35 break; | |
| 36 // else | |
| 37 // NOTREACHED() << "Missing template expression end brace '}'"; | |
| 38 FormatStringType key = | |
| 39 format_string.substr(copy_begin, key_end - copy_begin); | |
| 40 const auto& replacement = substitutions.find(key); | |
| 41 copy_begin = key_end + 1; | |
| 42 if (replacement != substitutions.end()) | |
| 43 formatted.append(replacement->second); | |
| 44 // else | |
| 45 // NOTREACHED() << "Missing template expression " << key; | |
| 46 } else if (format_string[copy_begin] == '$') { | |
| 47 copy_end = format_string.find_first_not_of('$', copy_begin); | |
| 48 formatted.append(format_string.data(), copy_begin, copy_end - copy_begin); | |
| 49 copy_begin = copy_end; | |
| 50 } | |
| 51 } | |
| 52 #else | |
| 53 typename FormatStringType::const_iterator i = format_string.begin(); | |
| 54 while (i < format_string.end()) { | |
| 55 if (*i == '$') { | |
| 56 ++i; | |
| 57 if (i < format_string.end()) { | |
| 58 if (*i == '{') { | |
| 59 ++i; | |
| 60 OutStringType index; | |
| 61 while (i < format_string.end()) { | |
| 62 if (*i == '}') { | |
| 63 ++i; | |
| 64 break; | |
| 65 } | |
| 66 index.push_back(*i); | |
| 67 ++i; | |
| 68 } | |
| 69 const auto& replacement = substitutions.find(index); | |
| 70 if (replacement != substitutions.end()) { | |
| 71 formatted.append(replacement->second); | |
| 72 } else { | |
| 73 // NOTREACHED() << "Missing template expression " << index; | |
|
Dan Beam
2015/07/14 17:14:35
I think this means it'd only blow up in debug. do
dschuyler
2015/07/14 21:05:30
Who should we ask?
| |
| 74 } | |
| 75 } else if (*i == '$') { | |
| 76 while (i < format_string.end() && *i == '$') { | |
| 77 formatted.push_back('$'); | |
| 78 ++i; | |
| 79 } | |
| 80 } | |
| 81 } | |
| 82 } else { | |
| 83 formatted.push_back(*i); | |
| 84 ++i; | |
| 85 } | |
| 86 } | |
|
Dan Beam
2015/07/14 17:14:35
I find this second one more readable, I think Nico
dschuyler
2015/07/14 21:05:30
Acknowledged.
| |
| 87 #endif | |
| 88 return formatted; | |
| 89 } | |
| 90 | |
| 91 } // namespace | |
| 92 | |
| 93 namespace ui { | |
| 94 | |
| 95 std::string ReplaceTemplateExpressions( | |
| 96 base::StringPiece format_string, | |
| 97 const std::map<base::StringPiece, std::string>& substitutions) { | |
| 98 return DoReplaceTemplateExpressions(format_string, substitutions); | |
| 99 } | |
| 100 | |
| 101 base::string16 ReplaceTemplateExpressions( | |
| 102 const base::string16& format_string, | |
| 103 const std::map<base::string16, base::string16>& substitutions) { | |
| 104 return DoReplaceTemplateExpressions(format_string, substitutions); | |
| 105 } | |
| 106 | |
| 107 std::string ReplaceTemplateExpressions( | |
| 108 const std::string& format_string, | |
| 109 const std::map<std::string, std::string>& substitutions) { | |
| 110 return DoReplaceTemplateExpressions(format_string, substitutions); | |
| 111 } | |
| 112 | |
| 113 } // namespace ui | |
| OLD | NEW |