]>
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, 2002
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 21 Strings library
35 /** @file char_traits.h
36 * This is an internal header file, included by other library headers.
37 * You should not attempt to use it directly.
40 #ifndef _CHAR_TRAITS_H
41 #define _CHAR_TRAITS_H 1
43 #pragma GCC system_header
45 #include <cstring> // For memmove, memset, memchr
46 #include <bits/fpos.h> // For streampos
52 * @brief Basis for explicit traits specializations.
54 * @note For any given actual character type, this definition is
57 * See http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#5
58 * for advice on how to make use of this class for "unusual" character
59 * types. Also, check out include/ext/pod_char_traits.h.
61 template<class _CharT
>
64 typedef _CharT char_type
;
65 typedef unsigned long int_type
;
66 typedef streampos pos_type
;
67 typedef streamoff off_type
;
68 typedef mbstate_t state_type
;
71 assign(char_type
& __c1
, const char_type
& __c2
);
74 eq(const char_type
& __c1
, const char_type
& __c2
);
77 lt(const char_type
& __c1
, const char_type
& __c2
);
80 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
);
83 length(const char_type
* __s
);
85 static const char_type
*
86 find(const char_type
* __s
, size_t __n
, const char_type
& __a
);
89 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
);
92 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
);
95 assign(char_type
* __s
, size_t __n
, char_type __a
);
98 to_char_type(const int_type
& __c
);
101 to_int_type(const char_type
& __c
);
104 eq_int_type(const int_type
& __c1
, const int_type
& __c2
);
110 not_eof(const int_type
& __c
);
114 /// 21.1.3.1 char_traits specializations
116 struct char_traits
<char>
118 typedef char char_type
;
119 typedef int int_type
;
120 typedef streampos pos_type
;
121 typedef streamoff off_type
;
122 typedef mbstate_t state_type
;
125 assign(char_type
& __c1
, const char_type
& __c2
)
129 eq(const char_type
& __c1
, const char_type
& __c2
)
130 { return __c1
== __c2
; }
133 lt(const char_type
& __c1
, const char_type
& __c2
)
134 { return __c1
< __c2
; }
137 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
138 { return memcmp(__s1
, __s2
, __n
); }
141 length(const char_type
* __s
)
142 { return strlen(__s
); }
144 static const char_type
*
145 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
146 { return static_cast<const char_type
*>(memchr(__s
, __a
, __n
)); }
149 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
150 { return static_cast<char_type
*>(memmove(__s1
, __s2
, __n
)); }
153 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
154 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
157 assign(char_type
* __s
, size_t __n
, char_type __a
)
158 { return static_cast<char_type
*>(memset(__s
, __a
, __n
)); }
161 to_char_type(const int_type
& __c
)
162 { return static_cast<char_type
>(__c
); }
164 // To keep both the byte 0xff and the eof symbol 0xffffffff
165 // from ending up as 0xffffffff.
167 to_int_type(const char_type
& __c
)
168 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
171 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
172 { return __c1
== __c2
; }
175 eof() { return static_cast<int_type
>(EOF
); }
178 not_eof(const int_type
& __c
)
179 { return (__c
== eof()) ? 0 : __c
; }
183 #ifdef _GLIBCXX_USE_WCHAR_T
184 /// 21.1.3.2 char_traits specializations
186 struct char_traits
<wchar_t>
188 typedef wchar_t char_type
;
189 typedef wint_t int_type
;
190 typedef streamoff off_type
;
191 typedef wstreampos pos_type
;
192 typedef mbstate_t state_type
;
195 assign(char_type
& __c1
, const char_type
& __c2
)
199 eq(const char_type
& __c1
, const char_type
& __c2
)
200 { return __c1
== __c2
; }
203 lt(const char_type
& __c1
, const char_type
& __c2
)
204 { return __c1
< __c2
; }
207 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
208 { return wmemcmp(__s1
, __s2
, __n
); }
211 length(const char_type
* __s
)
212 { return wcslen(__s
); }
214 static const char_type
*
215 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
216 { return wmemchr(__s
, __a
, __n
); }
219 move(char_type
* __s1
, const char_type
* __s2
, int_type __n
)
220 { return wmemmove(__s1
, __s2
, __n
); }
223 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
224 { return wmemcpy(__s1
, __s2
, __n
); }
227 assign(char_type
* __s
, size_t __n
, char_type __a
)
228 { return wmemset(__s
, __a
, __n
); }
231 to_char_type(const int_type
& __c
) { return char_type(__c
); }
234 to_int_type(const char_type
& __c
) { return int_type(__c
); }
237 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
238 { return __c1
== __c2
; }
241 eof() { return static_cast<int_type
>(WEOF
); }
244 not_eof(const int_type
& __c
)
245 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
247 #endif //_GLIBCXX_USE_WCHAR_T
249 template<typename _CharT
, typename _Traits
>
250 struct _Char_traits_match
253 _Char_traits_match(_CharT
const& __c
) : _M_c(__c
) { }
256 operator()(_CharT
const& __a
) { return _Traits::eq(_M_c
, __a
); }