@@ -625,6 +625,8 @@ typedef basic_string<char16_t> u16string;
625
625
typedef basic_string<char32_t > u32string;
626
626
#endif
627
627
628
+ struct __uninitialized_size_tag {};
629
+
628
630
template <class _CharT , class _Traits , class _Allocator >
629
631
class
630
632
_LIBCPP_TEMPLATE_VIS
@@ -746,6 +748,26 @@ private:
746
748
747
749
__compressed_pair<__rep, allocator_type> __r_;
748
750
751
+ // Construct a string with the given allocator and enough storage to hold `__size` characters, but
752
+ // don't initialize the characters. The contents of the string, including the null terminator, must be
753
+ // initialized separately.
754
+ _LIBCPP_HIDE_FROM_ABI explicit basic_string (__uninitialized_size_tag, size_type __size, const allocator_type& __a)
755
+ : __r_(__default_init_tag(), __a) {
756
+ if (__size > max_size ())
757
+ __throw_length_error ();
758
+ if (__fits_in_sso (__size)) {
759
+ __zero ();
760
+ __set_short_size (__size);
761
+ } else {
762
+ auto __capacity = __recommend (__size) + 1 ;
763
+ auto __allocation = __alloc_traits::allocate (__alloc (), __capacity);
764
+ __set_long_cap (__capacity);
765
+ __set_long_pointer (__allocation);
766
+ __set_long_size (__size);
767
+ }
768
+ std::__debug_db_insert_c (this );
769
+ }
770
+
749
771
public:
750
772
_LIBCPP_TEMPLATE_DATA_VIS
751
773
static const size_type npos = -1 ;
@@ -4156,11 +4178,15 @@ operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4156
4178
const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4157
4179
{
4158
4180
using _String = basic_string<_CharT, _Traits, _Allocator>;
4159
- _String __r (_String::__alloc_traits::select_on_container_copy_construction (__lhs.get_allocator ()));
4160
- typename _String::size_type __lhs_sz = __lhs.size ();
4161
- typename _String::size_type __rhs_sz = __rhs.size ();
4162
- __r.__init (__lhs.data (), __lhs_sz, __lhs_sz + __rhs_sz);
4163
- __r.append (__rhs.data (), __rhs_sz);
4181
+ auto __lhs_sz = __lhs.size ();
4182
+ auto __rhs_sz = __rhs.size ();
4183
+ _String __r (__uninitialized_size_tag (),
4184
+ __lhs_sz + __rhs_sz,
4185
+ _String::__alloc_traits::select_on_container_copy_construction (__lhs.get_allocator ()));
4186
+ auto __ptr = std::__to_address (__r.__get_pointer ());
4187
+ _Traits::copy (__ptr, __lhs.data (), __lhs_sz);
4188
+ _Traits::copy (__ptr + __lhs_sz, __rhs.data (), __rhs_sz);
4189
+ _Traits::assign (__ptr + __lhs_sz + __rhs_sz, 1 , _CharT ());
4164
4190
return __r;
4165
4191
}
4166
4192
@@ -4169,11 +4195,15 @@ basic_string<_CharT, _Traits, _Allocator>
4169
4195
operator +(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4170
4196
{
4171
4197
using _String = basic_string<_CharT, _Traits, _Allocator>;
4172
- _String __r (_String::__alloc_traits::select_on_container_copy_construction (__rhs.get_allocator ()));
4173
- typename _String::size_type __lhs_sz = _Traits::length (__lhs);
4174
- typename _String::size_type __rhs_sz = __rhs.size ();
4175
- __r.__init (__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4176
- __r.append (__rhs.data (), __rhs_sz);
4198
+ auto __lhs_sz = _Traits::length (__lhs);
4199
+ auto __rhs_sz = __rhs.size ();
4200
+ _String __r (__uninitialized_size_tag (),
4201
+ __lhs_sz + __rhs_sz,
4202
+ _String::__alloc_traits::select_on_container_copy_construction (__rhs.get_allocator ()));
4203
+ auto __ptr = std::__to_address (__r.__get_pointer ());
4204
+ _Traits::copy (__ptr, __lhs, __lhs_sz);
4205
+ _Traits::copy (__ptr + __lhs_sz, __rhs.data (), __rhs_sz);
4206
+ _Traits::assign (__ptr + __lhs_sz + __rhs_sz, 1 , _CharT ());
4177
4207
return __r;
4178
4208
}
4179
4209
@@ -4182,10 +4212,14 @@ basic_string<_CharT, _Traits, _Allocator>
4182
4212
operator +(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4183
4213
{
4184
4214
using _String = basic_string<_CharT, _Traits, _Allocator>;
4185
- _String __r (_String::__alloc_traits::select_on_container_copy_construction (__rhs.get_allocator ()));
4186
4215
typename _String::size_type __rhs_sz = __rhs.size ();
4187
- __r.__init (&__lhs, 1 , 1 + __rhs_sz);
4188
- __r.append (__rhs.data (), __rhs_sz);
4216
+ _String __r (__uninitialized_size_tag (),
4217
+ __rhs_sz + 1 ,
4218
+ _String::__alloc_traits::select_on_container_copy_construction (__rhs.get_allocator ()));
4219
+ auto __ptr = std::__to_address (__r.__get_pointer ());
4220
+ _Traits::assign (__ptr, 1 , __lhs);
4221
+ _Traits::copy (__ptr + 1 , __rhs.data (), __rhs_sz);
4222
+ _Traits::assign (__ptr + 1 + __rhs_sz, 1 , _CharT ());
4189
4223
return __r;
4190
4224
}
4191
4225
@@ -4195,11 +4229,15 @@ basic_string<_CharT, _Traits, _Allocator>
4195
4229
operator +(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4196
4230
{
4197
4231
using _String = basic_string<_CharT, _Traits, _Allocator>;
4198
- _String __r (_String::__alloc_traits::select_on_container_copy_construction (__lhs.get_allocator ()));
4199
4232
typename _String::size_type __lhs_sz = __lhs.size ();
4200
4233
typename _String::size_type __rhs_sz = _Traits::length (__rhs);
4201
- __r.__init (__lhs.data (), __lhs_sz, __lhs_sz + __rhs_sz);
4202
- __r.append (__rhs, __rhs_sz);
4234
+ _String __r (__uninitialized_size_tag (),
4235
+ __lhs_sz + __rhs_sz,
4236
+ _String::__alloc_traits::select_on_container_copy_construction (__lhs.get_allocator ()));
4237
+ auto __ptr = std::__to_address (__r.__get_pointer ());
4238
+ _Traits::copy (__ptr, __lhs.data (), __lhs_sz);
4239
+ _Traits::copy (__ptr + __lhs_sz, __rhs, __rhs_sz);
4240
+ _Traits::assign (__ptr + __lhs_sz + __rhs_sz, 1 , _CharT ());
4203
4241
return __r;
4204
4242
}
4205
4243
@@ -4208,10 +4246,14 @@ basic_string<_CharT, _Traits, _Allocator>
4208
4246
operator +(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4209
4247
{
4210
4248
using _String = basic_string<_CharT, _Traits, _Allocator>;
4211
- _String __r (_String::__alloc_traits::select_on_container_copy_construction (__lhs.get_allocator ()));
4212
4249
typename _String::size_type __lhs_sz = __lhs.size ();
4213
- __r.__init (__lhs.data (), __lhs_sz, __lhs_sz + 1 );
4214
- __r.push_back (__rhs);
4250
+ _String __r (__uninitialized_size_tag (),
4251
+ __lhs_sz + 1 ,
4252
+ _String::__alloc_traits::select_on_container_copy_construction (__lhs.get_allocator ()));
4253
+ auto __ptr = std::__to_address (__r.__get_pointer ());
4254
+ _Traits::copy (__ptr, __lhs.data (), __lhs_sz);
4255
+ _Traits::assign (__ptr + __lhs_sz, 1 , __rhs);
4256
+ _Traits::assign (__ptr + 1 + __lhs_sz, 1 , _CharT ());
4215
4257
return __r;
4216
4258
}
4217
4259
0 commit comments