OSDN Git Service

2003-02-13 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.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 _CPP_BITS_CHAR_TRAITS_H
41 #define _CPP_BITS_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.
60   */
61   template<class _CharT>
62     struct char_traits
63     {
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;
70       
71       static void 
72       assign(char_type& __c1, const char_type& __c2);
73
74       static bool 
75       eq(const char_type& __c1, const char_type& __c2);
76
77       static bool 
78       lt(const char_type& __c1, const char_type& __c2);
79
80       static int 
81       compare(const char_type* __s1, const char_type* __s2, size_t __n);
82
83       static size_t
84       length(const char_type* __s);
85
86       static const char_type* 
87       find(const char_type* __s, size_t __n, const char_type& __a);
88
89       static char_type* 
90       move(char_type* __s1, const char_type* __s2, size_t __n);
91
92       static char_type* 
93       copy(char_type* __s1, const char_type* __s2, size_t __n);
94
95       static char_type* 
96       assign(char_type* __s, size_t __n, char_type __a);
97
98       static char_type 
99       to_char_type(const int_type& __c);
100
101       static int_type 
102       to_int_type(const char_type& __c);
103
104       static bool 
105       eq_int_type(const int_type& __c1, const int_type& __c2);
106
107       static int_type 
108       eof(); 
109
110       static int_type 
111       not_eof(const int_type& __c);
112     };
113
114
115   /// 21.1.3.1  char_traits specializations
116   template<>
117     struct char_traits<char>
118     {
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;
124
125       static void 
126       assign(char_type& __c1, const char_type& __c2)
127       { __c1 = __c2; }
128
129       static bool 
130       eq(const char_type& __c1, const char_type& __c2)
131       { return __c1 == __c2; }
132
133       static bool 
134       lt(const char_type& __c1, const char_type& __c2)
135       { return __c1 < __c2; }
136
137       static int 
138       compare(const char_type* __s1, const char_type* __s2, size_t __n)
139       { return memcmp(__s1, __s2, __n); }
140
141       static size_t
142       length(const char_type* __s)
143       { return strlen(__s); }
144
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)); }
148
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)); }
152
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)); }
156
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)); }
160
161       static char_type 
162       to_char_type(const int_type& __c)
163       { return static_cast<char_type>(__c); }
164
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)); }
170
171       static bool 
172       eq_int_type(const int_type& __c1, const int_type& __c2)
173       { return __c1 == __c2; }
174
175       static int_type 
176       eof() { return static_cast<int_type>(EOF); }
177
178       static int_type 
179       not_eof(const int_type& __c)
180       { return (__c == eof()) ? 0 : __c; }
181   };
182
183
184 #ifdef _GLIBCPP_USE_WCHAR_T
185   /// 21.1.3.2  char_traits specializations
186   template<>
187     struct char_traits<wchar_t>
188     {
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;
194       
195       static void 
196       assign(char_type& __c1, const char_type& __c2)
197       { __c1 = __c2; }
198
199       static bool 
200       eq(const char_type& __c1, const char_type& __c2)
201       { return __c1 == __c2; }
202
203       static bool 
204       lt(const char_type& __c1, const char_type& __c2)
205       { return __c1 < __c2; }
206
207       static int 
208       compare(const char_type* __s1, const char_type* __s2, size_t __n)
209       { return wmemcmp(__s1, __s2, __n); }
210
211       static size_t
212       length(const char_type* __s)
213       { return wcslen(__s); }
214
215       static const char_type* 
216       find(const char_type* __s, size_t __n, const char_type& __a)
217       { return wmemchr(__s, __a, __n); }
218
219       static char_type* 
220       move(char_type* __s1, const char_type* __s2, int_type __n)
221       { return wmemmove(__s1, __s2, __n); }
222
223       static char_type* 
224       copy(char_type* __s1, const char_type* __s2, size_t __n)
225       { return wmemcpy(__s1, __s2, __n); }
226
227       static char_type* 
228       assign(char_type* __s, size_t __n, char_type __a)
229       { return wmemset(__s, __a, __n); }
230
231       static char_type 
232       to_char_type(const int_type& __c) { return char_type(__c); }
233
234       static int_type 
235       to_int_type(const char_type& __c) { return int_type(__c); }
236
237       static bool 
238       eq_int_type(const int_type& __c1, const int_type& __c2)
239       { return __c1 == __c2; }
240
241       static int_type 
242       eof() { return static_cast<int_type>(WEOF); }
243
244       static int_type 
245       not_eof(const int_type& __c)
246       { return eq_int_type(__c, eof()) ? 0 : __c; }
247   };
248 #endif //_GLIBCPP_USE_WCHAR_T
249
250   template<typename _CharT, typename _Traits>
251     struct _Char_traits_match
252     {
253       _CharT _M_c;
254       _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
255
256       bool 
257       operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
258     };
259 } // namespace std
260
261 #endif