Skip to content

Commit 345a0f2

Browse files
committed
Simplify string a bit
1 parent 3c2ba68 commit 345a0f2

File tree

1 file changed

+34
-49
lines changed

1 file changed

+34
-49
lines changed

libcxx/include/string

Lines changed: 34 additions & 49 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,38 +2758,36 @@ 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 __cap = capacity();
2762+
size_type __old_size = size();
2763+
if (__n <= __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_short ? (__set_short_size(__n), __get_short_pointer()) : (__set_long_size(__n), __get_long_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(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
27752774
}
27762775
return *this;
27772776
}
27782777

27792778
template <class _CharT, class _Traits, class _Allocator>
27802779
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27812780
basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
2782-
size_type __cap = capacity();
2781+
size_type __cap = capacity();
2782+
size_type __old_size = size();
27832783
if (__cap >= __n) {
2784-
size_type __old_size = size();
27852784
if (__n > __old_size)
27862785
__annotate_increase(__n - __old_size);
27872786
value_type* __p = std::__to_address(__get_pointer());
27882787
traits_type::move(__p, __s, __n);
27892788
return __null_terminate_at(__p, __n);
27902789
} else {
2791-
size_type __sz = size();
2792-
__grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2790+
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
27932791
return *this;
27942792
}
27952793
}
@@ -2807,8 +2805,7 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
28072805
size_type __cap = capacity();
28082806
size_type __old_size = size();
28092807
if (__cap < __n) {
2810-
size_type __sz = size();
2811-
__grow_by_without_replace(__cap, __n - __cap, __sz, 0, __sz);
2808+
__grow_by_without_replace(__cap, __n - __cap, __old_size, 0, __old_size);
28122809
__annotate_increase(__n);
28132810
} else if (__n > __old_size)
28142811
__annotate_increase(__n - __old_size);
@@ -2820,17 +2817,10 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
28202817
template <class _CharT, class _Traits, class _Allocator>
28212818
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
28222819
basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
2823-
pointer __p;
28242820
size_type __old_size = size();
28252821
if (__old_size == 0)
28262822
__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-
}
2823+
pointer __p = __is_long() ? (__set_long_size(1), __get_long_pointer()) : (__set_short_size(1), __get_short_pointer());
28342824
traits_type::assign(*__p, __c);
28352825
traits_type::assign(*++__p, value_type());
28362826
if (__old_size > 1)
@@ -2846,8 +2836,8 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
28462836
if (!__is_long()) {
28472837
if (!__str.__is_long()) {
28482838
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());
2839+
if (__old_size < __str.__get_short_size())
2840+
__annotate_increase(__str.__get_short_size() - __old_size);
28512841
__rep_ = __str.__rep_;
28522842
if (__old_size > __get_short_size())
28532843
__annotate_shrink(__old_size);
@@ -3014,10 +3004,11 @@ template <class _CharT, class _Traits, class _Allocator>
30143004
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
30153005
basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
30163006
_LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
3017-
return __builtin_constant_p(*__s)
3018-
? (__fits_in_sso(traits_type::length(__s)) ? __assign_short(__s, traits_type::length(__s))
3019-
: __assign_external(__s, traits_type::length(__s)))
3020-
: __assign_external(__s);
3007+
if (__builtin_constant_p(*__s)) {
3008+
const size_type __n = traits_type::length(__s);
3009+
return __fits_in_sso(__n) ? __assign_short(__s, __n) : __assign_external(__s, __n);
3010+
}
3011+
return __assign_external(__s);
30213012
}
30223013
// append
30233014

@@ -3089,18 +3080,11 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pu
30893080
}
30903081
if (__sz == __cap) {
30913082
__grow_by_without_replace(__cap, 1, __sz, __sz, 0);
3092-
__annotate_increase(1);
30933083
__is_short = false; // the string is always long after __grow_by
3094-
} else
3095-
__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);
31033084
}
3085+
__annotate_increase(1);
3086+
pointer __p = __is_short ? (__set_short_size(__sz + 1), __get_short_pointer() + __sz)
3087+
: (__set_long_size(__sz + 1), __get_long_pointer() + __sz);
31043088
traits_type::assign(*__p, __c);
31053089
traits_type::assign(*++__p, value_type());
31063090
}
@@ -3477,11 +3461,13 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34773461

34783462
template <class _CharT, class _Traits, class _Allocator>
34793463
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {
3480-
size_type __old_size = size();
3464+
size_type __old_size;
34813465
if (__is_long()) {
3466+
__old_size = __get_long_size();
34823467
traits_type::assign(*__get_long_pointer(), value_type());
34833468
__set_long_size(0);
34843469
} else {
3470+
__old_size = __get_short_size();
34853471
traits_type::assign(*__get_short_pointer(), value_type());
34863472
__set_short_size(0);
34873473
}
@@ -3539,11 +3525,12 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
35393525
_LIBCPP_ASSERT_INTERNAL(__is_long(), "Trying to shrink small string");
35403526

35413527
// We're a long string and we're shrinking into the small buffer.
3528+
auto __ptr = __get_long_pointer();
3529+
auto __size = __get_long_size();
3530+
auto __cap = __get_long_cap();
3531+
35423532
if (__fits_in_sso(__target_capacity)) {
35433533
__annotation_guard __g(*this);
3544-
auto __ptr = __get_long_pointer();
3545-
auto __size = __get_long_size();
3546-
auto __cap = __get_long_cap();
35473534
traits_type::copy(std::__to_address(__get_short_pointer()), std::__to_address(__ptr), __size + 1);
35483535
__set_short_size(__size);
35493536
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
@@ -3554,21 +3541,19 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
35543541
try {
35553542
# endif // _LIBCPP_HAS_EXCEPTIONS
35563543
__annotation_guard __g(*this);
3557-
auto __size = size();
35583544
auto __allocation = std::__allocate_at_least(__alloc_, __target_capacity + 1);
35593545

35603546
// The Standard mandates shrink_to_fit() does not increase the capacity.
35613547
// With equal capacity keep the existing buffer. This avoids extra work
35623548
// due to swapping the elements.
3563-
if (__allocation.count - 1 > capacity()) {
3549+
if (__allocation.count > __cap) {
35643550
__alloc_traits::deallocate(__alloc_, __allocation.ptr, __allocation.count);
35653551
return;
35663552
}
35673553

35683554
__begin_lifetime(__allocation.ptr, __allocation.count);
3569-
auto __ptr = __get_long_pointer();
35703555
traits_type::copy(std::__to_address(__allocation.ptr), std::__to_address(__ptr), __size + 1);
3571-
__alloc_traits::deallocate(__alloc_, __ptr, __get_long_cap());
3556+
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
35723557
__set_long_cap(__allocation.count);
35733558
__set_long_pointer(__allocation.ptr);
35743559
# if _LIBCPP_HAS_EXCEPTIONS

0 commit comments

Comments
 (0)