@@ -235,9 +235,9 @@ public:
235
235
template <class T>
236
236
basic_string& insert(size_type pos1, const T& t); // constexpr since C++20
237
237
basic_string& insert(size_type pos1, const basic_string& str,
238
- size_type pos2, size_type n); // constexpr since C++20
238
+ size_type pos2, size_type n2=npos); // constexpr since C++20
239
239
template <class T>
240
- basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17, constexpr since C++20
240
+ basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n=npos); // C++17, constexpr since C++20
241
241
basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20
242
242
basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20
243
243
basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20
@@ -260,7 +260,7 @@ public:
260
260
size_type pos2, size_type n2=npos); // C++14, constexpr since C++20
261
261
template <class T>
262
262
basic_string& replace(size_type pos1, size_type n1, const T& t,
263
- size_type pos2, size_type n); // C++17, constexpr since C++20
263
+ size_type pos2, size_type n2=npos); // C++17, constexpr since C++20
264
264
basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20
265
265
basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20
266
266
basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20
@@ -2307,7 +2307,7 @@ private:
2307
2307
if (!__str.__is_long ()) {
2308
2308
if (__is_long ()) {
2309
2309
__annotate_delete ();
2310
- __alloc_traits::deallocate (__alloc_, __get_long_pointer (), capacity () + 1 );
2310
+ __alloc_traits::deallocate (__alloc_, __get_long_pointer (), __get_long_cap () );
2311
2311
__rep_ = __rep ();
2312
2312
}
2313
2313
__alloc_ = __str.__alloc_ ;
@@ -2322,7 +2322,7 @@ private:
2322
2322
__alloc_ = std::move (__a);
2323
2323
__set_long_pointer (__allocation.ptr );
2324
2324
__set_long_cap (__allocation.count );
2325
- __set_long_size (__str.size ());
2325
+ __set_long_size (__str.__get_long_size ());
2326
2326
}
2327
2327
}
2328
2328
}
@@ -2364,8 +2364,14 @@ private:
2364
2364
size_type __old_size = size ();
2365
2365
if (__n > __old_size)
2366
2366
__annotate_increase (__n - __old_size);
2367
- pointer __p =
2368
- __is_long () ? (__set_long_size (__n), __get_long_pointer ()) : (__set_short_size (__n), __get_short_pointer ());
2367
+ pointer __p;
2368
+ if (__is_long ()) {
2369
+ __set_long_size (__n);
2370
+ __p = __get_long_pointer ();
2371
+ } else {
2372
+ __set_short_size (__n);
2373
+ __p = __get_short_pointer ();
2374
+ }
2369
2375
traits_type::move (std::__to_address (__p), __s, __n);
2370
2376
traits_type::assign (__p[__n], value_type ());
2371
2377
if (__old_size > __n)
@@ -2441,8 +2447,8 @@ template <class _CharT,
2441
2447
class _Traits ,
2442
2448
class _Allocator = allocator<_CharT>,
2443
2449
class = enable_if_t <__is_allocator<_Allocator>::value> >
2444
- explicit basic_string (basic_string_view<_CharT, _Traits>,
2445
- const _Allocator& = _Allocator()) -> basic_string<_CharT, _Traits, _Allocator>;
2450
+ explicit basic_string (basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
2451
+ -> basic_string<_CharT, _Traits, _Allocator>;
2446
2452
2447
2453
template <class _CharT ,
2448
2454
class _Traits ,
@@ -2705,38 +2711,42 @@ template <class _CharT, class _Traits, class _Allocator>
2705
2711
template <bool __is_short>
2706
2712
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
2707
2713
basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias (const value_type* __s, size_type __n) {
2708
- size_type __cap = __is_short ? static_cast <size_type>(__min_cap) : __get_long_cap ();
2714
+ const auto __cap = __is_short ? static_cast <size_type>(__min_cap) : __get_long_cap ();
2715
+ const auto __size = __is_short ? __get_short_size () : __get_long_size ();
2709
2716
if (__n < __cap) {
2710
- size_type __old_size = __is_short ? __get_short_size () : __get_long_size ();
2711
- if (__n > __old_size)
2712
- __annotate_increase (__n - __old_size);
2713
- pointer __p = __is_short ? __get_short_pointer () : __get_long_pointer ();
2714
- __is_short ? __set_short_size (__n) : __set_long_size (__n);
2717
+ if (__n > __size)
2718
+ __annotate_increase (__n - __size);
2719
+ pointer __p;
2720
+ if (__is_short) {
2721
+ __p = __get_short_pointer ();
2722
+ __set_short_size (__n);
2723
+ } else {
2724
+ __p = __get_long_pointer ();
2725
+ __set_long_size (__n);
2726
+ }
2715
2727
traits_type::copy (std::__to_address (__p), __s, __n);
2716
2728
traits_type::assign (__p[__n], value_type ());
2717
- if (__old_size > __n)
2718
- __annotate_shrink (__old_size );
2729
+ if (__size > __n)
2730
+ __annotate_shrink (__size );
2719
2731
} else {
2720
- size_type __sz = __is_short ? __get_short_size () : __get_long_size ();
2721
- __grow_by_and_replace (__cap - 1 , __n - __cap + 1 , __sz, 0 , __sz, __n, __s);
2732
+ __grow_by_and_replace (__cap - 1 , __n - __cap + 1 , __size, 0 , __size, __n, __s);
2722
2733
}
2723
2734
return *this ;
2724
2735
}
2725
2736
2726
2737
template <class _CharT , class _Traits , class _Allocator >
2727
2738
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
2728
2739
basic_string<_CharT, _Traits, _Allocator>::__assign_external (const value_type* __s, size_type __n) {
2729
- size_type __cap = capacity ();
2740
+ const auto __cap = capacity ();
2741
+ const auto __size = size ();
2730
2742
if (__cap >= __n) {
2731
- size_type __old_size = size ();
2732
- if (__n > __old_size)
2733
- __annotate_increase (__n - __old_size);
2743
+ if (__n > __size)
2744
+ __annotate_increase (__n - __size);
2734
2745
value_type* __p = std::__to_address (__get_pointer ());
2735
2746
traits_type::move (__p, __s, __n);
2736
2747
return __null_terminate_at (__p, __n);
2737
2748
} else {
2738
- size_type __sz = size ();
2739
- __grow_by_and_replace (__cap, __n - __cap, __sz, 0 , __sz, __n, __s);
2749
+ __grow_by_and_replace (__cap, __n - __cap, __size, 0 , __size, __n, __s);
2740
2750
return *this ;
2741
2751
}
2742
2752
}
@@ -2754,8 +2764,7 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2754
2764
size_type __cap = capacity ();
2755
2765
size_type __old_size = size ();
2756
2766
if (__cap < __n) {
2757
- size_type __sz = size ();
2758
- __grow_by_without_replace (__cap, __n - __cap, __sz, 0 , __sz);
2767
+ __grow_by_without_replace (__cap, __n - __cap, __old_size, 0 , __old_size);
2759
2768
__annotate_increase (__n);
2760
2769
} else if (__n > __old_size)
2761
2770
__annotate_increase (__n - __old_size);
@@ -2767,10 +2776,10 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2767
2776
template <class _CharT , class _Traits , class _Allocator >
2768
2777
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2769
2778
basic_string<_CharT, _Traits, _Allocator>::operator =(value_type __c) {
2770
- pointer __p;
2771
2779
size_type __old_size = size ();
2772
2780
if (__old_size == 0 )
2773
2781
__annotate_increase (1 );
2782
+ pointer __p;
2774
2783
if (__is_long ()) {
2775
2784
__p = __get_long_pointer ();
2776
2785
__set_long_size (1 );
@@ -2793,8 +2802,8 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2793
2802
if (!__is_long ()) {
2794
2803
if (!__str.__is_long ()) {
2795
2804
size_type __old_size = __get_short_size ();
2796
- if (__get_short_size () < __str.__get_short_size ())
2797
- __annotate_increase (__str.__get_short_size () - __get_short_size () );
2805
+ if (__old_size < __str.__get_short_size ())
2806
+ __annotate_increase (__str.__get_short_size () - __old_size );
2798
2807
__rep_ = __str.__rep_ ;
2799
2808
if (__old_size > __get_short_size ())
2800
2809
__annotate_shrink (__old_size);
@@ -2961,10 +2970,9 @@ template <class _CharT, class _Traits, class _Allocator>
2961
2970
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2962
2971
basic_string<_CharT, _Traits, _Allocator>::assign (const value_type* __s) {
2963
2972
_LIBCPP_ASSERT_NON_NULL (__s != nullptr , " string::assign received nullptr" );
2964
- return __builtin_constant_p (*__s)
2965
- ? (__fits_in_sso (traits_type::length (__s)) ? __assign_short (__s, traits_type::length (__s))
2966
- : __assign_external (__s, traits_type::length (__s)))
2967
- : __assign_external (__s);
2973
+ if (auto __len = traits_type::length (__s); __builtin_constant_p (__len) && __fits_in_sso (__len))
2974
+ return __assign_short (__s, __len);
2975
+ return __assign_external (__s);
2968
2976
}
2969
2977
// append
2970
2978
@@ -3036,11 +3044,10 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pu
3036
3044
}
3037
3045
if (__sz == __cap) {
3038
3046
__grow_by_without_replace (__cap, 1 , __sz, __sz, 0 );
3039
- __annotate_increase (1 );
3040
3047
__is_short = false ; // the string is always long after __grow_by
3041
- } else
3042
- __annotate_increase (1 );
3043
- pointer __p = __get_pointer () ;
3048
+ }
3049
+ __annotate_increase (1 );
3050
+ pointer __p;
3044
3051
if (__is_short) {
3045
3052
__p = __get_short_pointer () + __sz;
3046
3053
__set_short_size (__sz + 1 );
@@ -3424,11 +3431,13 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
3424
3431
3425
3432
template <class _CharT , class _Traits , class _Allocator >
3426
3433
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear () _NOEXCEPT {
3427
- size_type __old_size = size () ;
3434
+ size_type __old_size;
3428
3435
if (__is_long ()) {
3436
+ __old_size = __get_long_size ();
3429
3437
traits_type::assign (*__get_long_pointer (), value_type ());
3430
3438
__set_long_size (0 );
3431
3439
} else {
3440
+ __old_size = __get_short_size ();
3432
3441
traits_type::assign (*__get_short_pointer (), value_type ());
3433
3442
__set_short_size (0 );
3434
3443
}
@@ -3486,11 +3495,12 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
3486
3495
_LIBCPP_ASSERT_INTERNAL (__is_long (), " Trying to shrink small string" );
3487
3496
3488
3497
// We're a long string and we're shrinking into the small buffer.
3498
+ const auto __ptr = __get_long_pointer ();
3499
+ const auto __size = __get_long_size ();
3500
+ const auto __cap = __get_long_cap ();
3501
+
3489
3502
if (__fits_in_sso (__target_capacity)) {
3490
3503
__annotation_guard __g (*this );
3491
- auto __ptr = __get_long_pointer ();
3492
- auto __size = __get_long_size ();
3493
- auto __cap = __get_long_cap ();
3494
3504
traits_type::copy (std::__to_address (__get_short_pointer ()), std::__to_address (__ptr), __size + 1 );
3495
3505
__set_short_size (__size);
3496
3506
__alloc_traits::deallocate (__alloc_, __ptr, __cap);
@@ -3501,7 +3511,6 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
3501
3511
try {
3502
3512
# endif // _LIBCPP_HAS_EXCEPTIONS
3503
3513
__annotation_guard __g (*this );
3504
- auto __size = size ();
3505
3514
auto __allocation = std::__allocate_at_least (__alloc_, __target_capacity + 1 );
3506
3515
3507
3516
// The Standard mandates shrink_to_fit() does not increase the capacity.
@@ -3513,9 +3522,8 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
3513
3522
}
3514
3523
3515
3524
__begin_lifetime (__allocation.ptr , __allocation.count );
3516
- auto __ptr = __get_long_pointer ();
3517
3525
traits_type::copy (std::__to_address (__allocation.ptr ), std::__to_address (__ptr), __size + 1 );
3518
- __alloc_traits::deallocate (__alloc_, __ptr, __get_long_cap () );
3526
+ __alloc_traits::deallocate (__alloc_, __ptr, __cap );
3519
3527
__set_long_cap (__allocation.count );
3520
3528
__set_long_pointer (__allocation.ptr );
3521
3529
# if _LIBCPP_HAS_EXCEPTIONS
0 commit comments