@@ -2357,6 +2357,16 @@ private:
23572357 return __guess;
23582358 }
23592359
2360+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend_growing (size_type __required_size) {
2361+ size_type __max_size = max_size ();
2362+ if (__required_size > __max_size)
2363+ __throw_length_error ();
2364+ size_type __current_cap = capacity ();
2365+ if (__current_cap > __max_size / 2 - __alignment)
2366+ return __max_size;
2367+ return std::max (__required_size, 2 * __current_cap);
2368+ }
2369+
23602370 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init (const value_type* __s, size_type __sz);
23612371 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init (size_type __n, value_type __c);
23622372
@@ -2701,15 +2711,10 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__
27012711 size_type __n_del,
27022712 size_type __n_add,
27032713 const value_type* __p_new_stuff) {
2704- size_type __ms = max_size ();
2705- if (__delta_cap > __ms - __old_cap)
2706- __throw_length_error ();
2714+ __long __buffer = __allocate_long_buffer (__alloc_, __recommend_growing (__old_cap + __delta_cap));
27072715 pointer __old_p = __get_pointer ();
2708- size_type __cap =
2709- __old_cap < __ms / 2 - __alignment ? __recommend (std::max (__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
27102716 __annotate_delete ();
27112717 auto __guard = std::__make_scope_guard (__annotate_new_size (*this ));
2712- __long __buffer = __allocate_long_buffer (__alloc_, __cap);
27132718 if (__n_copy != 0 )
27142719 traits_type::copy (std::__to_address (__buffer.__data_ ), std::__to_address (__old_p), __n_copy);
27152720 if (__n_add != 0 )
@@ -2739,13 +2744,8 @@ _LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Trait
27392744 size_type __n_copy,
27402745 size_type __n_del,
27412746 size_type __n_add) {
2742- size_type __ms = max_size ();
2743- if (__delta_cap > __ms - __old_cap)
2744- this ->__throw_length_error ();
2747+ __long __buffer = __allocate_long_buffer (__alloc_, __recommend_growing (__old_cap + __delta_cap));
27452748 pointer __old_p = __get_pointer ();
2746- size_type __cap =
2747- __old_cap < __ms / 2 - __alignment ? __recommend (std::max (__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
2748- __long __buffer = __allocate_long_buffer (__alloc_, __cap);
27492749 if (__n_copy != 0 )
27502750 traits_type::copy (std::__to_address (__buffer.__data_ ), std::__to_address (__old_p), __n_copy);
27512751 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
0 commit comments