| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/strings/string_util.h" | 5 #include "base/strings/string_util.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <math.h> | 9 #include <math.h> |
| 10 #include <stdarg.h> | 10 #include <stdarg.h> |
| 11 #include <stdio.h> | 11 #include <stdio.h> |
| 12 #include <stdlib.h> | 12 #include <stdlib.h> |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 #include <time.h> | 14 #include <time.h> |
| 15 #include <wchar.h> | 15 #include <wchar.h> |
| 16 #include <wctype.h> | 16 #include <wctype.h> |
| 17 | 17 |
| 18 #include <algorithm> | 18 #include <algorithm> |
| 19 #include <map> |
| 19 #include <vector> | 20 #include <vector> |
| 20 | 21 |
| 21 #include "base/basictypes.h" | 22 #include "base/basictypes.h" |
| 22 #include "base/logging.h" | 23 #include "base/logging.h" |
| 23 #include "base/memory/singleton.h" | 24 #include "base/memory/singleton.h" |
| 24 #include "base/strings/string_split.h" | 25 #include "base/strings/string_split.h" |
| 25 #include "base/strings/utf_string_conversion_utils.h" | 26 #include "base/strings/utf_string_conversion_utils.h" |
| 26 #include "base/strings/utf_string_conversions.h" | 27 #include "base/strings/utf_string_conversions.h" |
| 27 #include "base/third_party/icu/icu_utf.h" | 28 #include "base/third_party/icu/icu_utf.h" |
| 28 #include "build/build_config.h" | 29 #include "build/build_config.h" |
| (...skipping 824 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 std::string JoinString(const std::vector<std::string>& parts, | 854 std::string JoinString(const std::vector<std::string>& parts, |
| 854 const std::string& separator) { | 855 const std::string& separator) { |
| 855 return JoinStringT(parts, separator); | 856 return JoinStringT(parts, separator); |
| 856 } | 857 } |
| 857 | 858 |
| 858 string16 JoinString(const std::vector<string16>& parts, | 859 string16 JoinString(const std::vector<string16>& parts, |
| 859 const string16& separator) { | 860 const string16& separator) { |
| 860 return JoinStringT(parts, separator); | 861 return JoinStringT(parts, separator); |
| 861 } | 862 } |
| 862 | 863 |
| 863 template<class FormatStringType, class OutStringType> | 864 template <class FormatStringType, class OutStringType> |
| 864 OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string, | 865 OutStringType DoReplaceStringPlaceholders( |
| 865 const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) { | 866 const FormatStringType& format_string, |
| 867 const std::map<std::string, OutStringType>& subst) { |
| 868 OutStringType formatted; |
| 869 const size_t kValueLengthGuess = 16; |
| 870 formatted.reserve(format_string.length() + subst.size() * kValueLengthGuess); |
| 871 for (typename FormatStringType::const_iterator i = format_string.begin(); |
| 872 i != format_string.end(); ++i) { |
| 873 if (*i == '$') { |
| 874 if (i + 1 != format_string.end()) { |
| 875 ++i; |
| 876 if (*i == '{') { |
| 877 std::string index; |
| 878 ++i; |
| 879 while (i != format_string.end() && *i != '}') { |
| 880 index.push_back(*i); |
| 881 ++i; |
| 882 } |
| 883 const auto& replacement = subst.find(index); |
| 884 if (replacement != subst.end()) |
| 885 formatted.append(replacement->second); |
| 886 } else if (*i == '$') { |
| 887 while (i != format_string.end() && *i == '$') { |
| 888 formatted.push_back('$'); |
| 889 ++i; |
| 890 } |
| 891 --i; |
| 892 } |
| 893 } |
| 894 } else { |
| 895 formatted.push_back(*i); |
| 896 } |
| 897 } |
| 898 return formatted; |
| 899 } |
| 900 |
| 901 std::string ReplaceStringPlaceholders( |
| 902 const std::string& format_string, |
| 903 const std::map<std::string, std::string>& subst) { |
| 904 return DoReplaceStringPlaceholders(format_string, subst); |
| 905 } |
| 906 |
| 907 template <class FormatStringType, class OutStringType> |
| 908 OutStringType DoReplaceStringPlaceholders( |
| 909 const FormatStringType& format_string, |
| 910 const std::vector<OutStringType>& subst, |
| 911 std::vector<size_t>* offsets) { |
| 866 size_t substitutions = subst.size(); | 912 size_t substitutions = subst.size(); |
| 867 | |
| 868 size_t sub_length = 0; | 913 size_t sub_length = 0; |
| 869 for (typename std::vector<OutStringType>::const_iterator iter = subst.begin(); | 914 for (typename std::vector<OutStringType>::const_iterator iter = subst.begin(); |
| 870 iter != subst.end(); ++iter) { | 915 iter != subst.end(); ++iter) { |
| 871 sub_length += iter->length(); | 916 sub_length += iter->length(); |
| 872 } | 917 } |
| 873 | 918 |
| 874 OutStringType formatted; | 919 OutStringType formatted; |
| 875 formatted.reserve(format_string.length() + sub_length); | 920 formatted.reserve(format_string.length() + sub_length); |
| 876 | 921 |
| 877 std::vector<ReplacementOffset> r_offsets; | 922 std::vector<ReplacementOffset> r_offsets; |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1126 } | 1171 } |
| 1127 | 1172 |
| 1128 } // namespace | 1173 } // namespace |
| 1129 | 1174 |
| 1130 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { | 1175 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { |
| 1131 return lcpyT<char>(dst, src, dst_size); | 1176 return lcpyT<char>(dst, src, dst_size); |
| 1132 } | 1177 } |
| 1133 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { | 1178 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { |
| 1134 return lcpyT<wchar_t>(dst, src, dst_size); | 1179 return lcpyT<wchar_t>(dst, src, dst_size); |
| 1135 } | 1180 } |
| OLD | NEW |