]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/locale_facets.h
c++config (_GLIBCXX_EXPORT_TEMPLATE): Remove.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / locale_facets.h
1 // Locale support -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011
5 // Free Software Foundation, Inc.
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
10 // Free Software Foundation; either version 3, or (at your option)
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
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.
21
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/>.
26
27 /** @file bits/locale_facets.h
28 * This is an internal header file, included by other library headers.
29 * Do not attempt to use it directly. @headername{locale}
30 */
31
32 //
33 // ISO C++ 14882: 22.1 Locales
34 //
35
36 #ifndef _LOCALE_FACETS_H
37 #define _LOCALE_FACETS_H 1
38
39 #pragma GCC system_header
40
41 #include <cwctype> // For wctype_t
42 #include <cctype>
43 #include <bits/ctype_base.h>
44 #include <iosfwd>
45 #include <bits/ios_base.h> // For ios_base, ios_base::iostate
46 #include <streambuf>
47 #include <bits/cpp_type_traits.h>
48 #include <ext/type_traits.h>
49 #include <ext/numeric_traits.h>
50 #include <bits/streambuf_iterator.h>
51
52 _GLIBCXX_BEGIN_NAMESPACE(std)
53
54 // NB: Don't instantiate required wchar_t facets if no wchar_t support.
55 #ifdef _GLIBCXX_USE_WCHAR_T
56 # define _GLIBCXX_NUM_FACETS 28
57 #else
58 # define _GLIBCXX_NUM_FACETS 14
59 #endif
60
61 // Convert string to numeric value of type _Tp and store results.
62 // NB: This is specialized for all required types, there is no
63 // generic definition.
64 template<typename _Tp>
65 void
66 __convert_to_v(const char*, _Tp&, ios_base::iostate&,
67 const __c_locale&) throw();
68
69 // Explicit specializations for required types.
70 template<>
71 void
72 __convert_to_v(const char*, float&, ios_base::iostate&,
73 const __c_locale&) throw();
74
75 template<>
76 void
77 __convert_to_v(const char*, double&, ios_base::iostate&,
78 const __c_locale&) throw();
79
80 template<>
81 void
82 __convert_to_v(const char*, long double&, ios_base::iostate&,
83 const __c_locale&) throw();
84
85 // NB: __pad is a struct, rather than a function, so it can be
86 // partially-specialized.
87 template<typename _CharT, typename _Traits>
88 struct __pad
89 {
90 static void
91 _S_pad(ios_base& __io, _CharT __fill, _CharT* __news,
92 const _CharT* __olds, streamsize __newlen, streamsize __oldlen);
93 };
94
95 // Used by both numeric and monetary facets.
96 // Inserts "group separator" characters into an array of characters.
97 // It's recursive, one iteration per group. It moves the characters
98 // in the buffer this way: "xxxx12345" -> "12,345xxx". Call this
99 // only with __gsize != 0.
100 template<typename _CharT>
101 _CharT*
102 __add_grouping(_CharT* __s, _CharT __sep,
103 const char* __gbeg, size_t __gsize,
104 const _CharT* __first, const _CharT* __last);
105
106 // This template permits specializing facet output code for
107 // ostreambuf_iterator. For ostreambuf_iterator, sputn is
108 // significantly more efficient than incrementing iterators.
109 template<typename _CharT>
110 inline
111 ostreambuf_iterator<_CharT>
112 __write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len)
113 {
114 __s._M_put(__ws, __len);
115 return __s;
116 }
117
118 // This is the unspecialized form of the template.
119 template<typename _CharT, typename _OutIter>
120 inline
121 _OutIter
122 __write(_OutIter __s, const _CharT* __ws, int __len)
123 {
124 for (int __j = 0; __j < __len; __j++, ++__s)
125 *__s = __ws[__j];
126 return __s;
127 }
128
129
130 // 22.2.1.1 Template class ctype
131 // Include host and configuration specific ctype enums for ctype_base.
132
133 /**
134 * @brief Common base for ctype facet
135 *
136 * This template class provides implementations of the public functions
137 * that forward to the protected virtual functions.
138 *
139 * This template also provides abstract stubs for the protected virtual
140 * functions.
141 */
142 template<typename _CharT>
143 class __ctype_abstract_base : public locale::facet, public ctype_base
144 {
145 public:
146 // Types:
147 /// Typedef for the template parameter
148 typedef _CharT char_type;
149
150 /**
151 * @brief Test char_type classification.
152 *
153 * This function finds a mask M for @a c and compares it to mask @a m.
154 * It does so by returning the value of ctype<char_type>::do_is().
155 *
156 * @param c The char_type to compare the mask of.
157 * @param m The mask to compare against.
158 * @return (M & m) != 0.
159 */
160 bool
161 is(mask __m, char_type __c) const
162 { return this->do_is(__m, __c); }
163
164 /**
165 * @brief Return a mask array.
166 *
167 * This function finds the mask for each char_type in the range [lo,hi)
168 * and successively writes it to vec. vec must have as many elements
169 * as the char array. It does so by returning the value of
170 * ctype<char_type>::do_is().
171 *
172 * @param lo Pointer to start of range.
173 * @param hi Pointer to end of range.
174 * @param vec Pointer to an array of mask storage.
175 * @return @a hi.
176 */
177 const char_type*
178 is(const char_type *__lo, const char_type *__hi, mask *__vec) const
179 { return this->do_is(__lo, __hi, __vec); }
180
181 /**
182 * @brief Find char_type matching a mask
183 *
184 * This function searches for and returns the first char_type c in
185 * [lo,hi) for which is(m,c) is true. It does so by returning
186 * ctype<char_type>::do_scan_is().
187 *
188 * @param m The mask to compare against.
189 * @param lo Pointer to start of range.
190 * @param hi Pointer to end of range.
191 * @return Pointer to matching char_type if found, else @a hi.
192 */
193 const char_type*
194 scan_is(mask __m, const char_type* __lo, const char_type* __hi) const
195 { return this->do_scan_is(__m, __lo, __hi); }
196
197 /**
198 * @brief Find char_type not matching a mask
199 *
200 * This function searches for and returns the first char_type c in
201 * [lo,hi) for which is(m,c) is false. It does so by returning
202 * ctype<char_type>::do_scan_not().
203 *
204 * @param m The mask to compare against.
205 * @param lo Pointer to first char in range.
206 * @param hi Pointer to end of range.
207 * @return Pointer to non-matching char if found, else @a hi.
208 */
209 const char_type*
210 scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
211 { return this->do_scan_not(__m, __lo, __hi); }
212
213 /**
214 * @brief Convert to uppercase.
215 *
216 * This function converts the argument to uppercase if possible.
217 * If not possible (for example, '2'), returns the argument. It does
218 * so by returning ctype<char_type>::do_toupper().
219 *
220 * @param c The char_type to convert.
221 * @return The uppercase char_type if convertible, else @a c.
222 */
223 char_type
224 toupper(char_type __c) const
225 { return this->do_toupper(__c); }
226
227 /**
228 * @brief Convert array to uppercase.
229 *
230 * This function converts each char_type in the range [lo,hi) to
231 * uppercase if possible. Other elements remain untouched. It does so
232 * by returning ctype<char_type>:: do_toupper(lo, hi).
233 *
234 * @param lo Pointer to start of range.
235 * @param hi Pointer to end of range.
236 * @return @a hi.
237 */
238 const char_type*
239 toupper(char_type *__lo, const char_type* __hi) const
240 { return this->do_toupper(__lo, __hi); }
241
242 /**
243 * @brief Convert to lowercase.
244 *
245 * This function converts the argument to lowercase if possible. If
246 * not possible (for example, '2'), returns the argument. It does so
247 * by returning ctype<char_type>::do_tolower(c).
248 *
249 * @param c The char_type to convert.
250 * @return The lowercase char_type if convertible, else @a c.
251 */
252 char_type
253 tolower(char_type __c) const
254 { return this->do_tolower(__c); }
255
256 /**
257 * @brief Convert array to lowercase.
258 *
259 * This function converts each char_type in the range [lo,hi) to
260 * lowercase if possible. Other elements remain untouched. It does so
261 * by returning ctype<char_type>:: do_tolower(lo, hi).
262 *
263 * @param lo Pointer to start of range.
264 * @param hi Pointer to end of range.
265 * @return @a hi.
266 */
267 const char_type*
268 tolower(char_type* __lo, const char_type* __hi) const
269 { return this->do_tolower(__lo, __hi); }
270
271 /**
272 * @brief Widen char to char_type
273 *
274 * This function converts the char argument to char_type using the
275 * simplest reasonable transformation. It does so by returning
276 * ctype<char_type>::do_widen(c).
277 *
278 * Note: this is not what you want for codepage conversions. See
279 * codecvt for that.
280 *
281 * @param c The char to convert.
282 * @return The converted char_type.
283 */
284 char_type
285 widen(char __c) const
286 { return this->do_widen(__c); }
287
288 /**
289 * @brief Widen array to char_type
290 *
291 * This function converts each char in the input to char_type using the
292 * simplest reasonable transformation. It does so by returning
293 * ctype<char_type>::do_widen(c).
294 *
295 * Note: this is not what you want for codepage conversions. See
296 * codecvt for that.
297 *
298 * @param lo Pointer to start of range.
299 * @param hi Pointer to end of range.
300 * @param to Pointer to the destination array.
301 * @return @a hi.
302 */
303 const char*
304 widen(const char* __lo, const char* __hi, char_type* __to) const
305 { return this->do_widen(__lo, __hi, __to); }
306
307 /**
308 * @brief Narrow char_type to char
309 *
310 * This function converts the char_type to char using the simplest
311 * reasonable transformation. If the conversion fails, dfault is
312 * returned instead. It does so by returning
313 * ctype<char_type>::do_narrow(c).
314 *
315 * Note: this is not what you want for codepage conversions. See
316 * codecvt for that.
317 *
318 * @param c The char_type to convert.
319 * @param dfault Char to return if conversion fails.
320 * @return The converted char.
321 */
322 char
323 narrow(char_type __c, char __dfault) const
324 { return this->do_narrow(__c, __dfault); }
325
326 /**
327 * @brief Narrow array to char array
328 *
329 * This function converts each char_type in the input to char using the
330 * simplest reasonable transformation and writes the results to the
331 * destination array. For any char_type in the input that cannot be
332 * converted, @a dfault is used instead. It does so by returning
333 * ctype<char_type>::do_narrow(lo, hi, dfault, to).
334 *
335 * Note: this is not what you want for codepage conversions. See
336 * codecvt for that.
337 *
338 * @param lo Pointer to start of range.
339 * @param hi Pointer to end of range.
340 * @param dfault Char to use if conversion fails.
341 * @param to Pointer to the destination array.
342 * @return @a hi.
343 */
344 const char_type*
345 narrow(const char_type* __lo, const char_type* __hi,
346 char __dfault, char *__to) const
347 { return this->do_narrow(__lo, __hi, __dfault, __to); }
348
349 protected:
350 explicit
351 __ctype_abstract_base(size_t __refs = 0): facet(__refs) { }
352
353 virtual
354 ~__ctype_abstract_base() { }
355
356 /**
357 * @brief Test char_type classification.
358 *
359 * This function finds a mask M for @a c and compares it to mask @a m.
360 *
361 * do_is() is a hook for a derived facet to change the behavior of
362 * classifying. do_is() must always return the same result for the
363 * same input.
364 *
365 * @param c The char_type to find the mask of.
366 * @param m The mask to compare against.
367 * @return (M & m) != 0.
368 */
369 virtual bool
370 do_is(mask __m, char_type __c) const = 0;
371
372 /**
373 * @brief Return a mask array.
374 *
375 * This function finds the mask for each char_type in the range [lo,hi)
376 * and successively writes it to vec. vec must have as many elements
377 * as the input.
378 *
379 * do_is() is a hook for a derived facet to change the behavior of
380 * classifying. do_is() must always return the same result for the
381 * same input.
382 *
383 * @param lo Pointer to start of range.
384 * @param hi Pointer to end of range.
385 * @param vec Pointer to an array of mask storage.
386 * @return @a hi.
387 */
388 virtual const char_type*
389 do_is(const char_type* __lo, const char_type* __hi,
390 mask* __vec) const = 0;
391
392 /**
393 * @brief Find char_type matching mask
394 *
395 * This function searches for and returns the first char_type c in
396 * [lo,hi) for which is(m,c) is true.
397 *
398 * do_scan_is() is a hook for a derived facet to change the behavior of
399 * match searching. do_is() must always return the same result for the
400 * same input.
401 *
402 * @param m The mask to compare against.
403 * @param lo Pointer to start of range.
404 * @param hi Pointer to end of range.
405 * @return Pointer to a matching char_type if found, else @a hi.
406 */
407 virtual const char_type*
408 do_scan_is(mask __m, const char_type* __lo,
409 const char_type* __hi) const = 0;
410
411 /**
412 * @brief Find char_type not matching mask
413 *
414 * This function searches for and returns a pointer to the first
415 * char_type c of [lo,hi) for which is(m,c) is false.
416 *
417 * do_scan_is() is a hook for a derived facet to change the behavior of
418 * match searching. do_is() must always return the same result for the
419 * same input.
420 *
421 * @param m The mask to compare against.
422 * @param lo Pointer to start of range.
423 * @param hi Pointer to end of range.
424 * @return Pointer to a non-matching char_type if found, else @a hi.
425 */
426 virtual const char_type*
427 do_scan_not(mask __m, const char_type* __lo,
428 const char_type* __hi) const = 0;
429
430 /**
431 * @brief Convert to uppercase.
432 *
433 * This virtual function converts the char_type argument to uppercase
434 * if possible. If not possible (for example, '2'), returns the
435 * argument.
436 *
437 * do_toupper() is a hook for a derived facet to change the behavior of
438 * uppercasing. do_toupper() must always return the same result for
439 * the same input.
440 *
441 * @param c The char_type to convert.
442 * @return The uppercase char_type if convertible, else @a c.
443 */
444 virtual char_type
445 do_toupper(char_type) const = 0;
446
447 /**
448 * @brief Convert array to uppercase.
449 *
450 * This virtual function converts each char_type in the range [lo,hi)
451 * to uppercase if possible. Other elements remain untouched.
452 *
453 * do_toupper() is a hook for a derived facet to change the behavior of
454 * uppercasing. do_toupper() must always return the same result for
455 * the same input.
456 *
457 * @param lo Pointer to start of range.
458 * @param hi Pointer to end of range.
459 * @return @a hi.
460 */
461 virtual const char_type*
462 do_toupper(char_type* __lo, const char_type* __hi) const = 0;
463
464 /**
465 * @brief Convert to lowercase.
466 *
467 * This virtual function converts the argument to lowercase if
468 * possible. If not possible (for example, '2'), returns the argument.
469 *
470 * do_tolower() is a hook for a derived facet to change the behavior of
471 * lowercasing. do_tolower() must always return the same result for
472 * the same input.
473 *
474 * @param c The char_type to convert.
475 * @return The lowercase char_type if convertible, else @a c.
476 */
477 virtual char_type
478 do_tolower(char_type) const = 0;
479
480 /**
481 * @brief Convert array to lowercase.
482 *
483 * This virtual function converts each char_type in the range [lo,hi)
484 * to lowercase if possible. Other elements remain untouched.
485 *
486 * do_tolower() is a hook for a derived facet to change the behavior of
487 * lowercasing. do_tolower() must always return the same result for
488 * the same input.
489 *
490 * @param lo Pointer to start of range.
491 * @param hi Pointer to end of range.
492 * @return @a hi.
493 */
494 virtual const char_type*
495 do_tolower(char_type* __lo, const char_type* __hi) const = 0;
496
497 /**
498 * @brief Widen char
499 *
500 * This virtual function converts the char to char_type using the
501 * simplest reasonable transformation.
502 *
503 * do_widen() is a hook for a derived facet to change the behavior of
504 * widening. do_widen() must always return the same result for the
505 * same input.
506 *
507 * Note: this is not what you want for codepage conversions. See
508 * codecvt for that.
509 *
510 * @param c The char to convert.
511 * @return The converted char_type
512 */
513 virtual char_type
514 do_widen(char) const = 0;
515
516 /**
517 * @brief Widen char array
518 *
519 * This function converts each char in the input to char_type using the
520 * simplest reasonable transformation.
521 *
522 * do_widen() is a hook for a derived facet to change the behavior of
523 * widening. do_widen() must always return the same result for the
524 * same input.
525 *
526 * Note: this is not what you want for codepage conversions. See
527 * codecvt for that.
528 *
529 * @param lo Pointer to start range.
530 * @param hi Pointer to end of range.
531 * @param to Pointer to the destination array.
532 * @return @a hi.
533 */
534 virtual const char*
535 do_widen(const char* __lo, const char* __hi,
536 char_type* __dest) const = 0;
537
538 /**
539 * @brief Narrow char_type to char
540 *
541 * This virtual function converts the argument to char using the
542 * simplest reasonable transformation. If the conversion fails, dfault
543 * is returned instead.
544 *
545 * do_narrow() is a hook for a derived facet to change the behavior of
546 * narrowing. do_narrow() must always return the same result for the
547 * same input.
548 *
549 * Note: this is not what you want for codepage conversions. See
550 * codecvt for that.
551 *
552 * @param c The char_type to convert.
553 * @param dfault Char to return if conversion fails.
554 * @return The converted char.
555 */
556 virtual char
557 do_narrow(char_type, char __dfault) const = 0;
558
559 /**
560 * @brief Narrow char_type array to char
561 *
562 * This virtual function converts each char_type in the range [lo,hi) to
563 * char using the simplest reasonable transformation and writes the
564 * results to the destination array. For any element in the input that
565 * cannot be converted, @a dfault is used instead.
566 *
567 * do_narrow() is a hook for a derived facet to change the behavior of
568 * narrowing. do_narrow() must always return the same result for the
569 * same input.
570 *
571 * Note: this is not what you want for codepage conversions. See
572 * codecvt for that.
573 *
574 * @param lo Pointer to start of range.
575 * @param hi Pointer to end of range.
576 * @param dfault Char to use if conversion fails.
577 * @param to Pointer to the destination array.
578 * @return @a hi.
579 */
580 virtual const char_type*
581 do_narrow(const char_type* __lo, const char_type* __hi,
582 char __dfault, char* __dest) const = 0;
583 };
584
585 /**
586 * @brief Primary class template ctype facet.
587 * @ingroup locales
588 *
589 * This template class defines classification and conversion functions for
590 * character sets. It wraps cctype functionality. Ctype gets used by
591 * streams for many I/O operations.
592 *
593 * This template provides the protected virtual functions the developer
594 * will have to replace in a derived class or specialization to make a
595 * working facet. The public functions that access them are defined in
596 * __ctype_abstract_base, to allow for implementation flexibility. See
597 * ctype<wchar_t> for an example. The functions are documented in
598 * __ctype_abstract_base.
599 *
600 * Note: implementations are provided for all the protected virtual
601 * functions, but will likely not be useful.
602 */
603 template<typename _CharT>
604 class ctype : public __ctype_abstract_base<_CharT>
605 {
606 public:
607 // Types:
608 typedef _CharT char_type;
609 typedef typename __ctype_abstract_base<_CharT>::mask mask;
610
611 /// The facet id for ctype<char_type>
612 static locale::id id;
613
614 explicit
615 ctype(size_t __refs = 0) : __ctype_abstract_base<_CharT>(__refs) { }
616
617 protected:
618 virtual
619 ~ctype();
620
621 virtual bool
622 do_is(mask __m, char_type __c) const;
623
624 virtual const char_type*
625 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
626
627 virtual const char_type*
628 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
629
630 virtual const char_type*
631 do_scan_not(mask __m, const char_type* __lo,
632 const char_type* __hi) const;
633
634 virtual char_type
635 do_toupper(char_type __c) const;
636
637 virtual const char_type*
638 do_toupper(char_type* __lo, const char_type* __hi) const;
639
640 virtual char_type
641 do_tolower(char_type __c) const;
642
643 virtual const char_type*
644 do_tolower(char_type* __lo, const char_type* __hi) const;
645
646 virtual char_type
647 do_widen(char __c) const;
648
649 virtual const char*
650 do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
651
652 virtual char
653 do_narrow(char_type, char __dfault) const;
654
655 virtual const char_type*
656 do_narrow(const char_type* __lo, const char_type* __hi,
657 char __dfault, char* __dest) const;
658 };
659
660 template<typename _CharT>
661 locale::id ctype<_CharT>::id;
662
663 /**
664 * @brief The ctype<char> specialization.
665 * @ingroup locales
666 *
667 * This class defines classification and conversion functions for
668 * the char type. It gets used by char streams for many I/O
669 * operations. The char specialization provides a number of
670 * optimizations as well.
671 */
672 template<>
673 class ctype<char> : public locale::facet, public ctype_base
674 {
675 public:
676 // Types:
677 /// Typedef for the template parameter char.
678 typedef char char_type;
679
680 protected:
681 // Data Members:
682 __c_locale _M_c_locale_ctype;
683 bool _M_del;
684 __to_type _M_toupper;
685 __to_type _M_tolower;
686 const mask* _M_table;
687 mutable char _M_widen_ok;
688 mutable char _M_widen[1 + static_cast<unsigned char>(-1)];
689 mutable char _M_narrow[1 + static_cast<unsigned char>(-1)];
690 mutable char _M_narrow_ok; // 0 uninitialized, 1 init,
691 // 2 memcpy can't be used
692
693 public:
694 /// The facet id for ctype<char>
695 static locale::id id;
696 /// The size of the mask table. It is SCHAR_MAX + 1.
697 static const size_t table_size = 1 + static_cast<unsigned char>(-1);
698
699 /**
700 * @brief Constructor performs initialization.
701 *
702 * This is the constructor provided by the standard.
703 *
704 * @param table If non-zero, table is used as the per-char mask.
705 * Else classic_table() is used.
706 * @param del If true, passes ownership of table to this facet.
707 * @param refs Passed to the base facet class.
708 */
709 explicit
710 ctype(const mask* __table = 0, bool __del = false, size_t __refs = 0);
711
712 /**
713 * @brief Constructor performs static initialization.
714 *
715 * This constructor is used to construct the initial C locale facet.
716 *
717 * @param cloc Handle to C locale data.
718 * @param table If non-zero, table is used as the per-char mask.
719 * @param del If true, passes ownership of table to this facet.
720 * @param refs Passed to the base facet class.
721 */
722 explicit
723 ctype(__c_locale __cloc, const mask* __table = 0, bool __del = false,
724 size_t __refs = 0);
725
726 /**
727 * @brief Test char classification.
728 *
729 * This function compares the mask table[c] to @a m.
730 *
731 * @param c The char to compare the mask of.
732 * @param m The mask to compare against.
733 * @return True if m & table[c] is true, false otherwise.
734 */
735 inline bool
736 is(mask __m, char __c) const;
737
738 /**
739 * @brief Return a mask array.
740 *
741 * This function finds the mask for each char in the range [lo, hi) and
742 * successively writes it to vec. vec must have as many elements as
743 * the char array.
744 *
745 * @param lo Pointer to start of range.
746 * @param hi Pointer to end of range.
747 * @param vec Pointer to an array of mask storage.
748 * @return @a hi.
749 */
750 inline const char*
751 is(const char* __lo, const char* __hi, mask* __vec) const;
752
753 /**
754 * @brief Find char matching a mask
755 *
756 * This function searches for and returns the first char in [lo,hi) for
757 * which is(m,char) is true.
758 *
759 * @param m The mask to compare against.
760 * @param lo Pointer to start of range.
761 * @param hi Pointer to end of range.
762 * @return Pointer to a matching char if found, else @a hi.
763 */
764 inline const char*
765 scan_is(mask __m, const char* __lo, const char* __hi) const;
766
767 /**
768 * @brief Find char not matching a mask
769 *
770 * This function searches for and returns a pointer to the first char
771 * in [lo,hi) for which is(m,char) is false.
772 *
773 * @param m The mask to compare against.
774 * @param lo Pointer to start of range.
775 * @param hi Pointer to end of range.
776 * @return Pointer to a non-matching char if found, else @a hi.
777 */
778 inline const char*
779 scan_not(mask __m, const char* __lo, const char* __hi) const;
780
781 /**
782 * @brief Convert to uppercase.
783 *
784 * This function converts the char argument to uppercase if possible.
785 * If not possible (for example, '2'), returns the argument.
786 *
787 * toupper() acts as if it returns ctype<char>::do_toupper(c).
788 * do_toupper() must always return the same result for the same input.
789 *
790 * @param c The char to convert.
791 * @return The uppercase char if convertible, else @a c.
792 */
793 char_type
794 toupper(char_type __c) const
795 { return this->do_toupper(__c); }
796
797 /**
798 * @brief Convert array to uppercase.
799 *
800 * This function converts each char in the range [lo,hi) to uppercase
801 * if possible. Other chars remain untouched.
802 *
803 * toupper() acts as if it returns ctype<char>:: do_toupper(lo, hi).
804 * do_toupper() must always return the same result for the same input.
805 *
806 * @param lo Pointer to first char in range.
807 * @param hi Pointer to end of range.
808 * @return @a hi.
809 */
810 const char_type*
811 toupper(char_type *__lo, const char_type* __hi) const
812 { return this->do_toupper(__lo, __hi); }
813
814 /**
815 * @brief Convert to lowercase.
816 *
817 * This function converts the char argument to lowercase if possible.
818 * If not possible (for example, '2'), returns the argument.
819 *
820 * tolower() acts as if it returns ctype<char>::do_tolower(c).
821 * do_tolower() must always return the same result for the same input.
822 *
823 * @param c The char to convert.
824 * @return The lowercase char if convertible, else @a c.
825 */
826 char_type
827 tolower(char_type __c) const
828 { return this->do_tolower(__c); }
829
830 /**
831 * @brief Convert array to lowercase.
832 *
833 * This function converts each char in the range [lo,hi) to lowercase
834 * if possible. Other chars remain untouched.
835 *
836 * tolower() acts as if it returns ctype<char>:: do_tolower(lo, hi).
837 * do_tolower() must always return the same result for the same input.
838 *
839 * @param lo Pointer to first char in range.
840 * @param hi Pointer to end of range.
841 * @return @a hi.
842 */
843 const char_type*
844 tolower(char_type* __lo, const char_type* __hi) const
845 { return this->do_tolower(__lo, __hi); }
846
847 /**
848 * @brief Widen char
849 *
850 * This function converts the char to char_type using the simplest
851 * reasonable transformation. For an underived ctype<char> facet, the
852 * argument will be returned unchanged.
853 *
854 * This function works as if it returns ctype<char>::do_widen(c).
855 * do_widen() must always return the same result for the same input.
856 *
857 * Note: this is not what you want for codepage conversions. See
858 * codecvt for that.
859 *
860 * @param c The char to convert.
861 * @return The converted character.
862 */
863 char_type
864 widen(char __c) const
865 {
866 if (_M_widen_ok)
867 return _M_widen[static_cast<unsigned char>(__c)];
868 this->_M_widen_init();
869 return this->do_widen(__c);
870 }
871
872 /**
873 * @brief Widen char array
874 *
875 * This function converts each char in the input to char using the
876 * simplest reasonable transformation. For an underived ctype<char>
877 * facet, the argument will be copied unchanged.
878 *
879 * This function works as if it returns ctype<char>::do_widen(c).
880 * do_widen() must always return the same result for the same input.
881 *
882 * Note: this is not what you want for codepage conversions. See
883 * codecvt for that.
884 *
885 * @param lo Pointer to first char in range.
886 * @param hi Pointer to end of range.
887 * @param to Pointer to the destination array.
888 * @return @a hi.
889 */
890 const char*
891 widen(const char* __lo, const char* __hi, char_type* __to) const
892 {
893 if (_M_widen_ok == 1)
894 {
895 __builtin_memcpy(__to, __lo, __hi - __lo);
896 return __hi;
897 }
898 if (!_M_widen_ok)
899 _M_widen_init();
900 return this->do_widen(__lo, __hi, __to);
901 }
902
903 /**
904 * @brief Narrow char
905 *
906 * This function converts the char to char using the simplest
907 * reasonable transformation. If the conversion fails, dfault is
908 * returned instead. For an underived ctype<char> facet, @a c
909 * will be returned unchanged.
910 *
911 * This function works as if it returns ctype<char>::do_narrow(c).
912 * do_narrow() must always return the same result for the same input.
913 *
914 * Note: this is not what you want for codepage conversions. See
915 * codecvt for that.
916 *
917 * @param c The char to convert.
918 * @param dfault Char to return if conversion fails.
919 * @return The converted character.
920 */
921 char
922 narrow(char_type __c, char __dfault) const
923 {
924 if (_M_narrow[static_cast<unsigned char>(__c)])
925 return _M_narrow[static_cast<unsigned char>(__c)];
926 const char __t = do_narrow(__c, __dfault);
927 if (__t != __dfault)
928 _M_narrow[static_cast<unsigned char>(__c)] = __t;
929 return __t;
930 }
931
932 /**
933 * @brief Narrow char array
934 *
935 * This function converts each char in the input to char using the
936 * simplest reasonable transformation and writes the results to the
937 * destination array. For any char in the input that cannot be
938 * converted, @a dfault is used instead. For an underived ctype<char>
939 * facet, the argument will be copied unchanged.
940 *
941 * This function works as if it returns ctype<char>::do_narrow(lo, hi,
942 * dfault, to). do_narrow() must always return the same result for the
943 * same input.
944 *
945 * Note: this is not what you want for codepage conversions. See
946 * codecvt for that.
947 *
948 * @param lo Pointer to start of range.
949 * @param hi Pointer to end of range.
950 * @param dfault Char to use if conversion fails.
951 * @param to Pointer to the destination array.
952 * @return @a hi.
953 */
954 const char_type*
955 narrow(const char_type* __lo, const char_type* __hi,
956 char __dfault, char *__to) const
957 {
958 if (__builtin_expect(_M_narrow_ok == 1, true))
959 {
960 __builtin_memcpy(__to, __lo, __hi - __lo);
961 return __hi;
962 }
963 if (!_M_narrow_ok)
964 _M_narrow_init();
965 return this->do_narrow(__lo, __hi, __dfault, __to);
966 }
967
968 // _GLIBCXX_RESOLVE_LIB_DEFECTS
969 // DR 695. ctype<char>::classic_table() not accessible.
970 /// Returns a pointer to the mask table provided to the constructor, or
971 /// the default from classic_table() if none was provided.
972 const mask*
973 table() const throw()
974 { return _M_table; }
975
976 /// Returns a pointer to the C locale mask table.
977 static const mask*
978 classic_table() throw();
979 protected:
980
981 /**
982 * @brief Destructor.
983 *
984 * This function deletes table() if @a del was true in the
985 * constructor.
986 */
987 virtual
988 ~ctype();
989
990 /**
991 * @brief Convert to uppercase.
992 *
993 * This virtual function converts the char argument to uppercase if
994 * possible. If not possible (for example, '2'), returns the argument.
995 *
996 * do_toupper() is a hook for a derived facet to change the behavior of
997 * uppercasing. do_toupper() must always return the same result for
998 * the same input.
999 *
1000 * @param c The char to convert.
1001 * @return The uppercase char if convertible, else @a c.
1002 */
1003 virtual char_type
1004 do_toupper(char_type) const;
1005
1006 /**
1007 * @brief Convert array to uppercase.
1008 *
1009 * This virtual function converts each char in the range [lo,hi) to
1010 * uppercase if possible. Other chars remain untouched.
1011 *
1012 * do_toupper() is a hook for a derived facet to change the behavior of
1013 * uppercasing. do_toupper() must always return the same result for
1014 * the same input.
1015 *
1016 * @param lo Pointer to start of range.
1017 * @param hi Pointer to end of range.
1018 * @return @a hi.
1019 */
1020 virtual const char_type*
1021 do_toupper(char_type* __lo, const char_type* __hi) const;
1022
1023 /**
1024 * @brief Convert to lowercase.
1025 *
1026 * This virtual function converts the char argument to lowercase if
1027 * possible. If not possible (for example, '2'), returns the argument.
1028 *
1029 * do_tolower() is a hook for a derived facet to change the behavior of
1030 * lowercasing. do_tolower() must always return the same result for
1031 * the same input.
1032 *
1033 * @param c The char to convert.
1034 * @return The lowercase char if convertible, else @a c.
1035 */
1036 virtual char_type
1037 do_tolower(char_type) const;
1038
1039 /**
1040 * @brief Convert array to lowercase.
1041 *
1042 * This virtual function converts each char in the range [lo,hi) to
1043 * lowercase if possible. Other chars remain untouched.
1044 *
1045 * do_tolower() is a hook for a derived facet to change the behavior of
1046 * lowercasing. do_tolower() must always return the same result for
1047 * the same input.
1048 *
1049 * @param lo Pointer to first char in range.
1050 * @param hi Pointer to end of range.
1051 * @return @a hi.
1052 */
1053 virtual const char_type*
1054 do_tolower(char_type* __lo, const char_type* __hi) const;
1055
1056 /**
1057 * @brief Widen char
1058 *
1059 * This virtual function converts the char to char using the simplest
1060 * reasonable transformation. For an underived ctype<char> facet, the
1061 * argument will be returned unchanged.
1062 *
1063 * do_widen() is a hook for a derived facet to change the behavior of
1064 * widening. do_widen() must always return the same result for the
1065 * same input.
1066 *
1067 * Note: this is not what you want for codepage conversions. See
1068 * codecvt for that.
1069 *
1070 * @param c The char to convert.
1071 * @return The converted character.
1072 */
1073 virtual char_type
1074 do_widen(char __c) const
1075 { return __c; }
1076
1077 /**
1078 * @brief Widen char array
1079 *
1080 * This function converts each char in the range [lo,hi) to char using
1081 * the simplest reasonable transformation. For an underived
1082 * ctype<char> facet, the argument will be copied unchanged.
1083 *
1084 * do_widen() is a hook for a derived facet to change the behavior of
1085 * widening. do_widen() must always return the same result for the
1086 * same input.
1087 *
1088 * Note: this is not what you want for codepage conversions. See
1089 * codecvt for that.
1090 *
1091 * @param lo Pointer to start of range.
1092 * @param hi Pointer to end of range.
1093 * @param to Pointer to the destination array.
1094 * @return @a hi.
1095 */
1096 virtual const char*
1097 do_widen(const char* __lo, const char* __hi, char_type* __dest) const
1098 {
1099 __builtin_memcpy(__dest, __lo, __hi - __lo);
1100 return __hi;
1101 }
1102
1103 /**
1104 * @brief Narrow char
1105 *
1106 * This virtual function converts the char to char using the simplest
1107 * reasonable transformation. If the conversion fails, dfault is
1108 * returned instead. For an underived ctype<char> facet, @a c will be
1109 * returned unchanged.
1110 *
1111 * do_narrow() is a hook for a derived facet to change the behavior of
1112 * narrowing. do_narrow() must always return the same result for the
1113 * same input.
1114 *
1115 * Note: this is not what you want for codepage conversions. See
1116 * codecvt for that.
1117 *
1118 * @param c The char to convert.
1119 * @param dfault Char to return if conversion fails.
1120 * @return The converted char.
1121 */
1122 virtual char
1123 do_narrow(char_type __c, char) const
1124 { return __c; }
1125
1126 /**
1127 * @brief Narrow char array to char array
1128 *
1129 * This virtual function converts each char in the range [lo,hi) to
1130 * char using the simplest reasonable transformation and writes the
1131 * results to the destination array. For any char in the input that
1132 * cannot be converted, @a dfault is used instead. For an underived
1133 * ctype<char> facet, the argument will be copied unchanged.
1134 *
1135 * do_narrow() is a hook for a derived facet to change the behavior of
1136 * narrowing. do_narrow() must always return the same result for the
1137 * same input.
1138 *
1139 * Note: this is not what you want for codepage conversions. See
1140 * codecvt for that.
1141 *
1142 * @param lo Pointer to start of range.
1143 * @param hi Pointer to end of range.
1144 * @param dfault Char to use if conversion fails.
1145 * @param to Pointer to the destination array.
1146 * @return @a hi.
1147 */
1148 virtual const char_type*
1149 do_narrow(const char_type* __lo, const char_type* __hi,
1150 char, char* __dest) const
1151 {
1152 __builtin_memcpy(__dest, __lo, __hi - __lo);
1153 return __hi;
1154 }
1155
1156 private:
1157 void _M_narrow_init() const;
1158 void _M_widen_init() const;
1159 };
1160
1161 #ifdef _GLIBCXX_USE_WCHAR_T
1162 /**
1163 * @brief The ctype<wchar_t> specialization.
1164 * @ingroup locales
1165 *
1166 * This class defines classification and conversion functions for the
1167 * wchar_t type. It gets used by wchar_t streams for many I/O operations.
1168 * The wchar_t specialization provides a number of optimizations as well.
1169 *
1170 * ctype<wchar_t> inherits its public methods from
1171 * __ctype_abstract_base<wchar_t>.
1172 */
1173 template<>
1174 class ctype<wchar_t> : public __ctype_abstract_base<wchar_t>
1175 {
1176 public:
1177 // Types:
1178 /// Typedef for the template parameter wchar_t.
1179 typedef wchar_t char_type;
1180 typedef wctype_t __wmask_type;
1181
1182 protected:
1183 __c_locale _M_c_locale_ctype;
1184
1185 // Pre-computed narrowed and widened chars.
1186 bool _M_narrow_ok;
1187 char _M_narrow[128];
1188 wint_t _M_widen[1 + static_cast<unsigned char>(-1)];
1189
1190 // Pre-computed elements for do_is.
1191 mask _M_bit[16];
1192 __wmask_type _M_wmask[16];
1193
1194 public:
1195 // Data Members:
1196 /// The facet id for ctype<wchar_t>
1197 static locale::id id;
1198
1199 /**
1200 * @brief Constructor performs initialization.
1201 *
1202 * This is the constructor provided by the standard.
1203 *
1204 * @param refs Passed to the base facet class.
1205 */
1206 explicit
1207 ctype(size_t __refs = 0);
1208
1209 /**
1210 * @brief Constructor performs static initialization.
1211 *
1212 * This constructor is used to construct the initial C locale facet.
1213 *
1214 * @param cloc Handle to C locale data.
1215 * @param refs Passed to the base facet class.
1216 */
1217 explicit
1218 ctype(__c_locale __cloc, size_t __refs = 0);
1219
1220 protected:
1221 __wmask_type
1222 _M_convert_to_wmask(const mask __m) const throw();
1223
1224 /// Destructor
1225 virtual
1226 ~ctype();
1227
1228 /**
1229 * @brief Test wchar_t classification.
1230 *
1231 * This function finds a mask M for @a c and compares it to mask @a m.
1232 *
1233 * do_is() is a hook for a derived facet to change the behavior of
1234 * classifying. do_is() must always return the same result for the
1235 * same input.
1236 *
1237 * @param c The wchar_t to find the mask of.
1238 * @param m The mask to compare against.
1239 * @return (M & m) != 0.
1240 */
1241 virtual bool
1242 do_is(mask __m, char_type __c) const;
1243
1244 /**
1245 * @brief Return a mask array.
1246 *
1247 * This function finds the mask for each wchar_t in the range [lo,hi)
1248 * and successively writes it to vec. vec must have as many elements
1249 * as the input.
1250 *
1251 * do_is() is a hook for a derived facet to change the behavior of
1252 * classifying. do_is() must always return the same result for the
1253 * same input.
1254 *
1255 * @param lo Pointer to start of range.
1256 * @param hi Pointer to end of range.
1257 * @param vec Pointer to an array of mask storage.
1258 * @return @a hi.
1259 */
1260 virtual const char_type*
1261 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
1262
1263 /**
1264 * @brief Find wchar_t matching mask
1265 *
1266 * This function searches for and returns the first wchar_t c in
1267 * [lo,hi) for which is(m,c) is true.
1268 *
1269 * do_scan_is() is a hook for a derived facet to change the behavior of
1270 * match searching. do_is() must always return the same result for the
1271 * same input.
1272 *
1273 * @param m The mask to compare against.
1274 * @param lo Pointer to start of range.
1275 * @param hi Pointer to end of range.
1276 * @return Pointer to a matching wchar_t if found, else @a hi.
1277 */
1278 virtual const char_type*
1279 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
1280
1281 /**
1282 * @brief Find wchar_t not matching mask
1283 *
1284 * This function searches for and returns a pointer to the first
1285 * wchar_t c of [lo,hi) for which is(m,c) is false.
1286 *
1287 * do_scan_is() is a hook for a derived facet to change the behavior of
1288 * match searching. do_is() must always return the same result for the
1289 * same input.
1290 *
1291 * @param m The mask to compare against.
1292 * @param lo Pointer to start of range.
1293 * @param hi Pointer to end of range.
1294 * @return Pointer to a non-matching wchar_t if found, else @a hi.
1295 */
1296 virtual const char_type*
1297 do_scan_not(mask __m, const char_type* __lo,
1298 const char_type* __hi) const;
1299
1300 /**
1301 * @brief Convert to uppercase.
1302 *
1303 * This virtual function converts the wchar_t argument to uppercase if
1304 * possible. If not possible (for example, '2'), returns the argument.
1305 *
1306 * do_toupper() is a hook for a derived facet to change the behavior of
1307 * uppercasing. do_toupper() must always return the same result for
1308 * the same input.
1309 *
1310 * @param c The wchar_t to convert.
1311 * @return The uppercase wchar_t if convertible, else @a c.
1312 */
1313 virtual char_type
1314 do_toupper(char_type) const;
1315
1316 /**
1317 * @brief Convert array to uppercase.
1318 *
1319 * This virtual function converts each wchar_t in the range [lo,hi) to
1320 * uppercase if possible. Other elements remain untouched.
1321 *
1322 * do_toupper() is a hook for a derived facet to change the behavior of
1323 * uppercasing. do_toupper() must always return the same result for
1324 * the same input.
1325 *
1326 * @param lo Pointer to start of range.
1327 * @param hi Pointer to end of range.
1328 * @return @a hi.
1329 */
1330 virtual const char_type*
1331 do_toupper(char_type* __lo, const char_type* __hi) const;
1332
1333 /**
1334 * @brief Convert to lowercase.
1335 *
1336 * This virtual function converts the argument to lowercase if
1337 * possible. If not possible (for example, '2'), returns the argument.
1338 *
1339 * do_tolower() is a hook for a derived facet to change the behavior of
1340 * lowercasing. do_tolower() must always return the same result for
1341 * the same input.
1342 *
1343 * @param c The wchar_t to convert.
1344 * @return The lowercase wchar_t if convertible, else @a c.
1345 */
1346 virtual char_type
1347 do_tolower(char_type) const;
1348
1349 /**
1350 * @brief Convert array to lowercase.
1351 *
1352 * This virtual function converts each wchar_t in the range [lo,hi) to
1353 * lowercase if possible. Other elements remain untouched.
1354 *
1355 * do_tolower() is a hook for a derived facet to change the behavior of
1356 * lowercasing. do_tolower() must always return the same result for
1357 * the same input.
1358 *
1359 * @param lo Pointer to start of range.
1360 * @param hi Pointer to end of range.
1361 * @return @a hi.
1362 */
1363 virtual const char_type*
1364 do_tolower(char_type* __lo, const char_type* __hi) const;
1365
1366 /**
1367 * @brief Widen char to wchar_t
1368 *
1369 * This virtual function converts the char to wchar_t using the
1370 * simplest reasonable transformation. For an underived ctype<wchar_t>
1371 * facet, the argument will be cast to wchar_t.
1372 *
1373 * do_widen() is a hook for a derived facet to change the behavior of
1374 * widening. do_widen() must always return the same result for the
1375 * same input.
1376 *
1377 * Note: this is not what you want for codepage conversions. See
1378 * codecvt for that.
1379 *
1380 * @param c The char to convert.
1381 * @return The converted wchar_t.
1382 */
1383 virtual char_type
1384 do_widen(char) const;
1385
1386 /**
1387 * @brief Widen char array to wchar_t array
1388 *
1389 * This function converts each char in the input to wchar_t using the
1390 * simplest reasonable transformation. For an underived ctype<wchar_t>
1391 * facet, the argument will be copied, casting each element to wchar_t.
1392 *
1393 * do_widen() is a hook for a derived facet to change the behavior of
1394 * widening. do_widen() must always return the same result for the
1395 * same input.
1396 *
1397 * Note: this is not what you want for codepage conversions. See
1398 * codecvt for that.
1399 *
1400 * @param lo Pointer to start range.
1401 * @param hi Pointer to end of range.
1402 * @param to Pointer to the destination array.
1403 * @return @a hi.
1404 */
1405 virtual const char*
1406 do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
1407
1408 /**
1409 * @brief Narrow wchar_t to char
1410 *
1411 * This virtual function converts the argument to char using
1412 * the simplest reasonable transformation. If the conversion
1413 * fails, dfault is returned instead. For an underived
1414 * ctype<wchar_t> facet, @a c will be cast to char and
1415 * returned.
1416 *
1417 * do_narrow() is a hook for a derived facet to change the
1418 * behavior of narrowing. do_narrow() must always return the
1419 * same result for the same input.
1420 *
1421 * Note: this is not what you want for codepage conversions. See
1422 * codecvt for that.
1423 *
1424 * @param c The wchar_t to convert.
1425 * @param dfault Char to return if conversion fails.
1426 * @return The converted char.
1427 */
1428 virtual char
1429 do_narrow(char_type, char __dfault) const;
1430
1431 /**
1432 * @brief Narrow wchar_t array to char array
1433 *
1434 * This virtual function converts each wchar_t in the range [lo,hi) to
1435 * char using the simplest reasonable transformation and writes the
1436 * results to the destination array. For any wchar_t in the input that
1437 * cannot be converted, @a dfault is used instead. For an underived
1438 * ctype<wchar_t> facet, the argument will be copied, casting each
1439 * element to char.
1440 *
1441 * do_narrow() is a hook for a derived facet to change the behavior of
1442 * narrowing. do_narrow() must always return the same result for the
1443 * same input.
1444 *
1445 * Note: this is not what you want for codepage conversions. See
1446 * codecvt for that.
1447 *
1448 * @param lo Pointer to start of range.
1449 * @param hi Pointer to end of range.
1450 * @param dfault Char to use if conversion fails.
1451 * @param to Pointer to the destination array.
1452 * @return @a hi.
1453 */
1454 virtual const char_type*
1455 do_narrow(const char_type* __lo, const char_type* __hi,
1456 char __dfault, char* __dest) const;
1457
1458 // For use at construction time only.
1459 void
1460 _M_initialize_ctype() throw();
1461 };
1462 #endif //_GLIBCXX_USE_WCHAR_T
1463
1464 /// class ctype_byname [22.2.1.2].
1465 template<typename _CharT>
1466 class ctype_byname : public ctype<_CharT>
1467 {
1468 public:
1469 typedef typename ctype<_CharT>::mask mask;
1470
1471 explicit
1472 ctype_byname(const char* __s, size_t __refs = 0);
1473
1474 protected:
1475 virtual
1476 ~ctype_byname() { };
1477 };
1478
1479 /// 22.2.1.4 Class ctype_byname specializations.
1480 template<>
1481 class ctype_byname<char> : public ctype<char>
1482 {
1483 public:
1484 explicit
1485 ctype_byname(const char* __s, size_t __refs = 0);
1486
1487 protected:
1488 virtual
1489 ~ctype_byname();
1490 };
1491
1492 #ifdef _GLIBCXX_USE_WCHAR_T
1493 template<>
1494 class ctype_byname<wchar_t> : public ctype<wchar_t>
1495 {
1496 public:
1497 explicit
1498 ctype_byname(const char* __s, size_t __refs = 0);
1499
1500 protected:
1501 virtual
1502 ~ctype_byname();
1503 };
1504 #endif
1505
1506 _GLIBCXX_END_NAMESPACE
1507
1508 // Include host and configuration specific ctype inlines.
1509 #include <bits/ctype_inline.h>
1510
1511 _GLIBCXX_BEGIN_NAMESPACE(std)
1512
1513 // 22.2.2 The numeric category.
1514 class __num_base
1515 {
1516 public:
1517 // NB: Code depends on the order of _S_atoms_out elements.
1518 // Below are the indices into _S_atoms_out.
1519 enum
1520 {
1521 _S_ominus,
1522 _S_oplus,
1523 _S_ox,
1524 _S_oX,
1525 _S_odigits,
1526 _S_odigits_end = _S_odigits + 16,
1527 _S_oudigits = _S_odigits_end,
1528 _S_oudigits_end = _S_oudigits + 16,
1529 _S_oe = _S_odigits + 14, // For scientific notation, 'e'
1530 _S_oE = _S_oudigits + 14, // For scientific notation, 'E'
1531 _S_oend = _S_oudigits_end
1532 };
1533
1534 // A list of valid numeric literals for output. This array
1535 // contains chars that will be passed through the current locale's
1536 // ctype<_CharT>.widen() and then used to render numbers.
1537 // For the standard "C" locale, this is
1538 // "-+xX0123456789abcdef0123456789ABCDEF".
1539 static const char* _S_atoms_out;
1540
1541 // String literal of acceptable (narrow) input, for num_get.
1542 // "-+xX0123456789abcdefABCDEF"
1543 static const char* _S_atoms_in;
1544
1545 enum
1546 {
1547 _S_iminus,
1548 _S_iplus,
1549 _S_ix,
1550 _S_iX,
1551 _S_izero,
1552 _S_ie = _S_izero + 14,
1553 _S_iE = _S_izero + 20,
1554 _S_iend = 26
1555 };
1556
1557 // num_put
1558 // Construct and return valid scanf format for floating point types.
1559 static void
1560 _S_format_float(const ios_base& __io, char* __fptr, char __mod) throw();
1561 };
1562
1563 template<typename _CharT>
1564 struct __numpunct_cache : public locale::facet
1565 {
1566 const char* _M_grouping;
1567 size_t _M_grouping_size;
1568 bool _M_use_grouping;
1569 const _CharT* _M_truename;
1570 size_t _M_truename_size;
1571 const _CharT* _M_falsename;
1572 size_t _M_falsename_size;
1573 _CharT _M_decimal_point;
1574 _CharT _M_thousands_sep;
1575
1576 // A list of valid numeric literals for output: in the standard
1577 // "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF".
1578 // This array contains the chars after having been passed
1579 // through the current locale's ctype<_CharT>.widen().
1580 _CharT _M_atoms_out[__num_base::_S_oend];
1581
1582 // A list of valid numeric literals for input: in the standard
1583 // "C" locale, this is "-+xX0123456789abcdefABCDEF"
1584 // This array contains the chars after having been passed
1585 // through the current locale's ctype<_CharT>.widen().
1586 _CharT _M_atoms_in[__num_base::_S_iend];
1587
1588 bool _M_allocated;
1589
1590 __numpunct_cache(size_t __refs = 0)
1591 : facet(__refs), _M_grouping(0), _M_grouping_size(0),
1592 _M_use_grouping(false),
1593 _M_truename(0), _M_truename_size(0), _M_falsename(0),
1594 _M_falsename_size(0), _M_decimal_point(_CharT()),
1595 _M_thousands_sep(_CharT()), _M_allocated(false)
1596 { }
1597
1598 ~__numpunct_cache();
1599
1600 void
1601 _M_cache(const locale& __loc);
1602
1603 private:
1604 __numpunct_cache&
1605 operator=(const __numpunct_cache&);
1606
1607 explicit
1608 __numpunct_cache(const __numpunct_cache&);
1609 };
1610
1611 template<typename _CharT>
1612 __numpunct_cache<_CharT>::~__numpunct_cache()
1613 {
1614 if (_M_allocated)
1615 {
1616 delete [] _M_grouping;
1617 delete [] _M_truename;
1618 delete [] _M_falsename;
1619 }
1620 }
1621
1622 /**
1623 * @brief Primary class template numpunct.
1624 * @ingroup locales
1625 *
1626 * This facet stores several pieces of information related to printing and
1627 * scanning numbers, such as the decimal point character. It takes a
1628 * template parameter specifying the char type. The numpunct facet is
1629 * used by streams for many I/O operations involving numbers.
1630 *
1631 * The numpunct template uses protected virtual functions to provide the
1632 * actual results. The public accessors forward the call to the virtual
1633 * functions. These virtual functions are hooks for developers to
1634 * implement the behavior they require from a numpunct facet.
1635 */
1636 template<typename _CharT>
1637 class numpunct : public locale::facet
1638 {
1639 public:
1640 // Types:
1641 //@{
1642 /// Public typedefs
1643 typedef _CharT char_type;
1644 typedef basic_string<_CharT> string_type;
1645 //@}
1646 typedef __numpunct_cache<_CharT> __cache_type;
1647
1648 protected:
1649 __cache_type* _M_data;
1650
1651 public:
1652 /// Numpunct facet id.
1653 static locale::id id;
1654
1655 /**
1656 * @brief Numpunct constructor.
1657 *
1658 * @param refs Refcount to pass to the base class.
1659 */
1660 explicit
1661 numpunct(size_t __refs = 0)
1662 : facet(__refs), _M_data(0)
1663 { _M_initialize_numpunct(); }
1664
1665 /**
1666 * @brief Internal constructor. Not for general use.
1667 *
1668 * This is a constructor for use by the library itself to set up the
1669 * predefined locale facets.
1670 *
1671 * @param cache __numpunct_cache object.
1672 * @param refs Refcount to pass to the base class.
1673 */
1674 explicit
1675 numpunct(__cache_type* __cache, size_t __refs = 0)
1676 : facet(__refs), _M_data(__cache)
1677 { _M_initialize_numpunct(); }
1678
1679 /**
1680 * @brief Internal constructor. Not for general use.
1681 *
1682 * This is a constructor for use by the library itself to set up new
1683 * locales.
1684 *
1685 * @param cloc The C locale.
1686 * @param refs Refcount to pass to the base class.
1687 */
1688 explicit
1689 numpunct(__c_locale __cloc, size_t __refs = 0)
1690 : facet(__refs), _M_data(0)
1691 { _M_initialize_numpunct(__cloc); }
1692
1693 /**
1694 * @brief Return decimal point character.
1695 *
1696 * This function returns a char_type to use as a decimal point. It
1697 * does so by returning returning
1698 * numpunct<char_type>::do_decimal_point().
1699 *
1700 * @return @a char_type representing a decimal point.
1701 */
1702 char_type
1703 decimal_point() const
1704 { return this->do_decimal_point(); }
1705
1706 /**
1707 * @brief Return thousands separator character.
1708 *
1709 * This function returns a char_type to use as a thousands
1710 * separator. It does so by returning returning
1711 * numpunct<char_type>::do_thousands_sep().
1712 *
1713 * @return char_type representing a thousands separator.
1714 */
1715 char_type
1716 thousands_sep() const
1717 { return this->do_thousands_sep(); }
1718
1719 /**
1720 * @brief Return grouping specification.
1721 *
1722 * This function returns a string representing groupings for the
1723 * integer part of a number. Groupings indicate where thousands
1724 * separators should be inserted in the integer part of a number.
1725 *
1726 * Each char in the return string is interpret as an integer
1727 * rather than a character. These numbers represent the number
1728 * of digits in a group. The first char in the string
1729 * represents the number of digits in the least significant
1730 * group. If a char is negative, it indicates an unlimited
1731 * number of digits for the group. If more chars from the
1732 * string are required to group a number, the last char is used
1733 * repeatedly.
1734 *
1735 * For example, if the grouping() returns "\003\002" and is
1736 * applied to the number 123456789, this corresponds to
1737 * 12,34,56,789. Note that if the string was "32", this would
1738 * put more than 50 digits into the least significant group if
1739 * the character set is ASCII.
1740 *
1741 * The string is returned by calling
1742 * numpunct<char_type>::do_grouping().
1743 *
1744 * @return string representing grouping specification.
1745 */
1746 string
1747 grouping() const
1748 { return this->do_grouping(); }
1749
1750 /**
1751 * @brief Return string representation of bool true.
1752 *
1753 * This function returns a string_type containing the text
1754 * representation for true bool variables. It does so by calling
1755 * numpunct<char_type>::do_truename().
1756 *
1757 * @return string_type representing printed form of true.
1758 */
1759 string_type
1760 truename() const
1761 { return this->do_truename(); }
1762
1763 /**
1764 * @brief Return string representation of bool false.
1765 *
1766 * This function returns a string_type containing the text
1767 * representation for false bool variables. It does so by calling
1768 * numpunct<char_type>::do_falsename().
1769 *
1770 * @return string_type representing printed form of false.
1771 */
1772 string_type
1773 falsename() const
1774 { return this->do_falsename(); }
1775
1776 protected:
1777 /// Destructor.
1778 virtual
1779 ~numpunct();
1780
1781 /**
1782 * @brief Return decimal point character.
1783 *
1784 * Returns a char_type to use as a decimal point. This function is a
1785 * hook for derived classes to change the value returned.
1786 *
1787 * @return @a char_type representing a decimal point.
1788 */
1789 virtual char_type
1790 do_decimal_point() const
1791 { return _M_data->_M_decimal_point; }
1792
1793 /**
1794 * @brief Return thousands separator character.
1795 *
1796 * Returns a char_type to use as a thousands separator. This function
1797 * is a hook for derived classes to change the value returned.
1798 *
1799 * @return @a char_type representing a thousands separator.
1800 */
1801 virtual char_type
1802 do_thousands_sep() const
1803 { return _M_data->_M_thousands_sep; }
1804
1805 /**
1806 * @brief Return grouping specification.
1807 *
1808 * Returns a string representing groupings for the integer part of a
1809 * number. This function is a hook for derived classes to change the
1810 * value returned. @see grouping() for details.
1811 *
1812 * @return String representing grouping specification.
1813 */
1814 virtual string
1815 do_grouping() const
1816 { return _M_data->_M_grouping; }
1817
1818 /**
1819 * @brief Return string representation of bool true.
1820 *
1821 * Returns a string_type containing the text representation for true
1822 * bool variables. This function is a hook for derived classes to
1823 * change the value returned.
1824 *
1825 * @return string_type representing printed form of true.
1826 */
1827 virtual string_type
1828 do_truename() const
1829 { return _M_data->_M_truename; }
1830
1831 /**
1832 * @brief Return string representation of bool false.
1833 *
1834 * Returns a string_type containing the text representation for false
1835 * bool variables. This function is a hook for derived classes to
1836 * change the value returned.
1837 *
1838 * @return string_type representing printed form of false.
1839 */
1840 virtual string_type
1841 do_falsename() const
1842 { return _M_data->_M_falsename; }
1843
1844 // For use at construction time only.
1845 void
1846 _M_initialize_numpunct(__c_locale __cloc = 0);
1847 };
1848
1849 template<typename _CharT>
1850 locale::id numpunct<_CharT>::id;
1851
1852 template<>
1853 numpunct<char>::~numpunct();
1854
1855 template<>
1856 void
1857 numpunct<char>::_M_initialize_numpunct(__c_locale __cloc);
1858
1859 #ifdef _GLIBCXX_USE_WCHAR_T
1860 template<>
1861 numpunct<wchar_t>::~numpunct();
1862
1863 template<>
1864 void
1865 numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc);
1866 #endif
1867
1868 /// class numpunct_byname [22.2.3.2].
1869 template<typename _CharT>
1870 class numpunct_byname : public numpunct<_CharT>
1871 {
1872 public:
1873 typedef _CharT char_type;
1874 typedef basic_string<_CharT> string_type;
1875
1876 explicit
1877 numpunct_byname(const char* __s, size_t __refs = 0)
1878 : numpunct<_CharT>(__refs)
1879 {
1880 if (__builtin_strcmp(__s, "C") != 0
1881 && __builtin_strcmp(__s, "POSIX") != 0)
1882 {
1883 __c_locale __tmp;
1884 this->_S_create_c_locale(__tmp, __s);
1885 this->_M_initialize_numpunct(__tmp);
1886 this->_S_destroy_c_locale(__tmp);
1887 }
1888 }
1889
1890 protected:
1891 virtual
1892 ~numpunct_byname() { }
1893 };
1894
1895 _GLIBCXX_BEGIN_LDBL_NAMESPACE
1896
1897 /**
1898 * @brief Primary class template num_get.
1899 * @ingroup locales
1900 *
1901 * This facet encapsulates the code to parse and return a number
1902 * from a string. It is used by the istream numeric extraction
1903 * operators.
1904 *
1905 * The num_get template uses protected virtual functions to provide the
1906 * actual results. The public accessors forward the call to the virtual
1907 * functions. These virtual functions are hooks for developers to
1908 * implement the behavior they require from the num_get facet.
1909 */
1910 template<typename _CharT, typename _InIter>
1911 class num_get : public locale::facet
1912 {
1913 public:
1914 // Types:
1915 //@{
1916 /// Public typedefs
1917 typedef _CharT char_type;
1918 typedef _InIter iter_type;
1919 //@}
1920
1921 /// Numpunct facet id.
1922 static locale::id id;
1923
1924 /**
1925 * @brief Constructor performs initialization.
1926 *
1927 * This is the constructor provided by the standard.
1928 *
1929 * @param refs Passed to the base facet class.
1930 */
1931 explicit
1932 num_get(size_t __refs = 0) : facet(__refs) { }
1933
1934 /**
1935 * @brief Numeric parsing.
1936 *
1937 * Parses the input stream into the bool @a v. It does so by calling
1938 * num_get::do_get().
1939 *
1940 * If ios_base::boolalpha is set, attempts to read
1941 * ctype<CharT>::truename() or ctype<CharT>::falsename(). Sets
1942 * @a v to true or false if successful. Sets err to
1943 * ios_base::failbit if reading the string fails. Sets err to
1944 * ios_base::eofbit if the stream is emptied.
1945 *
1946 * If ios_base::boolalpha is not set, proceeds as with reading a long,
1947 * except if the value is 1, sets @a v to true, if the value is 0, sets
1948 * @a v to false, and otherwise set err to ios_base::failbit.
1949 *
1950 * @param in Start of input stream.
1951 * @param end End of input stream.
1952 * @param io Source of locale and flags.
1953 * @param err Error flags to set.
1954 * @param v Value to format and insert.
1955 * @return Iterator after reading.
1956 */
1957 iter_type
1958 get(iter_type __in, iter_type __end, ios_base& __io,
1959 ios_base::iostate& __err, bool& __v) const
1960 { return this->do_get(__in, __end, __io, __err, __v); }
1961
1962 //@{
1963 /**
1964 * @brief Numeric parsing.
1965 *
1966 * Parses the input stream into the integral variable @a v. It does so
1967 * by calling num_get::do_get().
1968 *
1969 * Parsing is affected by the flag settings in @a io.
1970 *
1971 * The basic parse is affected by the value of io.flags() &
1972 * ios_base::basefield. If equal to ios_base::oct, parses like the
1973 * scanf %o specifier. Else if equal to ios_base::hex, parses like %X
1974 * specifier. Else if basefield equal to 0, parses like the %i
1975 * specifier. Otherwise, parses like %d for signed and %u for unsigned
1976 * types. The matching type length modifier is also used.
1977 *
1978 * Digit grouping is interpreted according to numpunct::grouping() and
1979 * numpunct::thousands_sep(). If the pattern of digit groups isn't
1980 * consistent, sets err to ios_base::failbit.
1981 *
1982 * If parsing the string yields a valid value for @a v, @a v is set.
1983 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
1984 * Sets err to ios_base::eofbit if the stream is emptied.
1985 *
1986 * @param in Start of input stream.
1987 * @param end End of input stream.
1988 * @param io Source of locale and flags.
1989 * @param err Error flags to set.
1990 * @param v Value to format and insert.
1991 * @return Iterator after reading.
1992 */
1993 iter_type
1994 get(iter_type __in, iter_type __end, ios_base& __io,
1995 ios_base::iostate& __err, long& __v) const
1996 { return this->do_get(__in, __end, __io, __err, __v); }
1997
1998 iter_type
1999 get(iter_type __in, iter_type __end, ios_base& __io,
2000 ios_base::iostate& __err, unsigned short& __v) const
2001 { return this->do_get(__in, __end, __io, __err, __v); }
2002
2003 iter_type
2004 get(iter_type __in, iter_type __end, ios_base& __io,
2005 ios_base::iostate& __err, unsigned int& __v) const
2006 { return this->do_get(__in, __end, __io, __err, __v); }
2007
2008 iter_type
2009 get(iter_type __in, iter_type __end, ios_base& __io,
2010 ios_base::iostate& __err, unsigned long& __v) const
2011 { return this->do_get(__in, __end, __io, __err, __v); }
2012
2013 #ifdef _GLIBCXX_USE_LONG_LONG
2014 iter_type
2015 get(iter_type __in, iter_type __end, ios_base& __io,
2016 ios_base::iostate& __err, long long& __v) const
2017 { return this->do_get(__in, __end, __io, __err, __v); }
2018
2019 iter_type
2020 get(iter_type __in, iter_type __end, ios_base& __io,
2021 ios_base::iostate& __err, unsigned long long& __v) const
2022 { return this->do_get(__in, __end, __io, __err, __v); }
2023 #endif
2024 //@}
2025
2026 //@{
2027 /**
2028 * @brief Numeric parsing.
2029 *
2030 * Parses the input stream into the integral variable @a v. It does so
2031 * by calling num_get::do_get().
2032 *
2033 * The input characters are parsed like the scanf %g specifier. The
2034 * matching type length modifier is also used.
2035 *
2036 * The decimal point character used is numpunct::decimal_point().
2037 * Digit grouping is interpreted according to numpunct::grouping() and
2038 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2039 * consistent, sets err to ios_base::failbit.
2040 *
2041 * If parsing the string yields a valid value for @a v, @a v is set.
2042 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2043 * Sets err to ios_base::eofbit if the stream is emptied.
2044 *
2045 * @param in Start of input stream.
2046 * @param end End of input stream.
2047 * @param io Source of locale and flags.
2048 * @param err Error flags to set.
2049 * @param v Value to format and insert.
2050 * @return Iterator after reading.
2051 */
2052 iter_type
2053 get(iter_type __in, iter_type __end, ios_base& __io,
2054 ios_base::iostate& __err, float& __v) const
2055 { return this->do_get(__in, __end, __io, __err, __v); }
2056
2057 iter_type
2058 get(iter_type __in, iter_type __end, ios_base& __io,
2059 ios_base::iostate& __err, double& __v) const
2060 { return this->do_get(__in, __end, __io, __err, __v); }
2061
2062 iter_type
2063 get(iter_type __in, iter_type __end, ios_base& __io,
2064 ios_base::iostate& __err, long double& __v) const
2065 { return this->do_get(__in, __end, __io, __err, __v); }
2066 //@}
2067
2068 /**
2069 * @brief Numeric parsing.
2070 *
2071 * Parses the input stream into the pointer variable @a v. It does so
2072 * by calling num_get::do_get().
2073 *
2074 * The input characters are parsed like the scanf %p specifier.
2075 *
2076 * Digit grouping is interpreted according to numpunct::grouping() and
2077 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2078 * consistent, sets err to ios_base::failbit.
2079 *
2080 * Note that the digit grouping effect for pointers is a bit ambiguous
2081 * in the standard and shouldn't be relied on. See DR 344.
2082 *
2083 * If parsing the string yields a valid value for @a v, @a v is set.
2084 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2085 * Sets err to ios_base::eofbit if the stream is emptied.
2086 *
2087 * @param in Start of input stream.
2088 * @param end End of input stream.
2089 * @param io Source of locale and flags.
2090 * @param err Error flags to set.
2091 * @param v Value to format and insert.
2092 * @return Iterator after reading.
2093 */
2094 iter_type
2095 get(iter_type __in, iter_type __end, ios_base& __io,
2096 ios_base::iostate& __err, void*& __v) const
2097 { return this->do_get(__in, __end, __io, __err, __v); }
2098
2099 protected:
2100 /// Destructor.
2101 virtual ~num_get() { }
2102
2103 iter_type
2104 _M_extract_float(iter_type, iter_type, ios_base&, ios_base::iostate&,
2105 string&) const;
2106
2107 template<typename _ValueT>
2108 iter_type
2109 _M_extract_int(iter_type, iter_type, ios_base&, ios_base::iostate&,
2110 _ValueT&) const;
2111
2112 template<typename _CharT2>
2113 typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, int>::__type
2114 _M_find(const _CharT2*, size_t __len, _CharT2 __c) const
2115 {
2116 int __ret = -1;
2117 if (__len <= 10)
2118 {
2119 if (__c >= _CharT2('0') && __c < _CharT2(_CharT2('0') + __len))
2120 __ret = __c - _CharT2('0');
2121 }
2122 else
2123 {
2124 if (__c >= _CharT2('0') && __c <= _CharT2('9'))
2125 __ret = __c - _CharT2('0');
2126 else if (__c >= _CharT2('a') && __c <= _CharT2('f'))
2127 __ret = 10 + (__c - _CharT2('a'));
2128 else if (__c >= _CharT2('A') && __c <= _CharT2('F'))
2129 __ret = 10 + (__c - _CharT2('A'));
2130 }
2131 return __ret;
2132 }
2133
2134 template<typename _CharT2>
2135 typename __gnu_cxx::__enable_if<!__is_char<_CharT2>::__value,
2136 int>::__type
2137 _M_find(const _CharT2* __zero, size_t __len, _CharT2 __c) const
2138 {
2139 int __ret = -1;
2140 const char_type* __q = char_traits<_CharT2>::find(__zero, __len, __c);
2141 if (__q)
2142 {
2143 __ret = __q - __zero;
2144 if (__ret > 15)
2145 __ret -= 6;
2146 }
2147 return __ret;
2148 }
2149
2150 //@{
2151 /**
2152 * @brief Numeric parsing.
2153 *
2154 * Parses the input stream into the variable @a v. This function is a
2155 * hook for derived classes to change the value returned. @see get()
2156 * for more details.
2157 *
2158 * @param in Start of input stream.
2159 * @param end End of input stream.
2160 * @param io Source of locale and flags.
2161 * @param err Error flags to set.
2162 * @param v Value to format and insert.
2163 * @return Iterator after reading.
2164 */
2165 virtual iter_type
2166 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const;
2167
2168 virtual iter_type
2169 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2170 ios_base::iostate& __err, long& __v) const
2171 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2172
2173 virtual iter_type
2174 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2175 ios_base::iostate& __err, unsigned short& __v) const
2176 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2177
2178 virtual iter_type
2179 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2180 ios_base::iostate& __err, unsigned int& __v) const
2181 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2182
2183 virtual iter_type
2184 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2185 ios_base::iostate& __err, unsigned long& __v) const
2186 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2187
2188 #ifdef _GLIBCXX_USE_LONG_LONG
2189 virtual iter_type
2190 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2191 ios_base::iostate& __err, long long& __v) const
2192 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2193
2194 virtual iter_type
2195 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2196 ios_base::iostate& __err, unsigned long long& __v) const
2197 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2198 #endif
2199
2200 virtual iter_type
2201 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2202 float&) const;
2203
2204 virtual iter_type
2205 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2206 double&) const;
2207
2208 // XXX GLIBCXX_ABI Deprecated
2209 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2210 virtual iter_type
2211 __do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2212 double&) const;
2213 #else
2214 virtual iter_type
2215 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2216 long double&) const;
2217 #endif
2218
2219 virtual iter_type
2220 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2221 void*&) const;
2222
2223 // XXX GLIBCXX_ABI Deprecated
2224 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2225 virtual iter_type
2226 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2227 long double&) const;
2228 #endif
2229 //@}
2230 };
2231
2232 template<typename _CharT, typename _InIter>
2233 locale::id num_get<_CharT, _InIter>::id;
2234
2235
2236 /**
2237 * @brief Primary class template num_put.
2238 * @ingroup locales
2239 *
2240 * This facet encapsulates the code to convert a number to a string. It is
2241 * used by the ostream numeric insertion operators.
2242 *
2243 * The num_put template uses protected virtual functions to provide the
2244 * actual results. The public accessors forward the call to the virtual
2245 * functions. These virtual functions are hooks for developers to
2246 * implement the behavior they require from the num_put facet.
2247 */
2248 template<typename _CharT, typename _OutIter>
2249 class num_put : public locale::facet
2250 {
2251 public:
2252 // Types:
2253 //@{
2254 /// Public typedefs
2255 typedef _CharT char_type;
2256 typedef _OutIter iter_type;
2257 //@}
2258
2259 /// Numpunct facet id.
2260 static locale::id id;
2261
2262 /**
2263 * @brief Constructor performs initialization.
2264 *
2265 * This is the constructor provided by the standard.
2266 *
2267 * @param refs Passed to the base facet class.
2268 */
2269 explicit
2270 num_put(size_t __refs = 0) : facet(__refs) { }
2271
2272 /**
2273 * @brief Numeric formatting.
2274 *
2275 * Formats the boolean @a v and inserts it into a stream. It does so
2276 * by calling num_put::do_put().
2277 *
2278 * If ios_base::boolalpha is set, writes ctype<CharT>::truename() or
2279 * ctype<CharT>::falsename(). Otherwise formats @a v as an int.
2280 *
2281 * @param s Stream to write to.
2282 * @param io Source of locale and flags.
2283 * @param fill Char_type to use for filling.
2284 * @param v Value to format and insert.
2285 * @return Iterator after writing.
2286 */
2287 iter_type
2288 put(iter_type __s, ios_base& __f, char_type __fill, bool __v) const
2289 { return this->do_put(__s, __f, __fill, __v); }
2290
2291 //@{
2292 /**
2293 * @brief Numeric formatting.
2294 *
2295 * Formats the integral value @a v and inserts it into a
2296 * stream. It does so by calling num_put::do_put().
2297 *
2298 * Formatting is affected by the flag settings in @a io.
2299 *
2300 * The basic format is affected by the value of io.flags() &
2301 * ios_base::basefield. If equal to ios_base::oct, formats like the
2302 * printf %o specifier. Else if equal to ios_base::hex, formats like
2303 * %x or %X with ios_base::uppercase unset or set respectively.
2304 * Otherwise, formats like %d, %ld, %lld for signed and %u, %lu, %llu
2305 * for unsigned values. Note that if both oct and hex are set, neither
2306 * will take effect.
2307 *
2308 * If ios_base::showpos is set, '+' is output before positive values.
2309 * If ios_base::showbase is set, '0' precedes octal values (except 0)
2310 * and '0[xX]' precedes hex values.
2311 *
2312 * Thousands separators are inserted according to numpunct::grouping()
2313 * and numpunct::thousands_sep(). The decimal point character used is
2314 * numpunct::decimal_point().
2315 *
2316 * If io.width() is non-zero, enough @a fill characters are inserted to
2317 * make the result at least that wide. If
2318 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2319 * padded at the end. If ios_base::internal, then padding occurs
2320 * immediately after either a '+' or '-' or after '0x' or '0X'.
2321 * Otherwise, padding occurs at the beginning.
2322 *
2323 * @param s Stream to write to.
2324 * @param io Source of locale and flags.
2325 * @param fill Char_type to use for filling.
2326 * @param v Value to format and insert.
2327 * @return Iterator after writing.
2328 */
2329 iter_type
2330 put(iter_type __s, ios_base& __f, char_type __fill, long __v) const
2331 { return this->do_put(__s, __f, __fill, __v); }
2332
2333 iter_type
2334 put(iter_type __s, ios_base& __f, char_type __fill,
2335 unsigned long __v) const
2336 { return this->do_put(__s, __f, __fill, __v); }
2337
2338 #ifdef _GLIBCXX_USE_LONG_LONG
2339 iter_type
2340 put(iter_type __s, ios_base& __f, char_type __fill, long long __v) const
2341 { return this->do_put(__s, __f, __fill, __v); }
2342
2343 iter_type
2344 put(iter_type __s, ios_base& __f, char_type __fill,
2345 unsigned long long __v) const
2346 { return this->do_put(__s, __f, __fill, __v); }
2347 #endif
2348 //@}
2349
2350 //@{
2351 /**
2352 * @brief Numeric formatting.
2353 *
2354 * Formats the floating point value @a v and inserts it into a stream.
2355 * It does so by calling num_put::do_put().
2356 *
2357 * Formatting is affected by the flag settings in @a io.
2358 *
2359 * The basic format is affected by the value of io.flags() &
2360 * ios_base::floatfield. If equal to ios_base::fixed, formats like the
2361 * printf %f specifier. Else if equal to ios_base::scientific, formats
2362 * like %e or %E with ios_base::uppercase unset or set respectively.
2363 * Otherwise, formats like %g or %G depending on uppercase. Note that
2364 * if both fixed and scientific are set, the effect will also be like
2365 * %g or %G.
2366 *
2367 * The output precision is given by io.precision(). This precision is
2368 * capped at numeric_limits::digits10 + 2 (different for double and
2369 * long double). The default precision is 6.
2370 *
2371 * If ios_base::showpos is set, '+' is output before positive values.
2372 * If ios_base::showpoint is set, a decimal point will always be
2373 * output.
2374 *
2375 * Thousands separators are inserted according to numpunct::grouping()
2376 * and numpunct::thousands_sep(). The decimal point character used is
2377 * numpunct::decimal_point().
2378 *
2379 * If io.width() is non-zero, enough @a fill characters are inserted to
2380 * make the result at least that wide. If
2381 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2382 * padded at the end. If ios_base::internal, then padding occurs
2383 * immediately after either a '+' or '-' or after '0x' or '0X'.
2384 * Otherwise, padding occurs at the beginning.
2385 *
2386 * @param s Stream to write to.
2387 * @param io Source of locale and flags.
2388 * @param fill Char_type to use for filling.
2389 * @param v Value to format and insert.
2390 * @return Iterator after writing.
2391 */
2392 iter_type
2393 put(iter_type __s, ios_base& __f, char_type __fill, double __v) const
2394 { return this->do_put(__s, __f, __fill, __v); }
2395
2396 iter_type
2397 put(iter_type __s, ios_base& __f, char_type __fill,
2398 long double __v) const
2399 { return this->do_put(__s, __f, __fill, __v); }
2400 //@}
2401
2402 /**
2403 * @brief Numeric formatting.
2404 *
2405 * Formats the pointer value @a v and inserts it into a stream. It
2406 * does so by calling num_put::do_put().
2407 *
2408 * This function formats @a v as an unsigned long with ios_base::hex
2409 * and ios_base::showbase set.
2410 *
2411 * @param s Stream to write to.
2412 * @param io Source of locale and flags.
2413 * @param fill Char_type to use for filling.
2414 * @param v Value to format and insert.
2415 * @return Iterator after writing.
2416 */
2417 iter_type
2418 put(iter_type __s, ios_base& __f, char_type __fill,
2419 const void* __v) const
2420 { return this->do_put(__s, __f, __fill, __v); }
2421
2422 protected:
2423 template<typename _ValueT>
2424 iter_type
2425 _M_insert_float(iter_type, ios_base& __io, char_type __fill,
2426 char __mod, _ValueT __v) const;
2427
2428 void
2429 _M_group_float(const char* __grouping, size_t __grouping_size,
2430 char_type __sep, const char_type* __p, char_type* __new,
2431 char_type* __cs, int& __len) const;
2432
2433 template<typename _ValueT>
2434 iter_type
2435 _M_insert_int(iter_type, ios_base& __io, char_type __fill,
2436 _ValueT __v) const;
2437
2438 void
2439 _M_group_int(const char* __grouping, size_t __grouping_size,
2440 char_type __sep, ios_base& __io, char_type* __new,
2441 char_type* __cs, int& __len) const;
2442
2443 void
2444 _M_pad(char_type __fill, streamsize __w, ios_base& __io,
2445 char_type* __new, const char_type* __cs, int& __len) const;
2446
2447 /// Destructor.
2448 virtual
2449 ~num_put() { };
2450
2451 //@{
2452 /**
2453 * @brief Numeric formatting.
2454 *
2455 * These functions do the work of formatting numeric values and
2456 * inserting them into a stream. This function is a hook for derived
2457 * classes to change the value returned.
2458 *
2459 * @param s Stream to write to.
2460 * @param io Source of locale and flags.
2461 * @param fill Char_type to use for filling.
2462 * @param v Value to format and insert.
2463 * @return Iterator after writing.
2464 */
2465 virtual iter_type
2466 do_put(iter_type, ios_base&, char_type __fill, bool __v) const;
2467
2468 virtual iter_type
2469 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
2470 { return _M_insert_int(__s, __io, __fill, __v); }
2471
2472 virtual iter_type
2473 do_put(iter_type __s, ios_base& __io, char_type __fill,
2474 unsigned long __v) const
2475 { return _M_insert_int(__s, __io, __fill, __v); }
2476
2477 #ifdef _GLIBCXX_USE_LONG_LONG
2478 virtual iter_type
2479 do_put(iter_type __s, ios_base& __io, char_type __fill,
2480 long long __v) const
2481 { return _M_insert_int(__s, __io, __fill, __v); }
2482
2483 virtual iter_type
2484 do_put(iter_type __s, ios_base& __io, char_type __fill,
2485 unsigned long long __v) const
2486 { return _M_insert_int(__s, __io, __fill, __v); }
2487 #endif
2488
2489 virtual iter_type
2490 do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2491
2492 // XXX GLIBCXX_ABI Deprecated
2493 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2494 virtual iter_type
2495 __do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2496 #else
2497 virtual iter_type
2498 do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2499 #endif
2500
2501 virtual iter_type
2502 do_put(iter_type, ios_base&, char_type __fill, const void* __v) const;
2503
2504 // XXX GLIBCXX_ABI Deprecated
2505 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2506 virtual iter_type
2507 do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2508 #endif
2509 //@}
2510 };
2511
2512 template <typename _CharT, typename _OutIter>
2513 locale::id num_put<_CharT, _OutIter>::id;
2514
2515 _GLIBCXX_END_LDBL_NAMESPACE
2516
2517 // Subclause convenience interfaces, inlines.
2518 // NB: These are inline because, when used in a loop, some compilers
2519 // can hoist the body out of the loop; then it's just as fast as the
2520 // C is*() function.
2521
2522 /// Convenience interface to ctype.is(ctype_base::space, __c).
2523 template<typename _CharT>
2524 inline bool
2525 isspace(_CharT __c, const locale& __loc)
2526 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); }
2527
2528 /// Convenience interface to ctype.is(ctype_base::print, __c).
2529 template<typename _CharT>
2530 inline bool
2531 isprint(_CharT __c, const locale& __loc)
2532 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); }
2533
2534 /// Convenience interface to ctype.is(ctype_base::cntrl, __c).
2535 template<typename _CharT>
2536 inline bool
2537 iscntrl(_CharT __c, const locale& __loc)
2538 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); }
2539
2540 /// Convenience interface to ctype.is(ctype_base::upper, __c).
2541 template<typename _CharT>
2542 inline bool
2543 isupper(_CharT __c, const locale& __loc)
2544 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); }
2545
2546 /// Convenience interface to ctype.is(ctype_base::lower, __c).
2547 template<typename _CharT>
2548 inline bool
2549 islower(_CharT __c, const locale& __loc)
2550 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); }
2551
2552 /// Convenience interface to ctype.is(ctype_base::alpha, __c).
2553 template<typename _CharT>
2554 inline bool
2555 isalpha(_CharT __c, const locale& __loc)
2556 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); }
2557
2558 /// Convenience interface to ctype.is(ctype_base::digit, __c).
2559 template<typename _CharT>
2560 inline bool
2561 isdigit(_CharT __c, const locale& __loc)
2562 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); }
2563
2564 /// Convenience interface to ctype.is(ctype_base::punct, __c).
2565 template<typename _CharT>
2566 inline bool
2567 ispunct(_CharT __c, const locale& __loc)
2568 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); }
2569
2570 /// Convenience interface to ctype.is(ctype_base::xdigit, __c).
2571 template<typename _CharT>
2572 inline bool
2573 isxdigit(_CharT __c, const locale& __loc)
2574 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); }
2575
2576 /// Convenience interface to ctype.is(ctype_base::alnum, __c).
2577 template<typename _CharT>
2578 inline bool
2579 isalnum(_CharT __c, const locale& __loc)
2580 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); }
2581
2582 /// Convenience interface to ctype.is(ctype_base::graph, __c).
2583 template<typename _CharT>
2584 inline bool
2585 isgraph(_CharT __c, const locale& __loc)
2586 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); }
2587
2588 /// Convenience interface to ctype.toupper(__c).
2589 template<typename _CharT>
2590 inline _CharT
2591 toupper(_CharT __c, const locale& __loc)
2592 { return use_facet<ctype<_CharT> >(__loc).toupper(__c); }
2593
2594 /// Convenience interface to ctype.tolower(__c).
2595 template<typename _CharT>
2596 inline _CharT
2597 tolower(_CharT __c, const locale& __loc)
2598 { return use_facet<ctype<_CharT> >(__loc).tolower(__c); }
2599
2600 _GLIBCXX_END_NAMESPACE
2601
2602 # include <bits/locale_facets.tcc>
2603
2604 #endif