]>
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-1999, 2000 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
; }
147 // 21.1.4 char_traits specializations
149 struct char_traits
<char>
151 typedef char char_type
;
152 typedef unsigned int int_type
;
153 typedef streampos pos_type
;
154 typedef streamoff off_type
;
155 typedef mbstate_t state_type
;
158 assign(char_type
& __c1
, const char_type
& __c2
)
162 eq(const char_type
& __c1
, const char_type
& __c2
)
163 { return __c1
== __c2
; }
166 lt(const char_type
& __c1
, const char_type
& __c2
)
167 { return __c1
< __c2
; }
170 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
171 { return memcmp(__s1
, __s2
, __n
); }
174 length(const char_type
* __s
)
175 { return strlen(__s
); }
177 static const char_type
*
178 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
179 { return static_cast<const char_type
*>(memchr(__s
, __a
, __n
)); }
182 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
183 { return static_cast<char_type
*>(memmove(__s1
, __s2
, __n
)); }
186 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
187 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
190 assign(char_type
* __s
, size_t __n
, char_type __a
)
191 { return static_cast<char_type
*>(memset(__s
, __a
, __n
)); }
194 to_char_type(const int_type
& __c
)
195 { return static_cast<char_type
>(__c
); }
197 // To keep both the byte 0xff and the eof symbol 0xffffffff
198 // from ending up as 0xffffffff.
200 to_int_type(const char_type
& __c
)
201 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
204 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
205 { return __c1
== __c2
; }
208 _S_get_state(const pos_type
& __pos
) { return __pos
.state(); }
211 eof() { return static_cast<int_type
>(EOF
); }
214 _S_eos() { return char_type(); }
217 not_eof(const int_type
& __c
)
218 { return (__c
== eof()) ? 0 : __c
; }
222 #ifdef _GLIBCPP_USE_WCHAR_T
224 struct char_traits
<wchar_t>
226 typedef wchar_t char_type
;
227 typedef wint_t int_type
;
228 typedef wstreamoff off_type
;
229 typedef wstreampos pos_type
;
230 typedef mbstate_t state_type
;
233 assign(char_type
& __c1
, const char_type
& __c2
)
237 eq(const char_type
& __c1
, const char_type
& __c2
)
238 { return __c1
== __c2
; }
241 lt(const char_type
& __c1
, const char_type
& __c2
)
242 { return __c1
< __c2
; }
245 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
246 { return wmemcmp(__s1
, __s2
, __n
); }
249 length(const char_type
* __s
)
250 { return wcslen(__s
); }
252 static const char_type
*
253 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
254 { return wmemchr(__s
, __a
, __n
); }
257 move(char_type
* __s1
, const char_type
* __s2
, int_type __n
)
258 { return wmemmove(__s1
, __s2
, __n
); }
261 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
262 { return wmemcpy(__s1
, __s2
, __n
); }
265 assign(char_type
* __s
, size_t __n
, char_type __a
)
266 { return wmemset(__s
, __a
, __n
); }
269 to_char_type(const int_type
& __c
) { return char_type(__c
); }
272 to_int_type(const char_type
& __c
) { return int_type(__c
); }
275 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
276 { return __c1
== __c2
; }
279 _S_get_state(const pos_type
& __pos
) { return __pos
.state(); }
282 eof() { return static_cast<int_type
>(WEOF
); }
285 _S_eos() { return char_type(); }
288 not_eof(const int_type
& __c
)
289 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
291 #endif //_GLIBCPP_USE_WCHAR_T
293 template<typename _CharT
, typename _Traits
>
294 struct _Char_traits_match
297 _Char_traits_match(_CharT
const& __c
) : _M_c(__c
) { }
300 operator()(_CharT
const& __a
) { return _Traits::eq(_M_c
,__a
); }
306 #endif /* _CPP_BITS_CHAR_TRAITS_H */