1 // Character Traits for use by standard string and iostream -*- C++ -*-
3 // Copyright (C) 1997-2018 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/char_traits.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{string}
31 // ISO C++ 14882: 21 Strings library
34 #ifndef _CHAR_TRAITS_H
35 #define _CHAR_TRAITS_H 1
37 #pragma GCC system_header
39 #include <bits/stl_algobase.h> // std::copy, std::fill_n
40 #include <bits/postypes.h> // For streampos
41 #include <cwchar> // For WEOF, wmemmove, wmemset, etc.
43 #ifndef _GLIBCXX_ALWAYS_INLINE
44 #define _GLIBCXX_ALWAYS_INLINE inline __attribute__((__always_inline__))
47 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 * @brief Mapping from character type to associated types.
54 * @note This is an implementation class for the generic version
55 * of char_traits. It defines int_type, off_type, pos_type, and
56 * state_type. By default these are unsigned long, streamoff,
57 * streampos, and mbstate_t. Users who need a different set of
58 * types, but who don't need to change the definitions of any function
59 * defined in char_traits, can specialize __gnu_cxx::_Char_types
60 * while leaving __gnu_cxx::char_traits alone. */
61 template<typename _CharT>
64 typedef unsigned long int_type;
65 typedef std::streampos pos_type;
66 typedef std::streamoff off_type;
67 typedef std::mbstate_t state_type;
72 * @brief Base class used to implement std::char_traits.
74 * @note For any given actual character type, this definition is
75 * probably wrong. (Most of the member functions are likely to be
76 * right, but the int_type and state_type typedefs, and the eof()
77 * member function, are likely to be wrong.) The reason this class
78 * exists is so users can specialize it. Classes in namespace std
79 * may not be specialized for fundamental types, but classes in
80 * namespace __gnu_cxx may be.
82 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/strings.html#strings.string.character_types
83 * for advice on how to make use of this class for @a unusual character
84 * types. Also, check out include/ext/pod_char_traits.h.
86 template<typename _CharT>
89 typedef _CharT char_type;
90 typedef typename _Char_types<_CharT>::int_type int_type;
91 typedef typename _Char_types<_CharT>::pos_type pos_type;
92 typedef typename _Char_types<_CharT>::off_type off_type;
93 typedef typename _Char_types<_CharT>::state_type state_type;
95 static _GLIBCXX14_CONSTEXPR void
96 assign(char_type& __c1, const char_type& __c2)
99 static _GLIBCXX_CONSTEXPR bool
100 eq(const char_type& __c1, const char_type& __c2)
101 { return __c1 == __c2; }
103 static _GLIBCXX_CONSTEXPR bool
104 lt(const char_type& __c1, const char_type& __c2)
105 { return __c1 < __c2; }
107 static _GLIBCXX14_CONSTEXPR int
108 compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
110 static _GLIBCXX14_CONSTEXPR std::size_t
111 length(const char_type* __s);
113 static _GLIBCXX14_CONSTEXPR const char_type*
114 find(const char_type* __s, std::size_t __n, const char_type& __a);
117 move(char_type* __s1, const char_type* __s2, std::size_t __n);
120 copy(char_type* __s1, const char_type* __s2, std::size_t __n);
123 assign(char_type* __s, std::size_t __n, char_type __a);
125 static _GLIBCXX_CONSTEXPR char_type
126 to_char_type(const int_type& __c)
127 { return static_cast<char_type>(__c); }
129 static _GLIBCXX_CONSTEXPR int_type
130 to_int_type(const char_type& __c)
131 { return static_cast<int_type>(__c); }
133 static _GLIBCXX_CONSTEXPR bool
134 eq_int_type(const int_type& __c1, const int_type& __c2)
135 { return __c1 == __c2; }
137 static _GLIBCXX_CONSTEXPR int_type
139 { return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
141 static _GLIBCXX_CONSTEXPR int_type
142 not_eof(const int_type& __c)
143 { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
146 template<typename _CharT>
147 _GLIBCXX14_CONSTEXPR int
148 char_traits<_CharT>::
149 compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
151 for (std::size_t __i = 0; __i < __n; ++__i)
152 if (lt(__s1[__i], __s2[__i]))
154 else if (lt(__s2[__i], __s1[__i]))
159 template<typename _CharT>
160 _GLIBCXX14_CONSTEXPR std::size_t
161 char_traits<_CharT>::
162 length(const char_type* __p)
165 while (!eq(__p[__i], char_type()))
170 template<typename _CharT>
171 _GLIBCXX14_CONSTEXPR const typename char_traits<_CharT>::char_type*
172 char_traits<_CharT>::
173 find(const char_type* __s, std::size_t __n, const char_type& __a)
175 for (std::size_t __i = 0; __i < __n; ++__i)
176 if (eq(__s[__i], __a))
181 template<typename _CharT>
182 typename char_traits<_CharT>::char_type*
183 char_traits<_CharT>::
184 move(char_type* __s1, const char_type* __s2, std::size_t __n)
188 return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
189 __n * sizeof(char_type)));
192 template<typename _CharT>
193 typename char_traits<_CharT>::char_type*
194 char_traits<_CharT>::
195 copy(char_type* __s1, const char_type* __s2, std::size_t __n)
197 // NB: Inline std::copy so no recursive dependencies.
198 std::copy(__s2, __s2 + __n, __s1);
202 template<typename _CharT>
203 typename char_traits<_CharT>::char_type*
204 char_traits<_CharT>::
205 assign(char_type* __s, std::size_t __n, char_type __a)
207 // NB: Inline std::fill_n so no recursive dependencies.
208 std::fill_n(__s, __n, __a);
212 _GLIBCXX_END_NAMESPACE_VERSION
215 namespace std _GLIBCXX_VISIBILITY(default)
217 _GLIBCXX_BEGIN_NAMESPACE_VERSION
219 #if __cplusplus > 201402
220 #define __cpp_lib_constexpr_char_traits 201611
223 * @brief Determine whether the characters of a NULL-terminated
224 * string are known at compile time.
225 * @param __s The string.
227 * Assumes that _CharT is a built-in character type.
229 template<typename _CharT>
230 static _GLIBCXX_ALWAYS_INLINE constexpr bool
231 __constant_string_p(const _CharT* __s)
233 while (__builtin_constant_p(*__s) && *__s)
235 return __builtin_constant_p(*__s);
239 * @brief Determine whether the characters of a character array are
240 * known at compile time.
241 * @param __a The character array.
242 * @param __n Number of characters.
244 * Assumes that _CharT is a built-in character type.
246 template<typename _CharT>
247 static _GLIBCXX_ALWAYS_INLINE constexpr bool
248 __constant_char_array_p(const _CharT* __a, size_t __n)
251 while (__i < __n && __builtin_constant_p(__a[__i]))
259 * @brief Basis for explicit traits specializations.
261 * @note For any given actual character type, this definition is
262 * probably wrong. Since this is just a thin wrapper around
263 * __gnu_cxx::char_traits, it is possible to achieve a more
264 * appropriate definition by specializing __gnu_cxx::char_traits.
266 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/strings.html#strings.string.character_types
267 * for advice on how to make use of this class for @a unusual character
268 * types. Also, check out include/ext/pod_char_traits.h.
270 template<class _CharT>
271 struct char_traits : public __gnu_cxx::char_traits<_CharT>
275 /// 21.1.3.1 char_traits specializations
277 struct char_traits<char>
279 typedef char char_type;
280 typedef int int_type;
281 typedef streampos pos_type;
282 typedef streamoff off_type;
283 typedef mbstate_t state_type;
285 static _GLIBCXX17_CONSTEXPR void
286 assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
289 static _GLIBCXX_CONSTEXPR bool
290 eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
291 { return __c1 == __c2; }
293 static _GLIBCXX_CONSTEXPR bool
294 lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
297 return (static_cast<unsigned char>(__c1)
298 < static_cast<unsigned char>(__c2));
301 static _GLIBCXX17_CONSTEXPR int
302 compare(const char_type* __s1, const char_type* __s2, size_t __n)
304 #if __cplusplus > 201402
305 if (__builtin_constant_p(__n)
306 && __constant_char_array_p(__s1, __n)
307 && __constant_char_array_p(__s2, __n))
308 return __gnu_cxx::char_traits<char_type>::compare(__s1, __s2, __n);
312 return __builtin_memcmp(__s1, __s2, __n);
315 static _GLIBCXX17_CONSTEXPR size_t
316 length(const char_type* __s)
318 #if __cplusplus > 201402
319 if (__constant_string_p(__s))
320 return __gnu_cxx::char_traits<char_type>::length(__s);
322 return __builtin_strlen(__s);
325 static _GLIBCXX17_CONSTEXPR const char_type*
326 find(const char_type* __s, size_t __n, const char_type& __a)
328 #if __cplusplus > 201402
329 if (__builtin_constant_p(__n)
330 && __builtin_constant_p(__a)
331 && __constant_char_array_p(__s, __n))
332 return __gnu_cxx::char_traits<char_type>::find(__s, __n, __a);
336 return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
340 move(char_type* __s1, const char_type* __s2, size_t __n)
344 return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n));
348 copy(char_type* __s1, const char_type* __s2, size_t __n)
352 return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
356 assign(char_type* __s, size_t __n, char_type __a)
360 return static_cast<char_type*>(__builtin_memset(__s, __a, __n));
363 static _GLIBCXX_CONSTEXPR char_type
364 to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
365 { return static_cast<char_type>(__c); }
367 // To keep both the byte 0xff and the eof symbol 0xffffffff
368 // from ending up as 0xffffffff.
369 static _GLIBCXX_CONSTEXPR int_type
370 to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
371 { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
373 static _GLIBCXX_CONSTEXPR bool
374 eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
375 { return __c1 == __c2; }
377 static _GLIBCXX_CONSTEXPR int_type
378 eof() _GLIBCXX_NOEXCEPT
379 { return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
381 static _GLIBCXX_CONSTEXPR int_type
382 not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
383 { return (__c == eof()) ? 0 : __c; }
387 #ifdef _GLIBCXX_USE_WCHAR_T
388 /// 21.1.3.2 char_traits specializations
390 struct char_traits<wchar_t>
392 typedef wchar_t char_type;
393 typedef wint_t int_type;
394 typedef streamoff off_type;
395 typedef wstreampos pos_type;
396 typedef mbstate_t state_type;
398 static _GLIBCXX17_CONSTEXPR void
399 assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
402 static _GLIBCXX_CONSTEXPR bool
403 eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
404 { return __c1 == __c2; }
406 static _GLIBCXX_CONSTEXPR bool
407 lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
408 { return __c1 < __c2; }
410 static _GLIBCXX17_CONSTEXPR int
411 compare(const char_type* __s1, const char_type* __s2, size_t __n)
413 #if __cplusplus > 201402
414 if (__builtin_constant_p(__n)
415 && __constant_char_array_p(__s1, __n)
416 && __constant_char_array_p(__s2, __n))
417 return __gnu_cxx::char_traits<char_type>::compare(__s1, __s2, __n);
422 return wmemcmp(__s1, __s2, __n);
425 static _GLIBCXX17_CONSTEXPR size_t
426 length(const char_type* __s)
428 #if __cplusplus > 201402
429 if (__constant_string_p(__s))
430 return __gnu_cxx::char_traits<char_type>::length(__s);
436 static _GLIBCXX17_CONSTEXPR const char_type*
437 find(const char_type* __s, size_t __n, const char_type& __a)
439 #if __cplusplus > 201402
440 if (__builtin_constant_p(__n)
441 && __builtin_constant_p(__a)
442 && __constant_char_array_p(__s, __n))
443 return __gnu_cxx::char_traits<char_type>::find(__s, __n, __a);
448 return wmemchr(__s, __a, __n);
452 move(char_type* __s1, const char_type* __s2, size_t __n)
456 return wmemmove(__s1, __s2, __n);
460 copy(char_type* __s1, const char_type* __s2, size_t __n)
464 return wmemcpy(__s1, __s2, __n);
468 assign(char_type* __s, size_t __n, char_type __a)
472 return wmemset(__s, __a, __n);
475 static _GLIBCXX_CONSTEXPR char_type
476 to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
477 { return char_type(__c); }
479 static _GLIBCXX_CONSTEXPR int_type
480 to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
481 { return int_type(__c); }
483 static _GLIBCXX_CONSTEXPR bool
484 eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
485 { return __c1 == __c2; }
487 static _GLIBCXX_CONSTEXPR int_type
488 eof() _GLIBCXX_NOEXCEPT
489 { return static_cast<int_type>(WEOF); }
491 static _GLIBCXX_CONSTEXPR int_type
492 not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
493 { return eq_int_type(__c, eof()) ? 0 : __c; }
495 #endif //_GLIBCXX_USE_WCHAR_T
497 _GLIBCXX_END_NAMESPACE_VERSION
500 #if ((__cplusplus >= 201103L) \
501 && defined(_GLIBCXX_USE_C99_STDINT_TR1))
505 namespace std _GLIBCXX_VISIBILITY(default)
507 _GLIBCXX_BEGIN_NAMESPACE_VERSION
510 struct char_traits<char16_t>
512 typedef char16_t char_type;
513 typedef uint_least16_t int_type;
514 typedef streamoff off_type;
515 typedef u16streampos pos_type;
516 typedef mbstate_t state_type;
518 static _GLIBCXX17_CONSTEXPR void
519 assign(char_type& __c1, const char_type& __c2) noexcept
522 static constexpr bool
523 eq(const char_type& __c1, const char_type& __c2) noexcept
524 { return __c1 == __c2; }
526 static constexpr bool
527 lt(const char_type& __c1, const char_type& __c2) noexcept
528 { return __c1 < __c2; }
530 static _GLIBCXX17_CONSTEXPR int
531 compare(const char_type* __s1, const char_type* __s2, size_t __n)
533 for (size_t __i = 0; __i < __n; ++__i)
534 if (lt(__s1[__i], __s2[__i]))
536 else if (lt(__s2[__i], __s1[__i]))
541 static _GLIBCXX17_CONSTEXPR size_t
542 length(const char_type* __s)
545 while (!eq(__s[__i], char_type()))
550 static _GLIBCXX17_CONSTEXPR const char_type*
551 find(const char_type* __s, size_t __n, const char_type& __a)
553 for (size_t __i = 0; __i < __n; ++__i)
554 if (eq(__s[__i], __a))
560 move(char_type* __s1, const char_type* __s2, size_t __n)
564 return (static_cast<char_type*>
565 (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
569 copy(char_type* __s1, const char_type* __s2, size_t __n)
573 return (static_cast<char_type*>
574 (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
578 assign(char_type* __s, size_t __n, char_type __a)
580 for (size_t __i = 0; __i < __n; ++__i)
581 assign(__s[__i], __a);
585 static constexpr char_type
586 to_char_type(const int_type& __c) noexcept
587 { return char_type(__c); }
589 static constexpr int_type
590 to_int_type(const char_type& __c) noexcept
591 { return __c == eof() ? int_type(0xfffd) : int_type(__c); }
593 static constexpr bool
594 eq_int_type(const int_type& __c1, const int_type& __c2) noexcept
595 { return __c1 == __c2; }
597 static constexpr int_type
599 { return static_cast<int_type>(-1); }
601 static constexpr int_type
602 not_eof(const int_type& __c) noexcept
603 { return eq_int_type(__c, eof()) ? 0 : __c; }
607 struct char_traits<char32_t>
609 typedef char32_t char_type;
610 typedef uint_least32_t int_type;
611 typedef streamoff off_type;
612 typedef u32streampos pos_type;
613 typedef mbstate_t state_type;
615 static _GLIBCXX17_CONSTEXPR void
616 assign(char_type& __c1, const char_type& __c2) noexcept
619 static constexpr bool
620 eq(const char_type& __c1, const char_type& __c2) noexcept
621 { return __c1 == __c2; }
623 static constexpr bool
624 lt(const char_type& __c1, const char_type& __c2) noexcept
625 { return __c1 < __c2; }
627 static _GLIBCXX17_CONSTEXPR int
628 compare(const char_type* __s1, const char_type* __s2, size_t __n)
630 for (size_t __i = 0; __i < __n; ++__i)
631 if (lt(__s1[__i], __s2[__i]))
633 else if (lt(__s2[__i], __s1[__i]))
638 static _GLIBCXX17_CONSTEXPR size_t
639 length(const char_type* __s)
642 while (!eq(__s[__i], char_type()))
647 static _GLIBCXX17_CONSTEXPR const char_type*
648 find(const char_type* __s, size_t __n, const char_type& __a)
650 for (size_t __i = 0; __i < __n; ++__i)
651 if (eq(__s[__i], __a))
657 move(char_type* __s1, const char_type* __s2, size_t __n)
661 return (static_cast<char_type*>
662 (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
666 copy(char_type* __s1, const char_type* __s2, size_t __n)
670 return (static_cast<char_type*>
671 (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
675 assign(char_type* __s, size_t __n, char_type __a)
677 for (size_t __i = 0; __i < __n; ++__i)
678 assign(__s[__i], __a);
682 static constexpr char_type
683 to_char_type(const int_type& __c) noexcept
684 { return char_type(__c); }
686 static constexpr int_type
687 to_int_type(const char_type& __c) noexcept
688 { return int_type(__c); }
690 static constexpr bool
691 eq_int_type(const int_type& __c1, const int_type& __c2) noexcept
692 { return __c1 == __c2; }
694 static constexpr int_type
696 { return static_cast<int_type>(-1); }
698 static constexpr int_type
699 not_eof(const int_type& __c) noexcept
700 { return eq_int_type(__c, eof()) ? 0 : __c; }
703 _GLIBCXX_END_NAMESPACE_VERSION
708 #endif // _CHAR_TRAITS_H