1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011
5 // Free Software Foundation, Inc.
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)
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.
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.
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/>.
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}
33 // ISO C++ 14882: 22.1 Locales
36 #ifndef _LOCALE_FACETS_H
37 #define _LOCALE_FACETS_H 1
39 #pragma GCC system_header
41 #include <cwctype> // For wctype_t
43 #include <bits/ctype_base.h>
45 #include <bits/ios_base.h> // For ios_base, ios_base::iostate
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>
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 // NB: Don't instantiate required wchar_t facets if no wchar_t support.
57 #ifdef _GLIBCXX_USE_WCHAR_T
58 # define _GLIBCXX_NUM_FACETS 28
60 # define _GLIBCXX_NUM_FACETS 14
63 // Convert string to numeric value of type _Tp and store results.
64 // NB: This is specialized for all required types, there is no
65 // generic definition.
66 template<typename _Tp>
68 __convert_to_v(const char*, _Tp&, ios_base::iostate&,
69 const __c_locale&) throw();
71 // Explicit specializations for required types.
74 __convert_to_v(const char*, float&, ios_base::iostate&,
75 const __c_locale&) throw();
79 __convert_to_v(const char*, double&, ios_base::iostate&,
80 const __c_locale&) throw();
84 __convert_to_v(const char*, long double&, ios_base::iostate&,
85 const __c_locale&) throw();
87 // NB: __pad is a struct, rather than a function, so it can be
88 // partially-specialized.
89 template<typename _CharT, typename _Traits>
93 _S_pad(ios_base& __io, _CharT __fill, _CharT* __news,
94 const _CharT* __olds, streamsize __newlen, streamsize __oldlen);
97 // Used by both numeric and monetary facets.
98 // Inserts "group separator" characters into an array of characters.
99 // It's recursive, one iteration per group. It moves the characters
100 // in the buffer this way: "xxxx12345" -> "12,345xxx". Call this
101 // only with __gsize != 0.
102 template<typename _CharT>
104 __add_grouping(_CharT* __s, _CharT __sep,
105 const char* __gbeg, size_t __gsize,
106 const _CharT* __first, const _CharT* __last);
108 // This template permits specializing facet output code for
109 // ostreambuf_iterator. For ostreambuf_iterator, sputn is
110 // significantly more efficient than incrementing iterators.
111 template<typename _CharT>
113 ostreambuf_iterator<_CharT>
114 __write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len)
116 __s._M_put(__ws, __len);
120 // This is the unspecialized form of the template.
121 template<typename _CharT, typename _OutIter>
124 __write(_OutIter __s, const _CharT* __ws, int __len)
126 for (int __j = 0; __j < __len; __j++, ++__s)
132 // 22.2.1.1 Template class ctype
133 // Include host and configuration specific ctype enums for ctype_base.
136 * @brief Common base for ctype facet
138 * This template class provides implementations of the public functions
139 * that forward to the protected virtual functions.
141 * This template also provides abstract stubs for the protected virtual
144 template<typename _CharT>
145 class __ctype_abstract_base : public locale::facet, public ctype_base
149 /// Typedef for the template parameter
150 typedef _CharT char_type;
153 * @brief Test char_type classification.
155 * This function finds a mask M for @a c and compares it to mask @a m.
156 * It does so by returning the value of ctype<char_type>::do_is().
158 * @param c The char_type to compare the mask of.
159 * @param m The mask to compare against.
160 * @return (M & m) != 0.
163 is(mask __m, char_type __c) const
164 { return this->do_is(__m, __c); }
167 * @brief Return a mask array.
169 * This function finds the mask for each char_type in the range [lo,hi)
170 * and successively writes it to vec. vec must have as many elements
171 * as the char array. It does so by returning the value of
172 * ctype<char_type>::do_is().
174 * @param lo Pointer to start of range.
175 * @param hi Pointer to end of range.
176 * @param vec Pointer to an array of mask storage.
180 is(const char_type *__lo, const char_type *__hi, mask *__vec) const
181 { return this->do_is(__lo, __hi, __vec); }
184 * @brief Find char_type matching a mask
186 * This function searches for and returns the first char_type c in
187 * [lo,hi) for which is(m,c) is true. It does so by returning
188 * ctype<char_type>::do_scan_is().
190 * @param m The mask to compare against.
191 * @param lo Pointer to start of range.
192 * @param hi Pointer to end of range.
193 * @return Pointer to matching char_type if found, else @a hi.
196 scan_is(mask __m, const char_type* __lo, const char_type* __hi) const
197 { return this->do_scan_is(__m, __lo, __hi); }
200 * @brief Find char_type not matching a mask
202 * This function searches for and returns the first char_type c in
203 * [lo,hi) for which is(m,c) is false. It does so by returning
204 * ctype<char_type>::do_scan_not().
206 * @param m The mask to compare against.
207 * @param lo Pointer to first char in range.
208 * @param hi Pointer to end of range.
209 * @return Pointer to non-matching char if found, else @a hi.
212 scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
213 { return this->do_scan_not(__m, __lo, __hi); }
216 * @brief Convert to uppercase.
218 * This function converts the argument to uppercase if possible.
219 * If not possible (for example, '2'), returns the argument. It does
220 * so by returning ctype<char_type>::do_toupper().
222 * @param c The char_type to convert.
223 * @return The uppercase char_type if convertible, else @a c.
226 toupper(char_type __c) const
227 { return this->do_toupper(__c); }
230 * @brief Convert array to uppercase.
232 * This function converts each char_type in the range [lo,hi) to
233 * uppercase if possible. Other elements remain untouched. It does so
234 * by returning ctype<char_type>:: do_toupper(lo, hi).
236 * @param lo Pointer to start of range.
237 * @param hi Pointer to end of range.
241 toupper(char_type *__lo, const char_type* __hi) const
242 { return this->do_toupper(__lo, __hi); }
245 * @brief Convert to lowercase.
247 * This function converts the argument to lowercase if possible. If
248 * not possible (for example, '2'), returns the argument. It does so
249 * by returning ctype<char_type>::do_tolower(c).
251 * @param c The char_type to convert.
252 * @return The lowercase char_type if convertible, else @a c.
255 tolower(char_type __c) const
256 { return this->do_tolower(__c); }
259 * @brief Convert array to lowercase.
261 * This function converts each char_type in the range [lo,hi) to
262 * lowercase if possible. Other elements remain untouched. It does so
263 * by returning ctype<char_type>:: do_tolower(lo, hi).
265 * @param lo Pointer to start of range.
266 * @param hi Pointer to end of range.
270 tolower(char_type* __lo, const char_type* __hi) const
271 { return this->do_tolower(__lo, __hi); }
274 * @brief Widen char to char_type
276 * This function converts the char argument to char_type using the
277 * simplest reasonable transformation. It does so by returning
278 * ctype<char_type>::do_widen(c).
280 * Note: this is not what you want for codepage conversions. See
283 * @param c The char to convert.
284 * @return The converted char_type.
287 widen(char __c) const
288 { return this->do_widen(__c); }
291 * @brief Widen array to char_type
293 * This function converts each char in the input to char_type using the
294 * simplest reasonable transformation. It does so by returning
295 * ctype<char_type>::do_widen(c).
297 * Note: this is not what you want for codepage conversions. See
300 * @param lo Pointer to start of range.
301 * @param hi Pointer to end of range.
302 * @param to Pointer to the destination array.
306 widen(const char* __lo, const char* __hi, char_type* __to) const
307 { return this->do_widen(__lo, __hi, __to); }
310 * @brief Narrow char_type to char
312 * This function converts the char_type to char using the simplest
313 * reasonable transformation. If the conversion fails, dfault is
314 * returned instead. It does so by returning
315 * ctype<char_type>::do_narrow(c).
317 * Note: this is not what you want for codepage conversions. See
320 * @param c The char_type to convert.
321 * @param dfault Char to return if conversion fails.
322 * @return The converted char.
325 narrow(char_type __c, char __dfault) const
326 { return this->do_narrow(__c, __dfault); }
329 * @brief Narrow array to char array
331 * This function converts each char_type in the input to char using the
332 * simplest reasonable transformation and writes the results to the
333 * destination array. For any char_type in the input that cannot be
334 * converted, @a dfault is used instead. It does so by returning
335 * ctype<char_type>::do_narrow(lo, hi, dfault, to).
337 * Note: this is not what you want for codepage conversions. See
340 * @param lo Pointer to start of range.
341 * @param hi Pointer to end of range.
342 * @param dfault Char to use if conversion fails.
343 * @param to Pointer to the destination array.
347 narrow(const char_type* __lo, const char_type* __hi,
348 char __dfault, char *__to) const
349 { return this->do_narrow(__lo, __hi, __dfault, __to); }
353 __ctype_abstract_base(size_t __refs = 0): facet(__refs) { }
356 ~__ctype_abstract_base() { }
359 * @brief Test char_type classification.
361 * This function finds a mask M for @a c and compares it to mask @a m.
363 * do_is() is a hook for a derived facet to change the behavior of
364 * classifying. do_is() must always return the same result for the
367 * @param c The char_type to find the mask of.
368 * @param m The mask to compare against.
369 * @return (M & m) != 0.
372 do_is(mask __m, char_type __c) const = 0;
375 * @brief Return a mask array.
377 * This function finds the mask for each char_type in the range [lo,hi)
378 * and successively writes it to vec. vec must have as many elements
381 * do_is() is a hook for a derived facet to change the behavior of
382 * classifying. do_is() must always return the same result for the
385 * @param lo Pointer to start of range.
386 * @param hi Pointer to end of range.
387 * @param vec Pointer to an array of mask storage.
390 virtual const char_type*
391 do_is(const char_type* __lo, const char_type* __hi,
392 mask* __vec) const = 0;
395 * @brief Find char_type matching mask
397 * This function searches for and returns the first char_type c in
398 * [lo,hi) for which is(m,c) is true.
400 * do_scan_is() is a hook for a derived facet to change the behavior of
401 * match searching. do_is() must always return the same result for the
404 * @param m The mask to compare against.
405 * @param lo Pointer to start of range.
406 * @param hi Pointer to end of range.
407 * @return Pointer to a matching char_type if found, else @a hi.
409 virtual const char_type*
410 do_scan_is(mask __m, const char_type* __lo,
411 const char_type* __hi) const = 0;
414 * @brief Find char_type not matching mask
416 * This function searches for and returns a pointer to the first
417 * char_type c of [lo,hi) for which is(m,c) is false.
419 * do_scan_is() is a hook for a derived facet to change the behavior of
420 * match searching. do_is() must always return the same result for the
423 * @param m The mask to compare against.
424 * @param lo Pointer to start of range.
425 * @param hi Pointer to end of range.
426 * @return Pointer to a non-matching char_type if found, else @a hi.
428 virtual const char_type*
429 do_scan_not(mask __m, const char_type* __lo,
430 const char_type* __hi) const = 0;
433 * @brief Convert to uppercase.
435 * This virtual function converts the char_type argument to uppercase
436 * if possible. If not possible (for example, '2'), returns the
439 * do_toupper() is a hook for a derived facet to change the behavior of
440 * uppercasing. do_toupper() must always return the same result for
443 * @param c The char_type to convert.
444 * @return The uppercase char_type if convertible, else @a c.
447 do_toupper(char_type) const = 0;
450 * @brief Convert array to uppercase.
452 * This virtual function converts each char_type in the range [lo,hi)
453 * to uppercase if possible. Other elements remain untouched.
455 * do_toupper() is a hook for a derived facet to change the behavior of
456 * uppercasing. do_toupper() must always return the same result for
459 * @param lo Pointer to start of range.
460 * @param hi Pointer to end of range.
463 virtual const char_type*
464 do_toupper(char_type* __lo, const char_type* __hi) const = 0;
467 * @brief Convert to lowercase.
469 * This virtual function converts the argument to lowercase if
470 * possible. If not possible (for example, '2'), returns the argument.
472 * do_tolower() is a hook for a derived facet to change the behavior of
473 * lowercasing. do_tolower() must always return the same result for
476 * @param c The char_type to convert.
477 * @return The lowercase char_type if convertible, else @a c.
480 do_tolower(char_type) const = 0;
483 * @brief Convert array to lowercase.
485 * This virtual function converts each char_type in the range [lo,hi)
486 * to lowercase if possible. Other elements remain untouched.
488 * do_tolower() is a hook for a derived facet to change the behavior of
489 * lowercasing. do_tolower() must always return the same result for
492 * @param lo Pointer to start of range.
493 * @param hi Pointer to end of range.
496 virtual const char_type*
497 do_tolower(char_type* __lo, const char_type* __hi) const = 0;
502 * This virtual function converts the char to char_type using the
503 * simplest reasonable transformation.
505 * do_widen() is a hook for a derived facet to change the behavior of
506 * widening. do_widen() must always return the same result for the
509 * Note: this is not what you want for codepage conversions. See
512 * @param c The char to convert.
513 * @return The converted char_type
516 do_widen(char) const = 0;
519 * @brief Widen char array
521 * This function converts each char in the input to char_type using the
522 * simplest reasonable transformation.
524 * do_widen() is a hook for a derived facet to change the behavior of
525 * widening. do_widen() must always return the same result for the
528 * Note: this is not what you want for codepage conversions. See
531 * @param lo Pointer to start range.
532 * @param hi Pointer to end of range.
533 * @param to Pointer to the destination array.
537 do_widen(const char* __lo, const char* __hi,
538 char_type* __dest) const = 0;
541 * @brief Narrow char_type to char
543 * This virtual function converts the argument to char using the
544 * simplest reasonable transformation. If the conversion fails, dfault
545 * is returned instead.
547 * do_narrow() is a hook for a derived facet to change the behavior of
548 * narrowing. do_narrow() must always return the same result for the
551 * Note: this is not what you want for codepage conversions. See
554 * @param c The char_type to convert.
555 * @param dfault Char to return if conversion fails.
556 * @return The converted char.
559 do_narrow(char_type, char __dfault) const = 0;
562 * @brief Narrow char_type array to char
564 * This virtual function converts each char_type in the range [lo,hi) to
565 * char using the simplest reasonable transformation and writes the
566 * results to the destination array. For any element in the input that
567 * cannot be converted, @a dfault is used instead.
569 * do_narrow() is a hook for a derived facet to change the behavior of
570 * narrowing. do_narrow() must always return the same result for the
573 * Note: this is not what you want for codepage conversions. See
576 * @param lo Pointer to start of range.
577 * @param hi Pointer to end of range.
578 * @param dfault Char to use if conversion fails.
579 * @param to Pointer to the destination array.
582 virtual const char_type*
583 do_narrow(const char_type* __lo, const char_type* __hi,
584 char __dfault, char* __dest) const = 0;
588 * @brief Primary class template ctype facet.
591 * This template class defines classification and conversion functions for
592 * character sets. It wraps cctype functionality. Ctype gets used by
593 * streams for many I/O operations.
595 * This template provides the protected virtual functions the developer
596 * will have to replace in a derived class or specialization to make a
597 * working facet. The public functions that access them are defined in
598 * __ctype_abstract_base, to allow for implementation flexibility. See
599 * ctype<wchar_t> for an example. The functions are documented in
600 * __ctype_abstract_base.
602 * Note: implementations are provided for all the protected virtual
603 * functions, but will likely not be useful.
605 template<typename _CharT>
606 class ctype : public __ctype_abstract_base<_CharT>
610 typedef _CharT char_type;
611 typedef typename __ctype_abstract_base<_CharT>::mask mask;
613 /// The facet id for ctype<char_type>
614 static locale::id id;
617 ctype(size_t __refs = 0) : __ctype_abstract_base<_CharT>(__refs) { }
624 do_is(mask __m, char_type __c) const;
626 virtual const char_type*
627 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
629 virtual const char_type*
630 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
632 virtual const char_type*
633 do_scan_not(mask __m, const char_type* __lo,
634 const char_type* __hi) const;
637 do_toupper(char_type __c) const;
639 virtual const char_type*
640 do_toupper(char_type* __lo, const char_type* __hi) const;
643 do_tolower(char_type __c) const;
645 virtual const char_type*
646 do_tolower(char_type* __lo, const char_type* __hi) const;
649 do_widen(char __c) const;
652 do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
655 do_narrow(char_type, char __dfault) const;
657 virtual const char_type*
658 do_narrow(const char_type* __lo, const char_type* __hi,
659 char __dfault, char* __dest) const;
662 template<typename _CharT>
663 locale::id ctype<_CharT>::id;
666 * @brief The ctype<char> specialization.
669 * This class defines classification and conversion functions for
670 * the char type. It gets used by char streams for many I/O
671 * operations. The char specialization provides a number of
672 * optimizations as well.
675 class ctype<char> : public locale::facet, public ctype_base
679 /// Typedef for the template parameter char.
680 typedef char char_type;
684 __c_locale _M_c_locale_ctype;
686 __to_type _M_toupper;
687 __to_type _M_tolower;
688 const mask* _M_table;
689 mutable char _M_widen_ok;
690 mutable char _M_widen[1 + static_cast<unsigned char>(-1)];
691 mutable char _M_narrow[1 + static_cast<unsigned char>(-1)];
692 mutable char _M_narrow_ok; // 0 uninitialized, 1 init,
693 // 2 memcpy can't be used
696 /// The facet id for ctype<char>
697 static locale::id id;
698 /// The size of the mask table. It is SCHAR_MAX + 1.
699 static const size_t table_size = 1 + static_cast<unsigned char>(-1);
702 * @brief Constructor performs initialization.
704 * This is the constructor provided by the standard.
706 * @param table If non-zero, table is used as the per-char mask.
707 * Else classic_table() is used.
708 * @param del If true, passes ownership of table to this facet.
709 * @param refs Passed to the base facet class.
712 ctype(const mask* __table = 0, bool __del = false, size_t __refs = 0);
715 * @brief Constructor performs static initialization.
717 * This constructor is used to construct the initial C locale facet.
719 * @param cloc Handle to C locale data.
720 * @param table If non-zero, table is used as the per-char mask.
721 * @param del If true, passes ownership of table to this facet.
722 * @param refs Passed to the base facet class.
725 ctype(__c_locale __cloc, const mask* __table = 0, bool __del = false,
729 * @brief Test char classification.
731 * This function compares the mask table[c] to @a m.
733 * @param c The char to compare the mask of.
734 * @param m The mask to compare against.
735 * @return True if m & table[c] is true, false otherwise.
738 is(mask __m, char __c) const;
741 * @brief Return a mask array.
743 * This function finds the mask for each char in the range [lo, hi) and
744 * successively writes it to vec. vec must have as many elements as
747 * @param lo Pointer to start of range.
748 * @param hi Pointer to end of range.
749 * @param vec Pointer to an array of mask storage.
753 is(const char* __lo, const char* __hi, mask* __vec) const;
756 * @brief Find char matching a mask
758 * This function searches for and returns the first char in [lo,hi) for
759 * which is(m,char) is true.
761 * @param m The mask to compare against.
762 * @param lo Pointer to start of range.
763 * @param hi Pointer to end of range.
764 * @return Pointer to a matching char if found, else @a hi.
767 scan_is(mask __m, const char* __lo, const char* __hi) const;
770 * @brief Find char not matching a mask
772 * This function searches for and returns a pointer to the first char
773 * in [lo,hi) for which is(m,char) is false.
775 * @param m The mask to compare against.
776 * @param lo Pointer to start of range.
777 * @param hi Pointer to end of range.
778 * @return Pointer to a non-matching char if found, else @a hi.
781 scan_not(mask __m, const char* __lo, const char* __hi) const;
784 * @brief Convert to uppercase.
786 * This function converts the char argument to uppercase if possible.
787 * If not possible (for example, '2'), returns the argument.
789 * toupper() acts as if it returns ctype<char>::do_toupper(c).
790 * do_toupper() must always return the same result for the same input.
792 * @param c The char to convert.
793 * @return The uppercase char if convertible, else @a c.
796 toupper(char_type __c) const
797 { return this->do_toupper(__c); }
800 * @brief Convert array to uppercase.
802 * This function converts each char in the range [lo,hi) to uppercase
803 * if possible. Other chars remain untouched.
805 * toupper() acts as if it returns ctype<char>:: do_toupper(lo, hi).
806 * do_toupper() must always return the same result for the same input.
808 * @param lo Pointer to first char in range.
809 * @param hi Pointer to end of range.
813 toupper(char_type *__lo, const char_type* __hi) const
814 { return this->do_toupper(__lo, __hi); }
817 * @brief Convert to lowercase.
819 * This function converts the char argument to lowercase if possible.
820 * If not possible (for example, '2'), returns the argument.
822 * tolower() acts as if it returns ctype<char>::do_tolower(c).
823 * do_tolower() must always return the same result for the same input.
825 * @param c The char to convert.
826 * @return The lowercase char if convertible, else @a c.
829 tolower(char_type __c) const
830 { return this->do_tolower(__c); }
833 * @brief Convert array to lowercase.
835 * This function converts each char in the range [lo,hi) to lowercase
836 * if possible. Other chars remain untouched.
838 * tolower() acts as if it returns ctype<char>:: do_tolower(lo, hi).
839 * do_tolower() must always return the same result for the same input.
841 * @param lo Pointer to first char in range.
842 * @param hi Pointer to end of range.
846 tolower(char_type* __lo, const char_type* __hi) const
847 { return this->do_tolower(__lo, __hi); }
852 * This function converts the char to char_type using the simplest
853 * reasonable transformation. For an underived ctype<char> facet, the
854 * argument will be returned unchanged.
856 * This function works as if it returns ctype<char>::do_widen(c).
857 * do_widen() must always return the same result for the same input.
859 * Note: this is not what you want for codepage conversions. See
862 * @param c The char to convert.
863 * @return The converted character.
866 widen(char __c) const
869 return _M_widen[static_cast<unsigned char>(__c)];
870 this->_M_widen_init();
871 return this->do_widen(__c);
875 * @brief Widen char array
877 * This function converts each char in the input to char using the
878 * simplest reasonable transformation. For an underived ctype<char>
879 * facet, the argument will be copied unchanged.
881 * This function works as if it returns ctype<char>::do_widen(c).
882 * do_widen() must always return the same result for the same input.
884 * Note: this is not what you want for codepage conversions. See
887 * @param lo Pointer to first char in range.
888 * @param hi Pointer to end of range.
889 * @param to Pointer to the destination array.
893 widen(const char* __lo, const char* __hi, char_type* __to) const
895 if (_M_widen_ok == 1)
897 __builtin_memcpy(__to, __lo, __hi - __lo);
902 return this->do_widen(__lo, __hi, __to);
908 * This function converts the char to char using the simplest
909 * reasonable transformation. If the conversion fails, dfault is
910 * returned instead. For an underived ctype<char> facet, @a c
911 * will be returned unchanged.
913 * This function works as if it returns ctype<char>::do_narrow(c).
914 * do_narrow() must always return the same result for the same input.
916 * Note: this is not what you want for codepage conversions. See
919 * @param c The char to convert.
920 * @param dfault Char to return if conversion fails.
921 * @return The converted character.
924 narrow(char_type __c, char __dfault) const
926 if (_M_narrow[static_cast<unsigned char>(__c)])
927 return _M_narrow[static_cast<unsigned char>(__c)];
928 const char __t = do_narrow(__c, __dfault);
930 _M_narrow[static_cast<unsigned char>(__c)] = __t;
935 * @brief Narrow char array
937 * This function converts each char in the input to char using the
938 * simplest reasonable transformation and writes the results to the
939 * destination array. For any char in the input that cannot be
940 * converted, @a dfault is used instead. For an underived ctype<char>
941 * facet, the argument will be copied unchanged.
943 * This function works as if it returns ctype<char>::do_narrow(lo, hi,
944 * dfault, to). do_narrow() must always return the same result for the
947 * Note: this is not what you want for codepage conversions. See
950 * @param lo Pointer to start of range.
951 * @param hi Pointer to end of range.
952 * @param dfault Char to use if conversion fails.
953 * @param to Pointer to the destination array.
957 narrow(const char_type* __lo, const char_type* __hi,
958 char __dfault, char *__to) const
960 if (__builtin_expect(_M_narrow_ok == 1, true))
962 __builtin_memcpy(__to, __lo, __hi - __lo);
967 return this->do_narrow(__lo, __hi, __dfault, __to);
970 // _GLIBCXX_RESOLVE_LIB_DEFECTS
971 // DR 695. ctype<char>::classic_table() not accessible.
972 /// Returns a pointer to the mask table provided to the constructor, or
973 /// the default from classic_table() if none was provided.
975 table() const throw()
978 /// Returns a pointer to the C locale mask table.
980 classic_table() throw();
986 * This function deletes table() if @a del was true in the
993 * @brief Convert to uppercase.
995 * This virtual function converts the char argument to uppercase if
996 * possible. If not possible (for example, '2'), returns the argument.
998 * do_toupper() is a hook for a derived facet to change the behavior of
999 * uppercasing. do_toupper() must always return the same result for
1002 * @param c The char to convert.
1003 * @return The uppercase char if convertible, else @a c.
1006 do_toupper(char_type) const;
1009 * @brief Convert array to uppercase.
1011 * This virtual function converts each char in the range [lo,hi) to
1012 * uppercase if possible. Other chars remain untouched.
1014 * do_toupper() is a hook for a derived facet to change the behavior of
1015 * uppercasing. do_toupper() must always return the same result for
1018 * @param lo Pointer to start of range.
1019 * @param hi Pointer to end of range.
1022 virtual const char_type*
1023 do_toupper(char_type* __lo, const char_type* __hi) const;
1026 * @brief Convert to lowercase.
1028 * This virtual function converts the char argument to lowercase if
1029 * possible. If not possible (for example, '2'), returns the argument.
1031 * do_tolower() is a hook for a derived facet to change the behavior of
1032 * lowercasing. do_tolower() must always return the same result for
1035 * @param c The char to convert.
1036 * @return The lowercase char if convertible, else @a c.
1039 do_tolower(char_type) const;
1042 * @brief Convert array to lowercase.
1044 * This virtual function converts each char in the range [lo,hi) to
1045 * lowercase if possible. Other chars remain untouched.
1047 * do_tolower() is a hook for a derived facet to change the behavior of
1048 * lowercasing. do_tolower() must always return the same result for
1051 * @param lo Pointer to first char in range.
1052 * @param hi Pointer to end of range.
1055 virtual const char_type*
1056 do_tolower(char_type* __lo, const char_type* __hi) const;
1061 * This virtual function converts the char to char using the simplest
1062 * reasonable transformation. For an underived ctype<char> facet, the
1063 * argument will be returned unchanged.
1065 * do_widen() is a hook for a derived facet to change the behavior of
1066 * widening. do_widen() must always return the same result for the
1069 * Note: this is not what you want for codepage conversions. See
1072 * @param c The char to convert.
1073 * @return The converted character.
1076 do_widen(char __c) const
1080 * @brief Widen char array
1082 * This function converts each char in the range [lo,hi) to char using
1083 * the simplest reasonable transformation. For an underived
1084 * ctype<char> facet, the argument will be copied unchanged.
1086 * do_widen() is a hook for a derived facet to change the behavior of
1087 * widening. do_widen() must always return the same result for the
1090 * Note: this is not what you want for codepage conversions. See
1093 * @param lo Pointer to start of range.
1094 * @param hi Pointer to end of range.
1095 * @param to Pointer to the destination array.
1099 do_widen(const char* __lo, const char* __hi, char_type* __dest) const
1101 __builtin_memcpy(__dest, __lo, __hi - __lo);
1106 * @brief Narrow char
1108 * This virtual function converts the char to char using the simplest
1109 * reasonable transformation. If the conversion fails, dfault is
1110 * returned instead. For an underived ctype<char> facet, @a c will be
1111 * returned unchanged.
1113 * do_narrow() is a hook for a derived facet to change the behavior of
1114 * narrowing. do_narrow() must always return the same result for the
1117 * Note: this is not what you want for codepage conversions. See
1120 * @param c The char to convert.
1121 * @param dfault Char to return if conversion fails.
1122 * @return The converted char.
1125 do_narrow(char_type __c, char) const
1129 * @brief Narrow char array to char array
1131 * This virtual function converts each char in the range [lo,hi) to
1132 * char using the simplest reasonable transformation and writes the
1133 * results to the destination array. For any char in the input that
1134 * cannot be converted, @a dfault is used instead. For an underived
1135 * ctype<char> facet, the argument will be copied unchanged.
1137 * do_narrow() is a hook for a derived facet to change the behavior of
1138 * narrowing. do_narrow() must always return the same result for the
1141 * Note: this is not what you want for codepage conversions. See
1144 * @param lo Pointer to start of range.
1145 * @param hi Pointer to end of range.
1146 * @param dfault Char to use if conversion fails.
1147 * @param to Pointer to the destination array.
1150 virtual const char_type*
1151 do_narrow(const char_type* __lo, const char_type* __hi,
1152 char, char* __dest) const
1154 __builtin_memcpy(__dest, __lo, __hi - __lo);
1159 void _M_narrow_init() const;
1160 void _M_widen_init() const;
1163 #ifdef _GLIBCXX_USE_WCHAR_T
1165 * @brief The ctype<wchar_t> specialization.
1168 * This class defines classification and conversion functions for the
1169 * wchar_t type. It gets used by wchar_t streams for many I/O operations.
1170 * The wchar_t specialization provides a number of optimizations as well.
1172 * ctype<wchar_t> inherits its public methods from
1173 * __ctype_abstract_base<wchar_t>.
1176 class ctype<wchar_t> : public __ctype_abstract_base<wchar_t>
1180 /// Typedef for the template parameter wchar_t.
1181 typedef wchar_t char_type;
1182 typedef wctype_t __wmask_type;
1185 __c_locale _M_c_locale_ctype;
1187 // Pre-computed narrowed and widened chars.
1189 char _M_narrow[128];
1190 wint_t _M_widen[1 + static_cast<unsigned char>(-1)];
1192 // Pre-computed elements for do_is.
1194 __wmask_type _M_wmask[16];
1198 /// The facet id for ctype<wchar_t>
1199 static locale::id id;
1202 * @brief Constructor performs initialization.
1204 * This is the constructor provided by the standard.
1206 * @param refs Passed to the base facet class.
1209 ctype(size_t __refs = 0);
1212 * @brief Constructor performs static initialization.
1214 * This constructor is used to construct the initial C locale facet.
1216 * @param cloc Handle to C locale data.
1217 * @param refs Passed to the base facet class.
1220 ctype(__c_locale __cloc, size_t __refs = 0);
1224 _M_convert_to_wmask(const mask __m) const throw();
1231 * @brief Test wchar_t classification.
1233 * This function finds a mask M for @a c and compares it to mask @a m.
1235 * do_is() is a hook for a derived facet to change the behavior of
1236 * classifying. do_is() must always return the same result for the
1239 * @param c The wchar_t to find the mask of.
1240 * @param m The mask to compare against.
1241 * @return (M & m) != 0.
1244 do_is(mask __m, char_type __c) const;
1247 * @brief Return a mask array.
1249 * This function finds the mask for each wchar_t in the range [lo,hi)
1250 * and successively writes it to vec. vec must have as many elements
1253 * do_is() is a hook for a derived facet to change the behavior of
1254 * classifying. do_is() must always return the same result for the
1257 * @param lo Pointer to start of range.
1258 * @param hi Pointer to end of range.
1259 * @param vec Pointer to an array of mask storage.
1262 virtual const char_type*
1263 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
1266 * @brief Find wchar_t matching mask
1268 * This function searches for and returns the first wchar_t c in
1269 * [lo,hi) for which is(m,c) is true.
1271 * do_scan_is() is a hook for a derived facet to change the behavior of
1272 * match searching. do_is() must always return the same result for the
1275 * @param m The mask to compare against.
1276 * @param lo Pointer to start of range.
1277 * @param hi Pointer to end of range.
1278 * @return Pointer to a matching wchar_t if found, else @a hi.
1280 virtual const char_type*
1281 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
1284 * @brief Find wchar_t not matching mask
1286 * This function searches for and returns a pointer to the first
1287 * wchar_t c of [lo,hi) for which is(m,c) is false.
1289 * do_scan_is() is a hook for a derived facet to change the behavior of
1290 * match searching. do_is() must always return the same result for the
1293 * @param m The mask to compare against.
1294 * @param lo Pointer to start of range.
1295 * @param hi Pointer to end of range.
1296 * @return Pointer to a non-matching wchar_t if found, else @a hi.
1298 virtual const char_type*
1299 do_scan_not(mask __m, const char_type* __lo,
1300 const char_type* __hi) const;
1303 * @brief Convert to uppercase.
1305 * This virtual function converts the wchar_t argument to uppercase if
1306 * possible. If not possible (for example, '2'), returns the argument.
1308 * do_toupper() is a hook for a derived facet to change the behavior of
1309 * uppercasing. do_toupper() must always return the same result for
1312 * @param c The wchar_t to convert.
1313 * @return The uppercase wchar_t if convertible, else @a c.
1316 do_toupper(char_type) const;
1319 * @brief Convert array to uppercase.
1321 * This virtual function converts each wchar_t in the range [lo,hi) to
1322 * uppercase if possible. Other elements remain untouched.
1324 * do_toupper() is a hook for a derived facet to change the behavior of
1325 * uppercasing. do_toupper() must always return the same result for
1328 * @param lo Pointer to start of range.
1329 * @param hi Pointer to end of range.
1332 virtual const char_type*
1333 do_toupper(char_type* __lo, const char_type* __hi) const;
1336 * @brief Convert to lowercase.
1338 * This virtual function converts the argument to lowercase if
1339 * possible. If not possible (for example, '2'), returns the argument.
1341 * do_tolower() is a hook for a derived facet to change the behavior of
1342 * lowercasing. do_tolower() must always return the same result for
1345 * @param c The wchar_t to convert.
1346 * @return The lowercase wchar_t if convertible, else @a c.
1349 do_tolower(char_type) const;
1352 * @brief Convert array to lowercase.
1354 * This virtual function converts each wchar_t in the range [lo,hi) to
1355 * lowercase if possible. Other elements remain untouched.
1357 * do_tolower() is a hook for a derived facet to change the behavior of
1358 * lowercasing. do_tolower() must always return the same result for
1361 * @param lo Pointer to start of range.
1362 * @param hi Pointer to end of range.
1365 virtual const char_type*
1366 do_tolower(char_type* __lo, const char_type* __hi) const;
1369 * @brief Widen char to wchar_t
1371 * This virtual function converts the char to wchar_t using the
1372 * simplest reasonable transformation. For an underived ctype<wchar_t>
1373 * facet, the argument will be cast to wchar_t.
1375 * do_widen() is a hook for a derived facet to change the behavior of
1376 * widening. do_widen() must always return the same result for the
1379 * Note: this is not what you want for codepage conversions. See
1382 * @param c The char to convert.
1383 * @return The converted wchar_t.
1386 do_widen(char) const;
1389 * @brief Widen char array to wchar_t array
1391 * This function converts each char in the input to wchar_t using the
1392 * simplest reasonable transformation. For an underived ctype<wchar_t>
1393 * facet, the argument will be copied, casting each element to wchar_t.
1395 * do_widen() is a hook for a derived facet to change the behavior of
1396 * widening. do_widen() must always return the same result for the
1399 * Note: this is not what you want for codepage conversions. See
1402 * @param lo Pointer to start range.
1403 * @param hi Pointer to end of range.
1404 * @param to Pointer to the destination array.
1408 do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
1411 * @brief Narrow wchar_t to char
1413 * This virtual function converts the argument to char using
1414 * the simplest reasonable transformation. If the conversion
1415 * fails, dfault is returned instead. For an underived
1416 * ctype<wchar_t> facet, @a c will be cast to char and
1419 * do_narrow() is a hook for a derived facet to change the
1420 * behavior of narrowing. do_narrow() must always return the
1421 * same result for the same input.
1423 * Note: this is not what you want for codepage conversions. See
1426 * @param c The wchar_t to convert.
1427 * @param dfault Char to return if conversion fails.
1428 * @return The converted char.
1431 do_narrow(char_type, char __dfault) const;
1434 * @brief Narrow wchar_t array to char array
1436 * This virtual function converts each wchar_t in the range [lo,hi) to
1437 * char using the simplest reasonable transformation and writes the
1438 * results to the destination array. For any wchar_t in the input that
1439 * cannot be converted, @a dfault is used instead. For an underived
1440 * ctype<wchar_t> facet, the argument will be copied, casting each
1443 * do_narrow() is a hook for a derived facet to change the behavior of
1444 * narrowing. do_narrow() must always return the same result for the
1447 * Note: this is not what you want for codepage conversions. See
1450 * @param lo Pointer to start of range.
1451 * @param hi Pointer to end of range.
1452 * @param dfault Char to use if conversion fails.
1453 * @param to Pointer to the destination array.
1456 virtual const char_type*
1457 do_narrow(const char_type* __lo, const char_type* __hi,
1458 char __dfault, char* __dest) const;
1460 // For use at construction time only.
1462 _M_initialize_ctype() throw();
1464 #endif //_GLIBCXX_USE_WCHAR_T
1466 /// class ctype_byname [22.2.1.2].
1467 template<typename _CharT>
1468 class ctype_byname : public ctype<_CharT>
1471 typedef typename ctype<_CharT>::mask mask;
1474 ctype_byname(const char* __s, size_t __refs = 0);
1478 ~ctype_byname() { };
1481 /// 22.2.1.4 Class ctype_byname specializations.
1483 class ctype_byname<char> : public ctype<char>
1487 ctype_byname(const char* __s, size_t __refs = 0);
1494 #ifdef _GLIBCXX_USE_WCHAR_T
1496 class ctype_byname<wchar_t> : public ctype<wchar_t>
1500 ctype_byname(const char* __s, size_t __refs = 0);
1508 _GLIBCXX_END_NAMESPACE_VERSION
1511 // Include host and configuration specific ctype inlines.
1512 #include <bits/ctype_inline.h>
1514 namespace std _GLIBCXX_VISIBILITY(default)
1516 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1518 // 22.2.2 The numeric category.
1522 // NB: Code depends on the order of _S_atoms_out elements.
1523 // Below are the indices into _S_atoms_out.
1531 _S_odigits_end = _S_odigits + 16,
1532 _S_oudigits = _S_odigits_end,
1533 _S_oudigits_end = _S_oudigits + 16,
1534 _S_oe = _S_odigits + 14, // For scientific notation, 'e'
1535 _S_oE = _S_oudigits + 14, // For scientific notation, 'E'
1536 _S_oend = _S_oudigits_end
1539 // A list of valid numeric literals for output. This array
1540 // contains chars that will be passed through the current locale's
1541 // ctype<_CharT>.widen() and then used to render numbers.
1542 // For the standard "C" locale, this is
1543 // "-+xX0123456789abcdef0123456789ABCDEF".
1544 static const char* _S_atoms_out;
1546 // String literal of acceptable (narrow) input, for num_get.
1547 // "-+xX0123456789abcdefABCDEF"
1548 static const char* _S_atoms_in;
1557 _S_ie = _S_izero + 14,
1558 _S_iE = _S_izero + 20,
1563 // Construct and return valid scanf format for floating point types.
1565 _S_format_float(const ios_base& __io, char* __fptr, char __mod) throw();
1568 template<typename _CharT>
1569 struct __numpunct_cache : public locale::facet
1571 const char* _M_grouping;
1572 size_t _M_grouping_size;
1573 bool _M_use_grouping;
1574 const _CharT* _M_truename;
1575 size_t _M_truename_size;
1576 const _CharT* _M_falsename;
1577 size_t _M_falsename_size;
1578 _CharT _M_decimal_point;
1579 _CharT _M_thousands_sep;
1581 // A list of valid numeric literals for output: in the standard
1582 // "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF".
1583 // This array contains the chars after having been passed
1584 // through the current locale's ctype<_CharT>.widen().
1585 _CharT _M_atoms_out[__num_base::_S_oend];
1587 // A list of valid numeric literals for input: in the standard
1588 // "C" locale, this is "-+xX0123456789abcdefABCDEF"
1589 // This array contains the chars after having been passed
1590 // through the current locale's ctype<_CharT>.widen().
1591 _CharT _M_atoms_in[__num_base::_S_iend];
1595 __numpunct_cache(size_t __refs = 0)
1596 : facet(__refs), _M_grouping(0), _M_grouping_size(0),
1597 _M_use_grouping(false),
1598 _M_truename(0), _M_truename_size(0), _M_falsename(0),
1599 _M_falsename_size(0), _M_decimal_point(_CharT()),
1600 _M_thousands_sep(_CharT()), _M_allocated(false)
1603 ~__numpunct_cache();
1606 _M_cache(const locale& __loc);
1610 operator=(const __numpunct_cache&);
1613 __numpunct_cache(const __numpunct_cache&);
1616 template<typename _CharT>
1617 __numpunct_cache<_CharT>::~__numpunct_cache()
1621 delete [] _M_grouping;
1622 delete [] _M_truename;
1623 delete [] _M_falsename;
1628 * @brief Primary class template numpunct.
1631 * This facet stores several pieces of information related to printing and
1632 * scanning numbers, such as the decimal point character. It takes a
1633 * template parameter specifying the char type. The numpunct facet is
1634 * used by streams for many I/O operations involving numbers.
1636 * The numpunct template uses protected virtual functions to provide the
1637 * actual results. The public accessors forward the call to the virtual
1638 * functions. These virtual functions are hooks for developers to
1639 * implement the behavior they require from a numpunct facet.
1641 template<typename _CharT>
1642 class numpunct : public locale::facet
1648 typedef _CharT char_type;
1649 typedef basic_string<_CharT> string_type;
1651 typedef __numpunct_cache<_CharT> __cache_type;
1654 __cache_type* _M_data;
1657 /// Numpunct facet id.
1658 static locale::id id;
1661 * @brief Numpunct constructor.
1663 * @param refs Refcount to pass to the base class.
1666 numpunct(size_t __refs = 0)
1667 : facet(__refs), _M_data(0)
1668 { _M_initialize_numpunct(); }
1671 * @brief Internal constructor. Not for general use.
1673 * This is a constructor for use by the library itself to set up the
1674 * predefined locale facets.
1676 * @param cache __numpunct_cache object.
1677 * @param refs Refcount to pass to the base class.
1680 numpunct(__cache_type* __cache, size_t __refs = 0)
1681 : facet(__refs), _M_data(__cache)
1682 { _M_initialize_numpunct(); }
1685 * @brief Internal constructor. Not for general use.
1687 * This is a constructor for use by the library itself to set up new
1690 * @param cloc The C locale.
1691 * @param refs Refcount to pass to the base class.
1694 numpunct(__c_locale __cloc, size_t __refs = 0)
1695 : facet(__refs), _M_data(0)
1696 { _M_initialize_numpunct(__cloc); }
1699 * @brief Return decimal point character.
1701 * This function returns a char_type to use as a decimal point. It
1702 * does so by returning returning
1703 * numpunct<char_type>::do_decimal_point().
1705 * @return @a char_type representing a decimal point.
1708 decimal_point() const
1709 { return this->do_decimal_point(); }
1712 * @brief Return thousands separator character.
1714 * This function returns a char_type to use as a thousands
1715 * separator. It does so by returning returning
1716 * numpunct<char_type>::do_thousands_sep().
1718 * @return char_type representing a thousands separator.
1721 thousands_sep() const
1722 { return this->do_thousands_sep(); }
1725 * @brief Return grouping specification.
1727 * This function returns a string representing groupings for the
1728 * integer part of a number. Groupings indicate where thousands
1729 * separators should be inserted in the integer part of a number.
1731 * Each char in the return string is interpret as an integer
1732 * rather than a character. These numbers represent the number
1733 * of digits in a group. The first char in the string
1734 * represents the number of digits in the least significant
1735 * group. If a char is negative, it indicates an unlimited
1736 * number of digits for the group. If more chars from the
1737 * string are required to group a number, the last char is used
1740 * For example, if the grouping() returns "\003\002" and is
1741 * applied to the number 123456789, this corresponds to
1742 * 12,34,56,789. Note that if the string was "32", this would
1743 * put more than 50 digits into the least significant group if
1744 * the character set is ASCII.
1746 * The string is returned by calling
1747 * numpunct<char_type>::do_grouping().
1749 * @return string representing grouping specification.
1753 { return this->do_grouping(); }
1756 * @brief Return string representation of bool true.
1758 * This function returns a string_type containing the text
1759 * representation for true bool variables. It does so by calling
1760 * numpunct<char_type>::do_truename().
1762 * @return string_type representing printed form of true.
1766 { return this->do_truename(); }
1769 * @brief Return string representation of bool false.
1771 * This function returns a string_type containing the text
1772 * representation for false bool variables. It does so by calling
1773 * numpunct<char_type>::do_falsename().
1775 * @return string_type representing printed form of false.
1779 { return this->do_falsename(); }
1787 * @brief Return decimal point character.
1789 * Returns a char_type to use as a decimal point. This function is a
1790 * hook for derived classes to change the value returned.
1792 * @return @a char_type representing a decimal point.
1795 do_decimal_point() const
1796 { return _M_data->_M_decimal_point; }
1799 * @brief Return thousands separator character.
1801 * Returns a char_type to use as a thousands separator. This function
1802 * is a hook for derived classes to change the value returned.
1804 * @return @a char_type representing a thousands separator.
1807 do_thousands_sep() const
1808 { return _M_data->_M_thousands_sep; }
1811 * @brief Return grouping specification.
1813 * Returns a string representing groupings for the integer part of a
1814 * number. This function is a hook for derived classes to change the
1815 * value returned. @see grouping() for details.
1817 * @return String representing grouping specification.
1821 { return _M_data->_M_grouping; }
1824 * @brief Return string representation of bool true.
1826 * Returns a string_type containing the text representation for true
1827 * bool variables. This function is a hook for derived classes to
1828 * change the value returned.
1830 * @return string_type representing printed form of true.
1834 { return _M_data->_M_truename; }
1837 * @brief Return string representation of bool false.
1839 * Returns a string_type containing the text representation for false
1840 * bool variables. This function is a hook for derived classes to
1841 * change the value returned.
1843 * @return string_type representing printed form of false.
1846 do_falsename() const
1847 { return _M_data->_M_falsename; }
1849 // For use at construction time only.
1851 _M_initialize_numpunct(__c_locale __cloc = 0);
1854 template<typename _CharT>
1855 locale::id numpunct<_CharT>::id;
1858 numpunct<char>::~numpunct();
1862 numpunct<char>::_M_initialize_numpunct(__c_locale __cloc);
1864 #ifdef _GLIBCXX_USE_WCHAR_T
1866 numpunct<wchar_t>::~numpunct();
1870 numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc);
1873 /// class numpunct_byname [22.2.3.2].
1874 template<typename _CharT>
1875 class numpunct_byname : public numpunct<_CharT>
1878 typedef _CharT char_type;
1879 typedef basic_string<_CharT> string_type;
1882 numpunct_byname(const char* __s, size_t __refs = 0)
1883 : numpunct<_CharT>(__refs)
1885 if (__builtin_strcmp(__s, "C") != 0
1886 && __builtin_strcmp(__s, "POSIX") != 0)
1889 this->_S_create_c_locale(__tmp, __s);
1890 this->_M_initialize_numpunct(__tmp);
1891 this->_S_destroy_c_locale(__tmp);
1897 ~numpunct_byname() { }
1900 _GLIBCXX_BEGIN_NAMESPACE_LDBL
1903 * @brief Primary class template num_get.
1906 * This facet encapsulates the code to parse and return a number
1907 * from a string. It is used by the istream numeric extraction
1910 * The num_get template uses protected virtual functions to provide the
1911 * actual results. The public accessors forward the call to the virtual
1912 * functions. These virtual functions are hooks for developers to
1913 * implement the behavior they require from the num_get facet.
1915 template<typename _CharT, typename _InIter>
1916 class num_get : public locale::facet
1922 typedef _CharT char_type;
1923 typedef _InIter iter_type;
1926 /// Numpunct facet id.
1927 static locale::id id;
1930 * @brief Constructor performs initialization.
1932 * This is the constructor provided by the standard.
1934 * @param refs Passed to the base facet class.
1937 num_get(size_t __refs = 0) : facet(__refs) { }
1940 * @brief Numeric parsing.
1942 * Parses the input stream into the bool @a v. It does so by calling
1943 * num_get::do_get().
1945 * If ios_base::boolalpha is set, attempts to read
1946 * ctype<CharT>::truename() or ctype<CharT>::falsename(). Sets
1947 * @a v to true or false if successful. Sets err to
1948 * ios_base::failbit if reading the string fails. Sets err to
1949 * ios_base::eofbit if the stream is emptied.
1951 * If ios_base::boolalpha is not set, proceeds as with reading a long,
1952 * except if the value is 1, sets @a v to true, if the value is 0, sets
1953 * @a v to false, and otherwise set err to ios_base::failbit.
1955 * @param in Start of input stream.
1956 * @param end End of input stream.
1957 * @param io Source of locale and flags.
1958 * @param err Error flags to set.
1959 * @param v Value to format and insert.
1960 * @return Iterator after reading.
1963 get(iter_type __in, iter_type __end, ios_base& __io,
1964 ios_base::iostate& __err, bool& __v) const
1965 { return this->do_get(__in, __end, __io, __err, __v); }
1969 * @brief Numeric parsing.
1971 * Parses the input stream into the integral variable @a v. It does so
1972 * by calling num_get::do_get().
1974 * Parsing is affected by the flag settings in @a io.
1976 * The basic parse is affected by the value of io.flags() &
1977 * ios_base::basefield. If equal to ios_base::oct, parses like the
1978 * scanf %o specifier. Else if equal to ios_base::hex, parses like %X
1979 * specifier. Else if basefield equal to 0, parses like the %i
1980 * specifier. Otherwise, parses like %d for signed and %u for unsigned
1981 * types. The matching type length modifier is also used.
1983 * Digit grouping is interpreted according to numpunct::grouping() and
1984 * numpunct::thousands_sep(). If the pattern of digit groups isn't
1985 * consistent, sets err to ios_base::failbit.
1987 * If parsing the string yields a valid value for @a v, @a v is set.
1988 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
1989 * Sets err to ios_base::eofbit if the stream is emptied.
1991 * @param in Start of input stream.
1992 * @param end End of input stream.
1993 * @param io Source of locale and flags.
1994 * @param err Error flags to set.
1995 * @param v Value to format and insert.
1996 * @return Iterator after reading.
1999 get(iter_type __in, iter_type __end, ios_base& __io,
2000 ios_base::iostate& __err, long& __v) const
2001 { return this->do_get(__in, __end, __io, __err, __v); }
2004 get(iter_type __in, iter_type __end, ios_base& __io,
2005 ios_base::iostate& __err, unsigned short& __v) const
2006 { return this->do_get(__in, __end, __io, __err, __v); }
2009 get(iter_type __in, iter_type __end, ios_base& __io,
2010 ios_base::iostate& __err, unsigned int& __v) const
2011 { return this->do_get(__in, __end, __io, __err, __v); }
2014 get(iter_type __in, iter_type __end, ios_base& __io,
2015 ios_base::iostate& __err, unsigned long& __v) const
2016 { return this->do_get(__in, __end, __io, __err, __v); }
2018 #ifdef _GLIBCXX_USE_LONG_LONG
2020 get(iter_type __in, iter_type __end, ios_base& __io,
2021 ios_base::iostate& __err, long long& __v) const
2022 { return this->do_get(__in, __end, __io, __err, __v); }
2025 get(iter_type __in, iter_type __end, ios_base& __io,
2026 ios_base::iostate& __err, unsigned long long& __v) const
2027 { return this->do_get(__in, __end, __io, __err, __v); }
2033 * @brief Numeric parsing.
2035 * Parses the input stream into the integral variable @a v. It does so
2036 * by calling num_get::do_get().
2038 * The input characters are parsed like the scanf %g specifier. The
2039 * matching type length modifier is also used.
2041 * The decimal point character used is numpunct::decimal_point().
2042 * Digit grouping is interpreted according to numpunct::grouping() and
2043 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2044 * consistent, sets err to ios_base::failbit.
2046 * If parsing the string yields a valid value for @a v, @a v is set.
2047 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2048 * Sets err to ios_base::eofbit if the stream is emptied.
2050 * @param in Start of input stream.
2051 * @param end End of input stream.
2052 * @param io Source of locale and flags.
2053 * @param err Error flags to set.
2054 * @param v Value to format and insert.
2055 * @return Iterator after reading.
2058 get(iter_type __in, iter_type __end, ios_base& __io,
2059 ios_base::iostate& __err, float& __v) const
2060 { return this->do_get(__in, __end, __io, __err, __v); }
2063 get(iter_type __in, iter_type __end, ios_base& __io,
2064 ios_base::iostate& __err, double& __v) const
2065 { return this->do_get(__in, __end, __io, __err, __v); }
2068 get(iter_type __in, iter_type __end, ios_base& __io,
2069 ios_base::iostate& __err, long double& __v) const
2070 { return this->do_get(__in, __end, __io, __err, __v); }
2074 * @brief Numeric parsing.
2076 * Parses the input stream into the pointer variable @a v. It does so
2077 * by calling num_get::do_get().
2079 * The input characters are parsed like the scanf %p specifier.
2081 * Digit grouping is interpreted according to numpunct::grouping() and
2082 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2083 * consistent, sets err to ios_base::failbit.
2085 * Note that the digit grouping effect for pointers is a bit ambiguous
2086 * in the standard and shouldn't be relied on. See DR 344.
2088 * If parsing the string yields a valid value for @a v, @a v is set.
2089 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2090 * Sets err to ios_base::eofbit if the stream is emptied.
2092 * @param in Start of input stream.
2093 * @param end End of input stream.
2094 * @param io Source of locale and flags.
2095 * @param err Error flags to set.
2096 * @param v Value to format and insert.
2097 * @return Iterator after reading.
2100 get(iter_type __in, iter_type __end, ios_base& __io,
2101 ios_base::iostate& __err, void*& __v) const
2102 { return this->do_get(__in, __end, __io, __err, __v); }
2106 virtual ~num_get() { }
2109 _M_extract_float(iter_type, iter_type, ios_base&, ios_base::iostate&,
2112 template<typename _ValueT>
2114 _M_extract_int(iter_type, iter_type, ios_base&, ios_base::iostate&,
2117 template<typename _CharT2>
2118 typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, int>::__type
2119 _M_find(const _CharT2*, size_t __len, _CharT2 __c) const
2124 if (__c >= _CharT2('0') && __c < _CharT2(_CharT2('0') + __len))
2125 __ret = __c - _CharT2('0');
2129 if (__c >= _CharT2('0') && __c <= _CharT2('9'))
2130 __ret = __c - _CharT2('0');
2131 else if (__c >= _CharT2('a') && __c <= _CharT2('f'))
2132 __ret = 10 + (__c - _CharT2('a'));
2133 else if (__c >= _CharT2('A') && __c <= _CharT2('F'))
2134 __ret = 10 + (__c - _CharT2('A'));
2139 template<typename _CharT2>
2140 typename __gnu_cxx::__enable_if<!__is_char<_CharT2>::__value,
2142 _M_find(const _CharT2* __zero, size_t __len, _CharT2 __c) const
2145 const char_type* __q = char_traits<_CharT2>::find(__zero, __len, __c);
2148 __ret = __q - __zero;
2157 * @brief Numeric parsing.
2159 * Parses the input stream into the variable @a v. This function is a
2160 * hook for derived classes to change the value returned. @see get()
2163 * @param in Start of input stream.
2164 * @param end End of input stream.
2165 * @param io Source of locale and flags.
2166 * @param err Error flags to set.
2167 * @param v Value to format and insert.
2168 * @return Iterator after reading.
2171 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const;
2174 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2175 ios_base::iostate& __err, long& __v) const
2176 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2179 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2180 ios_base::iostate& __err, unsigned short& __v) const
2181 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2184 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2185 ios_base::iostate& __err, unsigned int& __v) const
2186 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2189 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2190 ios_base::iostate& __err, unsigned long& __v) const
2191 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2193 #ifdef _GLIBCXX_USE_LONG_LONG
2195 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2196 ios_base::iostate& __err, long long& __v) const
2197 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2200 do_get(iter_type __beg, iter_type __end, ios_base& __io,
2201 ios_base::iostate& __err, unsigned long long& __v) const
2202 { return _M_extract_int(__beg, __end, __io, __err, __v); }
2206 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2210 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2213 // XXX GLIBCXX_ABI Deprecated
2214 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2216 __do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2220 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2221 long double&) const;
2225 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2228 // XXX GLIBCXX_ABI Deprecated
2229 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2231 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2232 long double&) const;
2237 template<typename _CharT, typename _InIter>
2238 locale::id num_get<_CharT, _InIter>::id;
2242 * @brief Primary class template num_put.
2245 * This facet encapsulates the code to convert a number to a string. It is
2246 * used by the ostream numeric insertion operators.
2248 * The num_put template uses protected virtual functions to provide the
2249 * actual results. The public accessors forward the call to the virtual
2250 * functions. These virtual functions are hooks for developers to
2251 * implement the behavior they require from the num_put facet.
2253 template<typename _CharT, typename _OutIter>
2254 class num_put : public locale::facet
2260 typedef _CharT char_type;
2261 typedef _OutIter iter_type;
2264 /// Numpunct facet id.
2265 static locale::id id;
2268 * @brief Constructor performs initialization.
2270 * This is the constructor provided by the standard.
2272 * @param refs Passed to the base facet class.
2275 num_put(size_t __refs = 0) : facet(__refs) { }
2278 * @brief Numeric formatting.
2280 * Formats the boolean @a v and inserts it into a stream. It does so
2281 * by calling num_put::do_put().
2283 * If ios_base::boolalpha is set, writes ctype<CharT>::truename() or
2284 * ctype<CharT>::falsename(). Otherwise formats @a v as an int.
2286 * @param s Stream to write to.
2287 * @param io Source of locale and flags.
2288 * @param fill Char_type to use for filling.
2289 * @param v Value to format and insert.
2290 * @return Iterator after writing.
2293 put(iter_type __s, ios_base& __f, char_type __fill, bool __v) const
2294 { return this->do_put(__s, __f, __fill, __v); }
2298 * @brief Numeric formatting.
2300 * Formats the integral value @a v and inserts it into a
2301 * stream. It does so by calling num_put::do_put().
2303 * Formatting is affected by the flag settings in @a io.
2305 * The basic format is affected by the value of io.flags() &
2306 * ios_base::basefield. If equal to ios_base::oct, formats like the
2307 * printf %o specifier. Else if equal to ios_base::hex, formats like
2308 * %x or %X with ios_base::uppercase unset or set respectively.
2309 * Otherwise, formats like %d, %ld, %lld for signed and %u, %lu, %llu
2310 * for unsigned values. Note that if both oct and hex are set, neither
2313 * If ios_base::showpos is set, '+' is output before positive values.
2314 * If ios_base::showbase is set, '0' precedes octal values (except 0)
2315 * and '0[xX]' precedes hex values.
2317 * Thousands separators are inserted according to numpunct::grouping()
2318 * and numpunct::thousands_sep(). The decimal point character used is
2319 * numpunct::decimal_point().
2321 * If io.width() is non-zero, enough @a fill characters are inserted to
2322 * make the result at least that wide. If
2323 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2324 * padded at the end. If ios_base::internal, then padding occurs
2325 * immediately after either a '+' or '-' or after '0x' or '0X'.
2326 * Otherwise, padding occurs at the beginning.
2328 * @param s Stream to write to.
2329 * @param io Source of locale and flags.
2330 * @param fill Char_type to use for filling.
2331 * @param v Value to format and insert.
2332 * @return Iterator after writing.
2335 put(iter_type __s, ios_base& __f, char_type __fill, long __v) const
2336 { return this->do_put(__s, __f, __fill, __v); }
2339 put(iter_type __s, ios_base& __f, char_type __fill,
2340 unsigned long __v) const
2341 { return this->do_put(__s, __f, __fill, __v); }
2343 #ifdef _GLIBCXX_USE_LONG_LONG
2345 put(iter_type __s, ios_base& __f, char_type __fill, long long __v) const
2346 { return this->do_put(__s, __f, __fill, __v); }
2349 put(iter_type __s, ios_base& __f, char_type __fill,
2350 unsigned long long __v) const
2351 { return this->do_put(__s, __f, __fill, __v); }
2357 * @brief Numeric formatting.
2359 * Formats the floating point value @a v and inserts it into a stream.
2360 * It does so by calling num_put::do_put().
2362 * Formatting is affected by the flag settings in @a io.
2364 * The basic format is affected by the value of io.flags() &
2365 * ios_base::floatfield. If equal to ios_base::fixed, formats like the
2366 * printf %f specifier. Else if equal to ios_base::scientific, formats
2367 * like %e or %E with ios_base::uppercase unset or set respectively.
2368 * Otherwise, formats like %g or %G depending on uppercase. Note that
2369 * if both fixed and scientific are set, the effect will also be like
2372 * The output precision is given by io.precision(). This precision is
2373 * capped at numeric_limits::digits10 + 2 (different for double and
2374 * long double). The default precision is 6.
2376 * If ios_base::showpos is set, '+' is output before positive values.
2377 * If ios_base::showpoint is set, a decimal point will always be
2380 * Thousands separators are inserted according to numpunct::grouping()
2381 * and numpunct::thousands_sep(). The decimal point character used is
2382 * numpunct::decimal_point().
2384 * If io.width() is non-zero, enough @a fill characters are inserted to
2385 * make the result at least that wide. If
2386 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2387 * padded at the end. If ios_base::internal, then padding occurs
2388 * immediately after either a '+' or '-' or after '0x' or '0X'.
2389 * Otherwise, padding occurs at the beginning.
2391 * @param s Stream to write to.
2392 * @param io Source of locale and flags.
2393 * @param fill Char_type to use for filling.
2394 * @param v Value to format and insert.
2395 * @return Iterator after writing.
2398 put(iter_type __s, ios_base& __f, char_type __fill, double __v) const
2399 { return this->do_put(__s, __f, __fill, __v); }
2402 put(iter_type __s, ios_base& __f, char_type __fill,
2403 long double __v) const
2404 { return this->do_put(__s, __f, __fill, __v); }
2408 * @brief Numeric formatting.
2410 * Formats the pointer value @a v and inserts it into a stream. It
2411 * does so by calling num_put::do_put().
2413 * This function formats @a v as an unsigned long with ios_base::hex
2414 * and ios_base::showbase set.
2416 * @param s Stream to write to.
2417 * @param io Source of locale and flags.
2418 * @param fill Char_type to use for filling.
2419 * @param v Value to format and insert.
2420 * @return Iterator after writing.
2423 put(iter_type __s, ios_base& __f, char_type __fill,
2424 const void* __v) const
2425 { return this->do_put(__s, __f, __fill, __v); }
2428 template<typename _ValueT>
2430 _M_insert_float(iter_type, ios_base& __io, char_type __fill,
2431 char __mod, _ValueT __v) const;
2434 _M_group_float(const char* __grouping, size_t __grouping_size,
2435 char_type __sep, const char_type* __p, char_type* __new,
2436 char_type* __cs, int& __len) const;
2438 template<typename _ValueT>
2440 _M_insert_int(iter_type, ios_base& __io, char_type __fill,
2444 _M_group_int(const char* __grouping, size_t __grouping_size,
2445 char_type __sep, ios_base& __io, char_type* __new,
2446 char_type* __cs, int& __len) const;
2449 _M_pad(char_type __fill, streamsize __w, ios_base& __io,
2450 char_type* __new, const char_type* __cs, int& __len) const;
2458 * @brief Numeric formatting.
2460 * These functions do the work of formatting numeric values and
2461 * inserting them into a stream. This function is a hook for derived
2462 * classes to change the value returned.
2464 * @param s Stream to write to.
2465 * @param io Source of locale and flags.
2466 * @param fill Char_type to use for filling.
2467 * @param v Value to format and insert.
2468 * @return Iterator after writing.
2471 do_put(iter_type, ios_base&, char_type __fill, bool __v) const;
2474 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
2475 { return _M_insert_int(__s, __io, __fill, __v); }
2478 do_put(iter_type __s, ios_base& __io, char_type __fill,
2479 unsigned long __v) const
2480 { return _M_insert_int(__s, __io, __fill, __v); }
2482 #ifdef _GLIBCXX_USE_LONG_LONG
2484 do_put(iter_type __s, ios_base& __io, char_type __fill,
2485 long long __v) const
2486 { return _M_insert_int(__s, __io, __fill, __v); }
2489 do_put(iter_type __s, ios_base& __io, char_type __fill,
2490 unsigned long long __v) const
2491 { return _M_insert_int(__s, __io, __fill, __v); }
2495 do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2497 // XXX GLIBCXX_ABI Deprecated
2498 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2500 __do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2503 do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2507 do_put(iter_type, ios_base&, char_type __fill, const void* __v) const;
2509 // XXX GLIBCXX_ABI Deprecated
2510 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2512 do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2517 template <typename _CharT, typename _OutIter>
2518 locale::id num_put<_CharT, _OutIter>::id;
2520 _GLIBCXX_END_NAMESPACE_LDBL
2522 // Subclause convenience interfaces, inlines.
2523 // NB: These are inline because, when used in a loop, some compilers
2524 // can hoist the body out of the loop; then it's just as fast as the
2525 // C is*() function.
2527 /// Convenience interface to ctype.is(ctype_base::space, __c).
2528 template<typename _CharT>
2530 isspace(_CharT __c, const locale& __loc)
2531 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); }
2533 /// Convenience interface to ctype.is(ctype_base::print, __c).
2534 template<typename _CharT>
2536 isprint(_CharT __c, const locale& __loc)
2537 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); }
2539 /// Convenience interface to ctype.is(ctype_base::cntrl, __c).
2540 template<typename _CharT>
2542 iscntrl(_CharT __c, const locale& __loc)
2543 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); }
2545 /// Convenience interface to ctype.is(ctype_base::upper, __c).
2546 template<typename _CharT>
2548 isupper(_CharT __c, const locale& __loc)
2549 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); }
2551 /// Convenience interface to ctype.is(ctype_base::lower, __c).
2552 template<typename _CharT>
2554 islower(_CharT __c, const locale& __loc)
2555 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); }
2557 /// Convenience interface to ctype.is(ctype_base::alpha, __c).
2558 template<typename _CharT>
2560 isalpha(_CharT __c, const locale& __loc)
2561 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); }
2563 /// Convenience interface to ctype.is(ctype_base::digit, __c).
2564 template<typename _CharT>
2566 isdigit(_CharT __c, const locale& __loc)
2567 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); }
2569 /// Convenience interface to ctype.is(ctype_base::punct, __c).
2570 template<typename _CharT>
2572 ispunct(_CharT __c, const locale& __loc)
2573 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); }
2575 /// Convenience interface to ctype.is(ctype_base::xdigit, __c).
2576 template<typename _CharT>
2578 isxdigit(_CharT __c, const locale& __loc)
2579 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); }
2581 /// Convenience interface to ctype.is(ctype_base::alnum, __c).
2582 template<typename _CharT>
2584 isalnum(_CharT __c, const locale& __loc)
2585 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); }
2587 /// Convenience interface to ctype.is(ctype_base::graph, __c).
2588 template<typename _CharT>
2590 isgraph(_CharT __c, const locale& __loc)
2591 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); }
2593 /// Convenience interface to ctype.toupper(__c).
2594 template<typename _CharT>
2596 toupper(_CharT __c, const locale& __loc)
2597 { return use_facet<ctype<_CharT> >(__loc).toupper(__c); }
2599 /// Convenience interface to ctype.tolower(__c).
2600 template<typename _CharT>
2602 tolower(_CharT __c, const locale& __loc)
2603 { return use_facet<ctype<_CharT> >(__loc).tolower(__c); }
2605 _GLIBCXX_END_NAMESPACE_VERSION
2608 # include <bits/locale_facets.tcc>