Branch data Line data Source code
1 : : // Character Traits for use by standard string and iostream -*- C++ -*-
2 : :
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.
12 : :
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.
17 : :
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.
22 : :
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.
31 : :
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.
35 : : */
36 : :
37 : : //
38 : : // ISO C++ 14882: 21 Strings library
39 : : //
40 : :
41 : : #ifndef _CHAR_TRAITS_H
42 : : #define _CHAR_TRAITS_H 1
43 : :
44 : : #pragma GCC system_header
45 : :
46 : : #include <bits/stl_algobase.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.
50 : :
51 : : _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
52 : :
53 : : /**
54 : : * @brief Mapping from character type to associated types.
55 : : *
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
65 : : {
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;
70 : : };
71 : :
72 : :
73 : : /**
74 : : * @brief Base class used to implement std::char_traits.
75 : : *
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 fundamental types, but classes in
82 : : * namespace __gnu_cxx may be.
83 : : *
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.
87 : : */
88 : : template<typename _CharT>
89 : : struct char_traits
90 : : {
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;
96 : :
97 : : static void
98 : : assign(char_type& __c1, const char_type& __c2)
99 : : { __c1 = __c2; }
100 : :
101 : : static bool
102 : : eq(const char_type& __c1, const char_type& __c2)
103 : : { return __c1 == __c2; }
104 : :
105 : : static bool
106 : : lt(const char_type& __c1, const char_type& __c2)
107 : : { return __c1 < __c2; }
108 : :
109 : : static int
110 : : compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
111 : :
112 : : static std::size_t
113 : : length(const char_type* __s);
114 : :
115 : : static const char_type*
116 : : find(const char_type* __s, std::size_t __n, const char_type& __a);
117 : :
118 : : static char_type*
119 : : move(char_type* __s1, const char_type* __s2, std::size_t __n);
120 : :
121 : : static char_type*
122 : : copy(char_type* __s1, const char_type* __s2, std::size_t __n);
123 : :
124 : : static char_type*
125 : : assign(char_type* __s, std::size_t __n, char_type __a);
126 : :
127 : : static char_type
128 : : to_char_type(const int_type& __c)
129 : : { return static_cast<char_type>(__c); }
130 : :
131 : : static int_type
132 : : to_int_type(const char_type& __c)
133 : : { return static_cast<int_type>(__c); }
134 : :
135 : : static bool
136 : : eq_int_type(const int_type& __c1, const int_type& __c2)
137 : : { return __c1 == __c2; }
138 : :
139 : : static int_type
140 : : eof()
141 : : { return static_cast<int_type>(EOF); }
142 : :
143 : : static int_type
144 : : not_eof(const int_type& __c)
145 : : { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
146 : : };
147 : :
148 : : template<typename _CharT>
149 : : int
150 : : char_traits<_CharT>::
151 : : compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
152 : : {
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;
159 : : }
160 : :
161 : : template<typename _CharT>
162 : : std::size_t
163 : : char_traits<_CharT>::
164 : : length(const char_type* __p)
165 : : {
166 : : std::size_t __i = 0;
167 : : while (!eq(__p[__i], char_type()))
168 : : ++__i;
169 : : return __i;
170 : : }
171 : :
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)
176 : : {
177 : : for (std::size_t __i = 0; __i < __n; ++__i)
178 : : if (eq(__s[__i], __a))
179 : : return __s + __i;
180 : : return 0;
181 : : }
182 : :
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)
187 : : {
188 : : return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
189 : : __n * sizeof(char_type)));
190 : : }
191 : :
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)
196 : : {
197 : : // NB: Inline std::copy so no recursive dependencies.
198 : : std::copy(__s2, __s2 + __n, __s1);
199 : : return __s1;
200 : : }
201 : :
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)
206 : : {
207 : : // NB: Inline std::fill_n so no recursive dependencies.
208 : : std::fill_n(__s, __n, __a);
209 : : return __s;
210 : : }
211 : :
212 : : _GLIBCXX_END_NAMESPACE
213 : :
214 : : _GLIBCXX_BEGIN_NAMESPACE(std)
215 : :
216 : : // 21.1
217 : : /**
218 : : * @brief Basis for explicit traits specializations.
219 : : *
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.
224 : : *
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.
228 : : */
229 : : template<class _CharT>
230 : : struct char_traits : public __gnu_cxx::char_traits<_CharT>
231 : : { };
232 : :
233 : :
234 : : /// 21.1.3.1 char_traits specializations
235 : : template<>
236 : : struct char_traits<char>
237 : : {
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;
243 : :
244 : : static void
245 : : assign(char_type& __c1, const char_type& __c2)
246 : 139784 : { __c1 = __c2; }
247 : :
248 : : static bool
249 : : eq(const char_type& __c1, const char_type& __c2)
250 : 1616 : { return __c1 == __c2; }
251 : :
252 : : static bool
253 : : lt(const char_type& __c1, const char_type& __c2)
254 : : { return __c1 < __c2; }
255 : :
256 : : static int
257 : : compare(const char_type* __s1, const char_type* __s2, size_t __n)
258 : 416457 : { return __builtin_memcmp(__s1, __s2, __n); }
259 : :
260 : : static size_t
261 : : length(const char_type* __s)
262 : 426207 : { return __builtin_strlen(__s); }
263 : :
264 : : static const char_type*
265 : : find(const char_type* __s, size_t __n, const char_type& __a)
266 : 3806 : { return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n)); }
267 : :
268 : : static char_type*
269 : : move(char_type* __s1, const char_type* __s2, size_t __n)
270 : 0 : { return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n)); }
271 : :
272 : : static char_type*
273 : : copy(char_type* __s1, const char_type* __s2, size_t __n)
274 : 373844 : { return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n)); }
275 : :
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)); }
279 : :
280 : : static char_type
281 : : to_char_type(const int_type& __c)
282 : 0 : { return static_cast<char_type>(__c); }
283 : :
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 : 0 : { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
289 : :
290 : : static bool
291 : : eq_int_type(const int_type& __c1, const int_type& __c2)
292 : 0 : { return __c1 == __c2; }
293 : :
294 : : static int_type
295 : 0 : eof() { return static_cast<int_type>(EOF); }
296 : :
297 : : static int_type
298 : : not_eof(const int_type& __c)
299 : : { return (__c == eof()) ? 0 : __c; }
300 : : };
301 : :
302 : :
303 : : #ifdef _GLIBCXX_USE_WCHAR_T
304 : : /// 21.1.3.2 char_traits specializations
305 : : template<>
306 : : struct char_traits<wchar_t>
307 : : {
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;
313 : :
314 : : static void
315 : : assign(char_type& __c1, const char_type& __c2)
316 : : { __c1 = __c2; }
317 : :
318 : : static bool
319 : : eq(const char_type& __c1, const char_type& __c2)
320 : : { return __c1 == __c2; }
321 : :
322 : : static bool
323 : : lt(const char_type& __c1, const char_type& __c2)
324 : : { return __c1 < __c2; }
325 : :
326 : : static int
327 : : compare(const char_type* __s1, const char_type* __s2, size_t __n)
328 : : { return wmemcmp(__s1, __s2, __n); }
329 : :
330 : : static size_t
331 : : length(const char_type* __s)
332 : : { return wcslen(__s); }
333 : :
334 : : static const char_type*
335 : : find(const char_type* __s, size_t __n, const char_type& __a)
336 : : { return wmemchr(__s, __a, __n); }
337 : :
338 : : static char_type*
339 : : move(char_type* __s1, const char_type* __s2, size_t __n)
340 : : { return wmemmove(__s1, __s2, __n); }
341 : :
342 : : static char_type*
343 : : copy(char_type* __s1, const char_type* __s2, size_t __n)
344 : : { return wmemcpy(__s1, __s2, __n); }
345 : :
346 : : static char_type*
347 : : assign(char_type* __s, size_t __n, char_type __a)
348 : : { return wmemset(__s, __a, __n); }
349 : :
350 : : static char_type
351 : : to_char_type(const int_type& __c) { return char_type(__c); }
352 : :
353 : : static int_type
354 : : to_int_type(const char_type& __c) { return int_type(__c); }
355 : :
356 : : static bool
357 : : eq_int_type(const int_type& __c1, const int_type& __c2)
358 : : { return __c1 == __c2; }
359 : :
360 : : static int_type
361 : : eof() { return static_cast<int_type>(WEOF); }
362 : :
363 : : static int_type
364 : : not_eof(const int_type& __c)
365 : : { return eq_int_type(__c, eof()) ? 0 : __c; }
366 : : };
367 : : #endif //_GLIBCXX_USE_WCHAR_T
368 : :
369 : : _GLIBCXX_END_NAMESPACE
370 : :
371 : : #endif
|