re PR target/33755 (Gcc 4.2.2 broken for mips linux kernel builds)
[official-gcc.git] / libstdc++-v3 / include / bits / char_traits.h
blobdaa13b7558cceb5d685553b83c8aadf6ca6c6c7b
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
5 // Free Software Foundation, Inc.
6 //
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)
11 // any later version.
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,
21 // USA.
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/algorithmfwd.h> // std::copy, std::fill_n
47 #include <bits/postypes.h> // For streampos
48 #include <cstdio> // For EOF
49 #include <cwchar> // For WEOF, wmemmove, wmemset, etc.
51 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
53 /**
54 * @brief Mapping from character type to associated types.
56 * @note This is an implementation class for the generic version
57 * of char_traits. It defines int_type, off_type, pos_type, and
58 * state_type. By default these are unsigned long, streamoff,
59 * streampos, and mbstate_t. Users who need a different set of
60 * types, but who don't need to change the definitions of any function
61 * defined in char_traits, can specialize __gnu_cxx::_Char_types
62 * while leaving __gnu_cxx::char_traits alone. */
63 template<typename _CharT>
64 struct _Char_types
66 typedef unsigned long int_type;
67 typedef std::streampos pos_type;
68 typedef std::streamoff off_type;
69 typedef std::mbstate_t state_type;
73 /**
74 * @brief Base class used to implement std::char_traits.
76 * @note For any given actual character type, this definition is
77 * probably wrong. (Most of the member functions are likely to be
78 * right, but the int_type and state_type typedefs, and the eof()
79 * member function, are likely to be wrong.) The reason this class
80 * exists is so users can specialize it. Classes in namespace std
81 * may not be specialized for fundamentl types, but classes in
82 * namespace __gnu_cxx may be.
84 * See http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#5
85 * for advice on how to make use of this class for "unusual" character
86 * types. Also, check out include/ext/pod_char_traits.h.
88 template<typename _CharT>
89 struct char_traits
91 typedef _CharT char_type;
92 typedef typename _Char_types<_CharT>::int_type int_type;
93 typedef typename _Char_types<_CharT>::pos_type pos_type;
94 typedef typename _Char_types<_CharT>::off_type off_type;
95 typedef typename _Char_types<_CharT>::state_type state_type;
97 static void
98 assign(char_type& __c1, const char_type& __c2)
99 { __c1 = __c2; }
101 static bool
102 eq(const char_type& __c1, const char_type& __c2)
103 { return __c1 == __c2; }
105 static bool
106 lt(const char_type& __c1, const char_type& __c2)
107 { return __c1 < __c2; }
109 static int
110 compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
112 static std::size_t
113 length(const char_type* __s);
115 static const char_type*
116 find(const char_type* __s, std::size_t __n, const char_type& __a);
118 static char_type*
119 move(char_type* __s1, const char_type* __s2, std::size_t __n);
121 static char_type*
122 copy(char_type* __s1, const char_type* __s2, std::size_t __n);
124 static char_type*
125 assign(char_type* __s, std::size_t __n, char_type __a);
127 static char_type
128 to_char_type(const int_type& __c)
129 { return static_cast<char_type>(__c); }
131 static int_type
132 to_int_type(const char_type& __c)
133 { return static_cast<int_type>(__c); }
135 static bool
136 eq_int_type(const int_type& __c1, const int_type& __c2)
137 { return __c1 == __c2; }
139 static int_type
140 eof()
141 { return static_cast<int_type>(EOF); }
143 static int_type
144 not_eof(const int_type& __c)
145 { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
148 template<typename _CharT>
150 char_traits<_CharT>::
151 compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
153 for (std::size_t __i = 0; __i < __n; ++__i)
154 if (lt(__s1[__i], __s2[__i]))
155 return -1;
156 else if (lt(__s2[__i], __s1[__i]))
157 return 1;
158 return 0;
161 template<typename _CharT>
162 std::size_t
163 char_traits<_CharT>::
164 length(const char_type* __p)
166 std::size_t __i = 0;
167 while (!eq(__p[__i], char_type()))
168 ++__i;
169 return __i;
172 template<typename _CharT>
173 const typename char_traits<_CharT>::char_type*
174 char_traits<_CharT>::
175 find(const char_type* __s, std::size_t __n, const char_type& __a)
177 for (std::size_t __i = 0; __i < __n; ++__i)
178 if (eq(__s[__i], __a))
179 return __s + __i;
180 return 0;
183 template<typename _CharT>
184 typename char_traits<_CharT>::char_type*
185 char_traits<_CharT>::
186 move(char_type* __s1, const char_type* __s2, std::size_t __n)
188 return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
189 __n * sizeof(char_type)));
192 template<typename _CharT>
193 typename char_traits<_CharT>::char_type*
194 char_traits<_CharT>::
195 copy(char_type* __s1, const char_type* __s2, std::size_t __n)
197 // NB: Inline std::copy so no recursive dependencies.
198 std::copy(__s2, __s2 + __n, __s1);
199 return __s1;
202 template<typename _CharT>
203 typename char_traits<_CharT>::char_type*
204 char_traits<_CharT>::
205 assign(char_type* __s, std::size_t __n, char_type __a)
207 // NB: Inline std::fill_n so no recursive dependencies.
208 std::fill_n(__s, __n, __a);
209 return __s;
212 _GLIBCXX_END_NAMESPACE
214 _GLIBCXX_BEGIN_NAMESPACE(std)
216 // 21.1
218 * @brief Basis for explicit traits specializations.
220 * @note For any given actual character type, this definition is
221 * probably wrong. Since this is just a thin wrapper around
222 * __gnu_cxx::char_traits, it is possible to achieve a more
223 * appropriate definition by specializing __gnu_cxx::char_traits.
225 * See http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#5
226 * for advice on how to make use of this class for "unusual" character
227 * types. Also, check out include/ext/pod_char_traits.h.
229 template<class _CharT>
230 struct char_traits : public __gnu_cxx::char_traits<_CharT>
231 { };
234 /// @brief 21.1.3.1 char_traits specializations
235 template<>
236 struct char_traits<char>
238 typedef char char_type;
239 typedef int int_type;
240 typedef streampos pos_type;
241 typedef streamoff off_type;
242 typedef mbstate_t state_type;
244 static void
245 assign(char_type& __c1, const char_type& __c2)
246 { __c1 = __c2; }
248 static bool
249 eq(const char_type& __c1, const char_type& __c2)
250 { return __c1 == __c2; }
252 static bool
253 lt(const char_type& __c1, const char_type& __c2)
254 { return __c1 < __c2; }
256 static int
257 compare(const char_type* __s1, const char_type* __s2, size_t __n)
258 { return __builtin_memcmp(__s1, __s2, __n); }
260 static size_t
261 length(const char_type* __s)
262 { return __builtin_strlen(__s); }
264 static const char_type*
265 find(const char_type* __s, size_t __n, const char_type& __a)
266 { return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n)); }
268 static char_type*
269 move(char_type* __s1, const char_type* __s2, size_t __n)
270 { return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n)); }
272 static char_type*
273 copy(char_type* __s1, const char_type* __s2, size_t __n)
274 { return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n)); }
276 static char_type*
277 assign(char_type* __s, size_t __n, char_type __a)
278 { return static_cast<char_type*>(__builtin_memset(__s, __a, __n)); }
280 static char_type
281 to_char_type(const int_type& __c)
282 { return static_cast<char_type>(__c); }
284 // To keep both the byte 0xff and the eof symbol 0xffffffff
285 // from ending up as 0xffffffff.
286 static int_type
287 to_int_type(const char_type& __c)
288 { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
290 static bool
291 eq_int_type(const int_type& __c1, const int_type& __c2)
292 { return __c1 == __c2; }
294 static int_type
295 eof() { return static_cast<int_type>(EOF); }
297 static int_type
298 not_eof(const int_type& __c)
299 { return (__c == eof()) ? 0 : __c; }
303 #ifdef _GLIBCXX_USE_WCHAR_T
304 /// @brief 21.1.3.2 char_traits specializations
305 template<>
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;
314 static void
315 assign(char_type& __c1, const char_type& __c2)
316 { __c1 = __c2; }
318 static bool
319 eq(const char_type& __c1, const char_type& __c2)
320 { return __c1 == __c2; }
322 static bool
323 lt(const char_type& __c1, const char_type& __c2)
324 { return __c1 < __c2; }
326 static int
327 compare(const char_type* __s1, const char_type* __s2, size_t __n)
328 { return wmemcmp(__s1, __s2, __n); }
330 static size_t
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); }
338 static char_type*
339 move(char_type* __s1, const char_type* __s2, size_t __n)
340 { return wmemmove(__s1, __s2, __n); }
342 static char_type*
343 copy(char_type* __s1, const char_type* __s2, size_t __n)
344 { return wmemcpy(__s1, __s2, __n); }
346 static char_type*
347 assign(char_type* __s, size_t __n, char_type __a)
348 { return wmemset(__s, __a, __n); }
350 static char_type
351 to_char_type(const int_type& __c) { return char_type(__c); }
353 static int_type
354 to_int_type(const char_type& __c) { return int_type(__c); }
356 static bool
357 eq_int_type(const int_type& __c1, const int_type& __c2)
358 { return __c1 == __c2; }
360 static int_type
361 eof() { return static_cast<int_type>(WEOF); }
363 static int_type
364 not_eof(const int_type& __c)
365 { return eq_int_type(__c, eof()) ? 0 : __c; }
367 #endif //_GLIBCXX_USE_WCHAR_T
369 _GLIBCXX_END_NAMESPACE
371 #endif