34 #ifndef _CHAR_TRAITS_H
35 #define _CHAR_TRAITS_H 1
37 #pragma GCC system_header
42 #if __cplusplus > 201703L
46 #ifndef _GLIBCXX_ALWAYS_INLINE
47 # define _GLIBCXX_ALWAYS_INLINE inline __attribute__((__always_inline__))
50 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 template<
typename _CharT>
67 typedef unsigned long int_type;
70 typedef std::mbstate_t state_type;
89 template<
typename _CharT>
92 typedef _CharT char_type;
93 typedef typename _Char_types<_CharT>::int_type int_type;
95 typedef typename _Char_types<_CharT>::off_type off_type;
96 typedef typename _Char_types<_CharT>::state_type state_type;
97 #if __cpp_lib_three_way_comparison
98 using comparison_category = std::strong_ordering;
101 static _GLIBCXX14_CONSTEXPR
void
102 assign(char_type& __c1,
const char_type& __c2)
105 static _GLIBCXX_CONSTEXPR
bool
106 eq(
const char_type& __c1,
const char_type& __c2)
107 {
return __c1 == __c2; }
109 static _GLIBCXX_CONSTEXPR
bool
110 lt(
const char_type& __c1,
const char_type& __c2)
111 {
return __c1 < __c2; }
113 static _GLIBCXX14_CONSTEXPR
int
114 compare(
const char_type* __s1,
const char_type* __s2, std::size_t __n);
116 static _GLIBCXX14_CONSTEXPR std::size_t
117 length(
const char_type* __s);
119 static _GLIBCXX14_CONSTEXPR
const char_type*
120 find(
const char_type* __s, std::size_t __n,
const char_type& __a);
122 static _GLIBCXX20_CONSTEXPR char_type*
123 move(char_type* __s1,
const char_type* __s2, std::size_t __n);
125 static _GLIBCXX20_CONSTEXPR char_type*
126 copy(char_type* __s1,
const char_type* __s2, std::size_t __n);
128 static _GLIBCXX20_CONSTEXPR char_type*
129 assign(char_type* __s, std::size_t __n, char_type __a);
131 static _GLIBCXX_CONSTEXPR char_type
132 to_char_type(
const int_type& __c)
133 {
return static_cast<char_type>(__c); }
135 static _GLIBCXX_CONSTEXPR int_type
136 to_int_type(
const char_type& __c)
137 {
return static_cast<int_type>(__c); }
139 static _GLIBCXX_CONSTEXPR
bool
140 eq_int_type(
const int_type& __c1,
const int_type& __c2)
141 {
return __c1 == __c2; }
143 static _GLIBCXX_CONSTEXPR int_type
145 {
return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
147 static _GLIBCXX_CONSTEXPR int_type
148 not_eof(
const int_type& __c)
149 {
return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
152 template<
typename _CharT>
153 _GLIBCXX14_CONSTEXPR
int
155 compare(
const char_type* __s1,
const char_type* __s2, std::size_t __n)
157 for (std::size_t __i = 0; __i < __n; ++__i)
158 if (lt(__s1[__i], __s2[__i]))
160 else if (lt(__s2[__i], __s1[__i]))
165 template<
typename _CharT>
166 _GLIBCXX14_CONSTEXPR std::size_t
167 char_traits<_CharT>::
168 length(
const char_type* __p)
171 while (!eq(__p[__i], char_type()))
176 template<
typename _CharT>
177 _GLIBCXX14_CONSTEXPR
const typename char_traits<_CharT>::char_type*
178 char_traits<_CharT>::
179 find(
const char_type* __s, std::size_t __n,
const char_type& __a)
181 for (std::size_t __i = 0; __i < __n; ++__i)
182 if (eq(__s[__i], __a))
187 template<
typename _CharT>
189 typename char_traits<_CharT>::char_type*
190 char_traits<_CharT>::
191 move(char_type* __s1,
const char_type* __s2, std::size_t __n)
195 #ifdef __cpp_lib_is_constant_evaluated
196 if (std::is_constant_evaluated())
198 if (__s1 > __s2 && __s1 < __s2 + __n)
201 std::copy(__s2, __s2 + __n, __s1);
205 return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
206 __n *
sizeof(char_type)));
209 template<
typename _CharT>
211 typename char_traits<_CharT>::char_type*
212 char_traits<_CharT>::
213 copy(char_type* __s1,
const char_type* __s2, std::size_t __n)
216 std::copy(__s2, __s2 + __n, __s1);
220 template<
typename _CharT>
222 typename char_traits<_CharT>::char_type*
223 char_traits<_CharT>::
224 assign(char_type* __s, std::size_t __n, char_type __a)
231 _GLIBCXX_END_NAMESPACE_VERSION
234 namespace std _GLIBCXX_VISIBILITY(default)
236 _GLIBCXX_BEGIN_NAMESPACE_VERSION
238 #if __cplusplus >= 201703L
239 #define __cpp_lib_constexpr_char_traits 201611
248 template<
typename _CharT>
249 static _GLIBCXX_ALWAYS_INLINE constexpr
bool
250 __constant_string_p(
const _CharT* __s)
252 #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
255 return __builtin_is_constant_evaluated();
257 while (__builtin_constant_p(*__s) && *__s)
259 return __builtin_constant_p(*__s);
271 template<
typename _CharT>
272 static _GLIBCXX_ALWAYS_INLINE constexpr
bool
273 __constant_char_array_p(
const _CharT* __a,
size_t __n)
275 #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
279 return __builtin_is_constant_evaluated();
282 while (__i < __n && __builtin_constant_p(__a[__i]))
302 template<
class _CharT>
311 typedef char char_type;
312 typedef int int_type;
315 typedef mbstate_t state_type;
316 #if __cpp_lib_three_way_comparison
317 using comparison_category = strong_ordering;
320 static _GLIBCXX17_CONSTEXPR
void
321 assign(char_type& __c1,
const char_type& __c2) _GLIBCXX_NOEXCEPT
324 static _GLIBCXX_CONSTEXPR
bool
325 eq(
const char_type& __c1,
const char_type& __c2) _GLIBCXX_NOEXCEPT
326 {
return __c1 == __c2; }
328 static _GLIBCXX_CONSTEXPR
bool
329 lt(
const char_type& __c1,
const char_type& __c2) _GLIBCXX_NOEXCEPT
332 return (static_cast<unsigned char>(__c1)
333 < static_cast<unsigned char>(__c2));
336 static _GLIBCXX17_CONSTEXPR
int
337 compare(
const char_type* __s1,
const char_type* __s2,
size_t __n)
341 #if __cplusplus >= 201703L
342 if (__builtin_constant_p(__n)
343 && __constant_char_array_p(__s1, __n)
344 && __constant_char_array_p(__s2, __n))
347 return __builtin_memcmp(__s1, __s2, __n);
350 static _GLIBCXX17_CONSTEXPR
size_t
351 length(
const char_type* __s)
353 #if __cplusplus >= 201703L
354 if (__constant_string_p(__s))
357 return __builtin_strlen(__s);
360 static _GLIBCXX17_CONSTEXPR
const char_type*
361 find(
const char_type* __s,
size_t __n,
const char_type& __a)
365 #if __cplusplus >= 201703L
366 if (__builtin_constant_p(__n)
367 && __builtin_constant_p(__a)
368 && __constant_char_array_p(__s, __n))
371 return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
374 static _GLIBCXX20_CONSTEXPR char_type*
375 move(char_type* __s1,
const char_type* __s2,
size_t __n)
379 #ifdef __cpp_lib_is_constant_evaluated
380 if (std::is_constant_evaluated())
383 return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n));
386 static _GLIBCXX20_CONSTEXPR char_type*
387 copy(char_type* __s1,
const char_type* __s2,
size_t __n)
391 #ifdef __cpp_lib_is_constant_evaluated
392 if (std::is_constant_evaluated())
395 return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
398 static _GLIBCXX20_CONSTEXPR char_type*
399 assign(char_type* __s,
size_t __n, char_type __a)
403 #ifdef __cpp_lib_is_constant_evaluated
404 if (std::is_constant_evaluated())
407 return static_cast<char_type*>(__builtin_memset(__s, __a, __n));
410 static _GLIBCXX_CONSTEXPR char_type
411 to_char_type(
const int_type& __c) _GLIBCXX_NOEXCEPT
412 {
return static_cast<char_type>(__c); }
416 static _GLIBCXX_CONSTEXPR int_type
417 to_int_type(
const char_type& __c) _GLIBCXX_NOEXCEPT
418 {
return static_cast<int_type>(static_cast<unsigned char>(__c)); }
420 static _GLIBCXX_CONSTEXPR
bool
421 eq_int_type(
const int_type& __c1,
const int_type& __c2) _GLIBCXX_NOEXCEPT
422 {
return __c1 == __c2; }
424 static _GLIBCXX_CONSTEXPR int_type
425 eof() _GLIBCXX_NOEXCEPT
426 {
return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
428 static _GLIBCXX_CONSTEXPR int_type
429 not_eof(
const int_type& __c) _GLIBCXX_NOEXCEPT
430 {
return (__c == eof()) ? 0 : __c; }
434 #ifdef _GLIBCXX_USE_WCHAR_T
439 typedef wchar_t char_type;
440 typedef wint_t int_type;
443 typedef mbstate_t state_type;
444 #if __cpp_lib_three_way_comparison
445 using comparison_category = strong_ordering;
448 static _GLIBCXX17_CONSTEXPR
void
449 assign(char_type& __c1,
const char_type& __c2) _GLIBCXX_NOEXCEPT
452 static _GLIBCXX_CONSTEXPR
bool
453 eq(
const char_type& __c1,
const char_type& __c2) _GLIBCXX_NOEXCEPT
454 {
return __c1 == __c2; }
456 static _GLIBCXX_CONSTEXPR
bool
457 lt(
const char_type& __c1,
const char_type& __c2) _GLIBCXX_NOEXCEPT
458 {
return __c1 < __c2; }
460 static _GLIBCXX17_CONSTEXPR
int
461 compare(
const char_type* __s1,
const char_type* __s2,
size_t __n)
465 #if __cplusplus >= 201703L
466 if (__builtin_constant_p(__n)
467 && __constant_char_array_p(__s1, __n)
468 && __constant_char_array_p(__s2, __n))
471 return wmemcmp(__s1, __s2, __n);
474 static _GLIBCXX17_CONSTEXPR
size_t
475 length(
const char_type* __s)
477 #if __cplusplus >= 201703L
478 if (__constant_string_p(__s))
484 static _GLIBCXX17_CONSTEXPR
const char_type*
485 find(
const char_type* __s,
size_t __n,
const char_type& __a)
489 #if __cplusplus >= 201703L
490 if (__builtin_constant_p(__n)
491 && __builtin_constant_p(__a)
492 && __constant_char_array_p(__s, __n))
495 return wmemchr(__s, __a, __n);
498 static _GLIBCXX20_CONSTEXPR char_type*
499 move(char_type* __s1,
const char_type* __s2,
size_t __n)
503 #ifdef __cpp_lib_is_constant_evaluated
504 if (std::is_constant_evaluated())
507 return wmemmove(__s1, __s2, __n);
510 static _GLIBCXX20_CONSTEXPR char_type*
511 copy(char_type* __s1,
const char_type* __s2,
size_t __n)
515 #ifdef __cpp_lib_is_constant_evaluated
516 if (std::is_constant_evaluated())
519 return wmemcpy(__s1, __s2, __n);
522 static _GLIBCXX20_CONSTEXPR char_type*
523 assign(char_type* __s,
size_t __n, char_type __a)
527 #ifdef __cpp_lib_is_constant_evaluated
528 if (std::is_constant_evaluated())
531 return wmemset(__s, __a, __n);
534 static _GLIBCXX_CONSTEXPR char_type
535 to_char_type(
const int_type& __c) _GLIBCXX_NOEXCEPT
536 {
return char_type(__c); }
538 static _GLIBCXX_CONSTEXPR int_type
539 to_int_type(
const char_type& __c) _GLIBCXX_NOEXCEPT
540 {
return int_type(__c); }
542 static _GLIBCXX_CONSTEXPR
bool
543 eq_int_type(
const int_type& __c1,
const int_type& __c2) _GLIBCXX_NOEXCEPT
544 {
return __c1 == __c2; }
546 static _GLIBCXX_CONSTEXPR int_type
547 eof() _GLIBCXX_NOEXCEPT
548 {
return static_cast<int_type>(WEOF); }
550 static _GLIBCXX_CONSTEXPR int_type
551 not_eof(
const int_type& __c) _GLIBCXX_NOEXCEPT
552 {
return eq_int_type(__c, eof()) ? 0 : __c; }
554 #endif //_GLIBCXX_USE_WCHAR_T
556 #ifdef _GLIBCXX_USE_CHAR8_T
560 typedef char8_t char_type;
561 typedef unsigned int int_type;
562 typedef u8streampos pos_type;
564 typedef mbstate_t state_type;
565 #if __cpp_lib_three_way_comparison
566 using comparison_category = strong_ordering;
569 static _GLIBCXX17_CONSTEXPR
void
570 assign(char_type& __c1,
const char_type& __c2) _GLIBCXX_NOEXCEPT
573 static _GLIBCXX_CONSTEXPR
bool
574 eq(
const char_type& __c1,
const char_type& __c2) _GLIBCXX_NOEXCEPT
575 {
return __c1 == __c2; }
577 static _GLIBCXX_CONSTEXPR
bool
578 lt(
const char_type& __c1,
const char_type& __c2) _GLIBCXX_NOEXCEPT
579 {
return __c1 < __c2; }
581 static _GLIBCXX17_CONSTEXPR
int
582 compare(
const char_type* __s1,
const char_type* __s2,
size_t __n)
586 #if __cplusplus > 201402
587 if (__builtin_constant_p(__n)
588 && __constant_char_array_p(__s1, __n)
589 && __constant_char_array_p(__s2, __n))
592 return __builtin_memcmp(__s1, __s2, __n);
595 static _GLIBCXX17_CONSTEXPR
size_t
596 length(
const char_type* __s)
598 #if __cplusplus > 201402
599 if (__constant_string_p(__s))
603 while (!eq(__s[__i], char_type()))
608 static _GLIBCXX17_CONSTEXPR
const char_type*
609 find(
const char_type* __s,
size_t __n,
const char_type& __a)
613 #if __cplusplus > 201402
614 if (__builtin_constant_p(__n)
615 && __builtin_constant_p(__a)
616 && __constant_char_array_p(__s, __n))
619 return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
622 static _GLIBCXX20_CONSTEXPR char_type*
623 move(char_type* __s1,
const char_type* __s2,
size_t __n)
627 #ifdef __cpp_lib_is_constant_evaluated
628 if (std::is_constant_evaluated())
631 return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n));
634 static _GLIBCXX20_CONSTEXPR char_type*
635 copy(char_type* __s1,
const char_type* __s2,
size_t __n)
639 #ifdef __cpp_lib_is_constant_evaluated
640 if (std::is_constant_evaluated())
643 return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
646 static _GLIBCXX20_CONSTEXPR char_type*
647 assign(char_type* __s,
size_t __n, char_type __a)
651 #ifdef __cpp_lib_is_constant_evaluated
652 if (std::is_constant_evaluated())
655 return static_cast<char_type*>(__builtin_memset(__s, __a, __n));
658 static _GLIBCXX_CONSTEXPR char_type
659 to_char_type(
const int_type& __c) _GLIBCXX_NOEXCEPT
660 {
return char_type(__c); }
662 static _GLIBCXX_CONSTEXPR int_type
663 to_int_type(
const char_type& __c) _GLIBCXX_NOEXCEPT
664 {
return int_type(__c); }
666 static _GLIBCXX_CONSTEXPR
bool
667 eq_int_type(
const int_type& __c1,
const int_type& __c2) _GLIBCXX_NOEXCEPT
668 {
return __c1 == __c2; }
670 static _GLIBCXX_CONSTEXPR int_type
671 eof() _GLIBCXX_NOEXCEPT
672 {
return static_cast<int_type>(-1); }
674 static _GLIBCXX_CONSTEXPR int_type
675 not_eof(
const int_type& __c) _GLIBCXX_NOEXCEPT
676 {
return eq_int_type(__c, eof()) ? 0 : __c; }
678 #endif //_GLIBCXX_USE_CHAR8_T
680 _GLIBCXX_END_NAMESPACE_VERSION
683 #if __cplusplus >= 201103L
687 namespace std _GLIBCXX_VISIBILITY(default)
689 _GLIBCXX_BEGIN_NAMESPACE_VERSION
692 struct char_traits<char16_t>
694 typedef char16_t char_type;
695 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
696 typedef uint_least16_t int_type;
697 #elif defined __UINT_LEAST16_TYPE__
698 typedef __UINT_LEAST16_TYPE__ int_type;
700 typedef make_unsigned<char16_t>::type int_type;
704 typedef mbstate_t state_type;
705 #if __cpp_lib_three_way_comparison
706 using comparison_category = strong_ordering;
709 static _GLIBCXX17_CONSTEXPR
void
710 assign(char_type& __c1,
const char_type& __c2) noexcept
713 static constexpr
bool
714 eq(
const char_type& __c1,
const char_type& __c2) noexcept
715 {
return __c1 == __c2; }
717 static constexpr
bool
718 lt(
const char_type& __c1,
const char_type& __c2) noexcept
719 {
return __c1 < __c2; }
721 static _GLIBCXX17_CONSTEXPR
int
722 compare(
const char_type* __s1,
const char_type* __s2,
size_t __n)
724 for (
size_t __i = 0; __i < __n; ++__i)
725 if (lt(__s1[__i], __s2[__i]))
727 else if (lt(__s2[__i], __s1[__i]))
732 static _GLIBCXX17_CONSTEXPR
size_t
733 length(
const char_type* __s)
736 while (!eq(__s[__i], char_type()))
741 static _GLIBCXX17_CONSTEXPR
const char_type*
742 find(
const char_type* __s,
size_t __n,
const char_type& __a)
744 for (
size_t __i = 0; __i < __n; ++__i)
745 if (eq(__s[__i], __a))
750 static _GLIBCXX20_CONSTEXPR char_type*
751 move(char_type* __s1,
const char_type* __s2,
size_t __n)
755 #ifdef __cpp_lib_is_constant_evaluated
756 if (std::is_constant_evaluated())
759 return (static_cast<char_type*>
760 (__builtin_memmove(__s1, __s2, __n *
sizeof(char_type))));
763 static _GLIBCXX20_CONSTEXPR char_type*
764 copy(char_type* __s1,
const char_type* __s2,
size_t __n)
768 #ifdef __cpp_lib_is_constant_evaluated
769 if (std::is_constant_evaluated())
772 return (static_cast<char_type*>
773 (__builtin_memcpy(__s1, __s2, __n *
sizeof(char_type))));
776 static _GLIBCXX20_CONSTEXPR char_type*
777 assign(char_type* __s,
size_t __n, char_type __a)
779 for (
size_t __i = 0; __i < __n; ++__i)
780 assign(__s[__i], __a);
784 static constexpr char_type
785 to_char_type(
const int_type& __c) noexcept
786 {
return char_type(__c); }
788 static constexpr int_type
789 to_int_type(
const char_type& __c) noexcept
790 {
return __c == eof() ? int_type(0xfffd) : int_type(__c); }
792 static constexpr
bool
793 eq_int_type(
const int_type& __c1,
const int_type& __c2) noexcept
794 {
return __c1 == __c2; }
796 static constexpr int_type
798 {
return static_cast<int_type>(-1); }
800 static constexpr int_type
801 not_eof(
const int_type& __c) noexcept
802 {
return eq_int_type(__c, eof()) ? 0 : __c; }
806 struct char_traits<char32_t>
808 typedef char32_t char_type;
809 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
810 typedef uint_least32_t int_type;
811 #elif defined __UINT_LEAST32_TYPE__
812 typedef __UINT_LEAST32_TYPE__ int_type;
814 typedef make_unsigned<char32_t>::type int_type;
818 typedef mbstate_t state_type;
819 #if __cpp_lib_three_way_comparison
820 using comparison_category = strong_ordering;
823 static _GLIBCXX17_CONSTEXPR
void
824 assign(char_type& __c1,
const char_type& __c2) noexcept
827 static constexpr
bool
828 eq(
const char_type& __c1,
const char_type& __c2) noexcept
829 {
return __c1 == __c2; }
831 static constexpr
bool
832 lt(
const char_type& __c1,
const char_type& __c2) noexcept
833 {
return __c1 < __c2; }
835 static _GLIBCXX17_CONSTEXPR
int
836 compare(
const char_type* __s1,
const char_type* __s2,
size_t __n)
838 for (
size_t __i = 0; __i < __n; ++__i)
839 if (lt(__s1[__i], __s2[__i]))
841 else if (lt(__s2[__i], __s1[__i]))
846 static _GLIBCXX17_CONSTEXPR
size_t
847 length(
const char_type* __s)
850 while (!eq(__s[__i], char_type()))
855 static _GLIBCXX17_CONSTEXPR
const char_type*
856 find(
const char_type* __s,
size_t __n,
const char_type& __a)
858 for (
size_t __i = 0; __i < __n; ++__i)
859 if (eq(__s[__i], __a))
864 static _GLIBCXX20_CONSTEXPR char_type*
865 move(char_type* __s1,
const char_type* __s2,
size_t __n)
869 #ifdef __cpp_lib_is_constant_evaluated
870 if (std::is_constant_evaluated())
873 return (static_cast<char_type*>
874 (__builtin_memmove(__s1, __s2, __n *
sizeof(char_type))));
877 static _GLIBCXX20_CONSTEXPR char_type*
878 copy(char_type* __s1,
const char_type* __s2,
size_t __n)
882 #ifdef __cpp_lib_is_constant_evaluated
883 if (std::is_constant_evaluated())
886 return (static_cast<char_type*>
887 (__builtin_memcpy(__s1, __s2, __n *
sizeof(char_type))));
890 static _GLIBCXX20_CONSTEXPR char_type*
891 assign(char_type* __s,
size_t __n, char_type __a)
893 for (
size_t __i = 0; __i < __n; ++__i)
894 assign(__s[__i], __a);
898 static constexpr char_type
899 to_char_type(
const int_type& __c) noexcept
900 {
return char_type(__c); }
902 static constexpr int_type
903 to_int_type(
const char_type& __c) noexcept
904 {
return int_type(__c); }
906 static constexpr
bool
907 eq_int_type(
const int_type& __c1,
const int_type& __c2) noexcept
908 {
return __c1 == __c2; }
910 static constexpr int_type
912 {
return static_cast<int_type>(-1); }
914 static constexpr int_type
915 not_eof(
const int_type& __c) noexcept
916 {
return eq_int_type(__c, eof()) ? 0 : __c; }
919 #if __cpp_lib_three_way_comparison
922 template<
typename _ChTraits>
924 __char_traits_cmp_cat(
int __cmp) noexcept
926 if constexpr (requires {
typename _ChTraits::comparison_category; })
928 using _Cat =
typename _ChTraits::comparison_category;
929 static_assert( !is_void_v<common_comparison_category_t<_Cat>> );
930 return static_cast<_Cat>(__cmp <=> 0);
933 return static_cast<weak_ordering>(__cmp <=> 0);
938 _GLIBCXX_END_NAMESPACE_VERSION
943 #endif // _CHAR_TRAITS_H