]>
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 #pragma GCC system_header
39 #include <bits/std_cwchar.h> // For mbstate_t.
40 #include <bits/std_cstring.h> // For memmove, memset, memchr
41 #include <bits/fpos.h> // For streamoff, streamsize
46 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
47 // Can't have self-recursive types for streampos.
48 // 21.1.3.1 char_traits sets size_type to streampos
50 // And here, where streampos is typedefed to fpos<traits::state_type>
51 typedef fpos
<mbstate_t> streampos
;
52 # ifdef _GLIBCPP_USE_WCHAR_T
53 typedef fpos
<mbstate_t> wstreampos
;
57 // 21.1.2 Basis for explicit _Traits specialization
58 // NB: That for any given actual character type this definition is
61 template<class _CharT
>
64 typedef _CharT char_type
;
65 // Unsigned as wint_t in unsigned.
66 typedef unsigned long int_type
;
67 typedef streampos pos_type
;
68 typedef streamoff off_type
;
69 typedef mbstate_t state_type
;
72 assign(char_type
& __c1
, const char_type
& __c2
)
76 eq(const char_type
& __c1
, const char_type
& __c2
)
77 { return __c1
== __c2
; }
80 lt(const char_type
& __c1
, const char_type
& __c2
)
81 { return __c1
< __c2
; }
84 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
86 for (size_t __i
= 0; __i
< __n
; ++__i
)
87 if (!eq(__s1
[__i
], __s2
[__i
]))
88 return lt(__s1
[__i
], __s2
[__i
]) ? -1 : 1;
93 length(const char_type
* __s
)
95 const char_type
* __p
= __s
;
100 static const char_type
*
101 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
103 for (const char_type
* __p
= __s
; size_t(__p
- __s
) < __n
; ++__p
)
104 if (*__p
== __a
) return __p
;
109 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
110 { return (char_type
*) memmove(__s1
, __s2
, __n
* sizeof(char_type
)); }
113 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
114 { return (char_type
*) memcpy(__s1
, __s2
, __n
* sizeof(char_type
)); }
117 assign(char_type
* __s
, size_t __n
, char_type __a
)
119 for (char_type
* __p
= __s
; __p
< __s
+ __n
; ++__p
)
125 to_char_type(const int_type
& __c
)
126 { return char_type(__c
); }
129 to_int_type(const char_type
& __c
) { return int_type(__c
); }
132 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
133 { return __c1
== __c2
; }
136 eof() { return static_cast<int_type
>(-1); }
139 not_eof(const int_type
& __c
)
140 { return eq_int_type(__c
, eof()) ? int_type(0) : __c
; }
144 // 21.1.4 char_traits specializations
146 struct char_traits
<char>
148 typedef char char_type
;
149 typedef int int_type
;
150 typedef streampos pos_type
;
151 typedef streamoff off_type
;
152 typedef mbstate_t state_type
;
155 assign(char_type
& __c1
, const char_type
& __c2
)
159 eq(const char_type
& __c1
, const char_type
& __c2
)
160 { return __c1
== __c2
; }
163 lt(const char_type
& __c1
, const char_type
& __c2
)
164 { return __c1
< __c2
; }
167 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
168 { return memcmp(__s1
, __s2
, __n
); }
171 length(const char_type
* __s
)
172 { return strlen(__s
); }
174 static const char_type
*
175 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
176 { return static_cast<const char_type
*>(memchr(__s
, __a
, __n
)); }
179 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
180 { return static_cast<char_type
*>(memmove(__s1
, __s2
, __n
)); }
183 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
184 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
187 assign(char_type
* __s
, size_t __n
, char_type __a
)
188 { return static_cast<char_type
*>(memset(__s
, __a
, __n
)); }
191 to_char_type(const int_type
& __c
)
192 { return static_cast<char_type
>(__c
); }
194 // To keep both the byte 0xff and the eof symbol 0xffffffff
195 // from ending up as 0xffffffff.
197 to_int_type(const char_type
& __c
)
198 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
201 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
202 { return __c1
== __c2
; }
205 eof() { return static_cast<int_type
>(EOF
); }
208 not_eof(const int_type
& __c
)
209 { return (__c
== eof()) ? 0 : __c
; }
213 #ifdef _GLIBCPP_USE_WCHAR_T
215 struct char_traits
<wchar_t>
217 typedef wchar_t char_type
;
218 typedef wint_t int_type
;
219 typedef wstreamoff off_type
;
220 typedef wstreampos pos_type
;
221 typedef mbstate_t state_type
;
224 assign(char_type
& __c1
, const char_type
& __c2
)
228 eq(const char_type
& __c1
, const char_type
& __c2
)
229 { return __c1
== __c2
; }
232 lt(const char_type
& __c1
, const char_type
& __c2
)
233 { return __c1
< __c2
; }
236 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
237 { return wmemcmp(__s1
, __s2
, __n
); }
240 length(const char_type
* __s
)
241 { return wcslen(__s
); }
243 static const char_type
*
244 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
245 { return wmemchr(__s
, __a
, __n
); }
248 move(char_type
* __s1
, const char_type
* __s2
, int_type __n
)
249 { return wmemmove(__s1
, __s2
, __n
); }
252 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
253 { return wmemcpy(__s1
, __s2
, __n
); }
256 assign(char_type
* __s
, size_t __n
, char_type __a
)
257 { return wmemset(__s
, __a
, __n
); }
260 to_char_type(const int_type
& __c
) { return char_type(__c
); }
263 to_int_type(const char_type
& __c
) { return int_type(__c
); }
266 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
267 { return __c1
== __c2
; }
270 eof() { return static_cast<int_type
>(WEOF
); }
273 not_eof(const int_type
& __c
)
274 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
276 #endif //_GLIBCPP_USE_WCHAR_T
278 template<typename _CharT
, typename _Traits
>
279 struct _Char_traits_match
282 _Char_traits_match(_CharT
const& __c
) : _M_c(__c
) { }
285 operator()(_CharT
const& __a
) { return _Traits::eq(_M_c
, __a
); }
291 #endif /* _CPP_BITS_CHAR_TRAITS_H */