diff --git a/libcxx/include/string b/libcxx/include/string --- a/libcxx/include/string +++ b/libcxx/include/string @@ -638,6 +638,8 @@ typedef basic_string u32string; #endif +struct __capacity_tag {}; + template class _LIBCPP_TEMPLATE_VIS @@ -762,6 +764,19 @@ __compressed_pair<__rep, allocator_type> __r_; + _LIBCPP_HIDE_FROM_ABI basic_string(__capacity_tag, size_type __alloc_capacity, const allocator_type& __a) + : __r_(__default_init_tag(), __a) { + if (__fits_in_sso(__alloc_capacity)) { + __zero(); + } else { + auto __capacity = __recommend(__alloc_capacity); + auto __allocation = __alloc_traits::allocate(__alloc(), __capacity + 1); + __set_long_cap(__capacity); + __set_long_pointer(__allocation); + __set_long_size(0); + } + } + public: _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1; @@ -4165,10 +4180,12 @@ const basic_string<_CharT, _Traits, _Allocator>& __rhs) { using _String = basic_string<_CharT, _Traits, _Allocator>; - _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator())); typename _String::size_type __lhs_sz = __lhs.size(); typename _String::size_type __rhs_sz = __rhs.size(); - __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz); + _String __r(__capacity_tag(), + __lhs_sz + __rhs_sz, + _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator())); + __r.append(__lhs.data(), __lhs_sz); __r.append(__rhs.data(), __rhs_sz); return __r; } @@ -4178,10 +4195,12 @@ operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs) { using _String = basic_string<_CharT, _Traits, _Allocator>; - _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator())); typename _String::size_type __lhs_sz = _Traits::length(__lhs); typename _String::size_type __rhs_sz = __rhs.size(); - __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz); + _String __r(__capacity_tag(), + __lhs_sz + __rhs_sz, + _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator())); + __r.append(__lhs, __lhs_sz); __r.append(__rhs.data(), __rhs_sz); return __r; } @@ -4191,9 +4210,11 @@ operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs) { using _String = basic_string<_CharT, _Traits, _Allocator>; - _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator())); typename _String::size_type __rhs_sz = __rhs.size(); - __r.__init(&__lhs, 1, 1 + __rhs_sz); + _String __r(__capacity_tag(), + __rhs_sz + 1, + _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator())); + __r.push_back(__lhs); __r.append(__rhs.data(), __rhs_sz); return __r; } @@ -4204,10 +4225,12 @@ operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) { using _String = basic_string<_CharT, _Traits, _Allocator>; - _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator())); typename _String::size_type __lhs_sz = __lhs.size(); typename _String::size_type __rhs_sz = _Traits::length(__rhs); - __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz); + _String __r(__capacity_tag(), + __lhs_sz + __rhs_sz, + _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator())); + __r.append(__lhs.data(), __lhs_sz); __r.append(__rhs, __rhs_sz); return __r; } @@ -4217,9 +4240,11 @@ operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs) { using _String = basic_string<_CharT, _Traits, _Allocator>; - _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator())); typename _String::size_type __lhs_sz = __lhs.size(); - __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1); + _String __r(__capacity_tag(), + __lhs_sz + 1, + _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator())); + __r.append(__lhs.data(), __lhs_sz); __r.push_back(__rhs); return __r; }