1*e4b17023SJohn Marino // Character Traits for use by standard string and iostream -*- C++ -*-
2*e4b17023SJohn Marino
3*e4b17023SJohn Marino // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4*e4b17023SJohn Marino // 2006, 2007, 2008, 2009, 2010, 2011
5*e4b17023SJohn Marino // Free Software Foundation, Inc.
6*e4b17023SJohn Marino //
7*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free
8*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
9*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
10*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
11*e4b17023SJohn Marino // any later version.
12*e4b17023SJohn Marino
13*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
14*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*e4b17023SJohn Marino // GNU General Public License for more details.
17*e4b17023SJohn Marino
18*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
19*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
20*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
23*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
24*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
26*e4b17023SJohn Marino
27*e4b17023SJohn Marino /** @file bits/char_traits.h
28*e4b17023SJohn Marino * This is an internal header file, included by other library headers.
29*e4b17023SJohn Marino * Do not attempt to use it directly. @headername{string}
30*e4b17023SJohn Marino */
31*e4b17023SJohn Marino
32*e4b17023SJohn Marino //
33*e4b17023SJohn Marino // ISO C++ 14882: 21 Strings library
34*e4b17023SJohn Marino //
35*e4b17023SJohn Marino
36*e4b17023SJohn Marino #ifndef _CHAR_TRAITS_H
37*e4b17023SJohn Marino #define _CHAR_TRAITS_H 1
38*e4b17023SJohn Marino
39*e4b17023SJohn Marino #pragma GCC system_header
40*e4b17023SJohn Marino
41*e4b17023SJohn Marino #include <bits/stl_algobase.h> // std::copy, std::fill_n
42*e4b17023SJohn Marino #include <bits/postypes.h> // For streampos
43*e4b17023SJohn Marino #include <cwchar> // For WEOF, wmemmove, wmemset, etc.
44*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)45*e4b17023SJohn Marino namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
46*e4b17023SJohn Marino {
47*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
48*e4b17023SJohn Marino
49*e4b17023SJohn Marino /**
50*e4b17023SJohn Marino * @brief Mapping from character type to associated types.
51*e4b17023SJohn Marino *
52*e4b17023SJohn Marino * @note This is an implementation class for the generic version
53*e4b17023SJohn Marino * of char_traits. It defines int_type, off_type, pos_type, and
54*e4b17023SJohn Marino * state_type. By default these are unsigned long, streamoff,
55*e4b17023SJohn Marino * streampos, and mbstate_t. Users who need a different set of
56*e4b17023SJohn Marino * types, but who don't need to change the definitions of any function
57*e4b17023SJohn Marino * defined in char_traits, can specialize __gnu_cxx::_Char_types
58*e4b17023SJohn Marino * while leaving __gnu_cxx::char_traits alone. */
59*e4b17023SJohn Marino template<typename _CharT>
60*e4b17023SJohn Marino struct _Char_types
61*e4b17023SJohn Marino {
62*e4b17023SJohn Marino typedef unsigned long int_type;
63*e4b17023SJohn Marino typedef std::streampos pos_type;
64*e4b17023SJohn Marino typedef std::streamoff off_type;
65*e4b17023SJohn Marino typedef std::mbstate_t state_type;
66*e4b17023SJohn Marino };
67*e4b17023SJohn Marino
68*e4b17023SJohn Marino
69*e4b17023SJohn Marino /**
70*e4b17023SJohn Marino * @brief Base class used to implement std::char_traits.
71*e4b17023SJohn Marino *
72*e4b17023SJohn Marino * @note For any given actual character type, this definition is
73*e4b17023SJohn Marino * probably wrong. (Most of the member functions are likely to be
74*e4b17023SJohn Marino * right, but the int_type and state_type typedefs, and the eof()
75*e4b17023SJohn Marino * member function, are likely to be wrong.) The reason this class
76*e4b17023SJohn Marino * exists is so users can specialize it. Classes in namespace std
77*e4b17023SJohn Marino * may not be specialized for fundamental types, but classes in
78*e4b17023SJohn Marino * namespace __gnu_cxx may be.
79*e4b17023SJohn Marino *
80*e4b17023SJohn Marino * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html
81*e4b17023SJohn Marino * for advice on how to make use of this class for @a unusual character
82*e4b17023SJohn Marino * types. Also, check out include/ext/pod_char_traits.h.
83*e4b17023SJohn Marino */
84*e4b17023SJohn Marino template<typename _CharT>
85*e4b17023SJohn Marino struct char_traits
86*e4b17023SJohn Marino {
87*e4b17023SJohn Marino typedef _CharT char_type;
88*e4b17023SJohn Marino typedef typename _Char_types<_CharT>::int_type int_type;
89*e4b17023SJohn Marino typedef typename _Char_types<_CharT>::pos_type pos_type;
90*e4b17023SJohn Marino typedef typename _Char_types<_CharT>::off_type off_type;
91*e4b17023SJohn Marino typedef typename _Char_types<_CharT>::state_type state_type;
92*e4b17023SJohn Marino
93*e4b17023SJohn Marino static void
94*e4b17023SJohn Marino assign(char_type& __c1, const char_type& __c2)
95*e4b17023SJohn Marino { __c1 = __c2; }
96*e4b17023SJohn Marino
97*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR bool
98*e4b17023SJohn Marino eq(const char_type& __c1, const char_type& __c2)
99*e4b17023SJohn Marino { return __c1 == __c2; }
100*e4b17023SJohn Marino
101*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR bool
102*e4b17023SJohn Marino lt(const char_type& __c1, const char_type& __c2)
103*e4b17023SJohn Marino { return __c1 < __c2; }
104*e4b17023SJohn Marino
105*e4b17023SJohn Marino static int
106*e4b17023SJohn Marino compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
107*e4b17023SJohn Marino
108*e4b17023SJohn Marino static std::size_t
109*e4b17023SJohn Marino length(const char_type* __s);
110*e4b17023SJohn Marino
111*e4b17023SJohn Marino static const char_type*
112*e4b17023SJohn Marino find(const char_type* __s, std::size_t __n, const char_type& __a);
113*e4b17023SJohn Marino
114*e4b17023SJohn Marino static char_type*
115*e4b17023SJohn Marino move(char_type* __s1, const char_type* __s2, std::size_t __n);
116*e4b17023SJohn Marino
117*e4b17023SJohn Marino static char_type*
118*e4b17023SJohn Marino copy(char_type* __s1, const char_type* __s2, std::size_t __n);
119*e4b17023SJohn Marino
120*e4b17023SJohn Marino static char_type*
121*e4b17023SJohn Marino assign(char_type* __s, std::size_t __n, char_type __a);
122*e4b17023SJohn Marino
123*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR char_type
124*e4b17023SJohn Marino to_char_type(const int_type& __c)
125*e4b17023SJohn Marino { return static_cast<char_type>(__c); }
126*e4b17023SJohn Marino
127*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR int_type
128*e4b17023SJohn Marino to_int_type(const char_type& __c)
129*e4b17023SJohn Marino { return static_cast<int_type>(__c); }
130*e4b17023SJohn Marino
131*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR bool
132*e4b17023SJohn Marino eq_int_type(const int_type& __c1, const int_type& __c2)
133*e4b17023SJohn Marino { return __c1 == __c2; }
134*e4b17023SJohn Marino
135*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR int_type
136*e4b17023SJohn Marino eof()
137*e4b17023SJohn Marino { return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
138*e4b17023SJohn Marino
139*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR int_type
140*e4b17023SJohn Marino not_eof(const int_type& __c)
141*e4b17023SJohn Marino { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
142*e4b17023SJohn Marino };
143*e4b17023SJohn Marino
144*e4b17023SJohn Marino template<typename _CharT>
145*e4b17023SJohn Marino int
146*e4b17023SJohn Marino char_traits<_CharT>::
147*e4b17023SJohn Marino compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
148*e4b17023SJohn Marino {
149*e4b17023SJohn Marino for (std::size_t __i = 0; __i < __n; ++__i)
150*e4b17023SJohn Marino if (lt(__s1[__i], __s2[__i]))
151*e4b17023SJohn Marino return -1;
152*e4b17023SJohn Marino else if (lt(__s2[__i], __s1[__i]))
153*e4b17023SJohn Marino return 1;
154*e4b17023SJohn Marino return 0;
155*e4b17023SJohn Marino }
156*e4b17023SJohn Marino
157*e4b17023SJohn Marino template<typename _CharT>
158*e4b17023SJohn Marino std::size_t
159*e4b17023SJohn Marino char_traits<_CharT>::
160*e4b17023SJohn Marino length(const char_type* __p)
161*e4b17023SJohn Marino {
162*e4b17023SJohn Marino std::size_t __i = 0;
163*e4b17023SJohn Marino while (!eq(__p[__i], char_type()))
164*e4b17023SJohn Marino ++__i;
165*e4b17023SJohn Marino return __i;
166*e4b17023SJohn Marino }
167*e4b17023SJohn Marino
168*e4b17023SJohn Marino template<typename _CharT>
169*e4b17023SJohn Marino const typename char_traits<_CharT>::char_type*
170*e4b17023SJohn Marino char_traits<_CharT>::
171*e4b17023SJohn Marino find(const char_type* __s, std::size_t __n, const char_type& __a)
172*e4b17023SJohn Marino {
173*e4b17023SJohn Marino for (std::size_t __i = 0; __i < __n; ++__i)
174*e4b17023SJohn Marino if (eq(__s[__i], __a))
175*e4b17023SJohn Marino return __s + __i;
176*e4b17023SJohn Marino return 0;
177*e4b17023SJohn Marino }
178*e4b17023SJohn Marino
179*e4b17023SJohn Marino template<typename _CharT>
180*e4b17023SJohn Marino typename char_traits<_CharT>::char_type*
181*e4b17023SJohn Marino char_traits<_CharT>::
182*e4b17023SJohn Marino move(char_type* __s1, const char_type* __s2, std::size_t __n)
183*e4b17023SJohn Marino {
184*e4b17023SJohn Marino return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
185*e4b17023SJohn Marino __n * sizeof(char_type)));
186*e4b17023SJohn Marino }
187*e4b17023SJohn Marino
188*e4b17023SJohn Marino template<typename _CharT>
189*e4b17023SJohn Marino typename char_traits<_CharT>::char_type*
190*e4b17023SJohn Marino char_traits<_CharT>::
191*e4b17023SJohn Marino copy(char_type* __s1, const char_type* __s2, std::size_t __n)
192*e4b17023SJohn Marino {
193*e4b17023SJohn Marino // NB: Inline std::copy so no recursive dependencies.
194*e4b17023SJohn Marino std::copy(__s2, __s2 + __n, __s1);
195*e4b17023SJohn Marino return __s1;
196*e4b17023SJohn Marino }
197*e4b17023SJohn Marino
198*e4b17023SJohn Marino template<typename _CharT>
199*e4b17023SJohn Marino typename char_traits<_CharT>::char_type*
200*e4b17023SJohn Marino char_traits<_CharT>::
201*e4b17023SJohn Marino assign(char_type* __s, std::size_t __n, char_type __a)
202*e4b17023SJohn Marino {
203*e4b17023SJohn Marino // NB: Inline std::fill_n so no recursive dependencies.
204*e4b17023SJohn Marino std::fill_n(__s, __n, __a);
205*e4b17023SJohn Marino return __s;
206*e4b17023SJohn Marino }
207*e4b17023SJohn Marino
208*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
209*e4b17023SJohn Marino } // namespace
210*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)211*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
212*e4b17023SJohn Marino {
213*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
214*e4b17023SJohn Marino
215*e4b17023SJohn Marino // 21.1
216*e4b17023SJohn Marino /**
217*e4b17023SJohn Marino * @brief Basis for explicit traits specializations.
218*e4b17023SJohn Marino *
219*e4b17023SJohn Marino * @note For any given actual character type, this definition is
220*e4b17023SJohn Marino * probably wrong. Since this is just a thin wrapper around
221*e4b17023SJohn Marino * __gnu_cxx::char_traits, it is possible to achieve a more
222*e4b17023SJohn Marino * appropriate definition by specializing __gnu_cxx::char_traits.
223*e4b17023SJohn Marino *
224*e4b17023SJohn Marino * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html
225*e4b17023SJohn Marino * for advice on how to make use of this class for @a unusual character
226*e4b17023SJohn Marino * types. Also, check out include/ext/pod_char_traits.h.
227*e4b17023SJohn Marino */
228*e4b17023SJohn Marino template<class _CharT>
229*e4b17023SJohn Marino struct char_traits : public __gnu_cxx::char_traits<_CharT>
230*e4b17023SJohn Marino { };
231*e4b17023SJohn Marino
232*e4b17023SJohn Marino
233*e4b17023SJohn Marino /// 21.1.3.1 char_traits specializations
234*e4b17023SJohn Marino template<>
235*e4b17023SJohn Marino struct char_traits<char>
236*e4b17023SJohn Marino {
237*e4b17023SJohn Marino typedef char char_type;
238*e4b17023SJohn Marino typedef int int_type;
239*e4b17023SJohn Marino typedef streampos pos_type;
240*e4b17023SJohn Marino typedef streamoff off_type;
241*e4b17023SJohn Marino typedef mbstate_t state_type;
242*e4b17023SJohn Marino
243*e4b17023SJohn Marino static void
244*e4b17023SJohn Marino assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
245*e4b17023SJohn Marino { __c1 = __c2; }
246*e4b17023SJohn Marino
247*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR bool
248*e4b17023SJohn Marino eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
249*e4b17023SJohn Marino { return __c1 == __c2; }
250*e4b17023SJohn Marino
251*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR bool
252*e4b17023SJohn Marino lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
253*e4b17023SJohn Marino { return __c1 < __c2; }
254*e4b17023SJohn Marino
255*e4b17023SJohn Marino static int
256*e4b17023SJohn Marino compare(const char_type* __s1, const char_type* __s2, size_t __n)
257*e4b17023SJohn Marino { return __builtin_memcmp(__s1, __s2, __n); }
258*e4b17023SJohn Marino
259*e4b17023SJohn Marino static size_t
260*e4b17023SJohn Marino length(const char_type* __s)
261*e4b17023SJohn Marino { return __builtin_strlen(__s); }
262*e4b17023SJohn Marino
263*e4b17023SJohn Marino static const char_type*
264*e4b17023SJohn Marino find(const char_type* __s, size_t __n, const char_type& __a)
265*e4b17023SJohn Marino { return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n)); }
266*e4b17023SJohn Marino
267*e4b17023SJohn Marino static char_type*
268*e4b17023SJohn Marino move(char_type* __s1, const char_type* __s2, size_t __n)
269*e4b17023SJohn Marino { return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n)); }
270*e4b17023SJohn Marino
271*e4b17023SJohn Marino static char_type*
272*e4b17023SJohn Marino copy(char_type* __s1, const char_type* __s2, size_t __n)
273*e4b17023SJohn Marino { return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n)); }
274*e4b17023SJohn Marino
275*e4b17023SJohn Marino static char_type*
276*e4b17023SJohn Marino assign(char_type* __s, size_t __n, char_type __a)
277*e4b17023SJohn Marino { return static_cast<char_type*>(__builtin_memset(__s, __a, __n)); }
278*e4b17023SJohn Marino
279*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR char_type
280*e4b17023SJohn Marino to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
281*e4b17023SJohn Marino { return static_cast<char_type>(__c); }
282*e4b17023SJohn Marino
283*e4b17023SJohn Marino // To keep both the byte 0xff and the eof symbol 0xffffffff
284*e4b17023SJohn Marino // from ending up as 0xffffffff.
285*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR int_type
286*e4b17023SJohn Marino to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
287*e4b17023SJohn Marino { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
288*e4b17023SJohn Marino
289*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR bool
290*e4b17023SJohn Marino eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
291*e4b17023SJohn Marino { return __c1 == __c2; }
292*e4b17023SJohn Marino
293*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR int_type
294*e4b17023SJohn Marino eof() _GLIBCXX_NOEXCEPT
295*e4b17023SJohn Marino { return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
296*e4b17023SJohn Marino
297*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR int_type
298*e4b17023SJohn Marino not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
299*e4b17023SJohn Marino { return (__c == eof()) ? 0 : __c; }
300*e4b17023SJohn Marino };
301*e4b17023SJohn Marino
302*e4b17023SJohn Marino
303*e4b17023SJohn Marino #ifdef _GLIBCXX_USE_WCHAR_T
304*e4b17023SJohn Marino /// 21.1.3.2 char_traits specializations
305*e4b17023SJohn Marino template<>
306*e4b17023SJohn Marino struct char_traits<wchar_t>
307*e4b17023SJohn Marino {
308*e4b17023SJohn Marino typedef wchar_t char_type;
309*e4b17023SJohn Marino typedef wint_t int_type;
310*e4b17023SJohn Marino typedef streamoff off_type;
311*e4b17023SJohn Marino typedef wstreampos pos_type;
312*e4b17023SJohn Marino typedef mbstate_t state_type;
313*e4b17023SJohn Marino
314*e4b17023SJohn Marino static void
315*e4b17023SJohn Marino assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
316*e4b17023SJohn Marino { __c1 = __c2; }
317*e4b17023SJohn Marino
318*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR bool
319*e4b17023SJohn Marino eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
320*e4b17023SJohn Marino { return __c1 == __c2; }
321*e4b17023SJohn Marino
322*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR bool
323*e4b17023SJohn Marino lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
324*e4b17023SJohn Marino { return __c1 < __c2; }
325*e4b17023SJohn Marino
326*e4b17023SJohn Marino static int
327*e4b17023SJohn Marino compare(const char_type* __s1, const char_type* __s2, size_t __n)
328*e4b17023SJohn Marino { return wmemcmp(__s1, __s2, __n); }
329*e4b17023SJohn Marino
330*e4b17023SJohn Marino static size_t
331*e4b17023SJohn Marino length(const char_type* __s)
332*e4b17023SJohn Marino { return wcslen(__s); }
333*e4b17023SJohn Marino
334*e4b17023SJohn Marino static const char_type*
335*e4b17023SJohn Marino find(const char_type* __s, size_t __n, const char_type& __a)
336*e4b17023SJohn Marino { return wmemchr(__s, __a, __n); }
337*e4b17023SJohn Marino
338*e4b17023SJohn Marino static char_type*
339*e4b17023SJohn Marino move(char_type* __s1, const char_type* __s2, size_t __n)
340*e4b17023SJohn Marino { return wmemmove(__s1, __s2, __n); }
341*e4b17023SJohn Marino
342*e4b17023SJohn Marino static char_type*
343*e4b17023SJohn Marino copy(char_type* __s1, const char_type* __s2, size_t __n)
344*e4b17023SJohn Marino { return wmemcpy(__s1, __s2, __n); }
345*e4b17023SJohn Marino
346*e4b17023SJohn Marino static char_type*
347*e4b17023SJohn Marino assign(char_type* __s, size_t __n, char_type __a)
348*e4b17023SJohn Marino { return wmemset(__s, __a, __n); }
349*e4b17023SJohn Marino
350*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR char_type
351*e4b17023SJohn Marino to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
352*e4b17023SJohn Marino { return char_type(__c); }
353*e4b17023SJohn Marino
354*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR int_type
355*e4b17023SJohn Marino to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
356*e4b17023SJohn Marino { return int_type(__c); }
357*e4b17023SJohn Marino
358*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR bool
359*e4b17023SJohn Marino eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
360*e4b17023SJohn Marino { return __c1 == __c2; }
361*e4b17023SJohn Marino
362*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR int_type
363*e4b17023SJohn Marino eof() _GLIBCXX_NOEXCEPT
364*e4b17023SJohn Marino { return static_cast<int_type>(WEOF); }
365*e4b17023SJohn Marino
366*e4b17023SJohn Marino static _GLIBCXX_CONSTEXPR int_type
367*e4b17023SJohn Marino not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
368*e4b17023SJohn Marino { return eq_int_type(__c, eof()) ? 0 : __c; }
369*e4b17023SJohn Marino };
370*e4b17023SJohn Marino #endif //_GLIBCXX_USE_WCHAR_T
371*e4b17023SJohn Marino
372*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
373*e4b17023SJohn Marino } // namespace
374*e4b17023SJohn Marino
375*e4b17023SJohn Marino #if (defined(__GXX_EXPERIMENTAL_CXX0X__) \
376*e4b17023SJohn Marino && defined(_GLIBCXX_USE_C99_STDINT_TR1))
377*e4b17023SJohn Marino
378*e4b17023SJohn Marino #include <cstdint>
379*e4b17023SJohn Marino
380*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
381*e4b17023SJohn Marino {
382*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
383*e4b17023SJohn Marino
384*e4b17023SJohn Marino template<>
385*e4b17023SJohn Marino struct char_traits<char16_t>
386*e4b17023SJohn Marino {
387*e4b17023SJohn Marino typedef char16_t char_type;
388*e4b17023SJohn Marino typedef uint_least16_t int_type;
389*e4b17023SJohn Marino typedef streamoff off_type;
390*e4b17023SJohn Marino typedef u16streampos pos_type;
391*e4b17023SJohn Marino typedef mbstate_t state_type;
392*e4b17023SJohn Marino
393*e4b17023SJohn Marino static void
394*e4b17023SJohn Marino assign(char_type& __c1, const char_type& __c2) noexcept
395*e4b17023SJohn Marino { __c1 = __c2; }
396*e4b17023SJohn Marino
397*e4b17023SJohn Marino static constexpr bool
398*e4b17023SJohn Marino eq(const char_type& __c1, const char_type& __c2) noexcept
399*e4b17023SJohn Marino { return __c1 == __c2; }
400*e4b17023SJohn Marino
401*e4b17023SJohn Marino static constexpr bool
402*e4b17023SJohn Marino lt(const char_type& __c1, const char_type& __c2) noexcept
403*e4b17023SJohn Marino { return __c1 < __c2; }
404*e4b17023SJohn Marino
405*e4b17023SJohn Marino static int
406*e4b17023SJohn Marino compare(const char_type* __s1, const char_type* __s2, size_t __n)
407*e4b17023SJohn Marino {
408*e4b17023SJohn Marino for (size_t __i = 0; __i < __n; ++__i)
409*e4b17023SJohn Marino if (lt(__s1[__i], __s2[__i]))
410*e4b17023SJohn Marino return -1;
411*e4b17023SJohn Marino else if (lt(__s2[__i], __s1[__i]))
412*e4b17023SJohn Marino return 1;
413*e4b17023SJohn Marino return 0;
414*e4b17023SJohn Marino }
415*e4b17023SJohn Marino
416*e4b17023SJohn Marino static size_t
417*e4b17023SJohn Marino length(const char_type* __s)
418*e4b17023SJohn Marino {
419*e4b17023SJohn Marino size_t __i = 0;
420*e4b17023SJohn Marino while (!eq(__s[__i], char_type()))
421*e4b17023SJohn Marino ++__i;
422*e4b17023SJohn Marino return __i;
423*e4b17023SJohn Marino }
424*e4b17023SJohn Marino
425*e4b17023SJohn Marino static const char_type*
426*e4b17023SJohn Marino find(const char_type* __s, size_t __n, const char_type& __a)
427*e4b17023SJohn Marino {
428*e4b17023SJohn Marino for (size_t __i = 0; __i < __n; ++__i)
429*e4b17023SJohn Marino if (eq(__s[__i], __a))
430*e4b17023SJohn Marino return __s + __i;
431*e4b17023SJohn Marino return 0;
432*e4b17023SJohn Marino }
433*e4b17023SJohn Marino
434*e4b17023SJohn Marino static char_type*
435*e4b17023SJohn Marino move(char_type* __s1, const char_type* __s2, size_t __n)
436*e4b17023SJohn Marino {
437*e4b17023SJohn Marino return (static_cast<char_type*>
438*e4b17023SJohn Marino (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
439*e4b17023SJohn Marino }
440*e4b17023SJohn Marino
441*e4b17023SJohn Marino static char_type*
442*e4b17023SJohn Marino copy(char_type* __s1, const char_type* __s2, size_t __n)
443*e4b17023SJohn Marino {
444*e4b17023SJohn Marino return (static_cast<char_type*>
445*e4b17023SJohn Marino (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
446*e4b17023SJohn Marino }
447*e4b17023SJohn Marino
448*e4b17023SJohn Marino static char_type*
449*e4b17023SJohn Marino assign(char_type* __s, size_t __n, char_type __a)
450*e4b17023SJohn Marino {
451*e4b17023SJohn Marino for (size_t __i = 0; __i < __n; ++__i)
452*e4b17023SJohn Marino assign(__s[__i], __a);
453*e4b17023SJohn Marino return __s;
454*e4b17023SJohn Marino }
455*e4b17023SJohn Marino
456*e4b17023SJohn Marino static constexpr char_type
457*e4b17023SJohn Marino to_char_type(const int_type& __c) noexcept
458*e4b17023SJohn Marino { return char_type(__c); }
459*e4b17023SJohn Marino
460*e4b17023SJohn Marino static constexpr int_type
461*e4b17023SJohn Marino to_int_type(const char_type& __c) noexcept
462*e4b17023SJohn Marino { return int_type(__c); }
463*e4b17023SJohn Marino
464*e4b17023SJohn Marino static constexpr bool
465*e4b17023SJohn Marino eq_int_type(const int_type& __c1, const int_type& __c2) noexcept
466*e4b17023SJohn Marino { return __c1 == __c2; }
467*e4b17023SJohn Marino
468*e4b17023SJohn Marino static constexpr int_type
469*e4b17023SJohn Marino eof() noexcept
470*e4b17023SJohn Marino { return static_cast<int_type>(-1); }
471*e4b17023SJohn Marino
472*e4b17023SJohn Marino static constexpr int_type
473*e4b17023SJohn Marino not_eof(const int_type& __c) noexcept
474*e4b17023SJohn Marino { return eq_int_type(__c, eof()) ? 0 : __c; }
475*e4b17023SJohn Marino };
476*e4b17023SJohn Marino
477*e4b17023SJohn Marino template<>
478*e4b17023SJohn Marino struct char_traits<char32_t>
479*e4b17023SJohn Marino {
480*e4b17023SJohn Marino typedef char32_t char_type;
481*e4b17023SJohn Marino typedef uint_least32_t int_type;
482*e4b17023SJohn Marino typedef streamoff off_type;
483*e4b17023SJohn Marino typedef u32streampos pos_type;
484*e4b17023SJohn Marino typedef mbstate_t state_type;
485*e4b17023SJohn Marino
486*e4b17023SJohn Marino static void
487*e4b17023SJohn Marino assign(char_type& __c1, const char_type& __c2) noexcept
488*e4b17023SJohn Marino { __c1 = __c2; }
489*e4b17023SJohn Marino
490*e4b17023SJohn Marino static constexpr bool
491*e4b17023SJohn Marino eq(const char_type& __c1, const char_type& __c2) noexcept
492*e4b17023SJohn Marino { return __c1 == __c2; }
493*e4b17023SJohn Marino
494*e4b17023SJohn Marino static constexpr bool
495*e4b17023SJohn Marino lt(const char_type& __c1, const char_type& __c2) noexcept
496*e4b17023SJohn Marino { return __c1 < __c2; }
497*e4b17023SJohn Marino
498*e4b17023SJohn Marino static int
499*e4b17023SJohn Marino compare(const char_type* __s1, const char_type* __s2, size_t __n)
500*e4b17023SJohn Marino {
501*e4b17023SJohn Marino for (size_t __i = 0; __i < __n; ++__i)
502*e4b17023SJohn Marino if (lt(__s1[__i], __s2[__i]))
503*e4b17023SJohn Marino return -1;
504*e4b17023SJohn Marino else if (lt(__s2[__i], __s1[__i]))
505*e4b17023SJohn Marino return 1;
506*e4b17023SJohn Marino return 0;
507*e4b17023SJohn Marino }
508*e4b17023SJohn Marino
509*e4b17023SJohn Marino static size_t
510*e4b17023SJohn Marino length(const char_type* __s)
511*e4b17023SJohn Marino {
512*e4b17023SJohn Marino size_t __i = 0;
513*e4b17023SJohn Marino while (!eq(__s[__i], char_type()))
514*e4b17023SJohn Marino ++__i;
515*e4b17023SJohn Marino return __i;
516*e4b17023SJohn Marino }
517*e4b17023SJohn Marino
518*e4b17023SJohn Marino static const char_type*
519*e4b17023SJohn Marino find(const char_type* __s, size_t __n, const char_type& __a)
520*e4b17023SJohn Marino {
521*e4b17023SJohn Marino for (size_t __i = 0; __i < __n; ++__i)
522*e4b17023SJohn Marino if (eq(__s[__i], __a))
523*e4b17023SJohn Marino return __s + __i;
524*e4b17023SJohn Marino return 0;
525*e4b17023SJohn Marino }
526*e4b17023SJohn Marino
527*e4b17023SJohn Marino static char_type*
528*e4b17023SJohn Marino move(char_type* __s1, const char_type* __s2, size_t __n)
529*e4b17023SJohn Marino {
530*e4b17023SJohn Marino return (static_cast<char_type*>
531*e4b17023SJohn Marino (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
532*e4b17023SJohn Marino }
533*e4b17023SJohn Marino
534*e4b17023SJohn Marino static char_type*
535*e4b17023SJohn Marino copy(char_type* __s1, const char_type* __s2, size_t __n)
536*e4b17023SJohn Marino {
537*e4b17023SJohn Marino return (static_cast<char_type*>
538*e4b17023SJohn Marino (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
539*e4b17023SJohn Marino }
540*e4b17023SJohn Marino
541*e4b17023SJohn Marino static char_type*
542*e4b17023SJohn Marino assign(char_type* __s, size_t __n, char_type __a)
543*e4b17023SJohn Marino {
544*e4b17023SJohn Marino for (size_t __i = 0; __i < __n; ++__i)
545*e4b17023SJohn Marino assign(__s[__i], __a);
546*e4b17023SJohn Marino return __s;
547*e4b17023SJohn Marino }
548*e4b17023SJohn Marino
549*e4b17023SJohn Marino static constexpr char_type
550*e4b17023SJohn Marino to_char_type(const int_type& __c) noexcept
551*e4b17023SJohn Marino { return char_type(__c); }
552*e4b17023SJohn Marino
553*e4b17023SJohn Marino static constexpr int_type
554*e4b17023SJohn Marino to_int_type(const char_type& __c) noexcept
555*e4b17023SJohn Marino { return int_type(__c); }
556*e4b17023SJohn Marino
557*e4b17023SJohn Marino static constexpr bool
558*e4b17023SJohn Marino eq_int_type(const int_type& __c1, const int_type& __c2) noexcept
559*e4b17023SJohn Marino { return __c1 == __c2; }
560*e4b17023SJohn Marino
561*e4b17023SJohn Marino static constexpr int_type
562*e4b17023SJohn Marino eof() noexcept
563*e4b17023SJohn Marino { return static_cast<int_type>(-1); }
564*e4b17023SJohn Marino
565*e4b17023SJohn Marino static constexpr int_type
566*e4b17023SJohn Marino not_eof(const int_type& __c) noexcept
567*e4b17023SJohn Marino { return eq_int_type(__c, eof()) ? 0 : __c; }
568*e4b17023SJohn Marino };
569*e4b17023SJohn Marino
570*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
571*e4b17023SJohn Marino } // namespace
572*e4b17023SJohn Marino
573*e4b17023SJohn Marino #endif
574*e4b17023SJohn Marino
575*e4b17023SJohn Marino #endif // _CHAR_TRAITS_H
576