Skip to content

Commit 9e89ce6

Browse files
committed
Simplify string a bit
1 parent 3c2ba68 commit 9e89ce6

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

libcxx/include/string

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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

24762476
template <class _CharT,
24772477
class _Traits,
@@ -2758,20 +2758,20 @@ template <class _CharT, class _Traits, class _Allocator>
27582758
template <bool __is_short>
27592759
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27602760
basic_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();
2761+
size_type __old_size;
2762+
size_type __cap =
2763+
__is_short ? (__old_size = __get_short_size(), __min_cap) : (__old_size = __get_long_size(), __get_long_cap());
27622764
if (__n < __cap) {
2763-
size_type __old_size = __is_short ? __get_short_size() : __get_long_size();
27642765
if (__n > __old_size)
27652766
__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);
2767+
pointer __p =
2768+
__is_short ? (__set_short_size(__n), __get_short_pointer()) : (__set_long_size(__n), __get_long_pointer());
27682769
traits_type::copy(std::__to_address(__p), __s, __n);
27692770
traits_type::assign(__p[__n], value_type());
27702771
if (__old_size > __n)
27712772
__annotate_shrink(__old_size);
27722773
} 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);
2774+
__grow_by_and_replace(__cap - 1, __n - __cap + 1, __old_size, 0, __old_size, __n, __s);
27752775
}
27762776
return *this;
27772777
}
@@ -2820,17 +2820,10 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
28202820
template <class _CharT, class _Traits, class _Allocator>
28212821
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
28222822
basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
2823-
pointer __p;
28242823
size_type __old_size = size();
28252824
if (__old_size == 0)
28262825
__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-
}
2826+
pointer __p = __is_long() ? (__set_long_size(1), __get_long_pointer()) : (__set_short_size(1), __get_short_pointer());
28342827
traits_type::assign(*__p, __c);
28352828
traits_type::assign(*++__p, value_type());
28362829
if (__old_size > 1)
@@ -2846,8 +2839,8 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
28462839
if (!__is_long()) {
28472840
if (!__str.__is_long()) {
28482841
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());
2842+
if (__old_size < __str.__get_short_size())
2843+
__annotate_increase(__str.__get_short_size() - __old_size);
28512844
__rep_ = __str.__rep_;
28522845
if (__old_size > __get_short_size())
28532846
__annotate_shrink(__old_size);
@@ -3093,14 +3086,8 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pu
30933086
__is_short = false; // the string is always long after __grow_by
30943087
} else
30953088
__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);
3103-
}
3089+
pointer __p = __is_short ? (__set_short_size(__sz + 1), __get_short_pointer() + __sz)
3090+
: (__set_long_size(__sz + 1), __get_long_pointer() + __sz);
31043091
traits_type::assign(*__p, __c);
31053092
traits_type::assign(*++__p, value_type());
31063093
}
@@ -3477,11 +3464,13 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34773464

34783465
template <class _CharT, class _Traits, class _Allocator>
34793466
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {
3480-
size_type __old_size = size();
3467+
size_type __old_size;
34813468
if (__is_long()) {
3469+
__old_size = __get_long_size();
34823470
traits_type::assign(*__get_long_pointer(), value_type());
34833471
__set_long_size(0);
34843472
} else {
3473+
__old_size = __get_short_size();
34853474
traits_type::assign(*__get_short_pointer(), value_type());
34863475
__set_short_size(0);
34873476
}
@@ -3539,11 +3528,12 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
35393528
_LIBCPP_ASSERT_INTERNAL(__is_long(), "Trying to shrink small string");
35403529

35413530
// We're a long string and we're shrinking into the small buffer.
3531+
auto __ptr = __get_long_pointer();
3532+
auto __size = __get_long_size();
3533+
auto __cap = __get_long_cap();
3534+
35423535
if (__fits_in_sso(__target_capacity)) {
35433536
__annotation_guard __g(*this);
3544-
auto __ptr = __get_long_pointer();
3545-
auto __size = __get_long_size();
3546-
auto __cap = __get_long_cap();
35473537
traits_type::copy(std::__to_address(__get_short_pointer()), std::__to_address(__ptr), __size + 1);
35483538
__set_short_size(__size);
35493539
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
@@ -3554,21 +3544,19 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
35543544
try {
35553545
# endif // _LIBCPP_HAS_EXCEPTIONS
35563546
__annotation_guard __g(*this);
3557-
auto __size = size();
35583547
auto __allocation = std::__allocate_at_least(__alloc_, __target_capacity + 1);
35593548

35603549
// The Standard mandates shrink_to_fit() does not increase the capacity.
35613550
// With equal capacity keep the existing buffer. This avoids extra work
35623551
// due to swapping the elements.
3563-
if (__allocation.count - 1 > capacity()) {
3552+
if (__allocation.count > __cap) {
35643553
__alloc_traits::deallocate(__alloc_, __allocation.ptr, __allocation.count);
35653554
return;
35663555
}
35673556

35683557
__begin_lifetime(__allocation.ptr, __allocation.count);
3569-
auto __ptr = __get_long_pointer();
35703558
traits_type::copy(std::__to_address(__allocation.ptr), std::__to_address(__ptr), __size + 1);
3571-
__alloc_traits::deallocate(__alloc_, __ptr, __get_long_cap());
3559+
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
35723560
__set_long_cap(__allocation.count);
35733561
__set_long_pointer(__allocation.ptr);
35743562
# if _LIBCPP_HAS_EXCEPTIONS

0 commit comments

Comments
 (0)