Skip to content

Commit 596189e

Browse files
committed
Simplify string a bit
1 parent 62ec7b8 commit 596189e

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
@@ -2303,7 +2303,7 @@ private:
23032303
if (!__str.__is_long()) {
23042304
if (__is_long()) {
23052305
__annotate_delete();
2306-
__alloc_traits::deallocate(__alloc_, __get_long_pointer(), capacity() + 1);
2306+
__alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
23072307
__rep_ = __rep();
23082308
}
23092309
__alloc_ = __str.__alloc_;
@@ -2318,7 +2318,7 @@ private:
23182318
__alloc_ = std::move(__a);
23192319
__set_long_pointer(__allocation.ptr);
23202320
__set_long_cap(__allocation.count);
2321-
__set_long_size(__str.size());
2321+
__set_long_size(__str.__get_long_size());
23222322
}
23232323
}
23242324
}
@@ -2437,8 +2437,8 @@ template <class _CharT,
24372437
class _Traits,
24382438
class _Allocator = allocator<_CharT>,
24392439
class = enable_if_t<__is_allocator<_Allocator>::value> >
2440-
explicit basic_string(basic_string_view<_CharT, _Traits>,
2441-
const _Allocator& = _Allocator()) -> basic_string<_CharT, _Traits, _Allocator>;
2440+
explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
2441+
-> basic_string<_CharT, _Traits, _Allocator>;
24422442

24432443
template <class _CharT,
24442444
class _Traits,
@@ -2701,38 +2701,36 @@ template <class _CharT, class _Traits, class _Allocator>
27012701
template <bool __is_short>
27022702
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27032703
basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {
2704-
size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
2705-
if (__n < __cap) {
2706-
size_type __old_size = __is_short ? __get_short_size() : __get_long_size();
2704+
size_type __cap = capacity();
2705+
size_type __old_size = size();
2706+
if (__n <= __cap) {
27072707
if (__n > __old_size)
27082708
__annotate_increase(__n - __old_size);
2709-
pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2710-
__is_short ? __set_short_size(__n) : __set_long_size(__n);
2709+
pointer __p =
2710+
__is_short ? (__set_short_size(__n), __get_short_pointer()) : (__set_long_size(__n), __get_long_pointer());
27112711
traits_type::copy(std::__to_address(__p), __s, __n);
27122712
traits_type::assign(__p[__n], value_type());
27132713
if (__old_size > __n)
27142714
__annotate_shrink(__old_size);
27152715
} else {
2716-
size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2717-
__grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2716+
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
27182717
}
27192718
return *this;
27202719
}
27212720

27222721
template <class _CharT, class _Traits, class _Allocator>
27232722
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27242723
basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
2725-
size_type __cap = capacity();
2724+
size_type __cap = capacity();
2725+
size_type __old_size = size();
27262726
if (__cap >= __n) {
2727-
size_type __old_size = size();
27282727
if (__n > __old_size)
27292728
__annotate_increase(__n - __old_size);
27302729
value_type* __p = std::__to_address(__get_pointer());
27312730
traits_type::move(__p, __s, __n);
27322731
return __null_terminate_at(__p, __n);
27332732
} else {
2734-
size_type __sz = size();
2735-
__grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2733+
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
27362734
return *this;
27372735
}
27382736
}
@@ -2750,8 +2748,7 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
27502748
size_type __cap = capacity();
27512749
size_type __old_size = size();
27522750
if (__cap < __n) {
2753-
size_type __sz = size();
2754-
__grow_by_without_replace(__cap, __n - __cap, __sz, 0, __sz);
2751+
__grow_by_without_replace(__cap, __n - __cap, __old_size, 0, __old_size);
27552752
__annotate_increase(__n);
27562753
} else if (__n > __old_size)
27572754
__annotate_increase(__n - __old_size);
@@ -2763,17 +2760,10 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
27632760
template <class _CharT, class _Traits, class _Allocator>
27642761
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
27652762
basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
2766-
pointer __p;
27672763
size_type __old_size = size();
27682764
if (__old_size == 0)
27692765
__annotate_increase(1);
2770-
if (__is_long()) {
2771-
__p = __get_long_pointer();
2772-
__set_long_size(1);
2773-
} else {
2774-
__p = __get_short_pointer();
2775-
__set_short_size(1);
2776-
}
2766+
pointer __p = __is_long() ? (__set_long_size(1), __get_long_pointer()) : (__set_short_size(1), __get_short_pointer());
27772767
traits_type::assign(*__p, __c);
27782768
traits_type::assign(*++__p, value_type());
27792769
if (__old_size > 1)
@@ -2789,8 +2779,8 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
27892779
if (!__is_long()) {
27902780
if (!__str.__is_long()) {
27912781
size_type __old_size = __get_short_size();
2792-
if (__get_short_size() < __str.__get_short_size())
2793-
__annotate_increase(__str.__get_short_size() - __get_short_size());
2782+
if (__old_size < __str.__get_short_size())
2783+
__annotate_increase(__str.__get_short_size() - __old_size);
27942784
__rep_ = __str.__rep_;
27952785
if (__old_size > __get_short_size())
27962786
__annotate_shrink(__old_size);
@@ -2957,10 +2947,11 @@ template <class _CharT, class _Traits, class _Allocator>
29572947
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
29582948
basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
29592949
_LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
2960-
return __builtin_constant_p(*__s)
2961-
? (__fits_in_sso(traits_type::length(__s)) ? __assign_short(__s, traits_type::length(__s))
2962-
: __assign_external(__s, traits_type::length(__s)))
2963-
: __assign_external(__s);
2950+
if (__builtin_constant_p(*__s)) {
2951+
const size_type __n = traits_type::length(__s);
2952+
return __fits_in_sso(__n) ? __assign_short(__s, __n) : __assign_external(__s, __n);
2953+
}
2954+
return __assign_external(__s);
29642955
}
29652956
// append
29662957

@@ -3032,18 +3023,11 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pu
30323023
}
30333024
if (__sz == __cap) {
30343025
__grow_by_without_replace(__cap, 1, __sz, __sz, 0);
3035-
__annotate_increase(1);
30363026
__is_short = false; // the string is always long after __grow_by
3037-
} else
3038-
__annotate_increase(1);
3039-
pointer __p = __get_pointer();
3040-
if (__is_short) {
3041-
__p = __get_short_pointer() + __sz;
3042-
__set_short_size(__sz + 1);
3043-
} else {
3044-
__p = __get_long_pointer() + __sz;
3045-
__set_long_size(__sz + 1);
30463027
}
3028+
__annotate_increase(1);
3029+
pointer __p = __is_short ? (__set_short_size(__sz + 1), __get_short_pointer() + __sz)
3030+
: (__set_long_size(__sz + 1), __get_long_pointer() + __sz);
30473031
traits_type::assign(*__p, __c);
30483032
traits_type::assign(*++__p, value_type());
30493033
}
@@ -3420,11 +3404,13 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34203404

