OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/string_util.h" | 5 #include "base/string_util.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #include <ctype.h> | 9 #include <ctype.h> |
10 #include <errno.h> | 10 #include <errno.h> |
(...skipping 813 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
824 } | 824 } |
825 | 825 |
826 string16 JoinString(const std::vector<string16>& parts, char16 sep) { | 826 string16 JoinString(const std::vector<string16>& parts, char16 sep) { |
827 return JoinStringT(parts, sep); | 827 return JoinStringT(parts, sep); |
828 } | 828 } |
829 | 829 |
830 template<class FormatStringType, class OutStringType> | 830 template<class FormatStringType, class OutStringType> |
831 OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string, | 831 OutStringType DoReplaceStringPlaceholders(const FormatStringType& format_string, |
832 const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) { | 832 const std::vector<OutStringType>& subst, std::vector<size_t>* offsets) { |
833 size_t substitutions = subst.size(); | 833 size_t substitutions = subst.size(); |
| 834 DCHECK(substitutions < 10); |
834 | 835 |
835 size_t sub_length = 0; | 836 size_t sub_length = 0; |
836 for (typename std::vector<OutStringType>::const_iterator iter = subst.begin(); | 837 for (typename std::vector<OutStringType>::const_iterator iter = subst.begin(); |
837 iter != subst.end(); ++iter) { | 838 iter != subst.end(); ++iter) { |
838 sub_length += iter->length(); | 839 sub_length += iter->length(); |
839 } | 840 } |
840 | 841 |
841 OutStringType formatted; | 842 OutStringType formatted; |
842 formatted.reserve(format_string.length() + sub_length); | 843 formatted.reserve(format_string.length() + sub_length); |
843 | 844 |
844 std::vector<ReplacementOffset> r_offsets; | 845 std::vector<ReplacementOffset> r_offsets; |
845 for (typename FormatStringType::const_iterator i = format_string.begin(); | 846 for (typename FormatStringType::const_iterator i = format_string.begin(); |
846 i != format_string.end(); ++i) { | 847 i != format_string.end(); ++i) { |
847 if ('$' == *i) { | 848 if ('$' == *i) { |
848 if (i + 1 != format_string.end()) { | 849 if (i + 1 != format_string.end()) { |
849 ++i; | 850 ++i; |
850 DCHECK('$' == *i || '1' <= *i) << "Invalid placeholder: " << *i; | 851 DCHECK('$' == *i || '1' <= *i) << "Invalid placeholder: " << *i; |
851 if ('$' == *i) { | 852 if ('$' == *i) { |
852 while (i != format_string.end() && '$' == *i) { | 853 while (i != format_string.end() && '$' == *i) { |
853 formatted.push_back('$'); | 854 formatted.push_back('$'); |
854 ++i; | 855 ++i; |
855 } | 856 } |
856 --i; | 857 --i; |
857 } else { | 858 } else { |
858 uintptr_t index = 0; | 859 uintptr_t index = *i - '1'; |
859 while ('0' <= *i && '9' >= *i) { | |
860 index *= 10; | |
861 index += *i - '0'; | |
862 ++i; | |
863 } | |
864 --i; | |
865 index -= 1; | |
866 if (offsets) { | 860 if (offsets) { |
867 ReplacementOffset r_offset(index, | 861 ReplacementOffset r_offset(index, |
868 static_cast<int>(formatted.size())); | 862 static_cast<int>(formatted.size())); |
869 r_offsets.insert(std::lower_bound(r_offsets.begin(), | 863 r_offsets.insert(std::lower_bound(r_offsets.begin(), |
870 r_offsets.end(), | 864 r_offsets.end(), |
871 r_offset, | 865 r_offset, |
872 &CompareParameter), | 866 &CompareParameter), |
873 r_offset); | 867 r_offset); |
874 } | 868 } |
875 if (index < substitutions) | 869 if (index < substitutions) |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1094 } | 1088 } |
1095 | 1089 |
1096 } // namespace | 1090 } // namespace |
1097 | 1091 |
1098 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { | 1092 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { |
1099 return lcpyT<char>(dst, src, dst_size); | 1093 return lcpyT<char>(dst, src, dst_size); |
1100 } | 1094 } |
1101 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { | 1095 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { |
1102 return lcpyT<wchar_t>(dst, src, dst_size); | 1096 return lcpyT<wchar_t>(dst, src, dst_size); |
1103 } | 1097 } |
OLD | NEW |