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 <cstring> // For memmove, memset, memchr
45 #include <bits/fpos.h> // For streampos
51 * @brief Basis for explicit traits specializations.
53 * @note For any given actual character type, this definition is
56 template<class _CharT
>
59 typedef _CharT char_type
;
60 // Unsigned as wint_t in unsigned.
61 typedef unsigned long int_type
;
62 typedef streampos pos_type
;
63 typedef streamoff off_type
;
64 typedef mbstate_t state_type
;
67 assign(char_type
& __c1
, const char_type
& __c2
)
71 eq(const char_type
& __c1
, const char_type
& __c2
)
72 { return __c1
== __c2
; }
75 lt(const char_type
& __c1
, const char_type
& __c2
)
76 { return __c1
< __c2
; }
79 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
81 for (size_t __i
= 0; __i
< __n
; ++__i
)
82 if (!eq(__s1
[__i
], __s2
[__i
]))
83 return lt(__s1
[__i
], __s2
[__i
]) ? -1 : 1;
88 length(const char_type
* __s
)
90 const char_type
* __p
= __s
;
95 static const char_type
*
96 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
98 for (const char_type
* __p
= __s
; size_t(__p
- __s
) < __n
; ++__p
)
99 if (*__p
== __a
) return __p
;
104 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
105 { return (char_type
*) memmove(__s1
, __s2
, __n
* sizeof(char_type
)); }
108 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
109 { return (char_type
*) memcpy(__s1
, __s2
, __n
* sizeof(char_type
)); }
112 assign(char_type
* __s
, size_t __n
, char_type __a
)
114 for (char_type
* __p
= __s
; __p
< __s
+ __n
; ++__p
)
120 to_char_type(const int_type
& __c
)
121 { return char_type(__c
); }
124 to_int_type(const char_type
& __c
) { return int_type(__c
); }
127 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
128 { return __c1
== __c2
; }
131 eof() { return static_cast<int_type
>(-1); }
134 not_eof(const int_type
& __c
)
135 { return eq_int_type(__c
, eof()) ? int_type(0) : __c
; }
139 /// 21.1.4 char_traits specializations
141 struct char_traits
<char>
143 typedef char char_type
;
144 typedef int int_type
;
145 typedef streampos pos_type
;
146 typedef streamoff off_type
;
147 typedef mbstate_t state_type
;
150 assign(char_type
& __c1
, const char_type
& __c2
)
154 eq(const char_type
& __c1
, const char_type
& __c2
)
155 { return __c1
== __c2
; }
158 lt(const char_type
& __c1
, const char_type
& __c2
)
159 { return __c1
< __c2
; }
162 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
163 { return memcmp(__s1
, __s2
, __n
); }
166 length(const char_type
* __s
)
167 { return strlen(__s
); }
169 static const char_type
*
170 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
171 { return static_cast<const char_type
*>(memchr(__s
, __a
, __n
)); }
174 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
175 { return static_cast<char_type
*>(memmove(__s1
, __s2
, __n
)); }
178 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
179 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
182 assign(char_type
* __s
, size_t __n
, char_type __a
)
183 { return static_cast<char_type
*>(memset(__s
, __a
, __n
)); }
186 to_char_type(const int_type
& __c
)
187 { return static_cast<char_type
>(__c
); }
189 // To keep both the byte 0xff and the eof symbol 0xffffffff
190 // from ending up as 0xffffffff.
192 to_int_type(const char_type
& __c
)
193 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
196 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
197 { return __c1
== __c2
; }
200 eof() { return static_cast<int_type
>(EOF
); }
203 not_eof(const int_type
& __c
)
204 { return (__c
== eof()) ? 0 : __c
; }
208 #ifdef _GLIBCPP_USE_WCHAR_T
210 struct char_traits
<wchar_t>
212 typedef wchar_t char_type
;
213 typedef wint_t int_type
;
214 typedef streamoff off_type
;
215 typedef wstreampos pos_type
;
216 typedef mbstate_t state_type
;
219 assign(char_type
& __c1
, const char_type
& __c2
)
223 eq(const char_type
& __c1
, const char_type
& __c2
)
224 { return __c1
== __c2
; }
227 lt(const char_type
& __c1
, const char_type
& __c2
)
228 { return __c1
< __c2
; }
231 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
232 { return wmemcmp(__s1
, __s2
, __n
); }
235 length(const char_type
* __s
)
236 { return wcslen(__s
); }
238 static const char_type
*
239 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
240 { return wmemchr(__s
, __a
, __n
); }
243 move(char_type
* __s1
, const char_type
* __s2
, int_type __n
)
244 { return wmemmove(__s1
, __s2
, __n
); }
247 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
248 { return wmemcpy(__s1
, __s2
, __n
); }
251 assign(char_type
* __s
, size_t __n
, char_type __a
)
252 { return wmemset(__s
, __a
, __n
); }
255 to_char_type(const int_type
& __c
) { return char_type(__c
); }
258 to_int_type(const char_type
& __c
) { return int_type(__c
); }
261 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
262 { return __c1
== __c2
; }
265 eof() { return static_cast<int_type
>(WEOF
); }
268 not_eof(const int_type
& __c
)
269 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
271 #endif //_GLIBCPP_USE_WCHAR_T
273 template<typename _CharT
, typename _Traits
>
274 struct _Char_traits_match
277 _Char_traits_match(_CharT
const& __c
) : _M_c(__c
) { }
280 operator()(_CharT
const& __a
) { return _Traits::eq(_M_c
, __a
); }