diff --git a/libcxx/docs/ReleaseNotes.rst b/libcxx/docs/ReleaseNotes.rst --- a/libcxx/docs/ReleaseNotes.rst +++ b/libcxx/docs/ReleaseNotes.rst @@ -100,6 +100,12 @@ - The ``_LIBCPP_DEBUG`` macro is not honored anymore, and it is an error to try to use it. Please migrate to ``_LIBCPP_ENABLE_DEBUG_MODE`` instead. +- A base template for ``std::char_traits`` is not provided anymore. The Standard mandates that the library + provides specializations for several types like ``char`` and ``wchar_t``, which libc++ does. However, we + also additionally provided a default implementation for ``std::char_traits`` for arbitrary ``T``. Not + only does the Standard not mandate that one is provided, but such an implementation is bound to be incorrect + for some types. + Upcoming Deprecations and Removals ---------------------------------- diff --git a/libcxx/include/__string/char_traits.h b/libcxx/include/__string/char_traits.h --- a/libcxx/include/__string/char_traits.h +++ b/libcxx/include/__string/char_traits.h @@ -39,132 +39,37 @@ _LIBCPP_BEGIN_NAMESPACE_STD template -struct _LIBCPP_TEMPLATE_VIS char_traits +struct char_traits; +/* +The Standard does not define the base template for char_traits because it is impossible to provide +a correct definition for arbitrary character types. Instead, it requires implementations to provide +specializations for predefined character types like `char`, `wchar_t` and others. We provide this as +exposition-only to document what members a char_traits specialization should provide: { using char_type = _CharT; - using int_type = int; - using off_type = streamoff; - using pos_type = streampos; - using state_type = mbstate_t; - - static inline void _LIBCPP_CONSTEXPR_SINCE_CXX17 - assign(char_type& __c1, const char_type& __c2) _NOEXCEPT {__c1 = __c2;} - static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT - {return __c1 == __c2;} - static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT - {return __c1 < __c2;} - - static _LIBCPP_CONSTEXPR_SINCE_CXX17 - int compare(const char_type* __s1, const char_type* __s2, size_t __n); - _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_SINCE_CXX17 - size_t length(const char_type* __s); - _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_SINCE_CXX17 - const char_type* find(const char_type* __s, size_t __n, const char_type& __a); - static _LIBCPP_CONSTEXPR_SINCE_CXX20 - char_type* move(char_type* __s1, const char_type* __s2, size_t __n); - _LIBCPP_INLINE_VISIBILITY - static _LIBCPP_CONSTEXPR_SINCE_CXX20 - char_type* copy(char_type* __s1, const char_type* __s2, size_t __n); - _LIBCPP_INLINE_VISIBILITY - static _LIBCPP_CONSTEXPR_SINCE_CXX20 - char_type* assign(char_type* __s, size_t __n, char_type __a); - - static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT - {return eq_int_type(__c, eof()) ? ~eof() : __c;} - static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT - {return char_type(__c);} - static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT - {return int_type(__c);} - static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT - {return __c1 == __c2;} - static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT - {return int_type(EOF);} + using int_type = ...; + using off_type = ...; + using pos_type = ...; + using state_type = ...; + + static void assign(char_type&, const char_type&); + static bool eq(char_type, char_type); + static bool lt(char_type, char_type); + + static int compare(const char_type*, const char_type*, size_t); + static size_t length(const char_type*); + static const char_type* find(const char_type*, size_t, const char_type&); + static char_type* move(char_type*, const char_type*, size_t); + static char_type* copy(char_type*, const char_type* __s2, size_t); + static char_type* assign(char_type*, size_t, char_type); + + static int_type not_eof(int_type); + static char_type to_char_type(int_type); + static int_type to_int_type(char_type); + static bool eq_int_type(int_type, int_type); + static int_type eof(); }; - -template -_LIBCPP_CONSTEXPR_SINCE_CXX17 int -char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n) -{ - for (; __n; --__n, ++__s1, ++__s2) - { - if (lt(*__s1, *__s2)) - return -1; - if (lt(*__s2, *__s1)) - return 1; - } - return 0; -} - -template -inline -_LIBCPP_CONSTEXPR_SINCE_CXX17 size_t -char_traits<_CharT>::length(const char_type* __s) -{ - size_t __len = 0; - for (; !eq(*__s, char_type(0)); ++__s) - ++__len; - return __len; -} - -template -inline -_LIBCPP_CONSTEXPR_SINCE_CXX17 const _CharT* -char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a) -{ - for (; __n; --__n) - { - if (eq(*__s, __a)) - return __s; - ++__s; - } - return nullptr; -} - -template -_LIBCPP_CONSTEXPR_SINCE_CXX20 _CharT* -char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n) -{ - if (__n == 0) return __s1; - char_type* __r = __s1; - if (__s1 < __s2) - { - for (; __n; --__n, ++__s1, ++__s2) - assign(*__s1, *__s2); - } - else if (__s2 < __s1) - { - __s1 += __n; - __s2 += __n; - for (; __n; --__n) - assign(*--__s1, *--__s2); - } - return __r; -} - -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -_CharT* -char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n) -{ - if (!__libcpp_is_constant_evaluated()) { - _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range"); - } - char_type* __r = __s1; - for (; __n; --__n, ++__s1, ++__s2) - assign(*__s1, *__s2); - return __r; -} - -template -inline _LIBCPP_CONSTEXPR_SINCE_CXX20 -_CharT* -char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a) -{ - char_type* __r = __s; - for (; __n; --__n, ++__s) - assign(*__s, __a); - return __r; -} +*/ template _LIBCPP_HIDE_FROM_ABI static inline _LIBCPP_CONSTEXPR_SINCE_CXX20 diff --git a/libcxx/test/std/ranges/range.adaptors/range.lazy.split/general.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.lazy.split/general.pass.cpp --- a/libcxx/test/std/ranges/range.adaptors/range.lazy.split/general.pass.cpp +++ b/libcxx/test/std/ranges/range.adaptors/range.lazy.split/general.pass.cpp @@ -26,20 +26,25 @@ #include #include "types.h" -// A constexpr-friendly lightweight string, primarily useful for comparisons. -// Unlike `std::string_view`, it copies the given string into an -// internal buffer and can work with non-contiguous inputs. +// Basic utility to convert a range to a string-like type. This handles ranges +// that do not contain character types and can work with non-contiguous inputs. template class BasicSmallString { - std::basic_string buffer_{}; + std::vector buffer_{}; public: - constexpr BasicSmallString(std::basic_string_view v) : buffer_(v) {} + constexpr BasicSmallString(std::basic_string_view v) : buffer_(v.begin(), v.end()) {} +#ifndef TEST_HAS_NO_WIDE_CHARACTERS + constexpr BasicSmallString(std::basic_string_view v) : buffer_(v.begin(), v.end()) {} +#endif + constexpr BasicSmallString(std::basic_string_view v) : buffer_(v.begin(), v.end()) {} + constexpr BasicSmallString(std::basic_string_view v) : buffer_(v.begin(), v.end()) {} + constexpr BasicSmallString(std::basic_string_view v) : buffer_(v.begin(), v.end()) {} template constexpr BasicSmallString(I b, const S& e) { for (; b != e; ++b) { - buffer_ += *b; + buffer_.push_back(*b); } } diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_append/push_back.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_append/push_back.pass.cpp --- a/libcxx/test/std/strings/basic.string/string.modifiers/string_append/push_back.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_append/push_back.pass.cpp @@ -11,17 +11,51 @@ // void push_back(charT c) // constexpr since C++20 #include +#include #include #include "test_macros.h" #include "min_allocator.h" -struct veryLarge -{ +struct VeryLarge { long long a; char b; }; +namespace std { + template <> + struct char_traits { + using char_type = VeryLarge; + using int_type = int; + using off_type = streamoff; + using pos_type = streampos; + using state_type = mbstate_t; + + static TEST_CONSTEXPR_CXX20 void assign(char_type& c1, const char_type& c2) { c1 = c2; } + static bool eq(char_type c1, char_type c2); + static bool lt(char_type c1, char_type c2); + + static int compare(const char_type* s1, const char_type* s2, size_t n); + static size_t length(const char_type* s); + static const char_type* find(const char_type* s, size_t n, const char_type& a); + static char_type* move(char_type* s1, const char_type* s2, size_t n); + static TEST_CONSTEXPR_CXX20 char_type* copy(char_type* s1, const char_type* s2, size_t n) { + std::copy_n(s2, n, s1); + return s1; + } + static TEST_CONSTEXPR_CXX20 char_type* assign(char_type* s, size_t n, char_type a) { + std::fill_n(s, n, a); + return s; + } + + static int_type not_eof(int_type c); + static char_type to_char_type(int_type c); + static int_type to_int_type(char_type c); + static bool eq_int_type(int_type c1, int_type c2); + static int_type eof(); + }; +} // end namespace std + template TEST_CONSTEXPR_CXX20 void test(S s, typename S::value_type c, S expected) @@ -48,9 +82,9 @@ #endif { -// https://llvm.org/PR31454 - std::basic_string s; - veryLarge vl = {}; + // https://llvm.org/PR31454 + std::basic_string s; + VeryLarge vl = {}; s.push_back(vl); s.push_back(vl); s.push_back(vl);