abi.txt: New file.
[official-gcc.git] / libstdc++-v3 / include / bits / char_traits.h
blob48ca669afc41915881555acabc92dc9ab599adec
1 // Character Traits for use by standard string and iostream -*- C++ -*-
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.
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.
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.
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.
31 // ISO C++ 14882: 21 Strings library
34 /** @file char_traits.h
35 * This is an internal header file, included by other library headers.
36 * You should not attempt to use it directly.
39 #ifndef _CPP_BITS_CHAR_TRAITS_H
40 #define _CPP_BITS_CHAR_TRAITS_H 1
42 #pragma GCC system_header
44 #include <cstring> // For memmove, memset, memchr
45 #include <bits/fpos.h> // For streampos
47 namespace std
49 // 21.1.2
50 /**
51 * @brief Basis for explicit traits specializations.
53 * @note For any given actual character type, this definition is
54 * probably wrong.
56 template<class _CharT>
57 struct char_traits
59 typedef _CharT char_type;
60 // Unsigned as wint_t in unsigned.
61 typedef unsigned long int_type;
62 typedef streampos pos_type;
63 typedef streamoff off_type;
64 typedef mbstate_t state_type;
66 static void
67 assign(char_type& __c1, const char_type& __c2)
68 { __c1 = __c2; }
70 static bool
71 eq(const char_type& __c1, const char_type& __c2)
72 { return __c1 == __c2; }
74 static bool
75 lt(const char_type& __c1, const char_type& __c2)
76 { return __c1 < __c2; }
78 static int
79 compare(const char_type* __s1, const char_type* __s2, size_t __n)
81 for (size_t __i = 0; __i < __n; ++__i)
82 if (!eq(__s1[__i], __s2[__i]))
83 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
84 return 0;
87 static size_t
88 length(const char_type* __s)
90 const char_type* __p = __s;
91 while (*__p) ++__p;
92 return (__p - __s);
95 static const char_type*
96 find(const char_type* __s, size_t __n, const char_type& __a)
98 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
99 if (*__p == __a) return __p;
100 return 0;
103 static char_type*
104 move(char_type* __s1, const char_type* __s2, size_t __n)
105 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
107 static char_type*
108 copy(char_type* __s1, const char_type* __s2, size_t __n)
109 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
111 static char_type*
112 assign(char_type* __s, size_t __n, char_type __a)
114 for (char_type* __p = __s; __p < __s + __n; ++__p)
115 assign(*__p, __a);
116 return __s;
119 static char_type
120 to_char_type(const int_type& __c)
121 { return char_type(__c); }
123 static int_type
124 to_int_type(const char_type& __c) { return int_type(__c); }
126 static bool
127 eq_int_type(const int_type& __c1, const int_type& __c2)
128 { return __c1 == __c2; }
130 static int_type
131 eof() { return static_cast<int_type>(-1); }
133 static int_type
134 not_eof(const int_type& __c)
135 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
139 /// 21.1.4 char_traits specializations
140 template<>
141 struct char_traits<char>
143 typedef char char_type;
144 typedef int int_type;
145 typedef streampos pos_type;
146 typedef streamoff off_type;
147 typedef mbstate_t state_type;
149 static void
150 assign(char_type& __c1, const char_type& __c2)
151 { __c1 = __c2; }
153 static bool
154 eq(const char_type& __c1, const char_type& __c2)
155 { return __c1 == __c2; }
157 static bool
158 lt(const char_type& __c1, const char_type& __c2)
159 { return __c1 < __c2; }
161 static int
162 compare(const char_type* __s1, const char_type* __s2, size_t __n)
163 { return memcmp(__s1, __s2, __n); }
165 static size_t
166 length(const char_type* __s)
167 { return strlen(__s); }
169 static const char_type*
170 find(const char_type* __s, size_t __n, const char_type& __a)
171 { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
173 static char_type*
174 move(char_type* __s1, const char_type* __s2, size_t __n)
175 { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
177 static char_type*
178 copy(char_type* __s1, const char_type* __s2, size_t __n)
179 { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
181 static char_type*
182 assign(char_type* __s, size_t __n, char_type __a)
183 { return static_cast<char_type*>(memset(__s, __a, __n)); }
185 static char_type
186 to_char_type(const int_type& __c)
187 { return static_cast<char_type>(__c); }
189 // To keep both the byte 0xff and the eof symbol 0xffffffff
190 // from ending up as 0xffffffff.
191 static int_type
192 to_int_type(const char_type& __c)
193 { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
195 static bool
196 eq_int_type(const int_type& __c1, const int_type& __c2)
197 { return __c1 == __c2; }
199 static int_type
200 eof() { return static_cast<int_type>(EOF); }
202 static int_type
203 not_eof(const int_type& __c)
204 { return (__c == eof()) ? 0 : __c; }
208 #ifdef _GLIBCPP_USE_WCHAR_T
209 template<>
210 struct char_traits<wchar_t>
212 typedef wchar_t char_type;
213 typedef wint_t int_type;
214 typedef streamoff off_type;
215 typedef wstreampos pos_type;
216 typedef mbstate_t state_type;
218 static void
219 assign(char_type& __c1, const char_type& __c2)
220 { __c1 = __c2; }
222 static bool
223 eq(const char_type& __c1, const char_type& __c2)
224 { return __c1 == __c2; }
226 static bool
227 lt(const char_type& __c1, const char_type& __c2)
228 { return __c1 < __c2; }
230 static int
231 compare(const char_type* __s1, const char_type* __s2, size_t __n)
232 { return wmemcmp(__s1, __s2, __n); }
234 static size_t
235 length(const char_type* __s)
236 { return wcslen(__s); }
238 static const char_type*
239 find(const char_type* __s, size_t __n, const char_type& __a)
240 { return wmemchr(__s, __a, __n); }
242 static char_type*
243 move(char_type* __s1, const char_type* __s2, int_type __n)
244 { return wmemmove(__s1, __s2, __n); }
246 static char_type*
247 copy(char_type* __s1, const char_type* __s2, size_t __n)
248 { return wmemcpy(__s1, __s2, __n); }
250 static char_type*
251 assign(char_type* __s, size_t __n, char_type __a)
252 { return wmemset(__s, __a, __n); }
254 static char_type
255 to_char_type(const int_type& __c) { return char_type(__c); }
257 static int_type
258 to_int_type(const char_type& __c) { return int_type(__c); }
260 static bool
261 eq_int_type(const int_type& __c1, const int_type& __c2)
262 { return __c1 == __c2; }
264 static int_type
265 eof() { return static_cast<int_type>(WEOF); }
267 static int_type
268 not_eof(const int_type& __c)
269 { return eq_int_type(__c, eof()) ? 0 : __c; }
271 #endif //_GLIBCPP_USE_WCHAR_T
273 template<typename _CharT, typename _Traits>
274 struct _Char_traits_match
276 _CharT _M_c;
277 _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
279 bool
280 operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
282 } // namespace std
284 #endif