]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/char_traits.h
Add #pragma system_header to header files.
[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 #pragma GCC system_header
38
39 #include <bits/std_cwchar.h> // For mbstate_t.
40 #include <bits/std_cstring.h> // For memmove, memset, memchr
41 #include <bits/fpos.h> // For streamoff, streamsize
42
43 namespace std {
44
45 // Same as iosfwd
46 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
47 // Can't have self-recursive types for streampos.
48 // 21.1.3.1 char_traits sets size_type to streampos
49 // 27.4.1
50 // And here, where streampos is typedefed to fpos<traits::state_type>
51 typedef fpos<mbstate_t> streampos;
52 # ifdef _GLIBCPP_USE_WCHAR_T
53 typedef fpos<mbstate_t> wstreampos;
54 # endif
55 #endif
56
57 // 21.1.2 Basis for explicit _Traits specialization
58 // NB: That for any given actual character type this definition is
59 // probably wrong.
60
61 template<class _CharT>
62 struct char_traits
63 {
64 typedef _CharT char_type;
65 // Unsigned as wint_t in unsigned.
66 typedef unsigned long int_type;
67 typedef streampos pos_type;
68 typedef streamoff off_type;
69 typedef mbstate_t state_type;
70
71 static void
72 assign(char_type& __c1, const char_type& __c2)
73 { __c1 = __c2; }
74
75 static bool
76 eq(const char_type& __c1, const char_type& __c2)
77 { return __c1 == __c2; }
78
79 static bool
80 lt(const char_type& __c1, const char_type& __c2)
81 { return __c1 < __c2; }
82
83 static int
84 compare(const char_type* __s1, const char_type* __s2, size_t __n)
85 {
86 for (size_t __i = 0; __i < __n; ++__i)
87 if (!eq(__s1[__i], __s2[__i]))
88 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
89 return 0;
90 }
91
92 static size_t
93 length(const char_type* __s)
94 {
95 const char_type* __p = __s;
96 while (*__p) ++__p;
97 return (__p - __s);
98 }
99
100 static const char_type*
101 find(const char_type* __s, size_t __n, const char_type& __a)
102 {
103 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
104 if (*__p == __a) return __p;
105 return 0;
106 }
107
108 static char_type*
109 move(char_type* __s1, const char_type* __s2, size_t __n)
110 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
111
112 static char_type*
113 copy(char_type* __s1, const char_type* __s2, size_t __n)
114 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
115
116 static char_type*
117 assign(char_type* __s, size_t __n, char_type __a)
118 {
119 for (char_type* __p = __s; __p < __s + __n; ++__p)
120 assign(*__p, __a);
121 return __s;
122 }
123
124 static char_type
125 to_char_type(const int_type& __c)
126 { return char_type(__c); }
127
128 static int_type
129 to_int_type(const char_type& __c) { return int_type(__c); }
130
131 static bool
132 eq_int_type(const int_type& __c1, const int_type& __c2)
133 { return __c1 == __c2; }
134
135 static int_type
136 eof() { return static_cast<int_type>(-1); }
137
138 static int_type
139 not_eof(const int_type& __c)
140 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
141 };
142
143
144 // 21.1.4 char_traits specializations
145 template<>
146 struct char_traits<char>
147 {
148 typedef char char_type;
149 typedef int int_type;
150 typedef streampos pos_type;
151 typedef streamoff off_type;
152 typedef mbstate_t state_type;
153
154 static void
155 assign(char_type& __c1, const char_type& __c2)
156 { __c1 = __c2; }
157
158 static bool
159 eq(const char_type& __c1, const char_type& __c2)
160 { return __c1 == __c2; }
161
162 static bool
163 lt(const char_type& __c1, const char_type& __c2)
164 { return __c1 < __c2; }
165
166 static int
167 compare(const char_type* __s1, const char_type* __s2, size_t __n)
168 { return memcmp(__s1, __s2, __n); }
169
170 static size_t
171 length(const char_type* __s)
172 { return strlen(__s); }
173
174 static const char_type*
175 find(const char_type* __s, size_t __n, const char_type& __a)
176 { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
177
178 static char_type*
179 move(char_type* __s1, const char_type* __s2, size_t __n)
180 { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
181
182 static char_type*
183 copy(char_type* __s1, const char_type* __s2, size_t __n)
184 { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
185
186 static char_type*
187 assign(char_type* __s, size_t __n, char_type __a)
188 { return static_cast<char_type*>(memset(__s, __a, __n)); }
189
190 static char_type
191 to_char_type(const int_type& __c)
192 { return static_cast<char_type>(__c); }
193
194 // To keep both the byte 0xff and the eof symbol 0xffffffff
195 // from ending up as 0xffffffff.
196 static int_type
197 to_int_type(const char_type& __c)
198 { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
199
200 static bool
201 eq_int_type(const int_type& __c1, const int_type& __c2)
202 { return __c1 == __c2; }
203
204 static int_type
205 eof() { return static_cast<int_type>(EOF); }
206
207 static int_type
208 not_eof(const int_type& __c)
209 { return (__c == eof()) ? 0 : __c; }
210 };
211
212
213 #ifdef _GLIBCPP_USE_WCHAR_T
214 template<>
215 struct char_traits<wchar_t>
216 {
217 typedef wchar_t char_type;
218 typedef wint_t int_type;
219 typedef wstreamoff off_type;
220 typedef wstreampos pos_type;
221 typedef mbstate_t state_type;
222
223 static void
224 assign(char_type& __c1, const char_type& __c2)
225 { __c1 = __c2; }
226
227 static bool
228 eq(const char_type& __c1, const char_type& __c2)
229 { return __c1 == __c2; }
230
231 static bool
232 lt(const char_type& __c1, const char_type& __c2)
233 { return __c1 < __c2; }
234
235 static int
236 compare(const char_type* __s1, const char_type* __s2, size_t __n)
237 { return wmemcmp(__s1, __s2, __n); }
238
239 static size_t
240 length(const char_type* __s)
241 { return wcslen(__s); }
242
243 static const char_type*
244 find(const char_type* __s, size_t __n, const char_type& __a)
245 { return wmemchr(__s, __a, __n); }
246
247 static char_type*
248 move(char_type* __s1, const char_type* __s2, int_type __n)
249 { return wmemmove(__s1, __s2, __n); }
250
251 static char_type*
252 copy(char_type* __s1, const char_type* __s2, size_t __n)
253 { return wmemcpy(__s1, __s2, __n); }
254
255 static char_type*
256 assign(char_type* __s, size_t __n, char_type __a)
257 { return wmemset(__s, __a, __n); }
258
259 static char_type
260 to_char_type(const int_type& __c) { return char_type(__c); }
261
262 static int_type
263 to_int_type(const char_type& __c) { return int_type(__c); }
264
265 static bool
266 eq_int_type(const int_type& __c1, const int_type& __c2)
267 { return __c1 == __c2; }
268
269 static int_type
270 eof() { return static_cast<int_type>(WEOF); }
271
272 static int_type
273 not_eof(const int_type& __c)
274 { return eq_int_type(__c, eof()) ? 0 : __c; }
275 };
276 #endif //_GLIBCPP_USE_WCHAR_T
277
278 template<typename _CharT, typename _Traits>
279 struct _Char_traits_match
280 {
281 _CharT _M_c;
282 _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
283
284 bool
285 operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
286 };
287
288 } // namespace std
289
290
291 #endif /* _CPP_BITS_CHAR_TRAITS_H */
292