34213405
template <class _CharT, class _Traits, class _Allocator>
34223406
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {
3423-
size_type __old_size = size();
3407+
size_type __old_size;
34243408
if (__is_long()) {
3409+
__old_size = __get_long_size();
34253410
traits_type::assign(*__get_long_pointer(), value_type());
34263411
__set_long_size(0);
34273412
} else {
3413+
__old_size = __get_short_size();
34283414
traits_type::assign(*__get_short_pointer(), value_type());
34293415
__set_short_size(0);
34303416
}
@@ -3482,11 +3468,12 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34823468
_LIBCPP_ASSERT_INTERNAL(__is_long(), "Trying to shrink small string");
34833469

34843470
// We're a long string and we're shrinking into the small buffer.
3471+
auto __ptr = __get_long_pointer();
3472+
auto __size = __get_long_size();
3473+
auto __cap = __get_long_cap();
3474+
34853475
if (__fits_in_sso(__target_capacity)) {
34863476
__annotation_guard __g(*this);
3487-
auto __ptr = __get_long_pointer();
3488-
auto __size = __get_long_size();
3489-
auto __cap = __get_long_cap();
34903477
traits_type::copy(std::__to_address(__get_short_pointer()), std::__to_address(__ptr), __size + 1);
34913478
__set_short_size(__size);
34923479
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
@@ -3497,7 +3484,6 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34973484
try {
34983485
# endif // _LIBCPP_HAS_EXCEPTIONS
34993486
__annotation_guard __g(*this);
3500-
auto __size = size();
35013487
auto __allocation = std::__allocate_at_least(__alloc_, __target_capacity + 1);
35023488

35033489
// The Standard mandates shrink_to_fit() does not increase the capacity.
@@ -3509,9 +3495,8 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
35093495
}
35103496

35113497
__begin_lifetime(__allocation.ptr, __allocation.count);
3512-
auto __ptr = __get_long_pointer();
35133498
traits_type::copy(std::__to_address(__allocation.ptr), std::__to_address(__ptr), __size + 1);
3514-
__alloc_traits::deallocate(__alloc_, __ptr, __get_long_cap());
3499+
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
35153500
__set_long_cap(__allocation.count);
35163501
__set_long_pointer(__allocation.ptr);
35173502
# if _LIBCPP_HAS_EXCEPTIONS

0 commit comments

Comments
 (0)