Skip to content

Commit 43a2800

Browse files
committed
Simplify string a bit
1 parent 3c90c90 commit 43a2800

File tree

1 file changed

+36
-51
lines changed

1 file changed

+36
-51
lines changed

libcxx/include/string

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ public:
235235
template <class T>
236236
basic_string& insert(size_type pos1, const T& t); // constexpr since C++20
237237
basic_string& insert(size_type pos1, const basic_string& str,
238-
size_type pos2, size_type n); // constexpr since C++20
238+
size_type pos2, size_type n2=npos); // constexpr since C++20
239239
template <class T>
240-
basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17, constexpr since C++20
240+
basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n=npos); // C++17, constexpr since C++20
241241
basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20
242242
basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20
243243
basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20
@@ -260,7 +260,7 @@ public:
260260
size_type pos2, size_type n2=npos); // C++14, constexpr since C++20
261261
template <class T>
262262
basic_string& replace(size_type pos1, size_type n1, const T& t,
263-
size_type pos2, size_type n); // C++17, constexpr since C++20
263+
size_type pos2, size_type n2=npos); // C++17, constexpr since C++20
264264
basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20
265265
basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20
266266
basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20
@@ -2307,7 +2307,7 @@ private:
23072307
if (!__str.__is_long()) {
23082308
if (__is_long()) {
23092309
__annotate_delete();
2310-
__alloc_traits::deallocate(__alloc_, __get_long_pointer(), capacity() + 1);
2310+
__alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
23112311
__rep_ = __rep();
23122312
}
23132313
__alloc_ = __str.__alloc_;
@@ -2322,7 +2322,7 @@ private:
23222322
__alloc_ = std::move(__a);
23232323
__set_long_pointer(__allocation.ptr);
23242324
__set_long_cap(__allocation.count);
2325-
__set_long_size(__str.size());
2325+
__set_long_size(__str.__get_long_size());
23262326
}
23272327
}
23282328
}
@@ -2441,8 +2441,8 @@ template <class _CharT,
24412441
class _Traits,
24422442
class _Allocator = allocator<_CharT>,
24432443
class = enable_if_t<__is_allocator<_Allocator>::value> >
2444-
explicit basic_string(basic_string_view<_CharT, _Traits>,
2445-
const _Allocator& = _Allocator()) -> basic_string<_CharT, _Traits, _Allocator>;
2444+
explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
2445+
-> basic_string<_CharT, _Traits, _Allocator>;
24462446

