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
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 char_traits.h
28 * This is an internal header file, included by other library headers.
29 * You should not attempt to use it directly.
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 #ifndef _GLIBCXX_STDIO_MACROS
46 # include <cstdio> // For EOF
47 # define _CHAR_TRAITS_EOF EOF
49 # define _CHAR_TRAITS_EOF (-1)
52 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
55 * @brief Mapping from character type to associated types.
57 * @note This is an implementation class for the generic version
58 * of char_traits. It defines int_type, off_type, pos_type, and
59 * state_type. By default these are unsigned long, streamoff,
60 * streampos, and mbstate_t. Users who need a different set of
61 * types, but who don't need to change the definitions of any function
62 * defined in char_traits, can specialize __gnu_cxx::_Char_types
63 * while leaving __gnu_cxx::char_traits alone. */
64 template<typename _CharT>
67 typedef unsigned long int_type;
68 typedef std::streampos pos_type;
69 typedef std::streamoff off_type;
70 typedef std::mbstate_t state_type;
75 * @brief Base class used to implement std::char_traits.
77 * @note For any given actual character type, this definition is
78 * probably wrong. (Most of the member functions are likely to be
79 * right, but the int_type and state_type typedefs, and the eof()
80 * member function, are likely to be wrong.) The reason this class
81 * exists is so users can specialize it. Classes in namespace std
82 * may not be specialized for fundamental types, but classes in
83 * namespace __gnu_cxx may be.
85 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html
86 * for advice on how to make use of this class for "unusual" character
87 * types. Also, check out include/ext/pod_char_traits.h.
89 template<typename _CharT>
92 typedef _CharT char_type;
93 typedef typename _Char_types<_CharT>::int_type int_type;
94 typedef typename _Char_types<_CharT>::pos_type pos_type;
95 typedef typename _Char_types<_CharT>::off_type off_type;
96 typedef typename _Char_types<_CharT>::state_type state_type;
99 assign(char_type& __c1, const char_type& __c2)
103 eq(const char_type& __c1, const char_type& __c2)
104 { return __c1 == __c2; }
107 lt(const char_type& __c1, const char_type& __c2)
108 { return __c1 < __c2; }
111 compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
114 length(const char_type* __s);
116 static const char_type*
117 find(const char_type* __s, std::size_t __n, const char_type& __a);
120 move(char_type* __s1, const char_type* __s2, std::size_t __n);
123 copy(char_type* __s1, const char_type* __s2, std::size_t __n);
126 assign(char_type* __s, std::size_t __n, char_type __a);
129 to_char_type(const int_type& __c)
130 { return static_cast<char_type>(__c); }
133 to_int_type(const char_type& __c)
134 { return static_cast<int_type>(__c); }
137 eq_int_type(const int_type& __c1, const int_type& __c2)
138 { return __c1 == __c2; }
142 { return static_cast<int_type>(_CHAR_TRAITS_EOF); }
145 not_eof(const int_type& __c)
146 { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
149 template<typename _CharT>
151 char_traits<_CharT>::
152 compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
154 for (std::size_t __i = 0; __i < __n; ++__i)
155 if (lt(__s1[__i], __s2[__i]))
157 else if (lt(__s2[__i], __s1[__i]))
162 template<typename _CharT>
164 char_traits<_CharT>::
165 length(const char_type* __p)
168 while (!eq(__p[__i], char_type()))
173 template<typename _CharT>
174 const typename char_traits<_CharT>::char_type*
175 char_traits<_CharT>::
176 find(const char_type* __s, std::size_t __n, const char_type& __a)
178 for (std::size_t __i = 0; __i < __n; ++__i)
179 if (eq(__s[__i], __a))
184 template<typename _CharT>
185 typename char_traits<_CharT>::char_type*
186 char_traits<_CharT>::
187 move(char_type* __s1, const char_type* __s2, std::size_t __n)
189 return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
190 __n * sizeof(char_type)));
193 template<typename _CharT>
194 typename char_traits<_CharT>::char_type*
195 char_traits<_CharT>::
196 copy(char_type* __s1, const char_type* __s2, std::size_t __n)
198 // NB: Inline std::copy so no recursive dependencies.
199 std::copy(__s2, __s2 + __n, __s1);
203 template<typename _CharT>
204 typename char_traits<_CharT>::char_type*
205 char_traits<_CharT>::
206 assign(char_type* __s, std::size_t __n, char_type __a)
208 // NB: Inline std::fill_n so no recursive dependencies.
209 std::fill_n(__s, __n, __a);
213 _GLIBCXX_END_NAMESPACE
215 _GLIBCXX_BEGIN_NAMESPACE(std)
219 * @brief Basis for explicit traits specializations.
221 * @note For any given actual character type, this definition is
222 * probably wrong. Since this is just a thin wrapper around
223 * __gnu_cxx::char_traits, it is possible to achieve a more
224 * appropriate definition by specializing __gnu_cxx::char_traits.
226 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html
227 * for advice on how to make use of this class for "unusual" character
228 * types. Also, check out include/ext/pod_char_traits.h.
230 template<class _CharT>
231 struct char_traits : public __gnu_cxx::char_traits<_CharT>
235 /// 21.1.3.1 char_traits specializations
237 struct char_traits<char>
239 typedef char char_type;
240 typedef int int_type;
241 typedef streampos pos_type;
242 typedef streamoff off_type;
243 typedef mbstate_t state_type;
246 assign(char_type& __c1, const char_type& __c2)
250 eq(const char_type& __c1, const char_type& __c2)
251 { return __c1 == __c2; }
254 lt(const char_type& __c1, const char_type& __c2)
255 { return __c1 < __c2; }
258 compare(const char_type* __s1, const char_type* __s2, size_t __n)
259 { return __builtin_memcmp(__s1, __s2, __n); }
262 length(const char_type* __s)
263 { return __builtin_strlen(__s); }
265 static const char_type*
266 find(const char_type* __s, size_t __n, const char_type& __a)
267 { return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n)); }
270 move(char_type* __s1, const char_type* __s2, size_t __n)
271 { return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n)); }
274 copy(char_type* __s1, const char_type* __s2, size_t __n)
275 { return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n)); }
278 assign(char_type* __s, size_t __n, char_type __a)
279 { return static_cast<char_type*>(__builtin_memset(__s, __a, __n)); }
282 to_char_type(const int_type& __c)
283 { return static_cast<char_type>(__c); }
285 // To keep both the byte 0xff and the eof symbol 0xffffffff
286 // from ending up as 0xffffffff.
288 to_int_type(const char_type& __c)
289 { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
292 eq_int_type(const int_type& __c1, const int_type& __c2)
293 { return __c1 == __c2; }
297 { return static_cast<int_type>(_CHAR_TRAITS_EOF); }
300 not_eof(const int_type& __c)
301 { return (__c == eof()) ? 0 : __c; }
305 #ifdef _GLIBCXX_USE_WCHAR_T
306 /// 21.1.3.2 char_traits specializations
308 struct char_traits<wchar_t>
310 typedef wchar_t char_type;
311 typedef wint_t int_type;
312 typedef streamoff off_type;
313 typedef wstreampos pos_type;
314 typedef mbstate_t state_type;
317 assign(char_type& __c1, const char_type& __c2)
321 eq(const char_type& __c1, const char_type& __c2)
322 { return __c1 == __c2; }
325 lt(const char_type& __c1, const char_type& __c2)
326 { return __c1 < __c2; }
329 compare(const char_type* __s1, const char_type* __s2, size_t __n)
330 { return wmemcmp(__s1, __s2, __n); }
333 length(const char_type* __s)
334 { return wcslen(__s); }
336 static const char_type*
337 find(const char_type* __s, size_t __n, const char_type& __a)
338 { return wmemchr(__s, __a, __n); }
341 move(char_type* __s1, const char_type* __s2, size_t __n)
342 { return wmemmove(__s1, __s2, __n); }
345 copy(char_type* __s1, const char_type* __s2, size_t __n)
346 { return wmemcpy(__s1, __s2, __n); }
349 assign(char_type* __s, size_t __n, char_type __a)
350 { return wmemset(__s, __a, __n); }
353 to_char_type(const int_type& __c)
354 { return char_type(__c); }
357 to_int_type(const char_type& __c)
358 { return int_type(__c); }
361 eq_int_type(const int_type& __c1, const int_type& __c2)
362 { return __c1 == __c2; }
366 { return static_cast<int_type>(WEOF); }
369 not_eof(const int_type& __c)
370 { return eq_int_type(__c, eof()) ? 0 : __c; }
372 #endif //_GLIBCXX_USE_WCHAR_T
374 _GLIBCXX_END_NAMESPACE
376 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) \
377 && defined(_GLIBCXX_USE_C99_STDINT_TR1))
381 _GLIBCXX_BEGIN_NAMESPACE(std)
384 struct char_traits<char16_t>
386 typedef char16_t char_type;
387 typedef uint_least16_t int_type;
388 typedef streamoff off_type;
389 typedef u16streampos pos_type;
390 typedef mbstate_t state_type;
393 assign(char_type& __c1, const char_type& __c2)
397 eq(const char_type& __c1, const char_type& __c2)
398 { return __c1 == __c2; }
401 lt(const char_type& __c1, const char_type& __c2)
402 { return __c1 < __c2; }
405 compare(const char_type* __s1, const char_type* __s2, size_t __n)
407 for (size_t __i = 0; __i < __n; ++__i)
408 if (lt(__s1[__i], __s2[__i]))
410 else if (lt(__s2[__i], __s1[__i]))
416 length(const char_type* __s)
419 while (!eq(__s[__i], char_type()))
424 static const char_type*
425 find(const char_type* __s, size_t __n, const char_type& __a)
427 for (size_t __i = 0; __i < __n; ++__i)
428 if (eq(__s[__i], __a))
434 move(char_type* __s1, const char_type* __s2, size_t __n)
436 return (static_cast<char_type*>
437 (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
441 copy(char_type* __s1, const char_type* __s2, size_t __n)
443 return (static_cast<char_type*>
444 (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
448 assign(char_type* __s, size_t __n, char_type __a)
450 for (size_t __i = 0; __i < __n; ++__i)
451 assign(__s[__i], __a);
456 to_char_type(const int_type& __c)
457 { return char_type(__c); }
460 to_int_type(const char_type& __c)
461 { return int_type(__c); }
464 eq_int_type(const int_type& __c1, const int_type& __c2)
465 { return __c1 == __c2; }
469 { return static_cast<int_type>(-1); }
472 not_eof(const int_type& __c)
473 { return eq_int_type(__c, eof()) ? 0 : __c; }
477 struct char_traits<char32_t>
479 typedef char32_t char_type;
480 typedef uint_least32_t int_type;
481 typedef streamoff off_type;
482 typedef u32streampos pos_type;
483 typedef mbstate_t state_type;
486 assign(char_type& __c1, const char_type& __c2)
490 eq(const char_type& __c1, const char_type& __c2)
491 { return __c1 == __c2; }
494 lt(const char_type& __c1, const char_type& __c2)
495 { return __c1 < __c2; }
498 compare(const char_type* __s1, const char_type* __s2, size_t __n)
500 for (size_t __i = 0; __i < __n; ++__i)
501 if (lt(__s1[__i], __s2[__i]))
503 else if (lt(__s2[__i], __s1[__i]))
509 length(const char_type* __s)
512 while (!eq(__s[__i], char_type()))
517 static const char_type*
518 find(const char_type* __s, size_t __n, const char_type& __a)
520 for (size_t __i = 0; __i < __n; ++__i)
521 if (eq(__s[__i], __a))
527 move(char_type* __s1, const char_type* __s2, size_t __n)
529 return (static_cast<char_type*>
530 (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
534 copy(char_type* __s1, const char_type* __s2, size_t __n)
536 return (static_cast<char_type*>
537 (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
541 assign(char_type* __s, size_t __n, char_type __a)
543 for (size_t __i = 0; __i < __n; ++__i)
544 assign(__s[__i], __a);
549 to_char_type(const int_type& __c)
550 { return char_type(__c); }
553 to_int_type(const char_type& __c)
554 { return int_type(__c); }
557 eq_int_type(const int_type& __c1, const int_type& __c2)
558 { return __c1 == __c2; }
562 { return static_cast<int_type>(-1); }
565 not_eof(const int_type& __c)
566 { return eq_int_type(__c, eof()) ? 0 : __c; }
569 _GLIBCXX_END_NAMESPACE
573 #undef _CHAR_TRAITS_EOF
575 #endif // _CHAR_TRAITS_H