Skip to content

Commit d911cc1

Browse files
committed
Simplify string a bit
1 parent 79fff6a commit d911cc1

File tree

1 file changed

+22
-35
lines changed

1 file changed

+22
-35
lines changed

libcxx/include/string

Lines changed: 22 additions & 35 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,19 @@ 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();
2762-
if (__n < __cap) {
2763-
size_type __old_size = __is_short ? __get_short_size() : __get_long_size();
2761+
size_type __old_size = size();
2762+
size_type __old_cap = capacity();
2763+
if (__n <= __old_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_long() ? (__set_long_size(__n), __get_long_pointer()) : (__set_short_size(__n), __get_short_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(__old_cap, __n - __old_cap, __old_size, 0, __old_size, __n, __s);
27752774
}
27762775
return *this;
27772776
}
@@ -2820,17 +2819,10 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
28202819
template <class _CharT, class _Traits, class _Allocator>
28212820
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
28222821
basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
2823-
pointer __p;
28242822
size_type __old_size = size();
28252823
if (__old_size == 0)
28262824
__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-
}
2825+
pointer __p = __is_long() ? (__set_long_size(1), __get_long_pointer()) : (__set_short_size(1), __get_short_pointer());
28342826
traits_type::assign(*__p, __c);
28352827
traits_type::assign(*++__p, value_type());
28362828
if (__old_size > 1)
@@ -3093,14 +3085,8 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pu
30933085
__is_short = false; // the string is always long after __grow_by
30943086
} else
30953087
__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-
}
3088+
pointer __p = __is_short ? (__set_short_size(__sz + 1), __get_short_pointer() + __sz)
3089+
: (__set_long_size(__sz + 1), __get_long_pointer() + __sz);
31043090
traits_type::assign(*__p, __c);
31053091
traits_type::assign(*++__p, value_type());
31063092
}
@@ -3477,11 +3463,13 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34773463

34783464
template <class _CharT, class _Traits, class _Allocator>
34793465
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {
3480-
size_type __old_size = size();
3466+
size_type __old_size;
34813467
if (__is_long()) {
3468+
__old_size = __get_long_size();
34823469
traits_type::assign(*__get_long_pointer(), value_type());
34833470
__set_long_size(0);
34843471
} else {
3472+
__old_size = __get_short_size();
34853473
traits_type::assign(*__get_short_pointer(), value_type());
34863474
__set_short_size(0);
34873475
}
@@ -3539,11 +3527,12 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
35393527
_LIBCPP_ASSERT_INTERNAL(__is_long(), "Trying to shrink small string");
35403528

35413529
// We're a long string and we're shrinking into the small buffer.
3530+
auto __ptr = __get_long_pointer();
3531+
auto __size = __get_long_size();
3532+
auto __cap = __get_long_cap();
3533+
35423534
if (__fits_in_sso(__target_capacity)) {
35433535
__annotation_guard __g(*this);
3544-
auto __ptr = __get_long_pointer();
3545-
auto __size = __get_long_size();
3546-
auto __cap = __get_long_cap();
35473536
traits_type::copy(std::__to_address(__get_short_pointer()), std::__to_address(__ptr), __size + 1);
35483537
__set_short_size(__size);
35493538
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
@@ -3554,21 +3543,19 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
35543543
try {
35553544
# endif // _LIBCPP_HAS_EXCEPTIONS
35563545
__annotation_guard __g(*this);
3557-
auto __size = size();
35583546
auto __allocation = std::__allocate_at_least(__alloc_, __target_capacity + 1);
35593547

35603548
// The Standard mandates shrink_to_fit() does not increase the capacity.
35613549
// With equal capacity keep the existing buffer. This avoids extra work
35623550
// due to swapping the elements.
3563-
if (__allocation.count - 1 > capacity()) {
3551+
if (__allocation.count > __cap) {
35643552
__alloc_traits::deallocate(__alloc_, __allocation.ptr, __allocation.count);
35653553
return;
35663554
}
35673555

35683556
__begin_lifetime(__allocation.ptr, __allocation.count);
3569-
auto __ptr = __get_long_pointer();
35703557
traits_type::copy(std::__to_address(__allocation.ptr), std::__to_address(__ptr), __size + 1);
3571-
__alloc_traits::deallocate(__alloc_, __ptr, __get_long_cap());
3558+
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
35723559
__set_long_cap(__allocation.count);
35733560
__set_long_pointer(__allocation.ptr);
35743561
# if _LIBCPP_HAS_EXCEPTIONS

0 commit comments

Comments
 (0)