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> |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 | 57 |
58 // Starting position in the string. | 58 // Starting position in the string. |
59 size_t offset; | 59 size_t offset; |
60 }; | 60 }; |
61 | 61 |
62 static bool CompareParameter(const ReplacementOffset& elem1, | 62 static bool CompareParameter(const ReplacementOffset& elem1, |
63 const ReplacementOffset& elem2) { | 63 const ReplacementOffset& elem2) { |
64 return elem1.parameter < elem2.parameter; | 64 return elem1.parameter < elem2.parameter; |
65 } | 65 } |
66 | 66 |
67 // Overloaded function to append one string onto the end of another. Having a | |
68 // separate overload for |source| as both stringpiece and string allows for more | |
danakj
2017/02/22 15:10:45
StringPiece
Matt Giuca
2017/02/22 22:39:36
I was trying to consistently use lowercase "string
danakj
2017/02/22 22:40:30
SP16 implied imo. lowercase breaks case-sensitive
Matt Giuca
2017/02/23 04:48:06
Done.
| |
69 // efficient usage from functions templated to work with either type (avoiding a | |
70 // redundant call to the BasicStringPiece constructor in both cases). | |
71 template <typename STR> | |
72 inline void AppendToString(STR* target, const STR& source) { | |
73 target->append(source); | |
74 } | |
75 template <typename STR> | |
danakj
2017/02/22 15:10:45
style: blank line between functions
Matt Giuca
2017/02/23 04:48:06
Done.
| |
76 inline void AppendToString(STR* target, const BasicStringPiece<STR>& source) { | |
77 source.AppendToString(target); | |
78 } | |
79 | |
67 // Assuming that a pointer is the size of a "machine word", then | 80 // Assuming that a pointer is the size of a "machine word", then |
68 // uintptr_t is an integer type that is also a machine word. | 81 // uintptr_t is an integer type that is also a machine word. |
69 typedef uintptr_t MachineWord; | 82 typedef uintptr_t MachineWord; |
70 const uintptr_t kMachineWordAlignmentMask = sizeof(MachineWord) - 1; | 83 const uintptr_t kMachineWordAlignmentMask = sizeof(MachineWord) - 1; |
71 | 84 |
72 inline bool IsAlignedToMachineWord(const void* pointer) { | 85 inline bool IsAlignedToMachineWord(const void* pointer) { |
73 return !(reinterpret_cast<MachineWord>(pointer) & kMachineWordAlignmentMask); | 86 return !(reinterpret_cast<MachineWord>(pointer) & kMachineWordAlignmentMask); |
74 } | 87 } |
75 | 88 |
76 template<typename T> inline T* AlignToMachineWord(T* pointer) { | 89 template<typename T> inline T* AlignToMachineWord(T* pointer) { |
(...skipping 769 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
846 } | 859 } |
847 | 860 |
848 char* WriteInto(std::string* str, size_t length_with_null) { | 861 char* WriteInto(std::string* str, size_t length_with_null) { |
849 return WriteIntoT(str, length_with_null); | 862 return WriteIntoT(str, length_with_null); |
850 } | 863 } |
851 | 864 |
852 char16* WriteInto(string16* str, size_t length_with_null) { | 865 char16* WriteInto(string16* str, size_t length_with_null) { |
853 return WriteIntoT(str, length_with_null); | 866 return WriteIntoT(str, length_with_null); |
854 } | 867 } |
855 | 868 |
856 template<typename STR> | 869 // Generic version for all JoinString overloads. |LIST| must be a sequence |
857 static STR JoinStringT(const std::vector<STR>& parts, | 870 // (std::vector or std::initializer_list) of strings/stringpieces (std::string, |
858 BasicStringPiece<STR> sep) { | 871 // string16, StringPiece or StringPiece16). |STR| is either std::string or |
859 if (parts.empty()) | 872 // string16. |
873 template <typename LIST, typename STR> | |
danakj
2017/02/22 15:10:45
It's not normal in chromium for template types to
Matt Giuca
2017/02/23 04:48:06
Done.
| |
874 static STR JoinStringT(const LIST& parts, BasicStringPiece<STR> sep) { | |
875 auto iter = parts.begin(); | |
876 // Early-exit to avoid bad access to the first element. | |
877 if (iter == parts.end()) | |
860 return STR(); | 878 return STR(); |
861 | 879 |
862 STR result(parts[0]); | 880 // Begin constructing the result from the first element. |
863 auto iter = parts.begin(); | 881 STR result(iter->data(), iter->size()); |
864 ++iter; | 882 ++iter; |
865 | 883 |
866 for (; iter != parts.end(); ++iter) { | 884 for (; iter != parts.end(); ++iter) { |
867 sep.AppendToString(&result); | 885 sep.AppendToString(&result); |
868 result += *iter; | 886 // Using the overloaded AppendToString allows this template function to work |
887 // on both strings and stringpieces without creating an intermediate | |
danakj
2017/02/22 15:10:46
StringPieces
Matt Giuca
2017/02/23 04:48:06
Done.
| |
888 // stringpiece object. | |
danakj
2017/02/22 15:10:45
StringPiece
Matt Giuca
2017/02/23 04:48:06
Done.
| |
889 AppendToString(&result, *iter); | |
869 } | 890 } |
870 | 891 |
871 return result; | 892 return result; |
872 } | 893 } |
873 | 894 |
874 std::string JoinString(const std::vector<std::string>& parts, | 895 std::string JoinString(const std::vector<std::string>& parts, |
875 StringPiece separator) { | 896 StringPiece separator) { |
876 return JoinStringT(parts, separator); | 897 return JoinStringT(parts, separator); |
877 } | 898 } |
878 | 899 |
879 string16 JoinString(const std::vector<string16>& parts, | 900 string16 JoinString(const std::vector<string16>& parts, |
880 StringPiece16 separator) { | 901 StringPiece16 separator) { |
881 return JoinStringT(parts, separator); | 902 return JoinStringT(parts, separator); |
882 } | 903 } |
883 | 904 |
905 std::string JoinString(const std::vector<StringPiece>& parts, | |
906 StringPiece separator) { | |
907 return JoinStringT(parts, separator); | |
908 } | |
909 | |
910 string16 JoinString(const std::vector<StringPiece16>& parts, | |
911 StringPiece16 separator) { | |
912 return JoinStringT(parts, separator); | |
913 } | |
914 | |
915 std::string JoinString(const std::initializer_list<StringPiece>& parts, | |
916 StringPiece separator) { | |
917 return JoinStringT(parts, separator); | |
918 } | |
919 | |
920 string16 JoinString(const std::initializer_list<StringPiece16>& parts, | |
921 StringPiece16 separator) { | |
922 return JoinStringT(parts, separator); | |
923 } | |
924 | |
884 template<class FormatStringType, class OutStringType> | 925 template<class FormatStringType, class OutStringType> |
885 OutStringType DoReplaceStringPlaceholders( | 926 OutStringType DoReplaceStringPlaceholders( |
886 const FormatStringType& format_string, | 927 const FormatStringType& format_string, |
887 const std::vector<OutStringType>& subst, | 928 const std::vector<OutStringType>& subst, |
888 std::vector<size_t>* offsets) { | 929 std::vector<size_t>* offsets) { |
889 size_t substitutions = subst.size(); | 930 size_t substitutions = subst.size(); |
890 DCHECK_LT(substitutions, 10U); | 931 DCHECK_LT(substitutions, 10U); |
891 | 932 |
892 size_t sub_length = 0; | 933 size_t sub_length = 0; |
893 for (const auto& cur : subst) | 934 for (const auto& cur : subst) |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
988 } // namespace | 1029 } // namespace |
989 | 1030 |
990 size_t strlcpy(char* dst, const char* src, size_t dst_size) { | 1031 size_t strlcpy(char* dst, const char* src, size_t dst_size) { |
991 return lcpyT<char>(dst, src, dst_size); | 1032 return lcpyT<char>(dst, src, dst_size); |
992 } | 1033 } |
993 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { | 1034 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { |
994 return lcpyT<wchar_t>(dst, src, dst_size); | 1035 return lcpyT<wchar_t>(dst, src, dst_size); |
995 } | 1036 } |
996 | 1037 |
997 } // namespace base | 1038 } // namespace base |
OLD | NEW |