2003-07-04 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / bits / char_traits.h
bloba6d1d8c3b3770dd4fb6f549868de77eece57301a
1 // Character Traits for use by standard string and iostream -*- C++ -*-
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.
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.
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.
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.
32 // ISO C++ 14882: 21 Strings library
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.
40 #ifndef _CHAR_TRAITS_H
41 #define _CHAR_TRAITS_H 1
43 #pragma GCC system_header
45 #include <cstring> // For memmove, memset, memchr
46 #include <bits/fpos.h> // For streampos
48 namespace std
50 // 21.1
51 /**
52 * @brief Basis for explicit traits specializations.
54 * @note For any given actual character type, this definition is
55 * probably wrong.
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.
61 template<class _CharT>
62 struct char_traits
64 typedef _CharT char_type;
65 // Unsigned as wint_t is unsigned.
66 typedef unsigned long int_type;
67 typedef streampos pos_type;
68 typedef streamoff off_type;
69 typedef mbstate_t state_type;
71 static void
72 assign(char_type& __c1, const char_type& __c2);
74 static bool
75 eq(const char_type& __c1, const char_type& __c2);
77 static bool
78 lt(const char_type& __c1, const char_type& __c2);
80 static int
81 compare(const char_type* __s1, const char_type* __s2, size_t __n);
83 static size_t
84 length(const char_type* __s);
86 static const char_type*
87 find(const char_type* __s, size_t __n, const char_type& __a);
89 static char_type*
90 move(char_type* __s1, const char_type* __s2, size_t __n);
92 static char_type*
93 copy(char_type* __s1, const char_type* __s2, size_t __n);
95 static char_type*
96 assign(char_type* __s, size_t __n, char_type __a);
98 static char_type
99 to_char_type(const int_type& __c);
101 static int_type
102 to_int_type(const char_type& __c);
104 static bool
105 eq_int_type(const int_type& __c1, const int_type& __c2);
107 static int_type
108 eof();
110 static int_type
111 not_eof(const int_type& __c);
115 /// 21.1.3.1 char_traits specializations
116 template<>
117 struct char_traits<char>
119 typedef char char_type;
120 typedef int int_type;
121 typedef streampos pos_type;
122 typedef streamoff off_type;
123 typedef mbstate_t state_type;
125 static void
126 assign(char_type& __c1, const char_type& __c2)
127 { __c1 = __c2; }
129 static bool
130 eq(const char_type& __c1, const char_type& __c2)
131 { return __c1 == __c2; }
133 static bool
134 lt(const char_type& __c1, const char_type& __c2)
135 { return __c1 < __c2; }
137 static int
138 compare(const char_type* __s1, const char_type* __s2, size_t __n)
139 { return memcmp(__s1, __s2, __n); }
141 static size_t
142 length(const char_type* __s)
143 { return strlen(__s); }
145 static const char_type*
146 find(const char_type* __s, size_t __n, const char_type& __a)
147 { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
149 static char_type*
150 move(char_type* __s1, const char_type* __s2, size_t __n)
151 { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
153 static char_type*
154 copy(char_type* __s1, const char_type* __s2, size_t __n)
155 { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
157 static char_type*
158 assign(char_type* __s, size_t __n, char_type __a)
159 { return static_cast<char_type*>(memset(__s, __a, __n)); }
161 static char_type
162 to_char_type(const int_type& __c)
163 { return static_cast<char_type>(__c); }
165 // To keep both the byte 0xff and the eof symbol 0xffffffff
166 // from ending up as 0xffffffff.
167 static int_type
168 to_int_type(const char_type& __c)
169 { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
171 static bool
172 eq_int_type(const int_type& __c1, const int_type& __c2)
173 { return __c1 == __c2; }
175 static int_type
176 eof() { return static_cast<int_type>(EOF); }
178 static int_type
179 not_eof(const int_type& __c)
180 { return (__c == eof()) ? 0 : __c; }
184 #ifdef _GLIBCXX_USE_WCHAR_T
185 /// 21.1.3.2 char_traits specializations
186 template<>
187 struct char_traits<wchar_t>
189 typedef wchar_t char_type;
190 typedef wint_t int_type;
191 typedef streamoff off_type;
192 typedef wstreampos pos_type;
193 typedef mbstate_t state_type;
195 static void
196 assign(char_type& __c1, const char_type& __c2)
197 { __c1 = __c2; }
199 static bool
200 eq(const char_type& __c1, const char_type& __c2)
201 { return __c1 == __c2; }
203 static bool
204 lt(const char_type& __c1, const char_type& __c2)
205 { return __c1 < __c2; }
207 static int
208 compare(const char_type* __s1, const char_type* __s2, size_t __n)
209 { return wmemcmp(__s1, __s2, __n); }
211 static size_t
212 length(const char_type* __s)
213 { return wcslen(__s); }
215 static const char_type*
216 find(const char_type* __s, size_t __n, const char_type& __a)
217 { return wmemchr(__s, __a, __n); }
219 static char_type*
220 move(char_type* __s1, const char_type* __s2, int_type __n)
221 { return wmemmove(__s1, __s2, __n); }
223 static char_type*
224 copy(char_type* __s1, const char_type* __s2, size_t __n)
225 { return wmemcpy(__s1, __s2, __n); }
227 static char_type*
228 assign(char_type* __s, size_t __n, char_type __a)
229 { return wmemset(__s, __a, __n); }
231 static char_type
232 to_char_type(const int_type& __c) { return char_type(__c); }
234 static int_type
235 to_int_type(const char_type& __c) { return int_type(__c); }
237 static bool
238 eq_int_type(const int_type& __c1, const int_type& __c2)
239 { return __c1 == __c2; }
241 static int_type
242 eof() { return static_cast<int_type>(WEOF); }
244 static int_type
245 not_eof(const int_type& __c)
246 { return eq_int_type(__c, eof()) ? 0 : __c; }
248 #endif //_GLIBCXX_USE_WCHAR_T
250 template<typename _CharT, typename _Traits>
251 struct _Char_traits_match
253 _CharT _M_c;
254 _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
256 bool
257 operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
259 } // namespace std
261 #endif