1 // Character Traits for use by standard string and iostream -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
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 2, 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 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 /** @file char_traits.h
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
38 // ISO C++ 14882: 21 Strings library
41 #ifndef _CHAR_TRAITS_H
42 #define _CHAR_TRAITS_H 1
44 #pragma GCC system_header
46 #include <bits/stl_algobase.h> // std::copy, std::fill_n
47 #include <bits/postypes.h> // For streampos
48 #include <cwchar> // For WEOF, wmemmove, wmemset, etc.
50 #ifndef _GLIBCXX_STDIO_MACROS
51 # include <cstdio> // For EOF
52 # define _CHAR_TRAITS_EOF EOF
54 # define _CHAR_TRAITS_EOF (-1)
57 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
60 * @brief Mapping from character type to associated types.
62 * @note This is an implementation class for the generic version
63 * of char_traits. It defines int_type, off_type, pos_type, and
64 * state_type. By default these are unsigned long, streamoff,
65 * streampos, and mbstate_t. Users who need a different set of
66 * types, but who don't need to change the definitions of any function
67 * defined in char_traits, can specialize __gnu_cxx::_Char_types
68 * while leaving __gnu_cxx::char_traits alone. */
69 template<typename _CharT
>
72 typedef unsigned long int_type
;
73 typedef std::streampos pos_type
;
74 typedef std::streamoff off_type
;
75 typedef std::mbstate_t state_type
;
80 * @brief Base class used to implement std::char_traits.
82 * @note For any given actual character type, this definition is
83 * probably wrong. (Most of the member functions are likely to be
84 * right, but the int_type and state_type typedefs, and the eof()
85 * member function, are likely to be wrong.) The reason this class
86 * exists is so users can specialize it. Classes in namespace std
87 * may not be specialized for fundamental types, but classes in
88 * namespace __gnu_cxx may be.
90 * See http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#5
91 * for advice on how to make use of this class for "unusual" character
92 * types. Also, check out include/ext/pod_char_traits.h.
94 template<typename _CharT
>
97 typedef _CharT char_type
;
98 typedef typename _Char_types
<_CharT
>::int_type int_type
;
99 typedef typename _Char_types
<_CharT
>::pos_type pos_type
;
100 typedef typename _Char_types
<_CharT
>::off_type off_type
;
101 typedef typename _Char_types
<_CharT
>::state_type state_type
;
104 assign(char_type
& __c1
, const char_type
& __c2
)
108 eq(const char_type
& __c1
, const char_type
& __c2
)
109 { return __c1
== __c2
; }
112 lt(const char_type
& __c1
, const char_type
& __c2
)
113 { return __c1
< __c2
; }
116 compare(const char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
119 length(const char_type
* __s
);
121 static const char_type
*
122 find(const char_type
* __s
, std::size_t __n
, const char_type
& __a
);
125 move(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
128 copy(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
131 assign(char_type
* __s
, std::size_t __n
, char_type __a
);
134 to_char_type(const int_type
& __c
)
135 { return static_cast<char_type
>(__c
); }
138 to_int_type(const char_type
& __c
)
139 { return static_cast<int_type
>(__c
); }
142 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
143 { return __c1
== __c2
; }
147 { return static_cast<int_type
>(_CHAR_TRAITS_EOF
); }
150 not_eof(const int_type
& __c
)
151 { return !eq_int_type(__c
, eof()) ? __c
: to_int_type(char_type()); }
154 template<typename _CharT
>
156 char_traits
<_CharT
>::
157 compare(const char_type
* __s1
, const char_type
* __s2
, std::size_t __n
)
159 for (std::size_t __i
= 0; __i
< __n
; ++__i
)
160 if (lt(__s1
[__i
], __s2
[__i
]))
162 else if (lt(__s2
[__i
], __s1
[__i
]))
167 template<typename _CharT
>
169 char_traits
<_CharT
>::
170 length(const char_type
* __p
)
173 while (!eq(__p
[__i
], char_type()))
178 template<typename _CharT
>
179 const typename char_traits
<_CharT
>::char_type
*
180 char_traits
<_CharT
>::
181 find(const char_type
* __s
, std::size_t __n
, const char_type
& __a
)
183 for (std::size_t __i
= 0; __i
< __n
; ++__i
)
184 if (eq(__s
[__i
], __a
))
189 template<typename _CharT
>
190 typename char_traits
<_CharT
>::char_type
*
191 char_traits
<_CharT
>::
192 move(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
)
194 return static_cast<_CharT
*>(__builtin_memmove(__s1
, __s2
,
195 __n
* sizeof(char_type
)));
198 template<typename _CharT
>
199 typename char_traits
<_CharT
>::char_type
*
200 char_traits
<_CharT
>::
201 copy(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
)
203 // NB: Inline std::copy so no recursive dependencies.
204 std::copy(__s2
, __s2
+ __n
, __s1
);
208 template<typename _CharT
>
209 typename char_traits
<_CharT
>::char_type
*
210 char_traits
<_CharT
>::
211 assign(char_type
* __s
, std::size_t __n
, char_type __a
)
213 // NB: Inline std::fill_n so no recursive dependencies.
214 std::fill_n(__s
, __n
, __a
);
218 _GLIBCXX_END_NAMESPACE
220 _GLIBCXX_BEGIN_NAMESPACE(std
)
224 * @brief Basis for explicit traits specializations.
226 * @note For any given actual character type, this definition is
227 * probably wrong. Since this is just a thin wrapper around
228 * __gnu_cxx::char_traits, it is possible to achieve a more
229 * appropriate definition by specializing __gnu_cxx::char_traits.
231 * See http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#5
232 * for advice on how to make use of this class for "unusual" character
233 * types. Also, check out include/ext/pod_char_traits.h.
235 template<class _CharT
>
236 struct char_traits
: public __gnu_cxx::char_traits
<_CharT
>
240 /// 21.1.3.1 char_traits specializations
242 struct char_traits
<char>
244 typedef char char_type
;
245 typedef int int_type
;
246 typedef streampos pos_type
;
247 typedef streamoff off_type
;
248 typedef mbstate_t state_type
;
251 assign(char_type
& __c1
, const char_type
& __c2
)
255 eq(const char_type
& __c1
, const char_type
& __c2
)
256 { return __c1
== __c2
; }
259 lt(const char_type
& __c1
, const char_type
& __c2
)
260 { return __c1
< __c2
; }
263 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
264 { return __builtin_memcmp(__s1
, __s2
, __n
); }
267 length(const char_type
* __s
)
268 { return __builtin_strlen(__s
); }
270 static const char_type
*
271 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
272 { return static_cast<const char_type
*>(__builtin_memchr(__s
, __a
, __n
)); }
275 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
276 { return static_cast<char_type
*>(__builtin_memmove(__s1
, __s2
, __n
)); }
279 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
280 { return static_cast<char_type
*>(__builtin_memcpy(__s1
, __s2
, __n
)); }
283 assign(char_type
* __s
, size_t __n
, char_type __a
)
284 { return static_cast<char_type
*>(__builtin_memset(__s
, __a
, __n
)); }
287 to_char_type(const int_type
& __c
)
288 { return static_cast<char_type
>(__c
); }
290 // To keep both the byte 0xff and the eof symbol 0xffffffff
291 // from ending up as 0xffffffff.
293 to_int_type(const char_type
& __c
)
294 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
297 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
298 { return __c1
== __c2
; }
301 eof() { return static_cast<int_type
>(_CHAR_TRAITS_EOF
); }
304 not_eof(const int_type
& __c
)
305 { return (__c
== eof()) ? 0 : __c
; }
309 #ifdef _GLIBCXX_USE_WCHAR_T
310 /// 21.1.3.2 char_traits specializations
312 struct char_traits
<wchar_t>
314 typedef wchar_t char_type
;
315 typedef wint_t int_type
;
316 typedef streamoff off_type
;
317 typedef wstreampos pos_type
;
318 typedef mbstate_t state_type
;
321 assign(char_type
& __c1
, const char_type
& __c2
)
325 eq(const char_type
& __c1
, const char_type
& __c2
)
326 { return __c1
== __c2
; }
329 lt(const char_type
& __c1
, const char_type
& __c2
)
330 { return __c1
< __c2
; }
333 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
334 { return wmemcmp(__s1
, __s2
, __n
); }
337 length(const char_type
* __s
)
338 { return wcslen(__s
); }
340 static const char_type
*
341 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
342 { return wmemchr(__s
, __a
, __n
); }
345 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
346 { return wmemmove(__s1
, __s2
, __n
); }
349 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
350 { return wmemcpy(__s1
, __s2
, __n
); }
353 assign(char_type
* __s
, size_t __n
, char_type __a
)
354 { return wmemset(__s
, __a
, __n
); }
357 to_char_type(const int_type
& __c
) { return char_type(__c
); }
360 to_int_type(const char_type
& __c
) { return int_type(__c
); }
363 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
364 { return __c1
== __c2
; }
367 eof() { return static_cast<int_type
>(WEOF
); }
370 not_eof(const int_type
& __c
)
371 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
373 #endif //_GLIBCXX_USE_WCHAR_T
375 _GLIBCXX_END_NAMESPACE
377 #undef _CHAR_TRAITS_EOF