]>
Commit | Line | Data |
---|---|---|
725dc051 BK |
1 | // Character Traits for use by standard string and iostream -*- C++ -*- |
2 | ||
39b8cd70 PC |
3 | // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
4 | // 2006, 2007 | |
f13a69ec | 5 | // Free Software Foundation, Inc. |
725dc051 BK |
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 | |
83f51799 | 20 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
725dc051 BK |
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 | ||
729e3d3f PE |
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 | ||
143c27b0 BK |
37 | // |
38 | // ISO C++ 14882: 21 Strings library | |
39 | // | |
40 | ||
3d7c150e BK |
41 | #ifndef _CHAR_TRAITS_H |
42 | #define _CHAR_TRAITS_H 1 | |
725dc051 | 43 | |
b0a85b86 GDR |
44 | #pragma GCC system_header |
45 | ||
39b8cd70 PC |
46 | #include <bits/stl_algobase.h> // For copy, fill_n |
47 | #include <bits/postypes.h> // For streampos | |
48 | #include <cstdio> // For EOF | |
49 | #include <cwchar> // For WEOF, wmemmove, wmemset, etc. | |
725dc051 | 50 | |
3cbc7af0 BK |
51 | _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) |
52 | ||
aa2d5ba2 | 53 | /** |
d5ff4e3f | 54 | * @brief Mapping from character type to associated types. |
ed6814f7 | 55 | * |
d5ff4e3f MA |
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, | |
ed6814f7 | 59 | * streampos, and mbstate_t. Users who need a different set of |
d5ff4e3f MA |
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. */ | |
65be6ddd | 63 | template<typename _CharT> |
d5ff4e3f MA |
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 fundamentl types, but classes in | |
82 | * namespace __gnu_cxx may be. | |
51122a42 PE |
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 | |
6309eefc BK |
86 | * types. Also, check out include/ext/pod_char_traits.h. |
87 | */ | |
d5ff4e3f | 88 | template<typename _CharT> |
725dc051 BK |
89 | struct char_traits |
90 | { | |
d5ff4e3f MA |
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; | |
ed6814f7 BI |
96 | |
97 | static void | |
d5ff4e3f MA |
98 | assign(char_type& __c1, const char_type& __c2) |
99 | { __c1 = __c2; } | |
725dc051 | 100 | |
ed6814f7 | 101 | static bool |
d5ff4e3f MA |
102 | eq(const char_type& __c1, const char_type& __c2) |
103 | { return __c1 == __c2; } | |
725dc051 | 104 | |
ed6814f7 | 105 | static bool |
d5ff4e3f MA |
106 | lt(const char_type& __c1, const char_type& __c2) |
107 | { return __c1 < __c2; } | |
725dc051 | 108 | |
ed6814f7 | 109 | static int |
d5ff4e3f | 110 | compare(const char_type* __s1, const char_type* __s2, std::size_t __n); |
725dc051 | 111 | |
d5ff4e3f | 112 | static std::size_t |
f13a69ec | 113 | length(const char_type* __s); |
725dc051 | 114 | |
ed6814f7 | 115 | static const char_type* |
d5ff4e3f | 116 | find(const char_type* __s, std::size_t __n, const char_type& __a); |
725dc051 | 117 | |
ed6814f7 | 118 | static char_type* |
d5ff4e3f | 119 | move(char_type* __s1, const char_type* __s2, std::size_t __n); |
725dc051 | 120 | |
ed6814f7 | 121 | static char_type* |
d5ff4e3f | 122 | copy(char_type* __s1, const char_type* __s2, std::size_t __n); |
725dc051 | 123 | |
ed6814f7 | 124 | static char_type* |
d5ff4e3f | 125 | assign(char_type* __s, std::size_t __n, char_type __a); |
725dc051 | 126 | |
ed6814f7 | 127 | static char_type |
d5ff4e3f MA |
128 | to_char_type(const int_type& __c) |
129 | { return static_cast<char_type>(__c); } | |
725dc051 | 130 | |
ed6814f7 | 131 | static int_type |
d5ff4e3f MA |
132 | to_int_type(const char_type& __c) |
133 | { return static_cast<int_type>(__c); } | |
725dc051 | 134 | |
ed6814f7 | 135 | static bool |
d5ff4e3f MA |
136 | eq_int_type(const int_type& __c1, const int_type& __c2) |
137 | { return __c1 == __c2; } | |
725dc051 | 138 | |
ed6814f7 | 139 | static int_type |
d5ff4e3f MA |
140 | eof() |
141 | { return static_cast<int_type>(EOF); } | |
725dc051 | 142 | |
ed6814f7 | 143 | static int_type |
d5ff4e3f | 144 | not_eof(const int_type& __c) |
5a9ed693 | 145 | { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); } |
725dc051 BK |
146 | }; |
147 | ||
d5ff4e3f MA |
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 | { | |
05a2763e | 153 | for (std::size_t __i = 0; __i < __n; ++__i) |
d5ff4e3f MA |
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> | |
ed6814f7 | 173 | const typename char_traits<_CharT>::char_type* |
d5ff4e3f MA |
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 | { | |
538075fe PC |
188 | return static_cast<_CharT*>(__builtin_memmove(__s1, __s2, |
189 | __n * sizeof(char_type))); | |
d5ff4e3f MA |
190 | } |
191 | ||
192 | template<typename _CharT> | |
ed6814f7 | 193 | typename char_traits<_CharT>::char_type* |
d5ff4e3f MA |
194 | char_traits<_CharT>:: |
195 | copy(char_type* __s1, const char_type* __s2, std::size_t __n) | |
196 | { | |
197 | std::copy(__s2, __s2 + __n, __s1); | |
198 | return __s1; | |
199 | } | |
200 | ||
201 | template<typename _CharT> | |
ed6814f7 | 202 | typename char_traits<_CharT>::char_type* |
d5ff4e3f MA |
203 | char_traits<_CharT>:: |
204 | assign(char_type* __s, std::size_t __n, char_type __a) | |
205 | { | |
206 | std::fill_n(__s, __n, __a); | |
207 | return __s; | |
208 | } | |
d5ff4e3f | 209 | |
3cbc7af0 BK |
210 | _GLIBCXX_END_NAMESPACE |
211 | ||
212 | _GLIBCXX_BEGIN_NAMESPACE(std) | |
213 | ||
d5ff4e3f MA |
214 | // 21.1 |
215 | /** | |
216 | * @brief Basis for explicit traits specializations. | |
217 | * | |
218 | * @note For any given actual character type, this definition is | |
219 | * probably wrong. Since this is just a thin wrapper around | |
220 | * __gnu_cxx::char_traits, it is possible to achieve a more | |
221 | * appropriate definition by specializing __gnu_cxx::char_traits. | |
222 | * | |
223 | * See http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#5 | |
224 | * for advice on how to make use of this class for "unusual" character | |
225 | * types. Also, check out include/ext/pod_char_traits.h. | |
226 | */ | |
227 | template<class _CharT> | |
6309eefc | 228 | struct char_traits : public __gnu_cxx::char_traits<_CharT> |
d5ff4e3f MA |
229 | { }; |
230 | ||
97644827 | 231 | |
6309eefc | 232 | /// @brief 21.1.3.1 char_traits specializations |
725dc051 BK |
233 | template<> |
234 | struct char_traits<char> | |
235 | { | |
d5ff4e3f MA |
236 | typedef char char_type; |
237 | typedef int int_type; | |
238 | typedef streampos pos_type; | |
239 | typedef streamoff off_type; | |
240 | typedef mbstate_t state_type; | |
725dc051 | 241 | |
ed6814f7 | 242 | static void |
725dc051 BK |
243 | assign(char_type& __c1, const char_type& __c2) |
244 | { __c1 = __c2; } | |
245 | ||
ed6814f7 | 246 | static bool |
725dc051 | 247 | eq(const char_type& __c1, const char_type& __c2) |
462ec415 | 248 | { return __c1 == __c2; } |
725dc051 | 249 | |
ed6814f7 | 250 | static bool |
725dc051 BK |
251 | lt(const char_type& __c1, const char_type& __c2) |
252 | { return __c1 < __c2; } | |
253 | ||
ed6814f7 | 254 | static int |
725dc051 | 255 | compare(const char_type* __s1, const char_type* __s2, size_t __n) |
538075fe | 256 | { return __builtin_memcmp(__s1, __s2, __n); } |
725dc051 BK |
257 | |
258 | static size_t | |
259 | length(const char_type* __s) | |
538075fe | 260 | { return __builtin_strlen(__s); } |
725dc051 | 261 | |
ed6814f7 | 262 | static const char_type* |
725dc051 | 263 | find(const char_type* __s, size_t __n, const char_type& __a) |
538075fe | 264 | { return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n)); } |
725dc051 | 265 | |
ed6814f7 | 266 | static char_type* |
725dc051 | 267 | move(char_type* __s1, const char_type* __s2, size_t __n) |
538075fe | 268 | { return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n)); } |
725dc051 | 269 | |
ed6814f7 | 270 | static char_type* |
725dc051 | 271 | copy(char_type* __s1, const char_type* __s2, size_t __n) |
538075fe | 272 | { return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n)); } |
725dc051 | 273 | |
ed6814f7 | 274 | static char_type* |
725dc051 | 275 | assign(char_type* __s, size_t __n, char_type __a) |
538075fe | 276 | { return static_cast<char_type*>(__builtin_memset(__s, __a, __n)); } |
725dc051 | 277 | |
ed6814f7 | 278 | static char_type |
725dc051 BK |
279 | to_char_type(const int_type& __c) |
280 | { return static_cast<char_type>(__c); } | |
281 | ||
282 | // To keep both the byte 0xff and the eof symbol 0xffffffff | |
283 | // from ending up as 0xffffffff. | |
ed6814f7 | 284 | static int_type |
725dc051 BK |
285 | to_int_type(const char_type& __c) |
286 | { return static_cast<int_type>(static_cast<unsigned char>(__c)); } | |
287 | ||
ed6814f7 | 288 | static bool |
725dc051 BK |
289 | eq_int_type(const int_type& __c1, const int_type& __c2) |
290 | { return __c1 == __c2; } | |
291 | ||
ed6814f7 | 292 | static int_type |
725dc051 BK |
293 | eof() { return static_cast<int_type>(EOF); } |
294 | ||
ed6814f7 | 295 | static int_type |
725dc051 BK |
296 | not_eof(const int_type& __c) |
297 | { return (__c == eof()) ? 0 : __c; } | |
298 | }; | |
299 | ||
300 | ||
3d7c150e | 301 | #ifdef _GLIBCXX_USE_WCHAR_T |
6309eefc | 302 | /// @brief 21.1.3.2 char_traits specializations |
725dc051 BK |
303 | template<> |
304 | struct char_traits<wchar_t> | |
305 | { | |
d5ff4e3f MA |
306 | typedef wchar_t char_type; |
307 | typedef wint_t int_type; | |
308 | typedef streamoff off_type; | |
309 | typedef wstreampos pos_type; | |
310 | typedef mbstate_t state_type; | |
ed6814f7 BI |
311 | |
312 | static void | |
725dc051 BK |
313 | assign(char_type& __c1, const char_type& __c2) |
314 | { __c1 = __c2; } | |
315 | ||
ed6814f7 | 316 | static bool |
462ec415 JJ |
317 | eq(const char_type& __c1, const char_type& __c2) |
318 | { return __c1 == __c2; } | |
725dc051 | 319 | |
ed6814f7 | 320 | static bool |
725dc051 BK |
321 | lt(const char_type& __c1, const char_type& __c2) |
322 | { return __c1 < __c2; } | |
323 | ||
ed6814f7 | 324 | static int |
725dc051 BK |
325 | compare(const char_type* __s1, const char_type* __s2, size_t __n) |
326 | { return wmemcmp(__s1, __s2, __n); } | |
327 | ||
328 | static size_t | |
329 | length(const char_type* __s) | |
330 | { return wcslen(__s); } | |
331 | ||
ed6814f7 | 332 | static const char_type* |
725dc051 BK |
333 | find(const char_type* __s, size_t __n, const char_type& __a) |
334 | { return wmemchr(__s, __a, __n); } | |
335 | ||
ed6814f7 | 336 | static char_type* |
73a530bd | 337 | move(char_type* __s1, const char_type* __s2, size_t __n) |
725dc051 BK |
338 | { return wmemmove(__s1, __s2, __n); } |
339 | ||
ed6814f7 | 340 | static char_type* |
725dc051 BK |
341 | copy(char_type* __s1, const char_type* __s2, size_t __n) |
342 | { return wmemcpy(__s1, __s2, __n); } | |
343 | ||
ed6814f7 | 344 | static char_type* |
725dc051 BK |
345 | assign(char_type* __s, size_t __n, char_type __a) |
346 | { return wmemset(__s, __a, __n); } | |
347 | ||
ed6814f7 | 348 | static char_type |
725dc051 BK |
349 | to_char_type(const int_type& __c) { return char_type(__c); } |
350 | ||
ed6814f7 | 351 | static int_type |
725dc051 BK |
352 | to_int_type(const char_type& __c) { return int_type(__c); } |
353 | ||
ed6814f7 | 354 | static bool |
725dc051 BK |
355 | eq_int_type(const int_type& __c1, const int_type& __c2) |
356 | { return __c1 == __c2; } | |
357 | ||
ed6814f7 | 358 | static int_type |
725dc051 BK |
359 | eof() { return static_cast<int_type>(WEOF); } |
360 | ||
ed6814f7 | 361 | static int_type |
725dc051 BK |
362 | not_eof(const int_type& __c) |
363 | { return eq_int_type(__c, eof()) ? 0 : __c; } | |
364 | }; | |
3d7c150e | 365 | #endif //_GLIBCXX_USE_WCHAR_T |
725dc051 | 366 | |
3cbc7af0 | 367 | _GLIBCXX_END_NAMESPACE |
725dc051 | 368 | |
d32c94be | 369 | #endif |