24472447
template <class _CharT,
24482448
class _Traits,
@@ -2705,38 +2705,36 @@ template <class _CharT, class _Traits, class _Allocator>
27052705
template <bool __is_short>
27062706
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27072707
basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {
2708-
size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
2709-
if (__n < __cap) {
2710-
size_type __old_size = __is_short ? __get_short_size() : __get_long_size();
2708+
size_type __cap = capacity();
2709+
size_type __old_size = size();
2710+
if (__n <= __cap) {
27112711
if (__n > __old_size)
27122712
__annotate_increase(__n - __old_size);
2713-
pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2714-
__is_short ? __set_short_size(__n) : __set_long_size(__n);
2713+
pointer __p =
2714+
__is_short ? (__set_short_size(__n), __get_short_pointer()) : (__set_long_size(__n), __get_long_pointer());
27152715
traits_type::copy(std::__to_address(__p), __s, __n);
27162716
traits_type::assign(__p[__n], value_type());
27172717
if (__old_size > __n)
27182718
__annotate_shrink(__old_size);
27192719
} else {
2720-
size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2721-
__grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2720+
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
27222721
}
27232722
return *this;
27242723
}
27252724

27262725
template <class _CharT, class _Traits, class _Allocator>
27272726
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27282727
basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
2729-
size_type __cap = capacity();
2728+
size_type __cap = capacity();
2729+
size_type __old_size = size();
27302730
if (__cap >= __n) {
2731-
size_type __old_size = size();
27322731
if (__n > __old_size)
27332732
__annotate_increase(__n - __old_size);
27342733
value_type* __p = std::__to_address(__get_pointer());
27352734
traits_type::move(__p, __s, __n);
27362735
return __null_terminate_at(__p, __n);
27372736
} else {
2738-
size_type __sz = size();
2739-
__grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2737+
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
27402738
return *this;
27412739
}
27422740
}
@@ -2754,8 +2752,7 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
27542752
size_type __cap = capacity();
27552753
size_type __old_size = size();
27562754
if (__cap < __n) {
2757-
size_type __sz = size();
2758-
__grow_by_without_replace(__cap, __n - __cap, __sz, 0, __sz);
2755+
__grow_by_without_replace(__cap, __n - __cap, __old_size, 0, __old_size);
27592756
__annotate_increase(__n);
27602757
} else if (__n > __old_size)
27612758
__annotate_increase(__n - __old_size);
@@ -2767,17 +2764,10 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
27672764
template <class _CharT, class _Traits, class _Allocator>
27682765
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
27692766
basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
2770-
pointer __p;
27712767
size_type __old_size = size();
27722768
if (__old_size == 0)
27732769
__annotate_increase(1);
2774-
if (__is_long()) {
2775-
__p = __get_long_pointer();
2776-
__set_long_size(1);
2777-
} else {
2778-
__p = __get_short_pointer();
2779-
__set_short_size(1);
2780-
}
2770+
pointer __p = __is_long() ? (__set_long_size(1), __get_long_pointer()) : (__set_short_size(1), __get_short_pointer());
27812771
traits_type::assign(*__p, __c);
27822772
traits_type::assign(*++__p, value_type());
27832773
if (__old_size > 1)
@@ -2793,8 +2783,8 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
27932783
if (!__is_long()) {
27942784
if (!__str.__is_long()) {
27952785
size_type __old_size = __get_short_size();
2796-
if (__get_short_size() < __str.__get_short_size())
2797-
__annotate_increase(__str.__get_short_size() - __get_short_size());
2786+
if (__old_size < __str.__get_short_size())
2787+
__annotate_increase(__str.__get_short_size() - __old_size);
27982788
__rep_ = __str.__rep_;
27992789
if (__old_size > __get_short_size())
28002790
__annotate_shrink(__old_size);
@@ -2961,10 +2951,11 @@ template <class _CharT, class _Traits, class _Allocator>
29612951
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
29622952
basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
29632953
_LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
2964-
return __builtin_constant_p(*__s)
2965-
? (__fits_in_sso(traits_type::length(__s)) ? __assign_short(__s, traits_type::length(__s))
2966-
: __assign_external(__s, traits_type::length(__s)))
2967-
: __assign_external(__s);
2954+
if (__builtin_constant_p(*__s)) {
2955+
const size_type __n = traits_type::length(__s);
2956+
return __fits_in_sso(__n) ? __assign_short(__s, __n) : __assign_external(__s, __n);
2957+
}
2958+
return __assign_external(__s);
29682959
}
29692960
// append
29702961

@@ -3036,18 +3027,11 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pu
30363027
}
30373028
if (__sz == __cap) {
30383029
__grow_by_without_replace(__cap, 1, __sz, __sz, 0);
3039-
__annotate_increase(1);
30403030
__is_short = false; // the string is always long after __grow_by
3041-
} else
3042-
__annotate_increase(1);
3043-
pointer __p = __get_pointer();
3044-
if (__is_short) {
3045-
__p = __get_short_pointer() + __sz;
3046-
__set_short_size(__sz + 1);
3047-
} else {
3048-
__p = __get_long_pointer() + __sz;
3049-
__set_long_size(__sz + 1);
30503031
}
3032+
__annotate_increase(1);
3033+
pointer __p = __is_short ? (__set_short_size(__sz + 1), __get_short_pointer() + __sz)
3034+
: (__set_long_size(__sz + 1), __get_long_pointer() + __sz);
30513035
traits_type::assign(*__p, __c);
30523036
traits_type::assign(*++__p, value_type());
30533037
}
@@ -3424,11 +3408,13 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34243408

34253409
template <class _CharT, class _Traits, class _Allocator>
34263410
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {
3427-
size_type __old_size = size();
3411+
size_type __old_size;
34283412
if (__is_long()) {
3413+
__old_size = __get_long_size();
34293414
traits_type::assign(*__get_long_pointer(), value_type());
34303415
__set_long_size(0);
34313416
} else {
3417+
__old_size = __get_short_size();
34323418
traits_type::assign(*__get_short_pointer(), value_type());
34333419
__set_short_size(0);
34343420
}
@@ -3486,11 +3472,12 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34863472
_LIBCPP_ASSERT_INTERNAL(__is_long(), "Trying to shrink small string");
34873473

34883474
// We're a long string and we're shrinking into the small buffer.
3475+
auto __ptr = __get_long_pointer();
3476+
auto __size = __get_long_size();
3477+
auto __cap = __get_long_cap();
3478+
34893479
if (__fits_in_sso(__target_capacity)) {
34903480
__annotation_guard __g(*this);
3491-
auto __ptr = __get_long_pointer();
3492-
auto __size = __get_long_size();
3493-
auto __cap = __get_long_cap();
34943481
traits_type::copy(std::__to_address(__get_short_pointer()), std::__to_address(__ptr), __size + 1);
34953482
__set_short_size(__size);
34963483
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
@@ -3501,7 +3488,6 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
35013488
try {
35023489
# endif // _LIBCPP_HAS_EXCEPTIONS
35033490
__annotation_guard __g(*this);
3504-
auto __size = size();
35053491
auto __allocation = std::__allocate_at_least(__alloc_, __target_capacity + 1);
35063492

35073493
// The Standard mandates shrink_to_fit() does not increase the capacity.
@@ -3513,9 +3499,8 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
35133499
}
35143500

35153501
__begin_lifetime(__allocation.ptr, __allocation.count);
3516-
auto __ptr = __get_long_pointer();
35173502
traits_type::copy(std::__to_address(__allocation.ptr), std::__to_address(__ptr), __size + 1);
3518-
__alloc_traits::deallocate(__alloc_, __ptr, __get_long_cap());
3503+
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
35193504
__set_long_cap(__allocation.count);
35203505
__set_long_pointer(__allocation.ptr);
35213506
# if _LIBCPP_HAS_EXCEPTIONS

0 commit comments

Comments
 (0)