1 // Character Traits for use by standard string and iostream -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file bits/char_traits.h
28 * This is an internal header file, included by other library headers.
29 * Do not attempt to use it directly. @headername{string}
33 // ISO C++ 14882: 21 Strings library
36 #ifndef _CHAR_TRAITS_H
37 #define _CHAR_TRAITS_H 1
39 #pragma GCC system_header
41 #include <bits/stl_algobase.h> // std::copy, std::fill_n
42 #include <bits/postypes.h> // For streampos
43 #include <cwchar> // For WEOF, wmemmove, wmemset, etc.
45 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 * @brief Mapping from character type to associated types.
52 * @note This is an implementation class for the generic version
53 * of char_traits. It defines int_type, off_type, pos_type, and
54 * state_type. By default these are unsigned long, streamoff,
55 * streampos, and mbstate_t. Users who need a different set of
56 * types, but who don't need to change the definitions of any function
57 * defined in char_traits, can specialize __gnu_cxx::_Char_types
58 * while leaving __gnu_cxx::char_traits alone. */
59 template<typename _CharT
>
62 typedef unsigned long int_type
;
63 typedef std::streampos pos_type
;
64 typedef std::streamoff off_type
;
65 typedef std::mbstate_t state_type
;
70 * @brief Base class used to implement std::char_traits.
72 * @note For any given actual character type, this definition is
73 * probably wrong. (Most of the member functions are likely to be
74 * right, but the int_type and state_type typedefs, and the eof()
75 * member function, are likely to be wrong.) The reason this class
76 * exists is so users can specialize it. Classes in namespace std
77 * may not be specialized for fundamental types, but classes in
78 * namespace __gnu_cxx may be.
80 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html
81 * for advice on how to make use of this class for @a unusual character
82 * types. Also, check out include/ext/pod_char_traits.h.
84 template<typename _CharT
>
87 typedef _CharT char_type
;
88 typedef typename _Char_types
<_CharT
>::int_type int_type
;
89 typedef typename _Char_types
<_CharT
>::pos_type pos_type
;
90 typedef typename _Char_types
<_CharT
>::off_type off_type
;
91 typedef typename _Char_types
<_CharT
>::state_type state_type
;
94 assign(char_type
& __c1
, const char_type
& __c2
)
97 static _GLIBCXX_CONSTEXPR
bool
98 eq(const char_type
& __c1
, const char_type
& __c2
)
99 { return __c1
== __c2
; }
101 static _GLIBCXX_CONSTEXPR
bool
102 lt(const char_type
& __c1
, const char_type
& __c2
)
103 { return __c1
< __c2
; }
106 compare(const char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
109 length(const char_type
* __s
);
111 static const char_type
*
112 find(const char_type
* __s
, std::size_t __n
, const char_type
& __a
);
115 move(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
118 copy(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
121 assign(char_type
* __s
, std::size_t __n
, char_type __a
);
123 static _GLIBCXX_CONSTEXPR char_type
124 to_char_type(const int_type
& __c
)
125 { return static_cast<char_type
>(__c
); }
127 static _GLIBCXX_CONSTEXPR int_type
128 to_int_type(const char_type
& __c
)
129 { return static_cast<int_type
>(__c
); }
131 static _GLIBCXX_CONSTEXPR
bool
132 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
133 { return __c1
== __c2
; }
135 static _GLIBCXX_CONSTEXPR int_type
137 { return static_cast<int_type
>(_GLIBCXX_STDIO_EOF
); }
139 static _GLIBCXX_CONSTEXPR int_type
140 not_eof(const int_type
& __c
)
141 { return !eq_int_type(__c
, eof()) ? __c
: to_int_type(char_type()); }
144 template<typename _CharT
>
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
>
159 char_traits
<_CharT
>::
160 length(const char_type
* __p
)
163 while (!eq(__p
[__i
], char_type()))
168 template<typename _CharT
>
169 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 http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html
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
;
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
253 { return __c1
< __c2
; }
256 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
257 { return __builtin_memcmp(__s1
, __s2
, __n
); }
260 length(const char_type
* __s
)
261 { return __builtin_strlen(__s
); }
263 static const char_type
*
264 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
265 { return static_cast<const char_type
*>(__builtin_memchr(__s
, __a
, __n
)); }
268 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
269 { return static_cast<char_type
*>(__builtin_memmove(__s1
, __s2
, __n
)); }
272 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
273 { return static_cast<char_type
*>(__builtin_memcpy(__s1
, __s2
, __n
)); }
276 assign(char_type
* __s
, size_t __n
, char_type __a
)
277 { return static_cast<char_type
*>(__builtin_memset(__s
, __a
, __n
)); }
279 static _GLIBCXX_CONSTEXPR char_type
280 to_char_type(const int_type
& __c
) _GLIBCXX_NOEXCEPT
281 { return static_cast<char_type
>(__c
); }
283 // To keep both the byte 0xff and the eof symbol 0xffffffff
284 // from ending up as 0xffffffff.
285 static _GLIBCXX_CONSTEXPR int_type
286 to_int_type(const char_type
& __c
) _GLIBCXX_NOEXCEPT
287 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
289 static _GLIBCXX_CONSTEXPR
bool
290 eq_int_type(const int_type
& __c1
, const int_type
& __c2
) _GLIBCXX_NOEXCEPT
291 { return __c1
== __c2
; }
293 static _GLIBCXX_CONSTEXPR int_type
294 eof() _GLIBCXX_NOEXCEPT
295 { return static_cast<int_type
>(_GLIBCXX_STDIO_EOF
); }
297 static _GLIBCXX_CONSTEXPR int_type
298 not_eof(const int_type
& __c
) _GLIBCXX_NOEXCEPT
299 { return (__c
== eof()) ? 0 : __c
; }
303 #ifdef _GLIBCXX_USE_WCHAR_T
304 /// 21.1.3.2 char_traits specializations
306 struct char_traits
<wchar_t>
308 typedef wchar_t char_type
;
309 typedef wint_t int_type
;
310 typedef streamoff off_type
;
311 typedef wstreampos pos_type
;
312 typedef mbstate_t state_type
;
315 assign(char_type
& __c1
, const char_type
& __c2
) _GLIBCXX_NOEXCEPT
318 static _GLIBCXX_CONSTEXPR
bool
319 eq(const char_type
& __c1
, const char_type
& __c2
) _GLIBCXX_NOEXCEPT
320 { return __c1
== __c2
; }
322 static _GLIBCXX_CONSTEXPR
bool
323 lt(const char_type
& __c1
, const char_type
& __c2
) _GLIBCXX_NOEXCEPT
324 { return __c1
< __c2
; }
327 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
328 { return wmemcmp(__s1
, __s2
, __n
); }
331 length(const char_type
* __s
)
332 { return wcslen(__s
); }
334 static const char_type
*
335 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
336 { return wmemchr(__s
, __a
, __n
); }
339 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
340 { return wmemmove(__s1
, __s2
, __n
); }
343 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
344 { return wmemcpy(__s1
, __s2
, __n
); }
347 assign(char_type
* __s
, size_t __n
, char_type __a
)
348 { return wmemset(__s
, __a
, __n
); }
350 static _GLIBCXX_CONSTEXPR char_type
351 to_char_type(const int_type
& __c
) _GLIBCXX_NOEXCEPT
352 { return char_type(__c
); }
354 static _GLIBCXX_CONSTEXPR int_type
355 to_int_type(const char_type
& __c
) _GLIBCXX_NOEXCEPT
356 { return int_type(__c
); }
358 static _GLIBCXX_CONSTEXPR
bool
359 eq_int_type(const int_type
& __c1
, const int_type
& __c2
) _GLIBCXX_NOEXCEPT
360 { return __c1
== __c2
; }
362 static _GLIBCXX_CONSTEXPR int_type
363 eof() _GLIBCXX_NOEXCEPT
364 { return static_cast<int_type
>(WEOF
); }
366 static _GLIBCXX_CONSTEXPR int_type
367 not_eof(const int_type
& __c
) _GLIBCXX_NOEXCEPT
368 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
370 #endif //_GLIBCXX_USE_WCHAR_T
372 _GLIBCXX_END_NAMESPACE_VERSION
375 #if ((__cplusplus >= 201103L) \
376 && defined(_GLIBCXX_USE_C99_STDINT_TR1))
380 namespace std
_GLIBCXX_VISIBILITY(default)
382 _GLIBCXX_BEGIN_NAMESPACE_VERSION
385 struct char_traits
<char16_t
>
387 typedef char16_t char_type
;
388 typedef uint_least16_t int_type
;
389 typedef streamoff off_type
;
390 typedef u16streampos pos_type
;
391 typedef mbstate_t state_type
;
394 assign(char_type
& __c1
, const char_type
& __c2
) noexcept
397 static constexpr bool
398 eq(const char_type
& __c1
, const char_type
& __c2
) noexcept
399 { return __c1
== __c2
; }
401 static constexpr bool
402 lt(const char_type
& __c1
, const char_type
& __c2
) noexcept
403 { return __c1
< __c2
; }
406 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
408 for (size_t __i
= 0; __i
< __n
; ++__i
)
409 if (lt(__s1
[__i
], __s2
[__i
]))
411 else if (lt(__s2
[__i
], __s1
[__i
]))
417 length(const char_type
* __s
)
420 while (!eq(__s
[__i
], char_type()))
425 static const char_type
*
426 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
428 for (size_t __i
= 0; __i
< __n
; ++__i
)
429 if (eq(__s
[__i
], __a
))
435 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
437 return (static_cast<char_type
*>
438 (__builtin_memmove(__s1
, __s2
, __n
* sizeof(char_type
))));
442 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
444 return (static_cast<char_type
*>
445 (__builtin_memcpy(__s1
, __s2
, __n
* sizeof(char_type
))));
449 assign(char_type
* __s
, size_t __n
, char_type __a
)
451 for (size_t __i
= 0; __i
< __n
; ++__i
)
452 assign(__s
[__i
], __a
);
456 static constexpr char_type
457 to_char_type(const int_type
& __c
) noexcept
458 { return char_type(__c
); }
460 static constexpr int_type
461 to_int_type(const char_type
& __c
) noexcept
462 { return int_type(__c
); }
464 static constexpr bool
465 eq_int_type(const int_type
& __c1
, const int_type
& __c2
) noexcept
466 { return __c1
== __c2
; }
468 static constexpr int_type
470 { return static_cast<int_type
>(-1); }
472 static constexpr int_type
473 not_eof(const int_type
& __c
) noexcept
474 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
478 struct char_traits
<char32_t
>
480 typedef char32_t char_type
;
481 typedef uint_least32_t int_type
;
482 typedef streamoff off_type
;
483 typedef u32streampos pos_type
;
484 typedef mbstate_t state_type
;
487 assign(char_type
& __c1
, const char_type
& __c2
) noexcept
490 static constexpr bool
491 eq(const char_type
& __c1
, const char_type
& __c2
) noexcept
492 { return __c1
== __c2
; }
494 static constexpr bool
495 lt(const char_type
& __c1
, const char_type
& __c2
) noexcept
496 { return __c1
< __c2
; }
499 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
501 for (size_t __i
= 0; __i
< __n
; ++__i
)
502 if (lt(__s1
[__i
], __s2
[__i
]))
504 else if (lt(__s2
[__i
], __s1
[__i
]))
510 length(const char_type
* __s
)
513 while (!eq(__s
[__i
], char_type()))
518 static const char_type
*
519 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
521 for (size_t __i
= 0; __i
< __n
; ++__i
)
522 if (eq(__s
[__i
], __a
))
528 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
530 return (static_cast<char_type
*>
531 (__builtin_memmove(__s1
, __s2
, __n
* sizeof(char_type
))));
535 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
537 return (static_cast<char_type
*>
538 (__builtin_memcpy(__s1
, __s2
, __n
* sizeof(char_type
))));
542 assign(char_type
* __s
, size_t __n
, char_type __a
)
544 for (size_t __i
= 0; __i
< __n
; ++__i
)
545 assign(__s
[__i
], __a
);
549 static constexpr char_type
550 to_char_type(const int_type
& __c
) noexcept
551 { return char_type(__c
); }
553 static constexpr int_type
554 to_int_type(const char_type
& __c
) noexcept
555 { return int_type(__c
); }
557 static constexpr bool
558 eq_int_type(const int_type
& __c1
, const int_type
& __c2
) noexcept
559 { return __c1
== __c2
; }
561 static constexpr int_type
563 { return static_cast<int_type
>(-1); }
565 static constexpr int_type
566 not_eof(const int_type
& __c
) noexcept
567 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
570 _GLIBCXX_END_NAMESPACE_VERSION
575 #endif // _CHAR_TRAITS_H