]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/char_traits.h
pod_char_traits.cc: New.
[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, 2002
4 // Free Software Foundation, Inc.
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
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
40 #ifndef _CHAR_TRAITS_H
41 #define _CHAR_TRAITS_H 1
42
43 #pragma GCC system_header
44
45 #include <cstring> // For memmove, memset, memchr
46 #include <bits/fpos.h> // For streampos
47
48 namespace std
49 {
50 // 21.1
51 /**
52 * @brief Basis for explicit traits specializations.
53 *
54 * @note For any given actual character type, this definition is
55 * probably wrong.
56 *
57 * See http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#5
58 * for advice on how to make use of this class for "unusual" character
59 * types. Also, check out include/ext/pod_char_traits.h.
60 */
61 template<class _CharT>
62 struct char_traits
63 {
64 typedef _CharT char_type;
65 typedef unsigned long int_type;
66 typedef streampos pos_type;
67 typedef streamoff off_type;
68 typedef mbstate_t state_type;
69
70 static void
71 assign(char_type& __c1, const char_type& __c2);
72
73 static bool
74 eq(const char_type& __c1, const char_type& __c2);
75
76 static bool
77 lt(const char_type& __c1, const char_type& __c2);
78
79 static int
80 compare(const char_type* __s1, const char_type* __s2, size_t __n);
81
82 static size_t
83 length(const char_type* __s);
84
85 static const char_type*
86 find(const char_type* __s, size_t __n, const char_type& __a);
87
88 static char_type*
89 move(char_type* __s1, const char_type* __s2, size_t __n);
90
91 static char_type*
92 copy(char_type* __s1, const char_type* __s2, size_t __n);
93
94 static char_type*
95 assign(char_type* __s, size_t __n, char_type __a);
96
97 static char_type
98 to_char_type(const int_type& __c);
99
100 static int_type
101 to_int_type(const char_type& __c);
102
103 static bool
104 eq_int_type(const int_type& __c1, const int_type& __c2);
105
106 static int_type
107 eof();
108
109 static int_type
110 not_eof(const int_type& __c);
111 };
112
113
114 /// 21.1.3.1 char_traits specializations
115 template<>
116 struct char_traits<char>
117 {
118 typedef char char_type;
119 typedef int int_type;
120 typedef streampos pos_type;
121 typedef streamoff off_type;
122 typedef mbstate_t state_type;
123
124 static void
125 assign(char_type& __c1, const char_type& __c2)
126 { __c1 = __c2; }
127
128 static bool
129 eq(const char_type& __c1, const char_type& __c2)
130 { return __c1 == __c2; }
131
132 static bool
133 lt(const char_type& __c1, const char_type& __c2)
134 { return __c1 < __c2; }
135
136 static int
137 compare(const char_type* __s1, const char_type* __s2, size_t __n)
138 { return memcmp(__s1, __s2, __n); }
139
140 static size_t
141 length(const char_type* __s)
142 { return strlen(__s); }
143
144 static const char_type*
145 find(const char_type* __s, size_t __n, const char_type& __a)
146 { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
147
148 static char_type*
149 move(char_type* __s1, const char_type* __s2, size_t __n)
150 { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
151
152 static char_type*
153 copy(char_type* __s1, const char_type* __s2, size_t __n)
154 { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
155
156 static char_type*
157 assign(char_type* __s, size_t __n, char_type __a)
158 { return static_cast<char_type*>(memset(__s, __a, __n)); }
159
160 static char_type
161 to_char_type(const int_type& __c)
162 { return static_cast<char_type>(__c); }
163
164 // To keep both the byte 0xff and the eof symbol 0xffffffff
165 // from ending up as 0xffffffff.
166 static int_type
167 to_int_type(const char_type& __c)
168 { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
169
170 static bool
171 eq_int_type(const int_type& __c1, const int_type& __c2)
172 { return __c1 == __c2; }
173
174 static int_type
175 eof() { return static_cast<int_type>(EOF); }
176
177 static int_type
178 not_eof(const int_type& __c)
179 { return (__c == eof()) ? 0 : __c; }
180 };
181
182
183 #ifdef _GLIBCXX_USE_WCHAR_T
184 /// 21.1.3.2 char_traits specializations
185 template<>
186 struct char_traits<wchar_t>
187 {
188 typedef wchar_t char_type;
189 typedef wint_t int_type;
190 typedef streamoff off_type;
191 typedef wstreampos pos_type;
192 typedef mbstate_t state_type;
193
194 static void
195 assign(char_type& __c1, const char_type& __c2)
196 { __c1 = __c2; }
197
198 static bool
199 eq(const char_type& __c1, const char_type& __c2)
200 { return __c1 == __c2; }
201
202 static bool
203 lt(const char_type& __c1, const char_type& __c2)
204 { return __c1 < __c2; }
205
206 static int
207 compare(const char_type* __s1, const char_type* __s2, size_t __n)
208 { return wmemcmp(__s1, __s2, __n); }
209
210 static size_t
211 length(const char_type* __s)
212 { return wcslen(__s); }
213
214 static const char_type*
215 find(const char_type* __s, size_t __n, const char_type& __a)
216 { return wmemchr(__s, __a, __n); }
217
218 static char_type*
219 move(char_type* __s1, const char_type* __s2, int_type __n)
220 { return wmemmove(__s1, __s2, __n); }
221
222 static char_type*
223 copy(char_type* __s1, const char_type* __s2, size_t __n)
224 { return wmemcpy(__s1, __s2, __n); }
225
226 static char_type*
227 assign(char_type* __s, size_t __n, char_type __a)
228 { return wmemset(__s, __a, __n); }
229
230 static char_type
231 to_char_type(const int_type& __c) { return char_type(__c); }
232
233 static int_type
234 to_int_type(const char_type& __c) { return int_type(__c); }
235
236 static bool
237 eq_int_type(const int_type& __c1, const int_type& __c2)
238 { return __c1 == __c2; }
239
240 static int_type
241 eof() { return static_cast<int_type>(WEOF); }
242
243 static int_type
244 not_eof(const int_type& __c)
245 { return eq_int_type(__c, eof()) ? 0 : __c; }
246 };
247 #endif //_GLIBCXX_USE_WCHAR_T
248
249 template<typename _CharT, typename _Traits>
250 struct _Char_traits_match
251 {
252 _CharT _M_c;
253 _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
254
255 bool
256 operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
257 };
258 } // namespace std
259
260 #endif