Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Side by Side Diff: base/strings/string_util.cc

Issue 2709983006: base::JoinString: Pre-allocate the string to its final size. (Closed)
Patch Set: Revert PS 3; removes the fast path. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 857 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 return WriteIntoT(str, length_with_null); 868 return WriteIntoT(str, length_with_null);
869 } 869 }
870 870
871 // Generic version for all JoinString overloads. |list_type| must be a sequence 871 // Generic version for all JoinString overloads. |list_type| must be a sequence
872 // (std::vector or std::initializer_list) of strings/StringPieces (std::string, 872 // (std::vector or std::initializer_list) of strings/StringPieces (std::string,
873 // string16, StringPiece or StringPiece16). |string_type| is either std::string 873 // string16, StringPiece or StringPiece16). |string_type| is either std::string
874 // or string16. 874 // or string16.
875 template <typename list_type, typename string_type> 875 template <typename list_type, typename string_type>
876 static string_type JoinStringT(const list_type& parts, 876 static string_type JoinStringT(const list_type& parts,
877 BasicStringPiece<string_type> sep) { 877 BasicStringPiece<string_type> sep) {
878 auto iter = parts.begin(); 878 if (parts.size() == 0)
879 // Early-exit to avoid bad access to the first element.
880 if (iter == parts.end())
881 return string_type(); 879 return string_type();
882 880
883 // Begin constructing the result from the first element. 881 // Pre-allocate the eventual size of the string. Start with the size of all of
884 string_type result(iter->data(), iter->size()); 882 // the separators (note that this *assumes* parts.size() > 0).
883 size_t total_size = (parts.size() - 1) * sep.size();
884 for (const auto& part : parts)
885 total_size += part.size();
886 string_type result;
887 result.reserve(total_size);
888
889 auto iter = parts.begin();
890 DCHECK(iter != parts.end());
891 AppendToString(&result, *iter);
885 ++iter; 892 ++iter;
886 893
887 for (; iter != parts.end(); ++iter) { 894 for (; iter != parts.end(); ++iter) {
888 sep.AppendToString(&result); 895 sep.AppendToString(&result);
889 // Using the overloaded AppendToString allows this template function to work 896 // Using the overloaded AppendToString allows this template function to work
890 // on both strings and StringPieces without creating an intermediate 897 // on both strings and StringPieces without creating an intermediate
891 // StringPiece object. 898 // StringPiece object.
892 AppendToString(&result, *iter); 899 AppendToString(&result, *iter);
893 } 900 }
894 901
902 // Sanity-check that we pre-allocated correctly.
903 DCHECK_EQ(total_size, result.size());
904
895 return result; 905 return result;
896 } 906 }
897 907
898 std::string JoinString(const std::vector<std::string>& parts, 908 std::string JoinString(const std::vector<std::string>& parts,
899 StringPiece separator) { 909 StringPiece separator) {
900 return JoinStringT(parts, separator); 910 return JoinStringT(parts, separator);
901 } 911 }
902 912
903 string16 JoinString(const std::vector<string16>& parts, 913 string16 JoinString(const std::vector<string16>& parts,
904 StringPiece16 separator) { 914 StringPiece16 separator) {
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1032 } // namespace 1042 } // namespace
1033 1043
1034 size_t strlcpy(char* dst, const char* src, size_t dst_size) { 1044 size_t strlcpy(char* dst, const char* src, size_t dst_size) {
1035 return lcpyT<char>(dst, src, dst_size); 1045 return lcpyT<char>(dst, src, dst_size);
1036 } 1046 }
1037 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { 1047 size_t wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) {
1038 return lcpyT<wchar_t>(dst, src, dst_size); 1048 return lcpyT<wchar_t>(dst, src, dst_size);
1039 } 1049 }
1040 1050
1041 } // namespace base 1051 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698