 Chromium Code Reviews
 Chromium Code Reviews Issue 2691193002:
  Added StringPiece overloads for base::JoinString.  (Closed)
    
  
    Issue 2691193002:
  Added StringPiece overloads for base::JoinString.  (Closed) 
  | 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); | |
| 
danakj
2017/02/16 16:34:32
Just throwing it out there, not for this CL, but:
 
danakj
2017/02/16 16:36:21
Ohh I see actually at the site that you deleted th
 
Matt Giuca
2017/02/16 23:51:11
Yes! Maybe you missed this in my original email:
 
danakj
2017/02/16 23:53:28
Oops, ya I missed that. I super doubt walking the
 | |
| 886 iter->AppendToString(&result); | |
| 887 } | |
| 888 | |
| 889 return result; | |
| 890 } | |
| 891 | |
| 874 std::string JoinString(const std::vector<std::string>& parts, | 892 std::string JoinString(const std::vector<std::string>& parts, | 
| 875 StringPiece separator) { | 893 StringPiece separator) { | 
| 876 return JoinStringT(parts, separator); | 894 return JoinStringT(parts, separator); | 
| 877 } | 895 } | 
| 878 | 896 | 
| 879 string16 JoinString(const std::vector<string16>& parts, | 897 string16 JoinString(const std::vector<string16>& parts, | 
| 880 StringPiece16 separator) { | 898 StringPiece16 separator) { | 
| 881 return JoinStringT(parts, separator); | 899 return JoinStringT(parts, separator); | 
| 882 } | 900 } | 
| 883 | 901 | 
| 902 std::string JoinStringPiece(const std::vector<StringPiece>& parts, | |
| 903 StringPiece separator) { | |
| 904 return JoinStringPieceT(parts, separator); | |
| 905 } | |
| 906 | |
| 907 string16 JoinStringPiece(const std::vector<StringPiece16>& parts, | |
| 908 StringPiece16 separator) { | |
| 909 return JoinStringPieceT(parts, separator); | |
| 910 } | |
| 911 | |
| 884 template<class FormatStringType, class OutStringType> | 912 template<class FormatStringType, class OutStringType> | 
| 885 OutStringType DoReplaceStringPlaceholders( | 913 OutStringType DoReplaceStringPlaceholders( | 
| 886 const FormatStringType& format_string, | 914 const FormatStringType& format_string, | 
| 887 const std::vector<OutStringType>& subst, | 915 const std::vector<OutStringType>& subst, | 
| 888 std::vector<size_t>* offsets) { | 916 std::vector<size_t>* offsets) { | 
| 889 size_t substitutions = subst.size(); | 917 size_t substitutions = subst.size(); | 
| 890 DCHECK_LT(substitutions, 10U); | 918 DCHECK_LT(substitutions, 10U); | 
| 891 | 919 | 
| 892 size_t sub_length = 0; | 920 size_t sub_length = 0; | 
| 893 for (const auto& cur : subst) | 921 for (const auto& cur : subst) | 
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 988 } // namespace | 1016 } // namespace | 
| 989 | 1017 | 
| 990 size_t strlcpy(char* dst, const char* src, size_t dst_size) { | 1018 size_t strlcpy(char* dst, const char* src, size_t dst_size) { | 
| 991 return lcpyT<char>(dst, src, dst_size); | 1019 return lcpyT<char>(dst, src, dst_size); | 
| 992 } | 1020 } | 
| 993 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { | 1021 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { | 
| 994 return lcpyT<wchar_t>(dst, src, dst_size); | 1022 return lcpyT<wchar_t>(dst, src, dst_size); | 
| 995 } | 1023 } | 
| 996 | 1024 | 
| 997 } // namespace base | 1025 } // namespace base | 
| OLD | NEW |