diff --git a/libcxx/include/string b/libcxx/include/string --- a/libcxx/include/string +++ b/libcxx/include/string @@ -1010,9 +1010,12 @@ } #ifndef _LIBCPP_CXX03_LANG - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 - basic_string& operator=(basic_string&& __str) - _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value)); + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(basic_string&& __str) + _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value)) { + __move_assign(__str, integral_constant()); + return *this; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(initializer_list __il) {return assign(__il.begin(), __il.size());} #endif @@ -1065,7 +1068,17 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT {return __is_long() ? __get_long_size() : __get_short_size();} _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT {return size();} - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT; + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT { + size_type __m = __alloc_traits::max_size(__alloc()); + if (__m <= std::numeric_limits::max() / 2) { + return __m - __alignment; + } else { + bool __uses_lsb = __endian_factor == 2; + return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment; + } + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT { return (__is_long() ? __get_long_cap() : static_cast(__min_cap)) - 1; } @@ -1093,9 +1106,15 @@ _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool empty() const _NOEXCEPT {return size() == 0;} - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 - const_reference operator[](size_type __pos) const _NOEXCEPT; - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT; + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference operator[](size_type __pos) const _NOEXCEPT { + _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds"); + return *(data() + __pos); + } + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT { + _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds"); + return *(__get_pointer() + __pos); + } _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const; _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n); @@ -1130,8 +1149,9 @@ basic_string& operator+=(initializer_list __il) { return append(__il); } #endif // _LIBCPP_CXX03_LANG - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 - basic_string& append(const basic_string& __str); + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str) { + return append(__str.data(), __str.size()); + } template _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 @@ -1189,10 +1209,26 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c); _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back(); - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT; - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT; - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT; - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT; + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT { + _LIBCPP_ASSERT(!empty(), "string::front(): string is empty"); + return *__get_pointer(); + } + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT { + _LIBCPP_ASSERT(!empty(), "string::front(): string is empty"); + return *data(); + } + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT { + _LIBCPP_ASSERT(!empty(), "string::back(): string is empty"); + return *(__get_pointer() + size() - 1); + } + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT { + _LIBCPP_ASSERT(!empty(), "string::back(): string is empty"); + return *(data() + size() - 1); + } template _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 @@ -1244,8 +1280,10 @@ basic_string& assign(initializer_list __il) {return assign(__il.begin(), __il.size());} #endif // _LIBCPP_CXX03_LANG - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 - basic_string& insert(size_type __pos1, const basic_string& __str); + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& + insert(size_type __pos1, const basic_string& __str) { + return insert(__pos1, __str.data(), __str.size()); + } template _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 @@ -1271,8 +1309,16 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s); _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c); _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c); - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 - iterator insert(const_iterator __pos, size_type __n, value_type __c); + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator + insert(const_iterator __pos, size_type __n, value_type __c) { + _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this, + "string::insert(iterator, n, value) called with an iterator not referring to this string"); + difference_type __p = __pos - begin(); + insert(static_cast(__p), __n, __c); + return begin() + __p; + } + template _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 __enable_if_t @@ -1301,8 +1347,10 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __first, const_iterator __last); - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 - basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str); + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& + replace(size_type __pos1, size_type __n1, const basic_string& __str) { + return replace(__pos1, __n1, __str.data(), __str.size()); + } template _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 @@ -1326,8 +1374,12 @@ basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2); _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s); _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c); - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 - basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str); + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& + replace(const_iterator __i1, const_iterator __i2, const basic_string& __str) { + return replace( + static_cast(__i1 - begin()), static_cast(__i2 - __i1), __str.data(), __str.size()); + } template _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 @@ -1338,12 +1390,21 @@ > replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); } - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 - basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n); - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 - basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s); - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 - basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c); + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& + replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n) { + return replace(static_cast(__i1 - begin()), static_cast(__i2 - __i1), __s, __n); + } + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& + replace(const_iterator __i1, const_iterator __i2, const value_type* __s) { + return replace(static_cast(__i1 - begin()), static_cast(__i2 - __i1), __s); + } + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& + replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c) { + return replace(static_cast(__i1 - begin()), static_cast(__i2 - __i1), __n, __c); + } + template _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 __enable_if_t @@ -1800,8 +1861,9 @@ template _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_no_alias(const value_type* __s, size_type __n); - _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 - void __erase_to_end(size_type __pos); + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_to_end(size_type __pos) { + __null_terminate_at(std::__to_address(__get_pointer()), __pos); + } // __erase_external_with_move is invoked for erase() invocations where // `n ~= npos`, likely requiring memory moves on the string data. @@ -2511,17 +2573,6 @@ } } -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -basic_string<_CharT, _Traits, _Allocator>& -basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str) - _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value)) -{ - __move_assign(__str, integral_constant()); - return *this; -} - #endif template @@ -2762,14 +2813,6 @@ return *this; } -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -basic_string<_CharT, _Traits, _Allocator>& -basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str) -{ - return append(__str.data(), __str.size()); -} - template _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>& @@ -2927,14 +2970,6 @@ } } -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -basic_string<_CharT, _Traits, _Allocator>& -basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str) -{ - return insert(__pos1, __str.data(), __str.size()); -} - template _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>& @@ -3005,19 +3040,6 @@ return begin() + static_cast(__ip); } -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -typename basic_string<_CharT, _Traits, _Allocator>::iterator -basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c) -{ - _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this, - "string::insert(iterator, n, value) called with an iterator not" - " referring to this string"); - difference_type __p = __pos - begin(); - insert(static_cast(__p), __n, __c); - return begin() + __p; -} - // replace template @@ -3119,14 +3141,6 @@ return replace(__i1, __i2, __temp); } -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -basic_string<_CharT, _Traits, _Allocator>& -basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str) -{ - return replace(__pos1, __n1, __str.data(), __str.size()); -} - template _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>& @@ -3166,39 +3180,6 @@ return replace(__pos, __n1, __s, traits_type::length(__s)); } -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -basic_string<_CharT, _Traits, _Allocator>& -basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str) -{ - return replace(static_cast(__i1 - begin()), static_cast(__i2 - __i1), - __str.data(), __str.size()); -} - -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -basic_string<_CharT, _Traits, _Allocator>& -basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n) -{ - return replace(static_cast(__i1 - begin()), static_cast(__i2 - __i1), __s, __n); -} - -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -basic_string<_CharT, _Traits, _Allocator>& -basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s) -{ - return replace(static_cast(__i1 - begin()), static_cast(__i2 - __i1), __s); -} - -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -basic_string<_CharT, _Traits, _Allocator>& -basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c) -{ - return replace(static_cast(__i1 - begin()), static_cast(__i2 - __i1), __n, __c); -} - // erase // 'externally instantiated' erase() implementation, called when __n != npos. @@ -3295,14 +3276,6 @@ } } -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -void -basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos) -{ - __null_terminate_at(std::__to_address(__get_pointer()), __pos); -} - template _LIBCPP_CONSTEXPR_SINCE_CXX20 void @@ -3326,20 +3299,6 @@ __erase_to_end(__n); } -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -typename basic_string<_CharT, _Traits, _Allocator>::size_type -basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT -{ - size_type __m = __alloc_traits::max_size(__alloc()); - if (__m <= std::numeric_limits::max() / 2) { - return __m - __alignment; - } else { - bool __uses_lsb = __endian_factor == 2; - return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment; - } -} - template _LIBCPP_CONSTEXPR_SINCE_CXX20 void @@ -3436,24 +3395,6 @@ std::__debug_db_invalidate_all(this); } -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -typename basic_string<_CharT, _Traits, _Allocator>::const_reference -basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT -{ - _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds"); - return *(data() + __pos); -} - -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -typename basic_string<_CharT, _Traits, _Allocator>::reference -basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT -{ - _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds"); - return *(__get_pointer() + __pos); -} - template _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::const_reference @@ -3474,42 +3415,6 @@ return (*this)[__n]; } -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -typename basic_string<_CharT, _Traits, _Allocator>::reference -basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT -{ - _LIBCPP_ASSERT(!empty(), "string::front(): string is empty"); - return *__get_pointer(); -} - -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -typename basic_string<_CharT, _Traits, _Allocator>::const_reference -basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT -{ - _LIBCPP_ASSERT(!empty(), "string::front(): string is empty"); - return *data(); -} - -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -typename basic_string<_CharT, _Traits, _Allocator>::reference -basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT -{ - _LIBCPP_ASSERT(!empty(), "string::back(): string is empty"); - return *(__get_pointer() + size() - 1); -} - -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -typename basic_string<_CharT, _Traits, _Allocator>::const_reference -basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT -{ - _LIBCPP_ASSERT(!empty(), "string::back(): string is empty"); - return *(data() + size() - 1); -} - template _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type