1 // Character Traits for use by standard string and iostream -*- C++ -*-
3 // Copyright (C) 1997-2017 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 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 * @brief Mapping from character type to associated types.
50 * @note This is an implementation class for the generic version
51 * of char_traits. It defines int_type, off_type, pos_type, and
52 * state_type. By default these are unsigned long, streamoff,
53 * streampos, and mbstate_t. Users who need a different set of
54 * types, but who don't need to change the definitions of any function
55 * defined in char_traits, can specialize __gnu_cxx::_Char_types
56 * while leaving __gnu_cxx::char_traits alone. */
57 template<typename _CharT
>
60 typedef unsigned long int_type
;
61 typedef std::streampos pos_type
;
62 typedef std::streamoff off_type
;
63 typedef std::mbstate_t state_type
;
68 * @brief Base class used to implement std::char_traits.
70 * @note For any given actual character type, this definition is
71 * probably wrong. (Most of the member functions are likely to be
72 * right, but the int_type and state_type typedefs, and the eof()
73 * member function, are likely to be wrong.) The reason this class
74 * exists is so users can specialize it. Classes in namespace std
75 * may not be specialized for fundamental types, but classes in
76 * namespace __gnu_cxx may be.
78 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/strings.html#strings.string.character_types
79 * for advice on how to make use of this class for @a unusual character
80 * types. Also, check out include/ext/pod_char_traits.h.
82 template<typename _CharT
>
85 typedef _CharT char_type
;
86 typedef typename _Char_types
<_CharT
>::int_type int_type
;
87 typedef typename _Char_types
<_CharT
>::pos_type pos_type
;
88 typedef typename _Char_types
<_CharT
>::off_type off_type
;
89 typedef typename _Char_types
<_CharT
>::state_type state_type
;
91 static _GLIBCXX14_CONSTEXPR
void
92 assign(char_type
& __c1
, const char_type
& __c2
)
95 static _GLIBCXX_CONSTEXPR
bool
96 eq(const char_type
& __c1
, const char_type
& __c2
)
97 { return __c1
== __c2
; }
99 static _GLIBCXX_CONSTEXPR
bool
100 lt(const char_type
& __c1
, const char_type
& __c2
)
101 { return __c1
< __c2
; }
103 static _GLIBCXX14_CONSTEXPR
int
104 compare(const char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
106 static _GLIBCXX14_CONSTEXPR
std::size_t
107 length(const char_type
* __s
);
109 static _GLIBCXX14_CONSTEXPR
const char_type
*
110 find(const char_type
* __s
, std::size_t __n
, const char_type
& __a
);
113 move(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
116 copy(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
119 assign(char_type
* __s
, std::size_t __n
, char_type __a
);
121 static _GLIBCXX_CONSTEXPR char_type
122 to_char_type(const int_type
& __c
)
123 { return static_cast<char_type
>(__c
); }
125 static _GLIBCXX_CONSTEXPR int_type
126 to_int_type(const char_type
& __c
)
127 { return static_cast<int_type
>(__c
); }
129 static _GLIBCXX_CONSTEXPR
bool
130 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
131 { return __c1
== __c2
; }
133 static _GLIBCXX_CONSTEXPR int_type
135 { return static_cast<int_type
>(_GLIBCXX_STDIO_EOF
); }
137 static _GLIBCXX_CONSTEXPR int_type
138 not_eof(const int_type
& __c
)
139 { return !eq_int_type(__c
, eof()) ? __c
: to_int_type(char_type()); }
142 // #define __cpp_lib_constexpr_char_traits 201611
144 template<typename _CharT
>
145 _GLIBCXX14_CONSTEXPR
int
146 char_traits
<_CharT
>::
147 compare(const char_type
* __s1
, const char_type
* __s2
, std::size_t __n
)
149 for (std::size_t __i
= 0; __i
< __n
; ++__i
)
150 if (lt(__s1
[__i
], __s2
[__i
]))
152 else if (lt(__s2
[__i
], __s1
[__i
]))
157 template<typename _CharT
>
158 _GLIBCXX14_CONSTEXPR
std::size_t
159 char_traits
<_CharT
>::
160 length(const char_type
* __p
)
163 while (!eq(__p
[__i
], char_type()))
168 template<typename _CharT
>
169 _GLIBCXX14_CONSTEXPR
const typename char_traits
<_CharT
>::char_type
*
170 char_traits
<_CharT
>::
171 find(const char_type
* __s
, std::size_t __n
, const char_type
& __a
)
173 for (std::size_t __i
= 0; __i
< __n
; ++__i
)
174 if (eq(__s
[__i
], __a
))
179 template<typename _CharT
>
180 typename char_traits
<_CharT
>::char_type
*
181 char_traits
<_CharT
>::
182 move(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
)
184 return static_cast<_CharT
*>(__builtin_memmove(__s1
, __s2
,
185 __n
* sizeof(char_type
)));
188 template<typename _CharT
>
189 typename char_traits
<_CharT
>::char_type
*
190 char_traits
<_CharT
>::
191 copy(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
)
193 // NB: Inline std::copy so no recursive dependencies.
194 std::copy(__s2
, __s2
+ __n
, __s1
);
198 template<typename _CharT
>
199 typename char_traits
<_CharT
>::char_type
*
200 char_traits
<_CharT
>::
201 assign(char_type
* __s
, std::size_t __n
, char_type __a
)
203 // NB: Inline std::fill_n so no recursive dependencies.
204 std::fill_n(__s
, __n
, __a
);
208 _GLIBCXX_END_NAMESPACE_VERSION
211 namespace std
_GLIBCXX_VISIBILITY(default)
213 _GLIBCXX_BEGIN_NAMESPACE_VERSION
217 * @brief Basis for explicit traits specializations.
219 * @note For any given actual character type, this definition is
220 * probably wrong. Since this is just a thin wrapper around
221 * __gnu_cxx::char_traits, it is possible to achieve a more
222 * appropriate definition by specializing __gnu_cxx::char_traits.
224 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/strings.html#strings.string.character_types
225 * for advice on how to make use of this class for @a unusual character
226 * types. Also, check out include/ext/pod_char_traits.h.
228 template<class _CharT
>
229 struct char_traits
: public __gnu_cxx::char_traits
<_CharT
>
233 /// 21.1.3.1 char_traits specializations
235 struct char_traits
<char>
237 typedef char char_type
;
238 typedef int int_type
;
239 typedef streampos pos_type
;
240 typedef streamoff off_type
;
241 typedef mbstate_t state_type
;
243 static _GLIBCXX17_CONSTEXPR
void
244 assign(char_type
& __c1
, const char_type
& __c2
) _GLIBCXX_NOEXCEPT
247 static _GLIBCXX_CONSTEXPR
bool
248 eq(const char_type
& __c1
, const char_type
& __c2
) _GLIBCXX_NOEXCEPT
249 { return __c1
== __c2
; }
251 static _GLIBCXX_CONSTEXPR
bool
252 lt(const char_type
& __c1
, const char_type
& __c2
) _GLIBCXX_NOEXCEPT
255 return (static_cast<unsigned char>(__c1
)
256 < static_cast<unsigned char>(__c2
));
259 static /* _GLIBCXX17_CONSTEXPR */ int
260 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
264 return __builtin_memcmp(__s1
, __s2
, __n
);
267 static /* _GLIBCXX17_CONSTEXPR */ size_t
268 length(const char_type
* __s
)
269 { return __builtin_strlen(__s
); }
271 static /* _GLIBCXX17_CONSTEXPR */ const char_type
*
272 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
276 return static_cast<const char_type
*>(__builtin_memchr(__s
, __a
, __n
));
280 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
284 return static_cast<char_type
*>(__builtin_memmove(__s1
, __s2
, __n
));
288 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
292 return static_cast<char_type
*>(__builtin_memcpy(__s1
, __s2
, __n
));
296 assign(char_type
* __s
, size_t __n
, char_type __a
)
300 return static_cast<char_type
*>(__builtin_memset(__s
, __a
, __n
));
303 static _GLIBCXX_CONSTEXPR char_type
304 to_char_type(const int_type
& __c
) _GLIBCXX_NOEXCEPT
305 { return static_cast<char_type
>(__c
); }
307 // To keep both the byte 0xff and the eof symbol 0xffffffff
308 // from ending up as 0xffffffff.
309 static _GLIBCXX_CONSTEXPR int_type
310 to_int_type(const char_type
& __c
) _GLIBCXX_NOEXCEPT
311 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
313 static _GLIBCXX_CONSTEXPR
bool
314 eq_int_type(const int_type
& __c1
, const int_type
& __c2
) _GLIBCXX_NOEXCEPT
315 { return __c1
== __c2
; }
317 static _GLIBCXX_CONSTEXPR int_type
318 eof() _GLIBCXX_NOEXCEPT
319 { return static_cast<int_type
>(_GLIBCXX_STDIO_EOF
); }
321 static _GLIBCXX_CONSTEXPR int_type
322 not_eof(const int_type
& __c
) _GLIBCXX_NOEXCEPT
323 { return (__c
== eof()) ? 0 : __c
; }
327 #ifdef _GLIBCXX_USE_WCHAR_T
328 /// 21.1.3.2 char_traits specializations
330 struct char_traits
<wchar_t>
332 typedef wchar_t char_type
;
333 typedef wint_t int_type
;
334 typedef streamoff off_type
;
335 typedef wstreampos pos_type
;
336 typedef mbstate_t state_type
;
338 static _GLIBCXX17_CONSTEXPR
void
339 assign(char_type
& __c1
, const char_type
& __c2
) _GLIBCXX_NOEXCEPT
342 static _GLIBCXX_CONSTEXPR
bool
343 eq(const char_type
& __c1
, const char_type
& __c2
) _GLIBCXX_NOEXCEPT
344 { return __c1
== __c2
; }
346 static _GLIBCXX_CONSTEXPR
bool
347 lt(const char_type
& __c1
, const char_type
& __c2
) _GLIBCXX_NOEXCEPT
348 { return __c1
< __c2
; }
350 static /* _GLIBCXX17_CONSTEXPR */ int
351 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
355 return wmemcmp(__s1
, __s2
, __n
);
358 static /* _GLIBCXX17_CONSTEXPR */ size_t
359 length(const char_type
* __s
)
360 { return wcslen(__s
); }
362 static /* _GLIBCXX17_CONSTEXPR */ const char_type
*
363 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
367 return wmemchr(__s
, __a
, __n
);
371 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
375 return wmemmove(__s1
, __s2
, __n
);
379 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
383 return wmemcpy(__s1
, __s2
, __n
);
387 assign(char_type
* __s
, size_t __n
, char_type __a
)
391 return wmemset(__s
, __a
, __n
);
394 static _GLIBCXX_CONSTEXPR char_type
395 to_char_type(const int_type
& __c
) _GLIBCXX_NOEXCEPT
396 { return char_type(__c
); }
398 static _GLIBCXX_CONSTEXPR int_type
399 to_int_type(const char_type
& __c
) _GLIBCXX_NOEXCEPT
400 { return int_type(__c
); }
402 static _GLIBCXX_CONSTEXPR
bool
403 eq_int_type(const int_type
& __c1
, const int_type
& __c2
) _GLIBCXX_NOEXCEPT
404 { return __c1
== __c2
; }
406 static _GLIBCXX_CONSTEXPR int_type
407 eof() _GLIBCXX_NOEXCEPT
408 { return static_cast<int_type
>(WEOF
); }
410 static _GLIBCXX_CONSTEXPR int_type
411 not_eof(const int_type
& __c
) _GLIBCXX_NOEXCEPT
412 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
414 #endif //_GLIBCXX_USE_WCHAR_T
416 _GLIBCXX_END_NAMESPACE_VERSION
419 #if ((__cplusplus >= 201103L) \
420 && defined(_GLIBCXX_USE_C99_STDINT_TR1))
424 namespace std
_GLIBCXX_VISIBILITY(default)
426 _GLIBCXX_BEGIN_NAMESPACE_VERSION
429 struct char_traits
<char16_t
>
431 typedef char16_t char_type
;
432 typedef uint_least16_t int_type
;
433 typedef streamoff off_type
;
434 typedef u16streampos pos_type
;
435 typedef mbstate_t state_type
;
437 static _GLIBCXX17_CONSTEXPR
void
438 assign(char_type
& __c1
, const char_type
& __c2
) noexcept
441 static constexpr bool
442 eq(const char_type
& __c1
, const char_type
& __c2
) noexcept
443 { return __c1
== __c2
; }
445 static constexpr bool
446 lt(const char_type
& __c1
, const char_type
& __c2
) noexcept
447 { return __c1
< __c2
; }
449 static _GLIBCXX17_CONSTEXPR
int
450 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
452 for (size_t __i
= 0; __i
< __n
; ++__i
)
453 if (lt(__s1
[__i
], __s2
[__i
]))
455 else if (lt(__s2
[__i
], __s1
[__i
]))
460 static _GLIBCXX17_CONSTEXPR
size_t
461 length(const char_type
* __s
)
464 while (!eq(__s
[__i
], char_type()))
469 static _GLIBCXX17_CONSTEXPR
const char_type
*
470 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
472 for (size_t __i
= 0; __i
< __n
; ++__i
)
473 if (eq(__s
[__i
], __a
))
479 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
483 return (static_cast<char_type
*>
484 (__builtin_memmove(__s1
, __s2
, __n
* sizeof(char_type
))));
488 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
492 return (static_cast<char_type
*>
493 (__builtin_memcpy(__s1
, __s2
, __n
* sizeof(char_type
))));
497 assign(char_type
* __s
, size_t __n
, char_type __a
)
499 for (size_t __i
= 0; __i
< __n
; ++__i
)
500 assign(__s
[__i
], __a
);
504 static constexpr char_type
505 to_char_type(const int_type
& __c
) noexcept
506 { return char_type(__c
); }
508 static constexpr int_type
509 to_int_type(const char_type
& __c
) noexcept
510 { return int_type(__c
); }
512 static constexpr bool
513 eq_int_type(const int_type
& __c1
, const int_type
& __c2
) noexcept
514 { return __c1
== __c2
; }
516 static constexpr int_type
518 { return static_cast<int_type
>(-1); }
520 static constexpr int_type
521 not_eof(const int_type
& __c
) noexcept
522 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
526 struct char_traits
<char32_t
>
528 typedef char32_t char_type
;
529 typedef uint_least32_t int_type
;
530 typedef streamoff off_type
;
531 typedef u32streampos pos_type
;
532 typedef mbstate_t state_type
;
534 static _GLIBCXX17_CONSTEXPR
void
535 assign(char_type
& __c1
, const char_type
& __c2
) noexcept
538 static constexpr bool
539 eq(const char_type
& __c1
, const char_type
& __c2
) noexcept
540 { return __c1
== __c2
; }
542 static constexpr bool
543 lt(const char_type
& __c1
, const char_type
& __c2
) noexcept
544 { return __c1
< __c2
; }
546 static _GLIBCXX17_CONSTEXPR
int
547 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
549 for (size_t __i
= 0; __i
< __n
; ++__i
)
550 if (lt(__s1
[__i
], __s2
[__i
]))
552 else if (lt(__s2
[__i
], __s1
[__i
]))
557 static _GLIBCXX17_CONSTEXPR
size_t
558 length(const char_type
* __s
)
561 while (!eq(__s
[__i
], char_type()))
566 static _GLIBCXX17_CONSTEXPR
const char_type
*
567 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
569 for (size_t __i
= 0; __i
< __n
; ++__i
)
570 if (eq(__s
[__i
], __a
))
576 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
580 return (static_cast<char_type
*>
581 (__builtin_memmove(__s1
, __s2
, __n
* sizeof(char_type
))));
585 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
589 return (static_cast<char_type
*>
590 (__builtin_memcpy(__s1
, __s2
, __n
* sizeof(char_type
))));
594 assign(char_type
* __s
, size_t __n
, char_type __a
)
596 for (size_t __i
= 0; __i
< __n
; ++__i
)
597 assign(__s
[__i
], __a
);
601 static constexpr char_type
602 to_char_type(const int_type
& __c
) noexcept
603 { return char_type(__c
); }
605 static constexpr int_type
606 to_int_type(const char_type
& __c
) noexcept
607 { return int_type(__c
); }
609 static constexpr bool
610 eq_int_type(const int_type
& __c1
, const int_type
& __c2
) noexcept
611 { return __c1
== __c2
; }
613 static constexpr int_type
615 { return static_cast<int_type
>(-1); }
617 static constexpr int_type
618 not_eof(const int_type
& __c
) noexcept
619 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
622 _GLIBCXX_END_NAMESPACE_VERSION
627 #endif // _CHAR_TRAITS_H