]>
Commit | Line | Data |
---|---|---|
725dc051 BK |
1 | // Character Traits for use by standard string and iostream -*- C++ -*- |
2 | ||
f13a69ec BK |
3 | // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 |
4 | // Free Software Foundation, Inc. | |
725dc051 BK |
5 | // |
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) | |
10 | // any later version. | |
11 | ||
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. | |
16 | ||
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, | |
20 | // USA. | |
21 | ||
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. | |
30 | ||
31 | // | |
32 | // ISO C++ 14882: 21 Strings library | |
33 | // | |
34 | ||
729e3d3f PE |
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. | |
38 | */ | |
39 | ||
725dc051 BK |
40 | #ifndef _CPP_BITS_CHAR_TRAITS_H |
41 | #define _CPP_BITS_CHAR_TRAITS_H 1 | |
42 | ||
b0a85b86 GDR |
43 | #pragma GCC system_header |
44 | ||
54c1bf78 | 45 | #include <cstring> // For memmove, memset, memchr |
d32c94be | 46 | #include <bits/fpos.h> // For streampos |
725dc051 | 47 | |
d32c94be BK |
48 | namespace std |
49 | { | |
aa2d5ba2 PE |
50 | // 21.1.2 |
51 | /** | |
52 | * @brief Basis for explicit traits specializations. | |
53 | * | |
54 | * @note For any given actual character type, this definition is | |
55 | * probably wrong. | |
56 | */ | |
725dc051 BK |
57 | template<class _CharT> |
58 | struct char_traits | |
59 | { | |
60 | typedef _CharT char_type; | |
f13a69ec | 61 | // Unsigned as wint_t is unsigned. |
725dc051 BK |
62 | typedef unsigned long int_type; |
63 | typedef streampos pos_type; | |
64 | typedef streamoff off_type; | |
65 | typedef mbstate_t state_type; | |
66 | ||
67 | static void | |
f13a69ec | 68 | assign(char_type& __c1, const char_type& __c2); |
725dc051 BK |
69 | |
70 | static bool | |
f13a69ec | 71 | eq(const char_type& __c1, const char_type& __c2); |
725dc051 BK |
72 | |
73 | static bool | |
f13a69ec | 74 | lt(const char_type& __c1, const char_type& __c2); |
725dc051 BK |
75 | |
76 | static int | |
f13a69ec | 77 | compare(const char_type* __s1, const char_type* __s2, size_t __n); |
725dc051 BK |
78 | |
79 | static size_t | |
f13a69ec | 80 | length(const char_type* __s); |
725dc051 BK |
81 | |
82 | static const char_type* | |
f13a69ec | 83 | find(const char_type* __s, size_t __n, const char_type& __a); |
725dc051 BK |
84 | |
85 | static char_type* | |
f13a69ec | 86 | move(char_type* __s1, const char_type* __s2, size_t __n); |
725dc051 BK |
87 | |
88 | static char_type* | |
f13a69ec | 89 | copy(char_type* __s1, const char_type* __s2, size_t __n); |
725dc051 BK |
90 | |
91 | static char_type* | |
f13a69ec | 92 | assign(char_type* __s, size_t __n, char_type __a); |
725dc051 BK |
93 | |
94 | static char_type | |
f13a69ec | 95 | to_char_type(const int_type& __c); |
725dc051 BK |
96 | |
97 | static int_type | |
f13a69ec | 98 | to_int_type(const char_type& __c); |
725dc051 BK |
99 | |
100 | static bool | |
f13a69ec | 101 | eq_int_type(const int_type& __c1, const int_type& __c2); |
725dc051 | 102 | |
725dc051 | 103 | static int_type |
f13a69ec | 104 | eof(); |
725dc051 | 105 | |
725dc051 | 106 | static int_type |
f13a69ec | 107 | not_eof(const int_type& __c); |
725dc051 BK |
108 | }; |
109 | ||
97644827 | 110 | |
669f7a03 | 111 | /// 21.1.4 char_traits specializations |
725dc051 BK |
112 | template<> |
113 | struct char_traits<char> | |
114 | { | |
115 | typedef char char_type; | |
b80253d7 | 116 | typedef int int_type; |
725dc051 BK |
117 | typedef streampos pos_type; |
118 | typedef streamoff off_type; | |
119 | typedef mbstate_t state_type; | |
120 | ||
121 | static void | |
122 | assign(char_type& __c1, const char_type& __c2) | |
123 | { __c1 = __c2; } | |
124 | ||
125 | static bool | |
126 | eq(const char_type& __c1, const char_type& __c2) | |
127 | { return __c1 == __c2; } | |
128 | ||
129 | static bool | |
130 | lt(const char_type& __c1, const char_type& __c2) | |
131 | { return __c1 < __c2; } | |
132 | ||
133 | static int | |
134 | compare(const char_type* __s1, const char_type* __s2, size_t __n) | |
135 | { return memcmp(__s1, __s2, __n); } | |
136 | ||
137 | static size_t | |
138 | length(const char_type* __s) | |
139 | { return strlen(__s); } | |
140 | ||
141 | static const char_type* | |
142 | find(const char_type* __s, size_t __n, const char_type& __a) | |
143 | { return static_cast<const char_type*>(memchr(__s, __a, __n)); } | |
144 | ||
145 | static char_type* | |
146 | move(char_type* __s1, const char_type* __s2, size_t __n) | |
147 | { return static_cast<char_type*>(memmove(__s1, __s2, __n)); } | |
148 | ||
149 | static char_type* | |
150 | copy(char_type* __s1, const char_type* __s2, size_t __n) | |
151 | { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); } | |
152 | ||
153 | static char_type* | |
154 | assign(char_type* __s, size_t __n, char_type __a) | |
155 | { return static_cast<char_type*>(memset(__s, __a, __n)); } | |
156 | ||
157 | static char_type | |
158 | to_char_type(const int_type& __c) | |
159 | { return static_cast<char_type>(__c); } | |
160 | ||
161 | // To keep both the byte 0xff and the eof symbol 0xffffffff | |
162 | // from ending up as 0xffffffff. | |
163 | static int_type | |
164 | to_int_type(const char_type& __c) | |
165 | { return static_cast<int_type>(static_cast<unsigned char>(__c)); } | |
166 | ||
167 | static bool | |
168 | eq_int_type(const int_type& __c1, const int_type& __c2) | |
169 | { return __c1 == __c2; } | |
170 | ||
725dc051 BK |
171 | static int_type |
172 | eof() { return static_cast<int_type>(EOF); } | |
173 | ||
725dc051 BK |
174 | static int_type |
175 | not_eof(const int_type& __c) | |
176 | { return (__c == eof()) ? 0 : __c; } | |
177 | }; | |
178 | ||
179 | ||
180 | #ifdef _GLIBCPP_USE_WCHAR_T | |
181 | template<> | |
182 | struct char_traits<wchar_t> | |
183 | { | |
184 | typedef wchar_t char_type; | |
185 | typedef wint_t int_type; | |
d32c94be | 186 | typedef streamoff off_type; |
725dc051 BK |
187 | typedef wstreampos pos_type; |
188 | typedef mbstate_t state_type; | |
189 | ||
190 | static void | |
191 | assign(char_type& __c1, const char_type& __c2) | |
192 | { __c1 = __c2; } | |
193 | ||
194 | static bool | |
195 | eq(const char_type& __c1, const char_type& __c2) | |
196 | { return __c1 == __c2; } | |
197 | ||
198 | static bool | |
199 | lt(const char_type& __c1, const char_type& __c2) | |
200 | { return __c1 < __c2; } | |
201 | ||
202 | static int | |
203 | compare(const char_type* __s1, const char_type* __s2, size_t __n) | |
204 | { return wmemcmp(__s1, __s2, __n); } | |
205 | ||
206 | static size_t | |
207 | length(const char_type* __s) | |
208 | { return wcslen(__s); } | |
209 | ||
210 | static const char_type* | |
211 | find(const char_type* __s, size_t __n, const char_type& __a) | |
212 | { return wmemchr(__s, __a, __n); } | |
213 | ||
214 | static char_type* | |
215 | move(char_type* __s1, const char_type* __s2, int_type __n) | |
216 | { return wmemmove(__s1, __s2, __n); } | |
217 | ||
218 | static char_type* | |
219 | copy(char_type* __s1, const char_type* __s2, size_t __n) | |
220 | { return wmemcpy(__s1, __s2, __n); } | |
221 | ||
222 | static char_type* | |
223 | assign(char_type* __s, size_t __n, char_type __a) | |
224 | { return wmemset(__s, __a, __n); } | |
225 | ||
226 | static char_type | |
227 | to_char_type(const int_type& __c) { return char_type(__c); } | |
228 | ||
229 | static int_type | |
230 | to_int_type(const char_type& __c) { return int_type(__c); } | |
231 | ||
232 | static bool | |
233 | eq_int_type(const int_type& __c1, const int_type& __c2) | |
234 | { return __c1 == __c2; } | |
235 | ||
725dc051 BK |
236 | static int_type |
237 | eof() { return static_cast<int_type>(WEOF); } | |
238 | ||
725dc051 BK |
239 | static int_type |
240 | not_eof(const int_type& __c) | |
241 | { return eq_int_type(__c, eof()) ? 0 : __c; } | |
242 | }; | |
243 | #endif //_GLIBCPP_USE_WCHAR_T | |
244 | ||
245 | template<typename _CharT, typename _Traits> | |
246 | struct _Char_traits_match | |
247 | { | |
248 | _CharT _M_c; | |
249 | _Char_traits_match(_CharT const& __c) : _M_c(__c) { } | |
250 | ||
251 | bool | |
97644827 | 252 | operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); } |
725dc051 | 253 | }; |
725dc051 BK |
254 | } // namespace std |
255 | ||
d32c94be | 256 | #endif |