]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/char_traits.h
char_traits.h (char_traits<char>::int_type): Change to `int' to match 21.1.3.1/2.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / char_traits.h
1 // Character Traits for use by standard string and iostream -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 //
31 // ISO C++ 14882: 21 Strings library
32 //
33
34 #ifndef _CPP_BITS_CHAR_TRAITS_H
35 #define _CPP_BITS_CHAR_TRAITS_H 1
36
37 #include <bits/std_cwchar.h> // For mbstate_t.
38 #include <bits/std_cstring.h> // For memmove, memset, memchr
39 #include <bits/fpos.h> // For streamoff, streamsize
40
41 namespace std {
42
43 // Same as iosfwd
44 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
45 // Can't have self-recursive types for streampos.
46 // 21.1.3.1 char_traits sets size_type to streampos
47 // 27.4.1
48 // And here, where streampos is typedefed to fpos<traits::state_type>
49 typedef fpos<mbstate_t> streampos;
50 # ifdef _GLIBCPP_USE_WCHAR_T
51 typedef fpos<mbstate_t> wstreampos;
52 # endif
53 #endif
54
55 // 21.1.2 Basis for explicit _Traits specialization
56 // NB: That for any given actual character type this definition is
57 // probably wrong.
58
59 template<class _CharT>
60 struct char_traits
61 {
62 typedef _CharT char_type;
63 // Unsigned as wint_t in unsigned.
64 typedef unsigned long int_type;
65 typedef streampos pos_type;
66 typedef streamoff off_type;
67 typedef mbstate_t state_type;
68
69 static void
70 assign(char_type& __c1, const char_type& __c2)
71 { __c1 = __c2; }
72
73 static bool
74 eq(const char_type& __c1, const char_type& __c2)
75 { return __c1 == __c2; }
76
77 static bool
78 lt(const char_type& __c1, const char_type& __c2)
79 { return __c1 < __c2; }
80
81 static int
82 compare(const char_type* __s1, const char_type* __s2, size_t __n)
83 {
84 for (size_t __i = 0; __i < __n; ++__i)
85 if (!eq(__s1[__i], __s2[__i]))
86 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
87 return 0;
88 }
89
90 static size_t
91 length(const char_type* __s)
92 {
93 const char_type* __p = __s;
94 while (*__p) ++__p;
95 return (__p - __s);
96 }
97
98 static const char_type*
99 find(const char_type* __s, size_t __n, const char_type& __a)
100 {
101 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
102 if (*__p == __a) return __p;
103 return 0;
104 }
105
106 static char_type*
107 move(char_type* __s1, const char_type* __s2, size_t __n)
108 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
109
110 static char_type*
111 copy(char_type* __s1, const char_type* __s2, size_t __n)
112 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
113
114 static char_type*
115 assign(char_type* __s, size_t __n, char_type __a)
116 {
117 for (char_type* __p = __s; __p < __s + __n; ++__p)
118 assign(*__p, __a);
119 return __s;
120 }
121
122 static char_type
123 to_char_type(const int_type& __c)
124 { return char_type(__c); }
125
126 static int_type
127 to_int_type(const char_type& __c) { return int_type(__c); }
128
129 static bool
130 eq_int_type(const int_type& __c1, const int_type& __c2)
131 { return __c1 == __c2; }
132
133 static state_type
134 _S_get_state(const pos_type& __pos) { return __pos.state(); }
135
136 static int_type
137 eof() { return static_cast<int_type>(-1); }
138
139 static int_type
140 _S_eos() { return char_type(); }
141
142 static int_type
143 not_eof(const int_type& __c)
144 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
145 };
146
147
148 // 21.1.4 char_traits specializations
149 template<>
150 struct char_traits<char>
151 {
152 typedef char char_type;
153 typedef int int_type;
154 typedef streampos pos_type;
155 typedef streamoff off_type;
156 typedef mbstate_t state_type;
157
158 static void
159 assign(char_type& __c1, const char_type& __c2)
160 { __c1 = __c2; }
161
162 static bool
163 eq(const char_type& __c1, const char_type& __c2)
164 { return __c1 == __c2; }
165
166 static bool
167 lt(const char_type& __c1, const char_type& __c2)
168 { return __c1 < __c2; }
169
170 static int
171 compare(const char_type* __s1, const char_type* __s2, size_t __n)
172 { return memcmp(__s1, __s2, __n); }
173
174 static size_t
175 length(const char_type* __s)
176 { return strlen(__s); }
177
178 static const char_type*
179 find(const char_type* __s, size_t __n, const char_type& __a)
180 { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
181
182 static char_type*
183 move(char_type* __s1, const char_type* __s2, size_t __n)
184 { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
185
186 static char_type*
187 copy(char_type* __s1, const char_type* __s2, size_t __n)
188 { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
189
190 static char_type*
191 assign(char_type* __s, size_t __n, char_type __a)
192 { return static_cast<char_type*>(memset(__s, __a, __n)); }
193
194 static char_type
195 to_char_type(const int_type& __c)
196 { return static_cast<char_type>(__c); }
197
198 // To keep both the byte 0xff and the eof symbol 0xffffffff
199 // from ending up as 0xffffffff.
200 static int_type
201 to_int_type(const char_type& __c)
202 { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
203
204 static bool
205 eq_int_type(const int_type& __c1, const int_type& __c2)
206 { return __c1 == __c2; }
207
208 static state_type
209 _S_get_state(const pos_type& __pos) { return __pos.state(); }
210
211 static int_type
212 eof() { return static_cast<int_type>(EOF); }
213
214 static int_type
215 _S_eos() { return char_type(); }
216
217 static int_type
218 not_eof(const int_type& __c)
219 { return (__c == eof()) ? 0 : __c; }
220 };
221
222
223 #ifdef _GLIBCPP_USE_WCHAR_T
224 template<>
225 struct char_traits<wchar_t>
226 {
227 typedef wchar_t char_type;
228 typedef wint_t int_type;
229 typedef wstreamoff off_type;
230 typedef wstreampos pos_type;
231 typedef mbstate_t state_type;
232
233 static void
234 assign(char_type& __c1, const char_type& __c2)
235 { __c1 = __c2; }
236
237 static bool
238 eq(const char_type& __c1, const char_type& __c2)
239 { return __c1 == __c2; }
240
241 static bool
242 lt(const char_type& __c1, const char_type& __c2)
243 { return __c1 < __c2; }
244
245 static int
246 compare(const char_type* __s1, const char_type* __s2, size_t __n)
247 { return wmemcmp(__s1, __s2, __n); }
248
249 static size_t
250 length(const char_type* __s)
251 { return wcslen(__s); }
252
253 static const char_type*
254 find(const char_type* __s, size_t __n, const char_type& __a)
255 { return wmemchr(__s, __a, __n); }
256
257 static char_type*
258 move(char_type* __s1, const char_type* __s2, int_type __n)
259 { return wmemmove(__s1, __s2, __n); }
260
261 static char_type*
262 copy(char_type* __s1, const char_type* __s2, size_t __n)
263 { return wmemcpy(__s1, __s2, __n); }
264
265 static char_type*
266 assign(char_type* __s, size_t __n, char_type __a)
267 { return wmemset(__s, __a, __n); }
268
269 static char_type
270 to_char_type(const int_type& __c) { return char_type(__c); }
271
272 static int_type
273 to_int_type(const char_type& __c) { return int_type(__c); }
274
275 static bool
276 eq_int_type(const int_type& __c1, const int_type& __c2)
277 { return __c1 == __c2; }
278
279 static state_type
280 _S_get_state(const pos_type& __pos) { return __pos.state(); }
281
282 static int_type
283 eof() { return static_cast<int_type>(WEOF); }
284
285 static int_type
286 _S_eos() { return char_type(); }
287
288 static int_type
289 not_eof(const int_type& __c)
290 { return eq_int_type(__c, eof()) ? 0 : __c; }
291 };
292 #endif //_GLIBCPP_USE_WCHAR_T
293
294 template<typename _CharT, typename _Traits>
295 struct _Char_traits_match
296 {
297 _CharT _M_c;
298 _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
299
300 bool
301 operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
302 };
303
304 } // namespace std
305
306
307 #endif /* _CPP_BITS_CHAR_TRAITS_H */
308