]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/char_traits.h
include: New directory.
[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-1999, 2000 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 // 21.1.4 char_traits specializations
148 template<>
149 struct char_traits<char>
150 {
151 typedef char char_type;
152 typedef unsigned int int_type;
153 typedef streampos pos_type;
154 typedef streamoff off_type;
155 typedef mbstate_t state_type;
156
157 static void
158 assign(char_type& __c1, const char_type& __c2)
159 { __c1 = __c2; }
160
161 static bool
162 eq(const char_type& __c1, const char_type& __c2)
163 { return __c1 == __c2; }
164
165 static bool
166 lt(const char_type& __c1, const char_type& __c2)
167 { return __c1 < __c2; }
168
169 static int
170 compare(const char_type* __s1, const char_type* __s2, size_t __n)
171 { return memcmp(__s1, __s2, __n); }
172
173 static size_t
174 length(const char_type* __s)
175 { return strlen(__s); }
176
177 static const char_type*
178 find(const char_type* __s, size_t __n, const char_type& __a)
179 { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
180
181 static char_type*
182 move(char_type* __s1, const char_type* __s2, size_t __n)
183 { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
184
185 static char_type*
186 copy(char_type* __s1, const char_type* __s2, size_t __n)
187 { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
188
189 static char_type*
190 assign(char_type* __s, size_t __n, char_type __a)
191 { return static_cast<char_type*>(memset(__s, __a, __n)); }
192
193 static char_type
194 to_char_type(const int_type& __c)
195 { return static_cast<char_type>(__c); }
196
197 // To keep both the byte 0xff and the eof symbol 0xffffffff
198 // from ending up as 0xffffffff.
199 static int_type
200 to_int_type(const char_type& __c)
201 { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
202
203 static bool
204 eq_int_type(const int_type& __c1, const int_type& __c2)
205 { return __c1 == __c2; }
206
207 static state_type
208 _S_get_state(const pos_type& __pos) { return __pos.state(); }
209
210 static int_type
211 eof() { return static_cast<int_type>(EOF); }
212
213 static int_type
214 _S_eos() { return char_type(); }
215
216 static int_type
217 not_eof(const int_type& __c)
218 { return (__c == eof()) ? 0 : __c; }
219 };
220
221
222 #ifdef _GLIBCPP_USE_WCHAR_T
223 template<>
224 struct char_traits<wchar_t>
225 {
226 typedef wchar_t char_type;
227 typedef wint_t int_type;
228 typedef wstreamoff off_type;
229 typedef wstreampos pos_type;
230 typedef mbstate_t state_type;
231
232 static void
233 assign(char_type& __c1, const char_type& __c2)
234 { __c1 = __c2; }
235
236 static bool
237 eq(const char_type& __c1, const char_type& __c2)
238 { return __c1 == __c2; }
239
240 static bool
241 lt(const char_type& __c1, const char_type& __c2)
242 { return __c1 < __c2; }
243
244 static int
245 compare(const char_type* __s1, const char_type* __s2, size_t __n)
246 { return wmemcmp(__s1, __s2, __n); }
247
248 static size_t
249 length(const char_type* __s)
250 { return wcslen(__s); }
251
252 static const char_type*
253 find(const char_type* __s, size_t __n, const char_type& __a)
254 { return wmemchr(__s, __a, __n); }
255
256 static char_type*
257 move(char_type* __s1, const char_type* __s2, int_type __n)
258 { return wmemmove(__s1, __s2, __n); }
259
260 static char_type*
261 copy(char_type* __s1, const char_type* __s2, size_t __n)
262 { return wmemcpy(__s1, __s2, __n); }
263
264 static char_type*
265 assign(char_type* __s, size_t __n, char_type __a)
266 { return wmemset(__s, __a, __n); }
267
268 static char_type
269 to_char_type(const int_type& __c) { return char_type(__c); }
270
271 static int_type
272 to_int_type(const char_type& __c) { return int_type(__c); }
273
274 static bool
275 eq_int_type(const int_type& __c1, const int_type& __c2)
276 { return __c1 == __c2; }
277
278 static state_type
279 _S_get_state(const pos_type& __pos) { return __pos.state(); }
280
281 static int_type
282 eof() { return static_cast<int_type>(WEOF); }
283
284 static int_type
285 _S_eos() { return char_type(); }
286
287 static int_type
288 not_eof(const int_type& __c)
289 { return eq_int_type(__c, eof()) ? 0 : __c; }
290 };
291 #endif //_GLIBCPP_USE_WCHAR_T
292
293 template<typename _CharT, typename _Traits>
294 struct _Char_traits_match
295 {
296 _CharT _M_c;
297 _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
298
299 bool
300 operator()(_CharT const& __a) { return _Traits::eq(_M_c,__a); }
301 };
302
303 } // namespace std
304
305
306 #endif /* _CPP_BITS_CHAR_TRAITS_H */
307