stl_bvector.h: Change calls to 3-argument distance() into standard 2-argument version.
[official-gcc.git] / libstdc++-v3 / include / bits / char_traits.h
blob710e0eb6a534a5d56356e51eb8c4f64bbbab8b6d
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 <bits/std_cstring.h> // For memmove, memset, memchr
45 #include <bits/fpos.h> // For streampos
47 namespace std
49 /// 21.1.2 Basis for explicit _Traits specialization
50 /// NB: That for any given actual character type this definition is
51 /// probably wrong.
52 template<class _CharT>
53 struct char_traits
55 typedef _CharT char_type;
56 // Unsigned as wint_t in unsigned.
57 typedef unsigned long int_type;
58 typedef streampos pos_type;
59 typedef streamoff off_type;
60 typedef mbstate_t state_type;
62 static void
63 assign(char_type& __c1, const char_type& __c2)
64 { __c1 = __c2; }
66 static bool
67 eq(const char_type& __c1, const char_type& __c2)
68 { return __c1 == __c2; }
70 static bool
71 lt(const char_type& __c1, const char_type& __c2)
72 { return __c1 < __c2; }
74 static int
75 compare(const char_type* __s1, const char_type* __s2, size_t __n)
77 for (size_t __i = 0; __i < __n; ++__i)
78 if (!eq(__s1[__i], __s2[__i]))
79 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
80 return 0;
83 static size_t
84 length(const char_type* __s)
86 const char_type* __p = __s;
87 while (*__p) ++__p;
88 return (__p - __s);
91 static const char_type*
92 find(const char_type* __s, size_t __n, const char_type& __a)
94 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
95 if (*__p == __a) return __p;
96 return 0;
99 static char_type*
100 move(char_type* __s1, const char_type* __s2, size_t __n)
101 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
103 static char_type*
104 copy(char_type* __s1, const char_type* __s2, size_t __n)
105 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
107 static char_type*
108 assign(char_type* __s, size_t __n, char_type __a)
110 for (char_type* __p = __s; __p < __s + __n; ++__p)
111 assign(*__p, __a);
112 return __s;
115 static char_type
116 to_char_type(const int_type& __c)
117 { return char_type(__c); }
119 static int_type
120 to_int_type(const char_type& __c) { return int_type(__c); }
122 static bool
123 eq_int_type(const int_type& __c1, const int_type& __c2)
124 { return __c1 == __c2; }
126 static int_type
127 eof() { return static_cast<int_type>(-1); }
129 static int_type
130 not_eof(const int_type& __c)
131 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
135 /// 21.1.4 char_traits specializations
136 template<>
137 struct char_traits<char>
139 typedef char char_type;
140 typedef int int_type;
141 typedef streampos pos_type;
142 typedef streamoff off_type;
143 typedef mbstate_t state_type;
145 static void
146 assign(char_type& __c1, const char_type& __c2)
147 { __c1 = __c2; }
149 static bool
150 eq(const char_type& __c1, const char_type& __c2)
151 { return __c1 == __c2; }
153 static bool
154 lt(const char_type& __c1, const char_type& __c2)
155 { return __c1 < __c2; }
157 static int
158 compare(const char_type* __s1, const char_type* __s2, size_t __n)
159 { return memcmp(__s1, __s2, __n); }
161 static size_t
162 length(const char_type* __s)
163 { return strlen(__s); }
165 static const char_type*
166 find(const char_type* __s, size_t __n, const char_type& __a)
167 { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
169 static char_type*
170 move(char_type* __s1, const char_type* __s2, size_t __n)
171 { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
173 static char_type*
174 copy(char_type* __s1, const char_type* __s2, size_t __n)
175 { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
177 static char_type*
178 assign(char_type* __s, size_t __n, char_type __a)
179 { return static_cast<char_type*>(memset(__s, __a, __n)); }
181 static char_type
182 to_char_type(const int_type& __c)
183 { return static_cast<char_type>(__c); }
185 // To keep both the byte 0xff and the eof symbol 0xffffffff
186 // from ending up as 0xffffffff.
187 static int_type
188 to_int_type(const char_type& __c)
189 { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
191 static bool
192 eq_int_type(const int_type& __c1, const int_type& __c2)
193 { return __c1 == __c2; }
195 static int_type
196 eof() { return static_cast<int_type>(EOF); }
198 static int_type
199 not_eof(const int_type& __c)
200 { return (__c == eof()) ? 0 : __c; }
204 #ifdef _GLIBCPP_USE_WCHAR_T
205 template<>
206 struct char_traits<wchar_t>
208 typedef wchar_t char_type;
209 typedef wint_t int_type;
210 typedef streamoff off_type;
211 typedef wstreampos pos_type;
212 typedef mbstate_t state_type;
214 static void
215 assign(char_type& __c1, const char_type& __c2)
216 { __c1 = __c2; }
218 static bool
219 eq(const char_type& __c1, const char_type& __c2)
220 { return __c1 == __c2; }
222 static bool
223 lt(const char_type& __c1, const char_type& __c2)
224 { return __c1 < __c2; }
226 static int
227 compare(const char_type* __s1, const char_type* __s2, size_t __n)
228 { return wmemcmp(__s1, __s2, __n); }
230 static size_t
231 length(const char_type* __s)
232 { return wcslen(__s); }
234 static const char_type*
235 find(const char_type* __s, size_t __n, const char_type& __a)
236 { return wmemchr(__s, __a, __n); }
238 static char_type*
239 move(char_type* __s1, const char_type* __s2, int_type __n)
240 { return wmemmove(__s1, __s2, __n); }
242 static char_type*
243 copy(char_type* __s1, const char_type* __s2, size_t __n)
244 { return wmemcpy(__s1, __s2, __n); }
246 static char_type*
247 assign(char_type* __s, size_t __n, char_type __a)
248 { return wmemset(__s, __a, __n); }
250 static char_type
251 to_char_type(const int_type& __c) { return char_type(__c); }
253 static int_type
254 to_int_type(const char_type& __c) { return int_type(__c); }
256 static bool
257 eq_int_type(const int_type& __c1, const int_type& __c2)
258 { return __c1 == __c2; }
260 static int_type
261 eof() { return static_cast<int_type>(WEOF); }
263 static int_type
264 not_eof(const int_type& __c)
265 { return eq_int_type(__c, eof()) ? 0 : __c; }
267 #endif //_GLIBCPP_USE_WCHAR_T
269 template<typename _CharT, typename _Traits>
270 struct _Char_traits_match
272 _CharT _M_c;
273 _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
275 bool
276 operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
278 } // namespace std
280 #endif