1 // Character Traits for use by standard string and iostream -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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)
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,
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 #ifndef _CPP_BITS_CHAR_TRAITS_H
35 #define _CPP_BITS_CHAR_TRAITS_H 1
37 #pragma GCC system_header
39 #include <bits/std_cstring.h> // For memmove, memset, memchr
40 #include <bits/fpos.h> // For streampos
44 /// 21.1.2 Basis for explicit _Traits specialization
45 /// NB: That for any given actual character type this definition is
47 template<class _CharT
>
50 typedef _CharT char_type
;
51 // Unsigned as wint_t in unsigned.
52 typedef unsigned long int_type
;
53 typedef streampos pos_type
;
54 typedef streamoff off_type
;
55 typedef mbstate_t state_type
;
58 assign(char_type
& __c1
, const char_type
& __c2
)
62 eq(const char_type
& __c1
, const char_type
& __c2
)
63 { return __c1
== __c2
; }
66 lt(const char_type
& __c1
, const char_type
& __c2
)
67 { return __c1
< __c2
; }
70 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
72 for (size_t __i
= 0; __i
< __n
; ++__i
)
73 if (!eq(__s1
[__i
], __s2
[__i
]))
74 return lt(__s1
[__i
], __s2
[__i
]) ? -1 : 1;
79 length(const char_type
* __s
)
81 const char_type
* __p
= __s
;
86 static const char_type
*
87 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
89 for (const char_type
* __p
= __s
; size_t(__p
- __s
) < __n
; ++__p
)
90 if (*__p
== __a
) return __p
;
95 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
96 { return (char_type
*) memmove(__s1
, __s2
, __n
* sizeof(char_type
)); }
99 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
100 { return (char_type
*) memcpy(__s1
, __s2
, __n
* sizeof(char_type
)); }
103 assign(char_type
* __s
, size_t __n
, char_type __a
)
105 for (char_type
* __p
= __s
; __p
< __s
+ __n
; ++__p
)
111 to_char_type(const int_type
& __c
)
112 { return char_type(__c
); }
115 to_int_type(const char_type
& __c
) { return int_type(__c
); }
118 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
119 { return __c1
== __c2
; }
122 eof() { return static_cast<int_type
>(-1); }
125 not_eof(const int_type
& __c
)
126 { return eq_int_type(__c
, eof()) ? int_type(0) : __c
; }
130 /// 21.1.4 char_traits specializations
132 struct char_traits
<char>
134 typedef char char_type
;
135 typedef int int_type
;
136 typedef streampos pos_type
;
137 typedef streamoff off_type
;
138 typedef mbstate_t state_type
;
141 assign(char_type
& __c1
, const char_type
& __c2
)
145 eq(const char_type
& __c1
, const char_type
& __c2
)
146 { return __c1
== __c2
; }
149 lt(const char_type
& __c1
, const char_type
& __c2
)
150 { return __c1
< __c2
; }
153 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
154 { return memcmp(__s1
, __s2
, __n
); }
157 length(const char_type
* __s
)
158 { return strlen(__s
); }
160 static const char_type
*
161 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
162 { return static_cast<const char_type
*>(memchr(__s
, __a
, __n
)); }
165 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
166 { return static_cast<char_type
*>(memmove(__s1
, __s2
, __n
)); }
169 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
170 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
173 assign(char_type
* __s
, size_t __n
, char_type __a
)
174 { return static_cast<char_type
*>(memset(__s
, __a
, __n
)); }
177 to_char_type(const int_type
& __c
)
178 { return static_cast<char_type
>(__c
); }
180 // To keep both the byte 0xff and the eof symbol 0xffffffff
181 // from ending up as 0xffffffff.
183 to_int_type(const char_type
& __c
)
184 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
187 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
188 { return __c1
== __c2
; }
191 eof() { return static_cast<int_type
>(EOF
); }
194 not_eof(const int_type
& __c
)
195 { return (__c
== eof()) ? 0 : __c
; }
199 #ifdef _GLIBCPP_USE_WCHAR_T
201 struct char_traits
<wchar_t>
203 typedef wchar_t char_type
;
204 typedef wint_t int_type
;
205 typedef streamoff off_type
;
206 typedef wstreampos pos_type
;
207 typedef mbstate_t state_type
;
210 assign(char_type
& __c1
, const char_type
& __c2
)
214 eq(const char_type
& __c1
, const char_type
& __c2
)
215 { return __c1
== __c2
; }
218 lt(const char_type
& __c1
, const char_type
& __c2
)
219 { return __c1
< __c2
; }
222 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
223 { return wmemcmp(__s1
, __s2
, __n
); }
226 length(const char_type
* __s
)
227 { return wcslen(__s
); }
229 static const char_type
*
230 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
231 { return wmemchr(__s
, __a
, __n
); }
234 move(char_type
* __s1
, const char_type
* __s2
, int_type __n
)
235 { return wmemmove(__s1
, __s2
, __n
); }
238 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
239 { return wmemcpy(__s1
, __s2
, __n
); }
242 assign(char_type
* __s
, size_t __n
, char_type __a
)
243 { return wmemset(__s
, __a
, __n
); }
246 to_char_type(const int_type
& __c
) { return char_type(__c
); }
249 to_int_type(const char_type
& __c
) { return int_type(__c
); }
252 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
253 { return __c1
== __c2
; }
256 eof() { return static_cast<int_type
>(WEOF
); }
259 not_eof(const int_type
& __c
)
260 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
262 #endif //_GLIBCPP_USE_WCHAR_T
264 template<typename _CharT
, typename _Traits
>
265 struct _Char_traits_match
268 _Char_traits_match(_CharT
const& __c
) : _M_c(__c
) { }
271 operator()(_CharT
const& __a
) { return _Traits::eq(_M_c
, __a
); }