]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/char_traits.h
710e0eb6a534a5d56356e51eb8c4f64bbbab8b6d
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 /** @file char_traits.h
35 * This is an internal header file, included by other library headers.
36 * You should not attempt to use it directly.
39 #ifndef _CPP_BITS_CHAR_TRAITS_H
40 #define _CPP_BITS_CHAR_TRAITS_H 1
42 #pragma GCC system_header
44 #include <bits/std_cstring.h> // For memmove, memset, memchr
45 #include <bits/fpos.h> // For streampos
49 /// 21.1.2 Basis for explicit _Traits specialization
50 /// NB: That for any given actual character type this definition is
52 template<class _CharT
>
55 typedef _CharT char_type
;
56 // Unsigned as wint_t in unsigned.
57 typedef unsigned long int_type
;
58 typedef streampos pos_type
;
59 typedef streamoff off_type
;
60 typedef mbstate_t state_type
;
63 assign(char_type
& __c1
, const char_type
& __c2
)
67 eq(const char_type
& __c1
, const char_type
& __c2
)
68 { return __c1
== __c2
; }
71 lt(const char_type
& __c1
, const char_type
& __c2
)
72 { return __c1
< __c2
; }
75 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
77 for (size_t __i
= 0; __i
< __n
; ++__i
)
78 if (!eq(__s1
[__i
], __s2
[__i
]))
79 return lt(__s1
[__i
], __s2
[__i
]) ? -1 : 1;
84 length(const char_type
* __s
)
86 const char_type
* __p
= __s
;
91 static const char_type
*
92 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
94 for (const char_type
* __p
= __s
; size_t(__p
- __s
) < __n
; ++__p
)
95 if (*__p
== __a
) return __p
;
100 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
101 { return (char_type
*) memmove(__s1
, __s2
, __n
* sizeof(char_type
)); }
104 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
105 { return (char_type
*) memcpy(__s1
, __s2
, __n
* sizeof(char_type
)); }
108 assign(char_type
* __s
, size_t __n
, char_type __a
)
110 for (char_type
* __p
= __s
; __p
< __s
+ __n
; ++__p
)
116 to_char_type(const int_type
& __c
)
117 { return char_type(__c
); }
120 to_int_type(const char_type
& __c
) { return int_type(__c
); }
123 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
124 { return __c1
== __c2
; }
127 eof() { return static_cast<int_type
>(-1); }
130 not_eof(const int_type
& __c
)
131 { return eq_int_type(__c
, eof()) ? int_type(0) : __c
; }
135 /// 21.1.4 char_traits specializations
137 struct char_traits
<char>
139 typedef char char_type
;
140 typedef int int_type
;
141 typedef streampos pos_type
;
142 typedef streamoff off_type
;
143 typedef mbstate_t state_type
;
146 assign(char_type
& __c1
, const char_type
& __c2
)
150 eq(const char_type
& __c1
, const char_type
& __c2
)
151 { return __c1
== __c2
; }
154 lt(const char_type
& __c1
, const char_type
& __c2
)
155 { return __c1
< __c2
; }
158 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
159 { return memcmp(__s1
, __s2
, __n
); }
162 length(const char_type
* __s
)
163 { return strlen(__s
); }
165 static const char_type
*
166 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
167 { return static_cast<const char_type
*>(memchr(__s
, __a
, __n
)); }
170 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
171 { return static_cast<char_type
*>(memmove(__s1
, __s2
, __n
)); }
174 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
175 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
178 assign(char_type
* __s
, size_t __n
, char_type __a
)
179 { return static_cast<char_type
*>(memset(__s
, __a
, __n
)); }
182 to_char_type(const int_type
& __c
)
183 { return static_cast<char_type
>(__c
); }
185 // To keep both the byte 0xff and the eof symbol 0xffffffff
186 // from ending up as 0xffffffff.
188 to_int_type(const char_type
& __c
)
189 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
192 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
193 { return __c1
== __c2
; }
196 eof() { return static_cast<int_type
>(EOF
); }
199 not_eof(const int_type
& __c
)
200 { return (__c
== eof()) ? 0 : __c
; }
204 #ifdef _GLIBCPP_USE_WCHAR_T
206 struct char_traits
<wchar_t>
208 typedef wchar_t char_type
;
209 typedef wint_t int_type
;
210 typedef streamoff off_type
;
211 typedef wstreampos pos_type
;
212 typedef mbstate_t state_type
;
215 assign(char_type
& __c1
, const char_type
& __c2
)
219 eq(const char_type
& __c1
, const char_type
& __c2
)
220 { return __c1
== __c2
; }
223 lt(const char_type
& __c1
, const char_type
& __c2
)
224 { return __c1
< __c2
; }
227 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
228 { return wmemcmp(__s1
, __s2
, __n
); }
231 length(const char_type
* __s
)
232 { return wcslen(__s
); }
234 static const char_type
*
235 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
236 { return wmemchr(__s
, __a
, __n
); }
239 move(char_type
* __s1
, const char_type
* __s2
, int_type __n
)
240 { return wmemmove(__s1
, __s2
, __n
); }
243 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
244 { return wmemcpy(__s1
, __s2
, __n
); }
247 assign(char_type
* __s
, size_t __n
, char_type __a
)
248 { return wmemset(__s
, __a
, __n
); }
251 to_char_type(const int_type
& __c
) { return char_type(__c
); }
254 to_int_type(const char_type
& __c
) { return int_type(__c
); }
257 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
258 { return __c1
== __c2
; }
261 eof() { return static_cast<int_type
>(WEOF
); }
264 not_eof(const int_type
& __c
)
265 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
267 #endif //_GLIBCPP_USE_WCHAR_T
269 template<typename _CharT
, typename _Traits
>
270 struct _Char_traits_match
273 _Char_traits_match(_CharT
const& __c
) : _M_c(__c
) { }
276 operator()(_CharT
const& __a
) { return _Traits::eq(_M_c
, __a
); }