Skip to content

Commit 99381cd

Browse files
committed
[libc++] Introduce basic_string::__recommend_growing
1 parent e95c5c8 commit 99381cd

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

libcxx/include/string

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,6 +2384,16 @@ private:
23842384
return __guess;
23852385
}
23862386

2387+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend_growing(size_type __required_size) {
2388+
size_type __max_size = max_size();
2389+
if (__required_size > __max_size)
2390+
__throw_length_error();
2391+
size_type __current_cap = capacity();
2392+
if (__current_cap > __max_size / 2 - __alignment)
2393+
return __max_size;
2394+
return std::max(__required_size, 2 * __current_cap);
2395+
}
2396+
23872397
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(const value_type* __s, size_type __sz);
23882398
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(size_type __n, value_type __c);
23892399

@@ -2714,14 +2724,10 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__
27142724
size_type __n_del,
27152725
size_type __n_add,
27162726
const value_type* __p_new_stuff) {
2717-
size_type __ms = max_size();
2718-
if (__delta_cap > __ms - __old_cap)
2719-
__throw_length_error();
2727+
__long __buffer = __allocate_long_buffer(__alloc_, __recommend_growing(__old_cap + __delta_cap));
27202728
pointer __old_p = __get_pointer();
2721-
size_type __cap = __old_cap < __ms / 2 - __alignment ? std::max(__old_cap + __delta_cap, 2 * __old_cap) : __ms;
27222729
__annotate_delete();
2723-
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2724-
__long __buffer = __allocate_long_buffer(__alloc_, __cap);
2730+
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
27252731
if (__n_copy != 0)
27262732
traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__old_p), __n_copy);
27272733
if (__n_add != 0)
@@ -2751,12 +2757,8 @@ _LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Trait
27512757
size_type __n_copy,
27522758
size_type __n_del,
27532759
size_type __n_add) {
2754-
size_type __ms = max_size();
2755-
if (__delta_cap > __ms - __old_cap)
2756-
this->__throw_length_error();
2760+
__long __buffer = __allocate_long_buffer(__alloc_, __recommend_growing(__old_cap + __delta_cap));
27572761
pointer __old_p = __get_pointer();
2758-
size_type __cap = __old_cap < __ms / 2 - __alignment ? std::max(__old_cap + __delta_cap, 2 * __old_cap) : __ms;
2759-
__long __buffer = __allocate_long_buffer(__alloc_, __cap);
27602762
if (__n_copy != 0)
27612763
traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__old_p), __n_copy);
27622764
size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;

0 commit comments

Comments
 (0)