1 // Character Traits for use by standard string and iostream -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 21 Strings library
35 /** @file char_traits.h
36 * This is an internal header file, included by other library headers.
37 * You should not attempt to use it directly.
40 #ifndef _CHAR_TRAITS_H
41 #define _CHAR_TRAITS_H 1
43 #pragma GCC system_header
45 #include <cstring> // For memmove, memset, memchr
46 #include <bits/stl_algobase.h>// For copy, lexicographical_compare, fill_n
47 #include <bits/postypes.h> // For streampos
52 * @brief Mapping from character type to associated types.
55 * @note This is an implementation class for the generic version
56 * of char_traits. It defines int_type, off_type, pos_type, and
57 * state_type. By default these are unsigned long, streamoff,
58 * streampos, and mbstate_t. Users who need a different set of
59 * types, but who don't need to change the definitions of any function
60 * defined in char_traits, can specialize __gnu_cxx::_Char_types
61 * while leaving __gnu_cxx::char_traits alone. */
62 template <class _CharT
>
65 typedef unsigned long int_type
;
66 typedef std::streampos pos_type
;
67 typedef std::streamoff off_type
;
68 typedef std::mbstate_t state_type
;
73 * @brief Base class used to implement std::char_traits.
75 * @note For any given actual character type, this definition is
76 * probably wrong. (Most of the member functions are likely to be
77 * right, but the int_type and state_type typedefs, and the eof()
78 * member function, are likely to be wrong.) The reason this class
79 * exists is so users can specialize it. Classes in namespace std
80 * may not be specialized for fundamentl types, but classes in
81 * namespace __gnu_cxx may be.
83 * See http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#5
84 * for advice on how to make use of this class for "unusual" character
85 * types. Also, check out include/ext/pod_char_traits.h. */
86 template<typename _CharT
>
89 typedef _CharT char_type
;
90 typedef typename _Char_types
<_CharT
>::int_type int_type
;
91 typedef typename _Char_types
<_CharT
>::pos_type pos_type
;
92 typedef typename _Char_types
<_CharT
>::off_type off_type
;
93 typedef typename _Char_types
<_CharT
>::state_type state_type
;
96 assign(char_type
& __c1
, const char_type
& __c2
)
100 eq(const char_type
& __c1
, const char_type
& __c2
)
101 { return __c1
== __c2
; }
104 lt(const char_type
& __c1
, const char_type
& __c2
)
105 { return __c1
< __c2
; }
108 compare(const char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
111 length(const char_type
* __s
);
113 static const char_type
*
114 find(const char_type
* __s
, std::size_t __n
, const char_type
& __a
);
117 move(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
120 copy(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
);
123 assign(char_type
* __s
, std::size_t __n
, char_type __a
);
126 to_char_type(const int_type
& __c
)
127 { return static_cast<char_type
>(__c
); }
130 to_int_type(const char_type
& __c
)
131 { return static_cast<int_type
>(__c
); }
134 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
135 { return __c1
== __c2
; }
139 { return static_cast<int_type
>(EOF
); }
142 not_eof(const int_type
& __c
)
143 { return !eq_int_type(__c
, eof()) ? __c
: to_int_type(char_type()); }
146 template<typename _CharT
>
148 char_traits
<_CharT
>::
149 compare(const char_type
* __s1
, const char_type
* __s2
, std::size_t __n
)
151 for (size_t __i
= 0; __i
< __n
; ++__i
)
152 if (lt(__s1
[__i
], __s2
[__i
]))
154 else if (lt(__s2
[__i
], __s1
[__i
]))
159 template<typename _CharT
>
161 char_traits
<_CharT
>::
162 length(const char_type
* __p
)
165 while (!eq(__p
[__i
], char_type()))
170 template<typename _CharT
>
171 const typename char_traits
<_CharT
>::char_type
*
172 char_traits
<_CharT
>::
173 find(const char_type
* __s
, std::size_t __n
, const char_type
& __a
)
175 for (std::size_t __i
= 0; __i
< __n
; ++__i
)
176 if (eq(__s
[__i
], __a
))
181 template<typename _CharT
>
182 typename char_traits
<_CharT
>::char_type
*
183 char_traits
<_CharT
>::
184 move(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
)
186 return static_cast<_CharT
*>(std::memmove(__s1
, __s2
,
187 __n
* sizeof(char_type
)));
190 template<typename _CharT
>
191 typename char_traits
<_CharT
>::char_type
*
192 char_traits
<_CharT
>::
193 copy(char_type
* __s1
, const char_type
* __s2
, std::size_t __n
)
195 std::copy(__s2
, __s2
+ __n
, __s1
);
199 template<typename _CharT
>
200 typename char_traits
<_CharT
>::char_type
*
201 char_traits
<_CharT
>::
202 assign(char_type
* __s
, std::size_t __n
, char_type __a
)
204 std::fill_n(__s
, __n
, __a
);
213 * @brief Basis for explicit traits specializations.
215 * @note For any given actual character type, this definition is
216 * probably wrong. Since this is just a thin wrapper around
217 * __gnu_cxx::char_traits, it is possible to achieve a more
218 * appropriate definition by specializing __gnu_cxx::char_traits.
220 * See http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#5
221 * for advice on how to make use of this class for "unusual" character
222 * types. Also, check out include/ext/pod_char_traits.h.
224 template<class _CharT
>
226 : public __gnu_cxx::char_traits
<_CharT
>
230 /// 21.1.3.1 char_traits specializations
232 struct char_traits
<char>
234 typedef char char_type
;
235 typedef int int_type
;
236 typedef streampos pos_type
;
237 typedef streamoff off_type
;
238 typedef mbstate_t state_type
;
241 assign(char_type
& __c1
, const char_type
& __c2
)
245 eq(const char_type
& __c1
, const char_type
& __c2
)
246 { return __c1
== __c2
; }
249 lt(const char_type
& __c1
, const char_type
& __c2
)
250 { return __c1
< __c2
; }
253 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
254 { return memcmp(__s1
, __s2
, __n
); }
257 length(const char_type
* __s
)
258 { return strlen(__s
); }
260 static const char_type
*
261 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
262 { return static_cast<const char_type
*>(memchr(__s
, __a
, __n
)); }
265 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
266 { return static_cast<char_type
*>(memmove(__s1
, __s2
, __n
)); }
269 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
270 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
273 assign(char_type
* __s
, size_t __n
, char_type __a
)
274 { return static_cast<char_type
*>(memset(__s
, __a
, __n
)); }
277 to_char_type(const int_type
& __c
)
278 { return static_cast<char_type
>(__c
); }
280 // To keep both the byte 0xff and the eof symbol 0xffffffff
281 // from ending up as 0xffffffff.
283 to_int_type(const char_type
& __c
)
284 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
287 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
288 { return __c1
== __c2
; }
291 eof() { return static_cast<int_type
>(EOF
); }
294 not_eof(const int_type
& __c
)
295 { return (__c
== eof()) ? 0 : __c
; }
299 #if defined (_GLIBCXX_USE_WCHAR_T) || defined (_GLIBCXX_USE_WSTRING)
300 /// 21.1.3.2 char_traits specializations
302 struct char_traits
<wchar_t>
304 typedef wchar_t char_type
;
305 typedef wint_t int_type
;
306 typedef streamoff off_type
;
307 #if defined (_GLIBCXX_USE_WCHAR_T)
308 typedef wstreampos pos_type
;
309 typedef mbstate_t state_type
;
313 assign(char_type
& __c1
, const char_type
& __c2
)
317 eq(const char_type
& __c1
, const char_type
& __c2
)
318 { return __c1
== __c2
; }
321 lt(const char_type
& __c1
, const char_type
& __c2
)
322 { return __c1
< __c2
; }
325 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
326 { return wmemcmp(__s1
, __s2
, __n
); }
329 length(const char_type
* __s
)
330 { return wcslen(__s
); }
332 static const char_type
*
333 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
334 { return wmemchr(__s
, __a
, __n
); }
337 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
338 { return wmemmove(__s1
, __s2
, __n
); }
341 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
342 { return wmemcpy(__s1
, __s2
, __n
); }
345 assign(char_type
* __s
, size_t __n
, char_type __a
)
346 { return wmemset(__s
, __a
, __n
); }
349 to_char_type(const int_type
& __c
) { return char_type(__c
); }
352 to_int_type(const char_type
& __c
) { return int_type(__c
); }
355 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
356 { return __c1
== __c2
; }
359 eof() { return static_cast<int_type
>(WEOF
); }
362 not_eof(const int_type
& __c
)
363 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
365 #endif //_GLIBCXX_USE_WCHAR_T
367 template<typename _CharT
, typename _Traits
>
368 struct _Char_traits_match
371 _Char_traits_match(_CharT
const& __c
) : _M_c(__c
) { }
374 operator()(_CharT
const& __a
) { return _Traits::eq(_M_c
, __a
); }