@@ -2335,7 +2335,7 @@ private:
23352335 if (!__str.__is_long ()) {
23362336 if (__is_long ()) {
23372337 __annotate_delete ();
2338- __alloc_traits::deallocate (__alloc_, __get_long_pointer (), capacity () + 1 );
2338+ __alloc_traits::deallocate (__alloc_, __get_long_pointer (), __get_long_cap () );
23392339 __rep_ = __rep ();
23402340 }
23412341 __alloc_ = __str.__alloc_ ;
@@ -2350,7 +2350,7 @@ private:
23502350 __alloc_ = std::move (__a);
23512351 __set_long_pointer (__allocation.ptr );
23522352 __set_long_cap (__allocation.count );
2353- __set_long_size (__str.size ());
2353+ __set_long_size (__str.__get_long_size ());
23542354 }
23552355 }
23562356 }
@@ -2470,8 +2470,8 @@ template <class _CharT,
24702470 class _Traits ,
24712471 class _Allocator = allocator<_CharT>,
24722472 class = enable_if_t <__is_allocator<_Allocator>::value> >
2473- explicit basic_string (basic_string_view<_CharT, _Traits>,
2474- const _Allocator& = _Allocator()) -> basic_string<_CharT, _Traits, _Allocator>;
2473+ explicit basic_string (basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
2474+ -> basic_string<_CharT, _Traits, _Allocator>;
24752475
24762476template <class _CharT ,
24772477 class _Traits ,
@@ -2758,38 +2758,36 @@ template <class _CharT, class _Traits, class _Allocator>
27582758template <bool __is_short>
27592759_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27602760basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias (const value_type* __s, size_type __n) {
2761- size_type __cap = __is_short ? static_cast <size_type>(__min_cap) : __get_long_cap ();
2762- if (__n < __cap) {
2763- size_type __old_size = __is_short ? __get_short_size () : __get_long_size ();
2761+ size_type __cap = capacity ();
2762+ size_type __old_size = size ();
2763+ if (__n <= __cap) {
27642764 if (__n > __old_size)
27652765 __annotate_increase (__n - __old_size);
2766- pointer __p = __is_short ? __get_short_pointer () : __get_long_pointer ();
2767- __is_short ? __set_short_size (__n) : __set_long_size (__n);
2766+ pointer __p =
2767+ __is_short ? ( __set_short_size (__n), __get_short_pointer ()) : ( __set_long_size (__n), __get_long_pointer () );
27682768 traits_type::copy (std::__to_address (__p), __s, __n);
27692769 traits_type::assign (__p[__n], value_type ());
27702770 if (__old_size > __n)
27712771 __annotate_shrink (__old_size);
27722772 } else {
2773- size_type __sz = __is_short ? __get_short_size () : __get_long_size ();
2774- __grow_by_and_replace (__cap - 1 , __n - __cap + 1 , __sz, 0 , __sz, __n, __s);
2773+ __grow_by_and_replace (__cap, __n - __cap, __old_size, 0 , __old_size, __n, __s);
27752774 }
27762775 return *this ;
27772776}
27782777
27792778template <class _CharT , class _Traits , class _Allocator >
27802779_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27812780basic_string<_CharT, _Traits, _Allocator>::__assign_external (const value_type* __s, size_type __n) {
2782- size_type __cap = capacity ();
2781+ size_type __cap = capacity ();
2782+ size_type __old_size = size ();
27832783 if (__cap >= __n) {
2784- size_type __old_size = size ();
27852784 if (__n > __old_size)
27862785 __annotate_increase (__n - __old_size);
27872786 value_type* __p = std::__to_address (__get_pointer ());
27882787 traits_type::move (__p, __s, __n);
27892788 return __null_terminate_at (__p, __n);
27902789 } else {
2791- size_type __sz = size ();
2792- __grow_by_and_replace (__cap, __n - __cap, __sz, 0 , __sz, __n, __s);
2790+ __grow_by_and_replace (__cap, __n - __cap, __old_size, 0 , __old_size, __n, __s);
27932791 return *this ;
27942792 }
27952793}
@@ -2807,8 +2805,7 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
28072805 size_type __cap = capacity ();
28082806 size_type __old_size = size ();
28092807 if (__cap < __n) {
2810- size_type __sz = size ();
2811- __grow_by_without_replace (__cap, __n - __cap, __sz, 0 , __sz);
2808+ __grow_by_without_replace (__cap, __n - __cap, __old_size, 0 , __old_size);
28122809 __annotate_increase (__n);
28132810 } else if (__n > __old_size)
28142811 __annotate_increase (__n - __old_size);
@@ -2820,17 +2817,10 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
28202817template <class _CharT , class _Traits , class _Allocator >
28212818_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
28222819basic_string<_CharT, _Traits, _Allocator>::operator =(value_type __c) {
2823- pointer __p;
28242820 size_type __old_size = size ();
28252821 if (__old_size == 0 )
28262822 __annotate_increase (1 );
2827- if (__is_long ()) {
2828- __p = __get_long_pointer ();
2829- __set_long_size (1 );
2830- } else {
2831- __p = __get_short_pointer ();
2832- __set_short_size (1 );
2833- }
2823+ pointer __p = __is_long () ? (__set_long_size (1 ), __get_long_pointer ()) : (__set_short_size (1 ), __get_short_pointer ());
28342824 traits_type::assign (*__p, __c);
28352825 traits_type::assign (*++__p, value_type ());
28362826 if (__old_size > 1 )
@@ -2846,8 +2836,8 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
28462836 if (!__is_long ()) {
28472837 if (!__str.__is_long ()) {
28482838 size_type __old_size = __get_short_size ();
2849- if (__get_short_size () < __str.__get_short_size ())
2850- __annotate_increase (__str.__get_short_size () - __get_short_size () );
2839+ if (__old_size < __str.__get_short_size ())
2840+ __annotate_increase (__str.__get_short_size () - __old_size );
28512841 __rep_ = __str.__rep_ ;
28522842 if (__old_size > __get_short_size ())
28532843 __annotate_shrink (__old_size);
@@ -3014,9 +3004,9 @@ template <class _CharT, class _Traits, class _Allocator>
30143004_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
30153005basic_string<_CharT, _Traits, _Allocator>::assign (const value_type* __s) {
30163006 _LIBCPP_ASSERT_NON_NULL (__s != nullptr , " string::assign received nullptr" );
3007+ size_type __n = traits_type::length (__s);
30173008 return __builtin_constant_p (*__s)
3018- ? (__fits_in_sso (traits_type::length (__s)) ? __assign_short (__s, traits_type::length (__s))
3019- : __assign_external (__s, traits_type::length (__s)))
3009+ ? (__fits_in_sso (__n) ? __assign_short (__s, __n) : __assign_external (__s, __n))
30203010 : __assign_external (__s);
30213011}
30223012// append
@@ -3089,18 +3079,11 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pu
30893079 }
30903080 if (__sz == __cap) {
30913081 __grow_by_without_replace (__cap, 1 , __sz, __sz, 0 );
3092- __annotate_increase (1 );
30933082 __is_short = false ; // the string is always long after __grow_by
3094- } else
3095- __annotate_increase (1 );
3096- pointer __p = __get_pointer ();
3097- if (__is_short) {
3098- __p = __get_short_pointer () + __sz;
3099- __set_short_size (__sz + 1 );
3100- } else {
3101- __p = __get_long_pointer () + __sz;
3102- __set_long_size (__sz + 1 );
31033083 }
3084+ __annotate_increase (1 );
3085+ pointer __p = __is_short ? (__set_short_size (__sz + 1 ), __get_short_pointer () + __sz)
3086+ : (__set_long_size (__sz + 1 ), __get_long_pointer () + __sz);
31043087 traits_type::assign (*__p, __c);
31053088 traits_type::assign (*++__p, value_type ());
31063089}
@@ -3477,11 +3460,13 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34773460
34783461template <class _CharT , class _Traits , class _Allocator >
34793462inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear () _NOEXCEPT {
3480- size_type __old_size = size () ;
3463+ size_type __old_size;
34813464 if (__is_long ()) {
3465+ __old_size = __get_long_size ();
34823466 traits_type::assign (*__get_long_pointer (), value_type ());
34833467 __set_long_size (0 );
34843468 } else {
3469+ __old_size = __get_short_size ();
34853470 traits_type::assign (*__get_short_pointer (), value_type ());
34863471 __set_short_size (0 );
34873472 }
@@ -3539,11 +3524,12 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
35393524 _LIBCPP_ASSERT_INTERNAL (__is_long (), " Trying to shrink small string" );
35403525
35413526 // We're a long string and we're shrinking into the small buffer.
3527+ auto __ptr = __get_long_pointer ();
3528+ auto __size = __get_long_size ();
3529+ auto __cap = __get_long_cap ();
3530+
35423531 if (__fits_in_sso (__target_capacity)) {
35433532 __annotation_guard __g (*this );
3544- auto __ptr = __get_long_pointer ();
3545- auto __size = __get_long_size ();
3546- auto __cap = __get_long_cap ();
35473533 traits_type::copy (std::__to_address (__get_short_pointer ()), std::__to_address (__ptr), __size + 1 );
35483534 __set_short_size (__size);
35493535 __alloc_traits::deallocate (__alloc_, __ptr, __cap);
@@ -3554,21 +3540,19 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
35543540 try {
35553541# endif // _LIBCPP_HAS_EXCEPTIONS
35563542 __annotation_guard __g (*this );
3557- auto __size = size ();
35583543 auto __allocation = std::__allocate_at_least (__alloc_, __target_capacity + 1 );
35593544
35603545 // The Standard mandates shrink_to_fit() does not increase the capacity.
35613546 // With equal capacity keep the existing buffer. This avoids extra work
35623547 // due to swapping the elements.
3563- if (__allocation.count - 1 > capacity () ) {
3548+ if (__allocation.count > __cap ) {
35643549 __alloc_traits::deallocate (__alloc_, __allocation.ptr , __allocation.count );
35653550 return ;
35663551 }
35673552
35683553 __begin_lifetime (__allocation.ptr , __allocation.count );
3569- auto __ptr = __get_long_pointer ();
35703554 traits_type::copy (std::__to_address (__allocation.ptr ), std::__to_address (__ptr), __size + 1 );
3571- __alloc_traits::deallocate (__alloc_, __ptr, __get_long_cap () );
3555+ __alloc_traits::deallocate (__alloc_, __ptr, __cap );
35723556 __set_long_cap (__allocation.count );
35733557 __set_long_pointer (__allocation.ptr );
35743558# if _LIBCPP_HAS_EXCEPTIONS
0 commit comments