]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/char_traits.h
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / char_traits.h
CommitLineData
725dc051
BK
1// Character Traits for use by standard string and iostream -*- C++ -*-
2
39b8cd70 3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
748086b7 4// 2006, 2007, 2008, 2009
f13a69ec 5// Free Software Foundation, Inc.
725dc051
BK
6//
7// This file is part of the GNU ISO C++ Library. This library is free
8// software; you can redistribute it and/or modify it under the
9// terms of the GNU General Public License as published by the
748086b7 10// Free Software Foundation; either version 3, or (at your option)
725dc051
BK
11// any later version.
12
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
748086b7
JJ
18// Under Section 7 of GPL version 3, you are granted additional
19// permissions described in the GCC Runtime Library Exception, version
20// 3.1, as published by the Free Software Foundation.
725dc051 21
748086b7
JJ
22// You should have received a copy of the GNU General Public License and
23// a copy of the GCC Runtime Library Exception along with this program;
24// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25// <http://www.gnu.org/licenses/>.
725dc051 26
729e3d3f
PE
27/** @file char_traits.h
28 * This is an internal header file, included by other library headers.
29 * You should not attempt to use it directly.
30 */
31
143c27b0
BK
32//
33// ISO C++ 14882: 21 Strings library
34//
35
3d7c150e
BK
36#ifndef _CHAR_TRAITS_H
37#define _CHAR_TRAITS_H 1
725dc051 38
b0a85b86
GDR
39#pragma GCC system_header
40
4f39bf5c 41#include <bits/stl_algobase.h> // std::copy, std::fill_n
39b8cd70 42#include <bits/postypes.h> // For streampos
39b8cd70 43#include <cwchar> // For WEOF, wmemmove, wmemset, etc.
725dc051 44
1814157e
PC
45#ifndef _GLIBCXX_STDIO_MACROS
46# include <cstdio> // For EOF
47# define _CHAR_TRAITS_EOF EOF
48#else
49# define _CHAR_TRAITS_EOF (-1)
50#endif
51
3cbc7af0
BK
52_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
53
aa2d5ba2 54 /**
d5ff4e3f 55 * @brief Mapping from character type to associated types.
ed6814f7 56 *
d5ff4e3f
MA
57 * @note This is an implementation class for the generic version
58 * of char_traits. It defines int_type, off_type, pos_type, and
59 * state_type. By default these are unsigned long, streamoff,
ed6814f7 60 * streampos, and mbstate_t. Users who need a different set of
d5ff4e3f
MA
61 * types, but who don't need to change the definitions of any function
62 * defined in char_traits, can specialize __gnu_cxx::_Char_types
63 * while leaving __gnu_cxx::char_traits alone. */
65be6ddd 64 template<typename _CharT>
d5ff4e3f
MA
65 struct _Char_types
66 {
67 typedef unsigned long int_type;
68 typedef std::streampos pos_type;
69 typedef std::streamoff off_type;
70 typedef std::mbstate_t state_type;
71 };
72
73
74 /**
75 * @brief Base class used to implement std::char_traits.
76 *
77 * @note For any given actual character type, this definition is
78 * probably wrong. (Most of the member functions are likely to be
79 * right, but the int_type and state_type typedefs, and the eof()
80 * member function, are likely to be wrong.) The reason this class
81 * exists is so users can specialize it. Classes in namespace std
28dac70a 82 * may not be specialized for fundamental types, but classes in
d5ff4e3f 83 * namespace __gnu_cxx may be.
51122a42 84 *
a40fff0e 85 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html
51122a42 86 * for advice on how to make use of this class for "unusual" character
6309eefc
BK
87 * types. Also, check out include/ext/pod_char_traits.h.
88 */
d5ff4e3f 89 template<typename _CharT>
725dc051
BK
90 struct char_traits
91 {
d5ff4e3f
MA
92 typedef _CharT char_type;
93 typedef typename _Char_types<_CharT>::int_type int_type;
94 typedef typename _Char_types<_CharT>::pos_type pos_type;
95 typedef typename _Char_types<_CharT>::off_type off_type;
96 typedef typename _Char_types<_CharT>::state_type state_type;
ed6814f7
BI
97
98 static void
d5ff4e3f
MA
99 assign(char_type& __c1, const char_type& __c2)
100 { __c1 = __c2; }
725dc051 101
ed6814f7 102 static bool
d5ff4e3f
MA
103 eq(const char_type& __c1, const char_type& __c2)
104 { return __c1 == __c2; }
725dc051 105
ed6814f7 106 static bool
d5ff4e3f
MA
107 lt(const char_type& __c1, const char_type& __c2)
108 { return __c1 < __c2; }
725dc051 109
ed6814f7 110 static int
d5ff4e3f 111 compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
725dc051 112
d5ff4e3f 113 static std::size_t
f13a69ec 114 length(const char_type* __s);
725dc051 115
ed6814f7 116 static const char_type*
d5ff4e3f 117 find(const char_type* __s, std::size_t __n, const char_type& __a);
725dc051 118
ed6814f7 119 static char_type*
d5ff4e3f 120 move(char_type* __s1, const char_type* __s2, std::size_t __n);
725dc051 121
ed6814f7 122 static char_type*
d5ff4e3f 123 copy(char_type* __s1, const char_type* __s2, std::size_t __n);
725dc051 124
ed6814f7 125 static char_type*
d5ff4e3f 126 assign(char_type* __s, std::size_t __n, char_type __a);
725dc051 127
ed6814f7 128 static char_type
d5ff4e3f
MA
129 to_char_type(const int_type& __c)
130 { return static_cast<char_type>(__c); }
725dc051 131
ed6814f7 132 static int_type
d5ff4e3f
MA
133 to_int_type(const char_type& __c)
134 { return static_cast<int_type>(__c); }
725dc051 135
ed6814f7 136 static bool
d5ff4e3f
MA
137 eq_int_type(const int_type& __c1, const int_type& __c2)
138 { return __c1 == __c2; }
725dc051 139
ed6814f7 140 static int_type
d5ff4e3f 141 eof()
1814157e 142 { return static_cast<int_type>(_CHAR_TRAITS_EOF); }
725dc051 143
ed6814f7 144 static int_type
d5ff4e3f 145 not_eof(const int_type& __c)
5a9ed693 146 { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
725dc051
BK
147 };
148
d5ff4e3f
MA
149 template<typename _CharT>
150 int
151 char_traits<_CharT>::
152 compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
153 {
05a2763e 154 for (std::size_t __i = 0; __i < __n; ++__i)
d5ff4e3f
MA
155 if (lt(__s1[__i], __s2[__i]))
156 return -1;
157 else if (lt(__s2[__i], __s1[__i]))
158 return 1;
159 return 0;
160 }
161
162 template<typename _CharT>
163 std::size_t
164 char_traits<_CharT>::
165 length(const char_type* __p)
166 {
167 std::size_t __i = 0;
168 while (!eq(__p[__i], char_type()))
169 ++__i;
170 return __i;
171 }
172
173 template<typename _CharT>
ed6814f7 174 const typename char_traits<_CharT>::char_type*
d5ff4e3f
MA
175 char_traits<_CharT>::
176 find(const char_type* __s, std::size_t __n, const char_type& __a)
177 {
178 for (std::size_t __i = 0; __i < __n; ++__i)
179 if (eq(__s[__i], __a))
180 return __s + __i;
181 return 0;
182 }
183
184 template<typename _CharT>
185 typename char_traits<_CharT>::char_type*
186 char_traits<_CharT>::
187 move(char_type* __s1, const char_type* __s2, std::size_t __n)
188 {
538075fe
PC
189 return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
190 __n * sizeof(char_type)));
d5ff4e3f
MA
191 }
192
193 template<typename _CharT>
ed6814f7 194 typename char_traits<_CharT>::char_type*
d5ff4e3f
MA
195 char_traits<_CharT>::
196 copy(char_type* __s1, const char_type* __s2, std::size_t __n)
197 {
c2ba9709 198 // NB: Inline std::copy so no recursive dependencies.
d5ff4e3f
MA
199 std::copy(__s2, __s2 + __n, __s1);
200 return __s1;
201 }
202
203 template<typename _CharT>
ed6814f7 204 typename char_traits<_CharT>::char_type*
d5ff4e3f
MA
205 char_traits<_CharT>::
206 assign(char_type* __s, std::size_t __n, char_type __a)
207 {
c2ba9709 208 // NB: Inline std::fill_n so no recursive dependencies.
d5ff4e3f
MA
209 std::fill_n(__s, __n, __a);
210 return __s;
211 }
d5ff4e3f 212
3cbc7af0
BK
213_GLIBCXX_END_NAMESPACE
214
215_GLIBCXX_BEGIN_NAMESPACE(std)
216
d5ff4e3f
MA
217 // 21.1
218 /**
219 * @brief Basis for explicit traits specializations.
220 *
221 * @note For any given actual character type, this definition is
222 * probably wrong. Since this is just a thin wrapper around
223 * __gnu_cxx::char_traits, it is possible to achieve a more
224 * appropriate definition by specializing __gnu_cxx::char_traits.
225 *
a40fff0e 226 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html
d5ff4e3f
MA
227 * for advice on how to make use of this class for "unusual" character
228 * types. Also, check out include/ext/pod_char_traits.h.
229 */
230 template<class _CharT>
6309eefc 231 struct char_traits : public __gnu_cxx::char_traits<_CharT>
d5ff4e3f
MA
232 { };
233
97644827 234
939759fc 235 /// 21.1.3.1 char_traits specializations
725dc051
BK
236 template<>
237 struct char_traits<char>
238 {
d5ff4e3f
MA
239 typedef char char_type;
240 typedef int int_type;
241 typedef streampos pos_type;
242 typedef streamoff off_type;
243 typedef mbstate_t state_type;
725dc051 244
ed6814f7 245 static void
725dc051
BK
246 assign(char_type& __c1, const char_type& __c2)
247 { __c1 = __c2; }
248
ed6814f7 249 static bool
725dc051 250 eq(const char_type& __c1, const char_type& __c2)
462ec415 251 { return __c1 == __c2; }
725dc051 252
ed6814f7 253 static bool
725dc051
BK
254 lt(const char_type& __c1, const char_type& __c2)
255 { return __c1 < __c2; }
256
ed6814f7 257 static int
725dc051 258 compare(const char_type* __s1, const char_type* __s2, size_t __n)
538075fe 259 { return __builtin_memcmp(__s1, __s2, __n); }
725dc051
BK
260
261 static size_t
262 length(const char_type* __s)
538075fe 263 { return __builtin_strlen(__s); }
725dc051 264
ed6814f7 265 static const char_type*
725dc051 266 find(const char_type* __s, size_t __n, const char_type& __a)
538075fe 267 { return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n)); }
725dc051 268
ed6814f7 269 static char_type*
725dc051 270 move(char_type* __s1, const char_type* __s2, size_t __n)
538075fe 271 { return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n)); }
725dc051 272
ed6814f7 273 static char_type*
725dc051 274 copy(char_type* __s1, const char_type* __s2, size_t __n)
538075fe 275 { return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n)); }
725dc051 276
ed6814f7 277 static char_type*
725dc051 278 assign(char_type* __s, size_t __n, char_type __a)
538075fe 279 { return static_cast<char_type*>(__builtin_memset(__s, __a, __n)); }
725dc051 280
ed6814f7 281 static char_type
725dc051
BK
282 to_char_type(const int_type& __c)
283 { return static_cast<char_type>(__c); }
284
285 // To keep both the byte 0xff and the eof symbol 0xffffffff
286 // from ending up as 0xffffffff.
ed6814f7 287 static int_type
725dc051
BK
288 to_int_type(const char_type& __c)
289 { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
290
ed6814f7 291 static bool
725dc051
BK
292 eq_int_type(const int_type& __c1, const int_type& __c2)
293 { return __c1 == __c2; }
294
ed6814f7 295 static int_type
2fb63453
PC
296 eof()
297 { return static_cast<int_type>(_CHAR_TRAITS_EOF); }
725dc051 298
ed6814f7 299 static int_type
725dc051
BK
300 not_eof(const int_type& __c)
301 { return (__c == eof()) ? 0 : __c; }
302 };
303
304
3d7c150e 305#ifdef _GLIBCXX_USE_WCHAR_T
939759fc 306 /// 21.1.3.2 char_traits specializations
725dc051
BK
307 template<>
308 struct char_traits<wchar_t>
309 {
d5ff4e3f
MA
310 typedef wchar_t char_type;
311 typedef wint_t int_type;
312 typedef streamoff off_type;
313 typedef wstreampos pos_type;
314 typedef mbstate_t state_type;
ed6814f7
BI
315
316 static void
725dc051
BK
317 assign(char_type& __c1, const char_type& __c2)
318 { __c1 = __c2; }
319
ed6814f7 320 static bool
462ec415
JJ
321 eq(const char_type& __c1, const char_type& __c2)
322 { return __c1 == __c2; }
725dc051 323
ed6814f7 324 static bool
725dc051
BK
325 lt(const char_type& __c1, const char_type& __c2)
326 { return __c1 < __c2; }
327
ed6814f7 328 static int
725dc051
BK
329 compare(const char_type* __s1, const char_type* __s2, size_t __n)
330 { return wmemcmp(__s1, __s2, __n); }
331
332 static size_t
333 length(const char_type* __s)
334 { return wcslen(__s); }
335
ed6814f7 336 static const char_type*
725dc051
BK
337 find(const char_type* __s, size_t __n, const char_type& __a)
338 { return wmemchr(__s, __a, __n); }
339
ed6814f7 340 static char_type*
73a530bd 341 move(char_type* __s1, const char_type* __s2, size_t __n)
725dc051
BK
342 { return wmemmove(__s1, __s2, __n); }
343
ed6814f7 344 static char_type*
725dc051
BK
345 copy(char_type* __s1, const char_type* __s2, size_t __n)
346 { return wmemcpy(__s1, __s2, __n); }
347
ed6814f7 348 static char_type*
725dc051
BK
349 assign(char_type* __s, size_t __n, char_type __a)
350 { return wmemset(__s, __a, __n); }
351
ed6814f7 352 static char_type
2fb63453
PC
353 to_char_type(const int_type& __c)
354 { return char_type(__c); }
725dc051 355
ed6814f7 356 static int_type
2fb63453
PC
357 to_int_type(const char_type& __c)
358 { return int_type(__c); }
725dc051 359
ed6814f7 360 static bool
725dc051
BK
361 eq_int_type(const int_type& __c1, const int_type& __c2)
362 { return __c1 == __c2; }
363
ed6814f7 364 static int_type
2fb63453
PC
365 eof()
366 { return static_cast<int_type>(WEOF); }
725dc051 367
ed6814f7 368 static int_type
725dc051
BK
369 not_eof(const int_type& __c)
370 { return eq_int_type(__c, eof()) ? 0 : __c; }
371 };
3d7c150e 372#endif //_GLIBCXX_USE_WCHAR_T
725dc051 373
3cbc7af0 374_GLIBCXX_END_NAMESPACE
725dc051 375
5e44d591
PC
376#if (defined(__GXX_EXPERIMENTAL_CXX0X__) \
377 && defined(_GLIBCXX_USE_C99_STDINT_TR1))
378
379#include <cstdint>
380
381_GLIBCXX_BEGIN_NAMESPACE(std)
382
383 template<>
384 struct char_traits<char16_t>
385 {
386 typedef char16_t char_type;
387 typedef uint_least16_t int_type;
388 typedef streamoff off_type;
389 typedef u16streampos pos_type;
390 typedef mbstate_t state_type;
391
392 static void
393 assign(char_type& __c1, const char_type& __c2)
394 { __c1 = __c2; }
395
396 static bool
397 eq(const char_type& __c1, const char_type& __c2)
398 { return __c1 == __c2; }
399
400 static bool
401 lt(const char_type& __c1, const char_type& __c2)
402 { return __c1 < __c2; }
403
404 static int
405 compare(const char_type* __s1, const char_type* __s2, size_t __n)
406 {
407 for (size_t __i = 0; __i < __n; ++__i)
408 if (lt(__s1[__i], __s2[__i]))
409 return -1;
410 else if (lt(__s2[__i], __s1[__i]))
411 return 1;
412 return 0;
413 }
414
415 static size_t
416 length(const char_type* __s)
417 {
418 size_t __i = 0;
419 while (!eq(__s[__i], char_type()))
420 ++__i;
421 return __i;
422 }
423
424 static const char_type*
425 find(const char_type* __s, size_t __n, const char_type& __a)
426 {
427 for (size_t __i = 0; __i < __n; ++__i)
428 if (eq(__s[__i], __a))
429 return __s + __i;
430 return 0;
431 }
432
433 static char_type*
434 move(char_type* __s1, const char_type* __s2, size_t __n)
435 {
436 return (static_cast<char_type*>
437 (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
438 }
439
440 static char_type*
441 copy(char_type* __s1, const char_type* __s2, size_t __n)
442 {
443 return (static_cast<char_type*>
444 (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
445 }
446
447 static char_type*
448 assign(char_type* __s, size_t __n, char_type __a)
2fb63453
PC
449 {
450 for (size_t __i = 0; __i < __n; ++__i)
451 assign(__s[__i], __a);
5e44d591
PC
452 return __s;
453 }
454
455 static char_type
2fb63453
PC
456 to_char_type(const int_type& __c)
457 { return char_type(__c); }
5e44d591
PC
458
459 static int_type
2fb63453
PC
460 to_int_type(const char_type& __c)
461 { return int_type(__c); }
5e44d591
PC
462
463 static bool
464 eq_int_type(const int_type& __c1, const int_type& __c2)
465 { return __c1 == __c2; }
466
467 static int_type
2fb63453
PC
468 eof()
469 { return static_cast<int_type>(-1); }
5e44d591
PC
470
471 static int_type
472 not_eof(const int_type& __c)
473 { return eq_int_type(__c, eof()) ? 0 : __c; }
474 };
475
476 template<>
477 struct char_traits<char32_t>
478 {
479 typedef char32_t char_type;
480 typedef uint_least32_t int_type;
481 typedef streamoff off_type;
482 typedef u32streampos pos_type;
483 typedef mbstate_t state_type;
484
485 static void
486 assign(char_type& __c1, const char_type& __c2)
487 { __c1 = __c2; }
488
489 static bool
490 eq(const char_type& __c1, const char_type& __c2)
491 { return __c1 == __c2; }
492
493 static bool
494 lt(const char_type& __c1, const char_type& __c2)
495 { return __c1 < __c2; }
496
497 static int
498 compare(const char_type* __s1, const char_type* __s2, size_t __n)
499 {
500 for (size_t __i = 0; __i < __n; ++__i)
501 if (lt(__s1[__i], __s2[__i]))
502 return -1;
503 else if (lt(__s2[__i], __s1[__i]))
504 return 1;
505 return 0;
506 }
507
508 static size_t
509 length(const char_type* __s)
510 {
511 size_t __i = 0;
512 while (!eq(__s[__i], char_type()))
513 ++__i;
514 return __i;
515 }
516
517 static const char_type*
518 find(const char_type* __s, size_t __n, const char_type& __a)
519 {
520 for (size_t __i = 0; __i < __n; ++__i)
521 if (eq(__s[__i], __a))
522 return __s + __i;
523 return 0;
524 }
525
526 static char_type*
527 move(char_type* __s1, const char_type* __s2, size_t __n)
528 {
529 return (static_cast<char_type*>
530 (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
531 }
532
533 static char_type*
534 copy(char_type* __s1, const char_type* __s2, size_t __n)
535 {
536 return (static_cast<char_type*>
537 (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
538 }
539
540 static char_type*
541 assign(char_type* __s, size_t __n, char_type __a)
542 {
2fb63453
PC
543 for (size_t __i = 0; __i < __n; ++__i)
544 assign(__s[__i], __a);
5e44d591
PC
545 return __s;
546 }
547
548 static char_type
2fb63453
PC
549 to_char_type(const int_type& __c)
550 { return char_type(__c); }
5e44d591
PC
551
552 static int_type
2fb63453
PC
553 to_int_type(const char_type& __c)
554 { return int_type(__c); }
5e44d591
PC
555
556 static bool
557 eq_int_type(const int_type& __c1, const int_type& __c2)
558 { return __c1 == __c2; }
559
560 static int_type
2fb63453
PC
561 eof()
562 { return static_cast<int_type>(-1); }
5e44d591
PC
563
564 static int_type
565 not_eof(const int_type& __c)
566 { return eq_int_type(__c, eof()) ? 0 : __c; }
567 };
568
569_GLIBCXX_END_NAMESPACE
570
571#endif
572
1de853b1
PC
573#undef _CHAR_TRAITS_EOF
574
5e44d591 575#endif // _CHAR_TRAITS_H