]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/char_traits.h
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 #include <bits/std_cwchar.h> // For mbstate_t.
38 #include <bits/std_cstring.h> // For memmove, memset, memchr
39 #include <bits/fpos.h> // For streamoff, streamsize
44 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
45 // Can't have self-recursive types for streampos.
46 // 21.1.3.1 char_traits sets size_type to streampos
48 // And here, where streampos is typedefed to fpos<traits::state_type>
49 typedef fpos
<mbstate_t> streampos
;
50 # ifdef _GLIBCPP_USE_WCHAR_T
51 typedef fpos
<mbstate_t> wstreampos
;
55 // 21.1.2 Basis for explicit _Traits specialization
56 // NB: That for any given actual character type this definition is
59 template<class _CharT
>
62 typedef _CharT char_type
;
63 // Unsigned as wint_t in unsigned.
64 typedef unsigned long int_type
;
65 typedef streampos pos_type
;
66 typedef streamoff off_type
;
67 typedef mbstate_t state_type
;
70 assign(char_type
& __c1
, const char_type
& __c2
)
74 eq(const char_type
& __c1
, const char_type
& __c2
)
75 { return __c1
== __c2
; }
78 lt(const char_type
& __c1
, const char_type
& __c2
)
79 { return __c1
< __c2
; }
82 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
84 for (size_t __i
= 0; __i
< __n
; ++__i
)
85 if (!eq(__s1
[__i
], __s2
[__i
]))
86 return lt(__s1
[__i
], __s2
[__i
]) ? -1 : 1;
91 length(const char_type
* __s
)
93 const char_type
* __p
= __s
;
98 static const char_type
*
99 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
101 for (const char_type
* __p
= __s
; size_t(__p
- __s
) < __n
; ++__p
)
102 if (*__p
== __a
) return __p
;
107 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
108 { return (char_type
*) memmove(__s1
, __s2
, __n
* sizeof(char_type
)); }
111 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
112 { return (char_type
*) memcpy(__s1
, __s2
, __n
* sizeof(char_type
)); }
115 assign(char_type
* __s
, size_t __n
, char_type __a
)
117 for (char_type
* __p
= __s
; __p
< __s
+ __n
; ++__p
)
123 to_char_type(const int_type
& __c
)
124 { return char_type(__c
); }
127 to_int_type(const char_type
& __c
) { return int_type(__c
); }
130 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
131 { return __c1
== __c2
; }
134 _S_get_state(const pos_type
& __pos
) { return __pos
.state(); }
137 eof() { return static_cast<int_type
>(-1); }
140 _S_eos() { return char_type(); }
143 not_eof(const int_type
& __c
)
144 { return eq_int_type(__c
, eof()) ? int_type(0) : __c
; }
148 // 21.1.4 char_traits specializations
150 struct char_traits
<char>
152 typedef char char_type
;
153 typedef int int_type
;
154 typedef streampos pos_type
;
155 typedef streamoff off_type
;
156 typedef mbstate_t state_type
;
159 assign(char_type
& __c1
, const char_type
& __c2
)
163 eq(const char_type
& __c1
, const char_type
& __c2
)
164 { return __c1
== __c2
; }
167 lt(const char_type
& __c1
, const char_type
& __c2
)
168 { return __c1
< __c2
; }
171 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
172 { return memcmp(__s1
, __s2
, __n
); }
175 length(const char_type
* __s
)
176 { return strlen(__s
); }
178 static const char_type
*
179 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
180 { return static_cast<const char_type
*>(memchr(__s
, __a
, __n
)); }
183 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
184 { return static_cast<char_type
*>(memmove(__s1
, __s2
, __n
)); }
187 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
188 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
191 assign(char_type
* __s
, size_t __n
, char_type __a
)
192 { return static_cast<char_type
*>(memset(__s
, __a
, __n
)); }
195 to_char_type(const int_type
& __c
)
196 { return static_cast<char_type
>(__c
); }
198 // To keep both the byte 0xff and the eof symbol 0xffffffff
199 // from ending up as 0xffffffff.
201 to_int_type(const char_type
& __c
)
202 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
205 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
206 { return __c1
== __c2
; }
209 _S_get_state(const pos_type
& __pos
) { return __pos
.state(); }
212 eof() { return static_cast<int_type
>(EOF
); }
215 _S_eos() { return char_type(); }
218 not_eof(const int_type
& __c
)
219 { return (__c
== eof()) ? 0 : __c
; }
223 #ifdef _GLIBCPP_USE_WCHAR_T
225 struct char_traits
<wchar_t>
227 typedef wchar_t char_type
;
228 typedef wint_t int_type
;
229 typedef wstreamoff off_type
;
230 typedef wstreampos pos_type
;
231 typedef mbstate_t state_type
;
234 assign(char_type
& __c1
, const char_type
& __c2
)
238 eq(const char_type
& __c1
, const char_type
& __c2
)
239 { return __c1
== __c2
; }
242 lt(const char_type
& __c1
, const char_type
& __c2
)
243 { return __c1
< __c2
; }
246 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
247 { return wmemcmp(__s1
, __s2
, __n
); }
250 length(const char_type
* __s
)
251 { return wcslen(__s
); }
253 static const char_type
*
254 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
255 { return wmemchr(__s
, __a
, __n
); }
258 move(char_type
* __s1
, const char_type
* __s2
, int_type __n
)
259 { return wmemmove(__s1
, __s2
, __n
); }
262 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
263 { return wmemcpy(__s1
, __s2
, __n
); }
266 assign(char_type
* __s
, size_t __n
, char_type __a
)
267 { return wmemset(__s
, __a
, __n
); }
270 to_char_type(const int_type
& __c
) { return char_type(__c
); }
273 to_int_type(const char_type
& __c
) { return int_type(__c
); }
276 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
277 { return __c1
== __c2
; }
280 _S_get_state(const pos_type
& __pos
) { return __pos
.state(); }
283 eof() { return static_cast<int_type
>(WEOF
); }
286 _S_eos() { return char_type(); }
289 not_eof(const int_type
& __c
)
290 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
292 #endif //_GLIBCPP_USE_WCHAR_T
294 template<typename _CharT
, typename _Traits
>
295 struct _Char_traits_match
298 _Char_traits_match(_CharT
const& __c
) : _M_c(__c
) { }
301 operator()(_CharT
const& __a
) { return _Traits::eq(_M_c
, __a
); }
307 #endif /* _CPP_BITS_CHAR_TRAITS_H */