Skip to content

Commit 859ea3a

Browse files
committed
[libc++] Merge basic_string::__{replace,reset}_internal_buffer
1 parent d30bd27 commit 859ea3a

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

libcxx/include/string

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,10 @@ private:
914914
union __rep {
915915
__short __s;
916916
__long __l;
917+
918+
__rep() = default;
919+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __rep(__short __r) : __s(__r) {}
920+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __rep(__long __r) : __l(__r) {}
917921
};
918922

919923
_LIBCPP_COMPRESSED_PAIR(__rep, __rep_, allocator_type, __alloc_);
@@ -2259,18 +2263,12 @@ private:
22592263
return __long(__buffer, __capacity);
22602264
}
22612265

2262-
// Deallocate the long buffer if it exists and clear the short buffer so we are an empty string
2263-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __reset_internal_buffer() {
2266+
// Replace the current buffer with __new_rep. Deallocate the old long buffer if it exists.
2267+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __reset_internal_buffer(__rep __new_rep = __short()) {
22642268
__annotate_delete();
22652269
if (__is_long())
22662270
__alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
2267-
__rep_.__s = __short();
2268-
}
2269-
2270-
// Replace the current buffer with __alloc; the first __size elements constitute a string
2271-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __replace_internal_buffer(__long __alloc) {
2272-
__reset_internal_buffer();
2273-
__rep_.__l = __alloc;
2271+
__rep_ = __new_rep;
22742272
}
22752273

22762274
// Initialize the internal buffer to hold __size elements
@@ -2444,7 +2442,7 @@ private:
24442442
__annotate_delete();
24452443
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
24462444
auto __alloc = __str.__alloc_;
2447-
__replace_internal_buffer(__allocate_long_buffer(__alloc, __str.size()));
2445+
__reset_internal_buffer(__allocate_long_buffer(__alloc, __str.size()));
24482446
__alloc_ = std::move(__alloc);
24492447
}
24502448
}
@@ -2710,7 +2708,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__
27102708
__sec_cp_sz);
27112709
__buffer.__size_ = __n_copy + __n_add + __sec_cp_sz;
27122710
traits_type::assign(__buffer.__data_[__buffer.__size_], value_type());
2713-
__replace_internal_buffer(__buffer);
2711+
__reset_internal_buffer(__buffer);
27142712
}
27152713

27162714
// __grow_by is deprecated because it does not set the size. It may not update the size when the size is changed, and it
@@ -2746,7 +2744,7 @@ _LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Trait
27462744
// This is -1 to make sure the caller sets the size properly, since old versions of this function didn't set the size
27472745
// at all.
27482746
__buffer.__size_ = -1;
2749-
__replace_internal_buffer(__buffer);
2747+
__reset_internal_buffer(__buffer);
27502748
}
27512749

27522750
template <class _CharT, class _Traits, class _Allocator>
@@ -3394,7 +3392,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::re
33943392
__long __buffer = __allocate_long_buffer(__alloc_, __requested_capacity);
33953393
__buffer.__size_ = size();
33963394
traits_type::copy(std::__to_address(__buffer.__data_), data(), __buffer.__size_ + 1);
3397-
__replace_internal_buffer(__buffer);
3395+
__reset_internal_buffer(__buffer);
33983396
}
33993397

34003398
template <class _CharT, class _Traits, class _Allocator>
@@ -3433,7 +3431,7 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34333431
}
34343432

34353433
traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__get_long_pointer()), __size + 1);
3436-
__replace_internal_buffer(__buffer);
3434+
__reset_internal_buffer(__buffer);
34373435
# if _LIBCPP_HAS_EXCEPTIONS
34383436
} catch (...) {
34393437
return;

0 commit comments

Comments
 (0)