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 835 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
846 } | 846 } |
847 | 847 |
848 char* WriteInto(std::string* str, size_t length_with_null) { | 848 char* WriteInto(std::string* str, size_t length_with_null) { |
849 return WriteIntoT(str, length_with_null); | 849 return WriteIntoT(str, length_with_null); |
850 } | 850 } |
851 | 851 |
852 char16* WriteInto(string16* str, size_t length_with_null) { | 852 char16* WriteInto(string16* str, size_t length_with_null) { |
853 return WriteIntoT(str, length_with_null); | 853 return WriteIntoT(str, length_with_null); |
854 } | 854 } |
855 | 855 |
856 template<typename STR> | 856 template <typename STR> |
857 static STR JoinStringT(const std::vector<STR>& parts, | 857 static STR JoinStringT(const std::vector<STR>& parts, |
858 BasicStringPiece<STR> sep) { | 858 BasicStringPiece<STR> sep) { |
859 if (parts.empty()) | 859 if (parts.empty()) |
860 return STR(); | 860 return STR(); |
861 | 861 |
862 STR result(parts[0]); | 862 STR result(parts[0]); |
863 auto iter = parts.begin(); | 863 auto iter = parts.begin(); |
864 ++iter; | 864 ++iter; |
865 | 865 |
866 for (; iter != parts.end(); ++iter) { | 866 for (; iter != parts.end(); ++iter) { |
867 sep.AppendToString(&result); | 867 sep.AppendToString(&result); |
868 result += *iter; | 868 result += *iter; |
869 } | 869 } |
870 | 870 |
871 return result; | 871 return result; |
872 } | 872 } |
873 | 873 |
874 template <typename STR> | |
875 static STR JoinStringPieceT(const std::vector<BasicStringPiece<STR>>& parts, | |
876 BasicStringPiece<STR> sep) { | |
877 if (parts.empty()) | |
878 return STR(); | |
879 | |
880 STR result(parts[0].data(), parts[0].size()); | |
881 auto iter = parts.begin(); | |
882 ++iter; | |
883 | |
884 for (; iter != parts.end(); ++iter) { | |
885 sep.AppendToString(&result); | |
886 iter->AppendToString(&result); | |
887 } | |
888 | |
889 return result; | |
890 } | |
891 | |
892 template <typename STR> | |
893 static STR JoinStringPieceT( | |
894 const std::initializer_list<BasicStringPiece<STR>>& parts, | |
Matt Giuca
2017/02/17 09:51:22
Upon further thought, I can probably template this
Matt Giuca
2017/02/20 23:55:41
Done.
I managed to combine all 3 into one version
danakj
2017/02/21 23:47:15
I would say let's avoid temporary objects. You cou
Matt Giuca
2017/02/22 00:10:15
What temporary object do you mean? (Are you saying
Matt Giuca
2017/02/22 06:20:13
Done.
| |
895 BasicStringPiece<STR> sep) { | |
896 if (parts.size() == 0) | |
897 return STR(); | |
898 | |
899 auto iter = parts.begin(); | |
900 STR result(iter->data(), iter->size()); | |
901 ++iter; | |
902 | |
903 for (; iter != parts.end(); ++iter) { | |
904 sep.AppendToString(&result); | |
905 iter->AppendToString(&result); | |
906 } | |
907 | |
908 return result; | |
909 } | |
910 | |
874 std::string JoinString(const std::vector<std::string>& parts, | 911 std::string JoinString(const std::vector<std::string>& parts, |
875 StringPiece separator) { | 912 StringPiece separator) { |
876 return JoinStringT(parts, separator); | 913 return JoinStringT(parts, separator); |
877 } | 914 } |
878 | 915 |
879 string16 JoinString(const std::vector<string16>& parts, | 916 string16 JoinString(const std::vector<string16>& parts, |
880 StringPiece16 separator) { | 917 StringPiece16 separator) { |
881 return JoinStringT(parts, separator); | 918 return JoinStringT(parts, separator); |
882 } | 919 } |
883 | 920 |
921 std::string JoinString(const std::vector<StringPiece>& parts, | |
922 StringPiece separator) { | |
923 return JoinStringPieceT(parts, separator); | |
924 } | |
925 | |
926 string16 JoinString(const std::vector<StringPiece16>& parts, | |
927 StringPiece16 separator) { | |
928 return JoinStringPieceT(parts, separator); | |
929 } | |
930 | |
931 std::string JoinString(const std::initializer_list<StringPiece>& parts, | |
932 StringPiece separator) { | |
933 return JoinStringPieceT(parts, separator); | |
934 } | |
935 | |
936 string16 JoinString(const std::initializer_list<StringPiece16>& parts, | |
937 StringPiece16 separator) { | |
938 return JoinStringPieceT(parts, separator); | |
939 } | |
940 | |
884 template<class FormatStringType, class OutStringType> | 941 template<class FormatStringType, class OutStringType> |
885 OutStringType DoReplaceStringPlaceholders( | 942 OutStringType DoReplaceStringPlaceholders( |
886 const FormatStringType& format_string, | 943 const FormatStringType& format_string, |
887 const std::vector<OutStringType>& subst, | 944 const std::vector<OutStringType>& subst, |
888 std::vector<size_t>* offsets) { | 945 std::vector<size_t>* offsets) { |
889 size_t substitutions = subst.size(); | 946 size_t substitutions = subst.size(); |
890 DCHECK_LT(substitutions, 10U); | 947 DCHECK_LT(substitutions, 10U); |
891 | 948 |
892 size_t sub_length = 0; | 949 size_t sub_length = 0; |
893 for (const auto& cur : subst) | 950 for (const auto& cur : subst) |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
988 } // namespace | 1045 } // namespace |
989 | 1046 |
990 size_t strlcpy(char* dst, const char* src, size_t dst_size) { | 1047 size_t strlcpy(char* dst, const char* src, size_t dst_size) { |
991 return lcpyT<char>(dst, src, dst_size); | 1048 return lcpyT<char>(dst, src, dst_size); |
992 } | 1049 } |
993 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { | 1050 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { |
994 return lcpyT<wchar_t>(dst, src, dst_size); | 1051 return lcpyT<wchar_t>(dst, src, dst_size); |
995 } | 1052 } |
996 | 1053 |
997 } // namespace base | 1054 } // namespace base |
OLD | NEW |