| 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 #include "base/strings/utf_string_conversions.h" |
| 9 |
| 10 namespace ui { |
| 11 |
| 12 std::string ReplaceTemplateExpressions( |
| 13 base::StringPiece format_string, |
| 14 const std::map<base::StringPiece, std::string>& substitutions) { |
| 15 std::string formatted; |
| 16 const size_t kValueLengthGuess = 16; |
| 17 formatted.reserve(format_string.length() + |
| 18 substitutions.size() * kValueLengthGuess); |
| 19 base::StringPiece::const_iterator i = format_string.begin(); |
| 20 while (i < format_string.end()) { |
| 21 if (*i == '$') { |
| 22 ++i; |
| 23 if (i < format_string.end()) { |
| 24 if (*i == '{') { |
| 25 ++i; |
| 26 std::string index; |
| 27 while (i < format_string.end()) { |
| 28 if (*i == '}') { |
| 29 ++i; |
| 30 break; |
| 31 } |
| 32 index.push_back(*i); |
| 33 ++i; |
| 34 } |
| 35 const auto& replacement = substitutions.find(index); |
| 36 if (replacement != substitutions.end()) { |
| 37 formatted.append(replacement->second); |
| 38 } else { |
| 39 // NOTREACHED() << "Missing template expression " << index; |
| 40 } |
| 41 } else if (*i == '$') { |
| 42 while (i < format_string.end() && *i == '$') { |
| 43 formatted.push_back('$'); |
| 44 ++i; |
| 45 } |
| 46 } |
| 47 } |
| 48 } else { |
| 49 formatted.push_back(*i); |
| 50 ++i; |
| 51 } |
| 52 } |
| 53 return formatted; |
| 54 } |
| 55 |
| 56 } // namespace ui |
| OLD | NEW |