[libc++][NFC] Inline the string constructors

This removes a lot of boilerplate code.

Reviewed By: ldionne, #libc

Spies: EricWF, libcxx-commits

Differential Revision: https://reviews.llvm.org/D128081
This commit is contained in:
Nikolas Klauser 2022-08-26 18:35:25 +02:00
parent 510383626f
commit 87abc5013a
1 changed files with 127 additions and 215 deletions

View File

@ -816,94 +816,156 @@ private:
} }
public: public:
_LIBCPP_TEMPLATE_DATA_VIS _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1;
static const size_type npos = -1;
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string() _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
_NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value); _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
: __r_(__default_init_tag(), __default_init_tag()) {
std::__debug_db_insert_c(this);
__default_init();
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a) _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
#if _LIBCPP_STD_VER <= 14 #if _LIBCPP_STD_VER <= 14
_NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value); _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
#else #else
_NOEXCEPT; _NOEXCEPT
#endif #endif
: __r_(__default_init_tag(), __a) {
std::__debug_db_insert_c(this);
__default_init();
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str); _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str);
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, const allocator_type& __a); _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, const allocator_type& __a);
#ifndef _LIBCPP_CXX03_LANG #ifndef _LIBCPP_CXX03_LANG
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str)
basic_string(basic_string&& __str) # if _LIBCPP_STD_VER <= 14
#if _LIBCPP_STD_VER <= 14 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
_NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value); # else
#else _NOEXCEPT
_NOEXCEPT; # endif
#endif : __r_(std::move(__str.__r_)) {
__str.__default_init();
std::__debug_db_insert_c(this);
if (__is_long())
std::__debug_db_swap(this, &__str);
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str, const allocator_type& __a)
basic_string(basic_string&& __str, const allocator_type& __a); : __r_(__default_init_tag(), __a) {
if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
__init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
else {
if (__libcpp_is_constant_evaluated())
__r_.first() = __rep();
__r_.first() = __str.__r_.first();
__str.__default_init();
}
std::__debug_db_insert_c(this);
if (__is_long())
std::__debug_db_swap(this, &__str);
}
#endif // _LIBCPP_CXX03_LANG #endif // _LIBCPP_CXX03_LANG
template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> > template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s)
basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) { : __r_(__default_init_tag(), __default_init_tag()) {
_LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr"); _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
__init(__s, traits_type::length(__s)); __init(__s, traits_type::length(__s));
std::__debug_db_insert_c(this); std::__debug_db_insert_c(this);
} }
template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> > template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, const _Allocator& __a);
basic_string(const _CharT* __s, const _Allocator& __a);
#if _LIBCPP_STD_VER > 20 #if _LIBCPP_STD_VER > 20
basic_string(nullptr_t) = delete; basic_string(nullptr_t) = delete;
#endif #endif
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, size_type __n)
basic_string(const _CharT* __s, size_type __n); : __r_(__default_init_tag(), __default_init_tag()) {
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
basic_string(const _CharT* __s, size_type __n, const _Allocator& __a); __init(__s, __n);
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 std::__debug_db_insert_c(this);
basic_string(size_type __n, _CharT __c); }
template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> > _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
basic_string(size_type __n, _CharT __c, const _Allocator& __a); : __r_(__default_init_tag(), __a) {
_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
__init(__s, __n);
std::__debug_db_insert_c(this);
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c)
basic_string(const basic_string& __str, size_type __pos, size_type __n, : __r_(__default_init_tag(), __default_init_tag()) {
const _Allocator& __a = _Allocator()); __init(__n, __c);
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 std::__debug_db_insert_c(this);
basic_string(const basic_string& __str, size_type __pos, }
const _Allocator& __a = _Allocator());
template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> > template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
basic_string(const _Tp& __t, size_type __pos, size_type __n,
const allocator_type& __a = allocator_type());
template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && _LIBCPP_CONSTEXPR_SINCE_CXX20
!__is_same_uncvref<_Tp, basic_string>::value> > basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Allocator& __a = _Allocator());
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
explicit basic_string(const _Tp& __t);
template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> > _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, size_type __pos, const _Allocator& __a = _Allocator())
explicit basic_string(const _Tp& __t, const allocator_type& __a); : __r_(__default_init_tag(), __a) {
size_type __str_sz = __str.size();
if (__pos > __str_sz)
__throw_out_of_range();
__init(__str.data() + __pos, __str_sz - __pos);
std::__debug_db_insert_c(this);
}
template <class _Tp,
class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
!__is_same_uncvref<_Tp, basic_string>::value> >
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string(const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a = allocator_type());
template <class _Tp,
class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
!__is_same_uncvref<_Tp, basic_string>::value> >
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
const _Tp& __t);
template <class _Tp,
class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
!__is_same_uncvref<_Tp, basic_string>::value> >
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
const _Tp& __t, const allocator_type& __a);
template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(_InputIterator __first, _InputIterator __last)
: __r_(__default_init_tag(), __default_init_tag()) {
__init(__first, __last);
std::__debug_db_insert_c(this);
}
template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
: __r_(__default_init_tag(), __a) {
__init(__first, __last);
std::__debug_db_insert_c(this);
}
template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string(_InputIterator __first, _InputIterator __last);
template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
#ifndef _LIBCPP_CXX03_LANG #ifndef _LIBCPP_CXX03_LANG
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il)
basic_string(initializer_list<_CharT> __il); : __r_(__default_init_tag(), __default_init_tag()) {
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __init(__il.begin(), __il.end());
basic_string(initializer_list<_CharT> __il, const _Allocator& __a); std::__debug_db_insert_c(this);
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il, const _Allocator& __a)
: __r_(__default_init_tag(), __a) {
__init(__il.begin(), __il.end());
std::__debug_db_insert_c(this);
}
#endif // _LIBCPP_CXX03_LANG #endif // _LIBCPP_CXX03_LANG
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string(); inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string();
@ -1896,30 +1958,6 @@ basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
#endif // _LIBCPP_ENABLE_DEBUG_MODE #endif // _LIBCPP_ENABLE_DEBUG_MODE
} }
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string()
_NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
: __r_(__default_init_tag(), __default_init_tag())
{
std::__debug_db_insert_c(this);
__default_init();
}
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
#if _LIBCPP_STD_VER <= 14
_NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
#else
_NOEXCEPT
#endif
: __r_(__default_init_tag(), __a)
{
std::__debug_db_insert_c(this);
__default_init();
}
template <class _CharT, class _Traits, class _Allocator> template <class _CharT, class _Traits, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_CONSTEXPR_SINCE_CXX20
void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
@ -1988,26 +2026,6 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const
std::__debug_db_insert_c(this); std::__debug_db_insert_c(this);
} }
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
: __r_(__default_init_tag(), __default_init_tag())
{
_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
__init(__s, __n);
std::__debug_db_insert_c(this);
}
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
: __r_(__default_init_tag(), __a)
{
_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
__init(__s, __n);
std::__debug_db_insert_c(this);
}
template <class _CharT, class _Traits, class _Allocator> template <class _CharT, class _Traits, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str) basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
@ -2059,45 +2077,6 @@ void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
traits_type::copy(std::__to_address(__p), __s, __sz + 1); traits_type::copy(std::__to_address(__p), __s, __sz + 1);
} }
#ifndef _LIBCPP_CXX03_LANG
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
#if _LIBCPP_STD_VER <= 14
_NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
#else
_NOEXCEPT
#endif
: __r_(std::move(__str.__r_))
{
__str.__default_init();
std::__debug_db_insert_c(this);
if (__is_long())
std::__debug_db_swap(this, &__str);
}
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
: __r_(__default_init_tag(), __a)
{
if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
__init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
else
{
if (__libcpp_is_constant_evaluated())
__r_.first() = __rep();
__r_.first() = __str.__r_.first();
__str.__default_init();
}
std::__debug_db_insert_c(this);
if (__is_long())
std::__debug_db_swap(this, &__str);
}
#endif // _LIBCPP_CXX03_LANG
template <class _CharT, class _Traits, class _Allocator> template <class _CharT, class _Traits, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_CONSTEXPR_SINCE_CXX20
void void
@ -2127,15 +2106,6 @@ basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
traits_type::assign(__p[__n], value_type()); traits_type::assign(__p[__n], value_type());
} }
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
: __r_(__default_init_tag(), __default_init_tag())
{
__init(__n, __c);
std::__debug_db_insert_c(this);
}
template <class _CharT, class _Traits, class _Allocator> template <class _CharT, class _Traits, class _Allocator>
template <class> template <class>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_CONSTEXPR_SINCE_CXX20
@ -2160,19 +2130,6 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __st
std::__debug_db_insert_c(this); std::__debug_db_insert_c(this);
} }
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
const _Allocator& __a)
: __r_(__default_init_tag(), __a)
{
size_type __str_sz = __str.size();
if (__pos > __str_sz)
__throw_out_of_range();
__init(__str.data() + __pos, __str_sz - __pos);
std::__debug_db_insert_c(this);
}
template <class _CharT, class _Traits, class _Allocator> template <class _CharT, class _Traits, class _Allocator>
template <class _Tp, class> template <class _Tp, class>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_CONSTEXPR_SINCE_CXX20
@ -2283,51 +2240,6 @@ basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _For
#endif // _LIBCPP_NO_EXCEPTIONS #endif // _LIBCPP_NO_EXCEPTIONS
} }
template <class _CharT, class _Traits, class _Allocator>
template<class _InputIterator, class>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
: __r_(__default_init_tag(), __default_init_tag())
{
__init(__first, __last);
std::__debug_db_insert_c(this);
}
template <class _CharT, class _Traits, class _Allocator>
template<class _InputIterator, class>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
const allocator_type& __a)
: __r_(__default_init_tag(), __a)
{
__init(__first, __last);
std::__debug_db_insert_c(this);
}
#ifndef _LIBCPP_CXX03_LANG
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(
initializer_list<_CharT> __il)
: __r_(__default_init_tag(), __default_init_tag())
{
__init(__il.begin(), __il.end());
std::__debug_db_insert_c(this);
}
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::basic_string(
initializer_list<_CharT> __il, const _Allocator& __a)
: __r_(__default_init_tag(), __a)
{
__init(__il.begin(), __il.end());
std::__debug_db_insert_c(this);
}
#endif // _LIBCPP_CXX03_LANG
template <class _CharT, class _Traits, class _Allocator> template <class _CharT, class _Traits, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_CONSTEXPR_SINCE_CXX20
basic_string<_CharT, _Traits, _Allocator>::~basic_string() basic_string<_CharT, _Traits, _Allocator>::~basic_string()