From 95796d7e43bb7d55623b9279a6bb89e5847d5ae8 Mon Sep 17 00:00:00 2001 From: Ryan Prichard Date: Wed, 28 Feb 2024 17:02:17 -0800 Subject: Remove std::char_traits for non-standard types The C++ standard specifies that std::char_traits is defined for char, wchar_t, char8_t, char16_t, and char32_t. Upstream libc++ previously provided a base template implementation for std::char_traits, but this was not well-thought-out. New versions of libc++ omit this definition. In particular, this removal means that std::char_traits is now undefined, which means that std::basic_string and std::basic_string_view no longer work with an unsigned char element type. See this link for details: https://discourse.llvm.org/t/deprecating-std-string-t-for-non-character-t/66779 Remove the base template for std::char_traits preemptively to help catch users of this now-removed extension, and prevent new ones. Bug: http://b/175635923 Test: treehugger Change-Id: Ib4356e07fe87aa664a4a522388bb10d55efcc2d9 --- include/__string | 149 +++++++++++-------------------------------------------- 1 file changed, 30 insertions(+), 119 deletions(-) diff --git a/include/__string b/include/__string index 1ddeec714..b7c0767f9 100644 --- a/include/__string +++ b/include/__string @@ -73,126 +73,37 @@ _LIBCPP_BEGIN_NAMESPACE_STD // char_traits template -struct _LIBCPP_TEMPLATE_VIS char_traits -{ - typedef _CharT char_type; - typedef int int_type; - typedef streamoff off_type; - typedef streampos pos_type; - typedef mbstate_t state_type; - - static inline void _LIBCPP_CONSTEXPR_AFTER_CXX14 - 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_AFTER_CXX14 - int compare(const char_type* __s1, const char_type* __s2, size_t __n); - _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14 - size_t length(const char_type* __s); - _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR_AFTER_CXX14 - 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); - _LIBCPP_INLINE_VISIBILITY - static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n); - _LIBCPP_INLINE_VISIBILITY - static 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);} +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 = ...; + 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*, 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_AFTER_CXX14 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_AFTER_CXX14 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_AFTER_CXX14 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 0; -} - -template -_CharT* -char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n) -{ - 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 -_CharT* -char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n) -{ - _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 -_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; -} +*/ // char_traits -- cgit v1.2.3