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 _CPP_BITS_CHAR_TRAITS_H
41 #define _CPP_BITS_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
61 template<class _CharT
>
64 typedef _CharT char_type
;
65 // Unsigned as wint_t is 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
);
75 eq(const char_type
& __c1
, const char_type
& __c2
);
78 lt(const char_type
& __c1
, const char_type
& __c2
);
81 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
);
84 length(const char_type
* __s
);
86 static const char_type
*
87 find(const char_type
* __s
, size_t __n
, const char_type
& __a
);
90 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
);
93 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
);
96 assign(char_type
* __s
, size_t __n
, char_type __a
);
99 to_char_type(const int_type
& __c
);
102 to_int_type(const char_type
& __c
);
105 eq_int_type(const int_type
& __c1
, const int_type
& __c2
);
111 not_eof(const int_type
& __c
);
115 /// 21.1.3.1 char_traits specializations
117 struct char_traits
<char>
119 typedef char char_type
;
120 typedef int int_type
;
121 typedef streampos pos_type
;
122 typedef streamoff off_type
;
123 typedef mbstate_t state_type
;
126 assign(char_type
& __c1
, const char_type
& __c2
)
130 eq(const char_type
& __c1
, const char_type
& __c2
)
131 { return __c1
== __c2
; }
134 lt(const char_type
& __c1
, const char_type
& __c2
)
135 { return __c1
< __c2
; }
138 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
139 { return memcmp(__s1
, __s2
, __n
); }
142 length(const char_type
* __s
)
143 { return strlen(__s
); }
145 static const char_type
*
146 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
147 { return static_cast<const char_type
*>(memchr(__s
, __a
, __n
)); }
150 move(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
151 { return static_cast<char_type
*>(memmove(__s1
, __s2
, __n
)); }
154 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
155 { return static_cast<char_type
*>(memcpy(__s1
, __s2
, __n
)); }
158 assign(char_type
* __s
, size_t __n
, char_type __a
)
159 { return static_cast<char_type
*>(memset(__s
, __a
, __n
)); }
162 to_char_type(const int_type
& __c
)
163 { return static_cast<char_type
>(__c
); }
165 // To keep both the byte 0xff and the eof symbol 0xffffffff
166 // from ending up as 0xffffffff.
168 to_int_type(const char_type
& __c
)
169 { return static_cast<int_type
>(static_cast<unsigned char>(__c
)); }
172 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
173 { return __c1
== __c2
; }
176 eof() { return static_cast<int_type
>(EOF
); }
179 not_eof(const int_type
& __c
)
180 { return (__c
== eof()) ? 0 : __c
; }
184 #ifdef _GLIBCPP_USE_WCHAR_T
185 /// 21.1.3.2 char_traits specializations
187 struct char_traits
<wchar_t>
189 typedef wchar_t char_type
;
190 typedef wint_t int_type
;
191 typedef streamoff off_type
;
192 typedef wstreampos pos_type
;
193 typedef mbstate_t state_type
;
196 assign(char_type
& __c1
, const char_type
& __c2
)
200 eq(const char_type
& __c1
, const char_type
& __c2
)
201 { return __c1
== __c2
; }
204 lt(const char_type
& __c1
, const char_type
& __c2
)
205 { return __c1
< __c2
; }
208 compare(const char_type
* __s1
, const char_type
* __s2
, size_t __n
)
209 { return wmemcmp(__s1
, __s2
, __n
); }
212 length(const char_type
* __s
)
213 { return wcslen(__s
); }
215 static const char_type
*
216 find(const char_type
* __s
, size_t __n
, const char_type
& __a
)
217 { return wmemchr(__s
, __a
, __n
); }
220 move(char_type
* __s1
, const char_type
* __s2
, int_type __n
)
221 { return wmemmove(__s1
, __s2
, __n
); }
224 copy(char_type
* __s1
, const char_type
* __s2
, size_t __n
)
225 { return wmemcpy(__s1
, __s2
, __n
); }
228 assign(char_type
* __s
, size_t __n
, char_type __a
)
229 { return wmemset(__s
, __a
, __n
); }
232 to_char_type(const int_type
& __c
) { return char_type(__c
); }
235 to_int_type(const char_type
& __c
) { return int_type(__c
); }
238 eq_int_type(const int_type
& __c1
, const int_type
& __c2
)
239 { return __c1
== __c2
; }
242 eof() { return static_cast<int_type
>(WEOF
); }
245 not_eof(const int_type
& __c
)
246 { return eq_int_type(__c
, eof()) ? 0 : __c
; }
248 #endif //_GLIBCPP_USE_WCHAR_T
250 template<typename _CharT
, typename _Traits
>
251 struct _Char_traits_match
254 _Char_traits_match(_CharT
const& __c
) : _M_c(__c
) { }
257 operator()(_CharT
const& __a
) { return _Traits::eq(_M_c
, __a
); }