1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
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 2, 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 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 /** @file locale_facets.tcc
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
37 #ifndef _LOCALE_FACETS_TCC
38 #define _LOCALE_FACETS_TCC 1
40 #pragma GCC system_header
42 _GLIBCXX_BEGIN_NAMESPACE(std)
44 // Routine to access a cache for the facet. If the cache didn't
45 // exist before, it gets constructed on the fly.
46 template<typename _Facet>
50 operator() (const locale& __loc) const;
54 template<typename _CharT>
55 struct __use_cache<__numpunct_cache<_CharT> >
57 const __numpunct_cache<_CharT>*
58 operator() (const locale& __loc) const
60 const size_t __i = numpunct<_CharT>::id._M_id();
61 const locale::facet** __caches = __loc._M_impl->_M_caches;
64 __numpunct_cache<_CharT>* __tmp = NULL;
67 __tmp = new __numpunct_cache<_CharT>;
68 __tmp->_M_cache(__loc);
73 __throw_exception_again;
75 __loc._M_impl->_M_install_cache(__tmp, __i);
77 return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]);
81 template<typename _CharT>
83 __numpunct_cache<_CharT>::_M_cache(const locale& __loc)
87 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
89 _M_grouping_size = __np.grouping().size();
90 char* __grouping = new char[_M_grouping_size];
91 __np.grouping().copy(__grouping, _M_grouping_size);
92 _M_grouping = __grouping;
93 _M_use_grouping = (_M_grouping_size
94 && static_cast<signed char>(_M_grouping[0]) > 0
96 != __gnu_cxx::__numeric_traits<char>::__max));
98 _M_truename_size = __np.truename().size();
99 _CharT* __truename = new _CharT[_M_truename_size];
100 __np.truename().copy(__truename, _M_truename_size);
101 _M_truename = __truename;
103 _M_falsename_size = __np.falsename().size();
104 _CharT* __falsename = new _CharT[_M_falsename_size];
105 __np.falsename().copy(__falsename, _M_falsename_size);
106 _M_falsename = __falsename;
108 _M_decimal_point = __np.decimal_point();
109 _M_thousands_sep = __np.thousands_sep();
111 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
112 __ct.widen(__num_base::_S_atoms_out,
113 __num_base::_S_atoms_out + __num_base::_S_oend, _M_atoms_out);
114 __ct.widen(__num_base::_S_atoms_in,
115 __num_base::_S_atoms_in + __num_base::_S_iend, _M_atoms_in);
118 // Used by both numeric and monetary facets.
119 // Check to make sure that the __grouping_tmp string constructed in
120 // money_get or num_get matches the canonical grouping for a given
122 // __grouping_tmp is parsed L to R
123 // 1,222,444 == __grouping_tmp of "\1\3\3"
124 // __grouping is parsed R to L
125 // 1,222,444 == __grouping of "\3" == "\3\3\3"
127 __verify_grouping(const char* __grouping, size_t __grouping_size,
128 const string& __grouping_tmp);
130 _GLIBCXX_BEGIN_LDBL_NAMESPACE
132 template<typename _CharT, typename _InIter>
134 num_get<_CharT, _InIter>::
135 _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
136 ios_base::iostate& __err, string& __xtrc) const
138 typedef char_traits<_CharT> __traits_type;
139 typedef __numpunct_cache<_CharT> __cache_type;
140 __use_cache<__cache_type> __uc;
141 const locale& __loc = __io._M_getloc();
142 const __cache_type* __lc = __uc(__loc);
143 const _CharT* __lit = __lc->_M_atoms_in;
144 char_type __c = char_type();
146 // True if __beg becomes equal to __end.
147 bool __testeof = __beg == __end;
149 // First check for sign.
153 const bool __plus = __c == __lit[__num_base::_S_iplus];
154 if ((__plus || __c == __lit[__num_base::_S_iminus])
155 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
156 && !(__c == __lc->_M_decimal_point))
158 __xtrc += __plus ? '+' : '-';
159 if (++__beg != __end)
166 // Next, look for leading zeros.
167 bool __found_mantissa = false;
171 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
172 || __c == __lc->_M_decimal_point)
174 else if (__c == __lit[__num_base::_S_izero])
176 if (!__found_mantissa)
179 __found_mantissa = true;
183 if (++__beg != __end)
192 // Only need acceptable digits for floating point numbers.
193 bool __found_dec = false;
194 bool __found_sci = false;
195 string __found_grouping;
196 if (__lc->_M_use_grouping)
197 __found_grouping.reserve(32);
198 const char_type* __lit_zero = __lit + __num_base::_S_izero;
200 if (!__lc->_M_allocated)
204 const int __digit = _M_find(__lit_zero, 10, __c);
207 __xtrc += '0' + __digit;
208 __found_mantissa = true;
210 else if (__c == __lc->_M_decimal_point
211 && !__found_dec && !__found_sci)
216 else if ((__c == __lit[__num_base::_S_ie]
217 || __c == __lit[__num_base::_S_iE])
218 && !__found_sci && __found_mantissa)
220 // Scientific notation.
224 // Remove optional plus or minus sign, if they exist.
225 if (++__beg != __end)
228 const bool __plus = __c == __lit[__num_base::_S_iplus];
229 if (__plus || __c == __lit[__num_base::_S_iminus])
230 __xtrc += __plus ? '+' : '-';
243 if (++__beg != __end)
251 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
252 // and decimal_point.
253 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
255 if (!__found_dec && !__found_sci)
257 // NB: Thousands separator at the beginning of a string
258 // is a no-no, as is two consecutive thousands separators.
261 __found_grouping += static_cast<char>(__sep_pos);
266 // NB: __convert_to_v will not assign __v and will
275 else if (__c == __lc->_M_decimal_point)
277 if (!__found_dec && !__found_sci)
279 // If no grouping chars are seen, no grouping check
280 // is applied. Therefore __found_grouping is adjusted
281 // only if decimal_point comes after some thousands_sep.
282 if (__found_grouping.size())
283 __found_grouping += static_cast<char>(__sep_pos);
292 const char_type* __q =
293 __traits_type::find(__lit_zero, 10, __c);
296 __xtrc += '0' + (__q - __lit_zero);
297 __found_mantissa = true;
300 else if ((__c == __lit[__num_base::_S_ie]
301 || __c == __lit[__num_base::_S_iE])
302 && !__found_sci && __found_mantissa)
304 // Scientific notation.
305 if (__found_grouping.size() && !__found_dec)
306 __found_grouping += static_cast<char>(__sep_pos);
310 // Remove optional plus or minus sign, if they exist.
311 if (++__beg != __end)
314 const bool __plus = __c == __lit[__num_base::_S_iplus];
315 if ((__plus || __c == __lit[__num_base::_S_iminus])
316 && !(__lc->_M_use_grouping
317 && __c == __lc->_M_thousands_sep)
318 && !(__c == __lc->_M_decimal_point))
319 __xtrc += __plus ? '+' : '-';
333 if (++__beg != __end)
339 // Digit grouping is checked. If grouping and found_grouping don't
340 // match, then get very very upset, and set failbit.
341 if (__found_grouping.size())
343 // Add the ending grouping if a decimal or 'e'/'E' wasn't found.
344 if (!__found_dec && !__found_sci)
345 __found_grouping += static_cast<char>(__sep_pos);
347 if (!std::__verify_grouping(__lc->_M_grouping,
348 __lc->_M_grouping_size,
350 __err = ios_base::failbit;
356 template<typename _CharT, typename _InIter>
357 template<typename _ValueT>
359 num_get<_CharT, _InIter>::
360 _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
361 ios_base::iostate& __err, _ValueT& __v) const
363 typedef char_traits<_CharT> __traits_type;
364 using __gnu_cxx::__add_unsigned;
365 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
366 typedef __numpunct_cache<_CharT> __cache_type;
367 __use_cache<__cache_type> __uc;
368 const locale& __loc = __io._M_getloc();
369 const __cache_type* __lc = __uc(__loc);
370 const _CharT* __lit = __lc->_M_atoms_in;
371 char_type __c = char_type();
373 // NB: Iff __basefield == 0, __base can change based on contents.
374 const ios_base::fmtflags __basefield = __io.flags()
375 & ios_base::basefield;
376 const bool __oct = __basefield == ios_base::oct;
377 int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10);
379 // True if __beg becomes equal to __end.
380 bool __testeof = __beg == __end;
382 // First check for sign.
383 bool __negative = false;
387 if (__gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
388 __negative = __c == __lit[__num_base::_S_iminus];
389 if ((__negative || __c == __lit[__num_base::_S_iplus])
390 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
391 && !(__c == __lc->_M_decimal_point))
393 if (++__beg != __end)
400 // Next, look for leading zeros and check required digits
402 bool __found_zero = false;
406 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
407 || __c == __lc->_M_decimal_point)
409 else if (__c == __lit[__num_base::_S_izero]
410 && (!__found_zero || __base == 10))
414 if (__basefield == 0)
419 else if (__found_zero
420 && (__c == __lit[__num_base::_S_ix]
421 || __c == __lit[__num_base::_S_iX]))
423 if (__basefield == 0)
427 __found_zero = false;
436 if (++__beg != __end)
446 // At this point, base is determined. If not hex, only allow
447 // base digits as valid input.
448 const size_t __len = (__base == 16 ? __num_base::_S_iend
449 - __num_base::_S_izero : __base);
452 string __found_grouping;
453 if (__lc->_M_use_grouping)
454 __found_grouping.reserve(32);
455 bool __testfail = false;
456 bool __testoverflow = false;
457 const __unsigned_type __max = __negative
458 ? -__gnu_cxx::__numeric_traits<_ValueT>::__min
459 : __gnu_cxx::__numeric_traits<_ValueT>::__max;
460 const __unsigned_type __smax = __max / __base;
461 __unsigned_type __result = 0;
463 const char_type* __lit_zero = __lit + __num_base::_S_izero;
465 if (!__lc->_M_allocated)
469 __digit = _M_find(__lit_zero, __len, __c);
473 if (__result > __smax)
474 __testoverflow = true;
478 __testoverflow |= __result > __max - __digit;
483 if (++__beg != __end)
491 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
492 // and decimal_point.
493 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
495 // NB: Thousands separator at the beginning of a string
496 // is a no-no, as is two consecutive thousands separators.
499 __found_grouping += static_cast<char>(__sep_pos);
508 else if (__c == __lc->_M_decimal_point)
512 const char_type* __q =
513 __traits_type::find(__lit_zero, __len, __c);
517 __digit = __q - __lit_zero;
520 if (__result > __smax)
521 __testoverflow = true;
525 __testoverflow |= __result > __max - __digit;
531 if (++__beg != __end)
537 // Digit grouping is checked. If grouping and found_grouping don't
538 // match, then get very very upset, and set failbit.
539 if (__found_grouping.size())
541 // Add the ending grouping.
542 __found_grouping += static_cast<char>(__sep_pos);
544 if (!std::__verify_grouping(__lc->_M_grouping,
545 __lc->_M_grouping_size,
547 __err = ios_base::failbit;
550 // _GLIBCXX_RESOLVE_LIB_DEFECTS
551 // 23. Num_get overflow result.
552 if ((!__sep_pos && !__found_zero && !__found_grouping.size())
556 __err = ios_base::failbit;
558 else if (__testoverflow)
561 __v = __gnu_cxx::__numeric_traits<_ValueT>::__min;
563 __v = __gnu_cxx::__numeric_traits<_ValueT>::__max;
564 __err = ios_base::failbit;
567 __v = __negative ? -__result : __result;
570 __err |= ios_base::eofbit;
574 // _GLIBCXX_RESOLVE_LIB_DEFECTS
575 // 17. Bad bool parsing
576 template<typename _CharT, typename _InIter>
578 num_get<_CharT, _InIter>::
579 do_get(iter_type __beg, iter_type __end, ios_base& __io,
580 ios_base::iostate& __err, bool& __v) const
582 if (!(__io.flags() & ios_base::boolalpha))
584 // Parse bool values as long.
585 // NB: We can't just call do_get(long) here, as it might
586 // refer to a derived class.
588 __beg = _M_extract_int(__beg, __end, __io, __err, __l);
589 if (__l == 0 || __l == 1)
593 // _GLIBCXX_RESOLVE_LIB_DEFECTS
594 // 23. Num_get overflow result.
596 __err = ios_base::failbit;
598 __err |= ios_base::eofbit;
603 // Parse bool values as alphanumeric.
604 typedef __numpunct_cache<_CharT> __cache_type;
605 __use_cache<__cache_type> __uc;
606 const locale& __loc = __io._M_getloc();
607 const __cache_type* __lc = __uc(__loc);
611 bool __donef = __lc->_M_falsename_size == 0;
612 bool __donet = __lc->_M_truename_size == 0;
613 bool __testeof = false;
615 while (!__donef || !__donet)
623 const char_type __c = *__beg;
626 __testf = __c == __lc->_M_falsename[__n];
628 if (!__testf && __donet)
632 __testt = __c == __lc->_M_truename[__n];
634 if (!__testt && __donef)
637 if (!__testt && !__testf)
643 __donef = !__testf || __n >= __lc->_M_falsename_size;
644 __donet = !__testt || __n >= __lc->_M_truename_size;
646 if (__testf && __n == __lc->_M_falsename_size && __n)
649 if (__testt && __n == __lc->_M_truename_size)
650 __err = ios_base::failbit;
652 __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
654 else if (__testt && __n == __lc->_M_truename_size && __n)
657 __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
661 // _GLIBCXX_RESOLVE_LIB_DEFECTS
662 // 23. Num_get overflow result.
664 __err = ios_base::failbit;
666 __err |= ios_base::eofbit;
672 template<typename _CharT, typename _InIter>
674 num_get<_CharT, _InIter>::
675 do_get(iter_type __beg, iter_type __end, ios_base& __io,
676 ios_base::iostate& __err, float& __v) const
680 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
681 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
683 __err |= ios_base::eofbit;
687 template<typename _CharT, typename _InIter>
689 num_get<_CharT, _InIter>::
690 do_get(iter_type __beg, iter_type __end, ios_base& __io,
691 ios_base::iostate& __err, double& __v) const
695 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
696 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
698 __err |= ios_base::eofbit;
702 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
703 template<typename _CharT, typename _InIter>
705 num_get<_CharT, _InIter>::
706 __do_get(iter_type __beg, iter_type __end, ios_base& __io,
707 ios_base::iostate& __err, double& __v) const
711 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
712 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
714 __err |= ios_base::eofbit;
719 template<typename _CharT, typename _InIter>
721 num_get<_CharT, _InIter>::
722 do_get(iter_type __beg, iter_type __end, ios_base& __io,
723 ios_base::iostate& __err, long double& __v) const
727 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
728 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
730 __err |= ios_base::eofbit;
734 template<typename _CharT, typename _InIter>
736 num_get<_CharT, _InIter>::
737 do_get(iter_type __beg, iter_type __end, ios_base& __io,
738 ios_base::iostate& __err, void*& __v) const
740 // Prepare for hex formatted input.
741 typedef ios_base::fmtflags fmtflags;
742 const fmtflags __fmt = __io.flags();
743 __io.flags((__fmt & ~ios_base::basefield) | ios_base::hex);
745 typedef __gnu_cxx::__conditional_type<(sizeof(void*)
746 <= sizeof(unsigned long)),
747 unsigned long, unsigned long long>::__type _UIntPtrType;
750 __beg = _M_extract_int(__beg, __end, __io, __err, __ul);
752 // Reset from hex formatted input.
755 __v = reinterpret_cast<void*>(__ul);
759 // For use by integer and floating-point types after they have been
760 // converted into a char_type string.
761 template<typename _CharT, typename _OutIter>
763 num_put<_CharT, _OutIter>::
764 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
765 _CharT* __new, const _CharT* __cs, int& __len) const
767 // [22.2.2.2.2] Stage 3.
768 // If necessary, pad.
769 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new,
771 __len = static_cast<int>(__w);
774 _GLIBCXX_END_LDBL_NAMESPACE
776 template<typename _CharT, typename _ValueT>
778 __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
779 ios_base::fmtflags __flags, bool __dec)
781 _CharT* __buf = __bufend;
782 if (__builtin_expect(__dec, true))
787 *--__buf = __lit[(__v % 10) + __num_base::_S_odigits];
792 else if ((__flags & ios_base::basefield) == ios_base::oct)
797 *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits];
805 const bool __uppercase = __flags & ios_base::uppercase;
806 const int __case_offset = __uppercase ? __num_base::_S_oudigits
807 : __num_base::_S_odigits;
810 *--__buf = __lit[(__v & 0xf) + __case_offset];
815 return __bufend - __buf;
818 _GLIBCXX_BEGIN_LDBL_NAMESPACE
820 template<typename _CharT, typename _OutIter>
822 num_put<_CharT, _OutIter>::
823 _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep,
824 ios_base&, _CharT* __new, _CharT* __cs, int& __len) const
826 _CharT* __p = std::__add_grouping(__new, __sep, __grouping,
827 __grouping_size, __cs, __cs + __len);
831 template<typename _CharT, typename _OutIter>
832 template<typename _ValueT>
834 num_put<_CharT, _OutIter>::
835 _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
838 using __gnu_cxx::__add_unsigned;
839 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
840 typedef __numpunct_cache<_CharT> __cache_type;
841 __use_cache<__cache_type> __uc;
842 const locale& __loc = __io._M_getloc();
843 const __cache_type* __lc = __uc(__loc);
844 const _CharT* __lit = __lc->_M_atoms_out;
845 const ios_base::fmtflags __flags = __io.flags();
847 // Long enough to hold hex, dec, and octal representations.
848 const int __ilen = 5 * sizeof(_ValueT);
849 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
852 // [22.2.2.2.2] Stage 1, numeric conversion to character.
853 // Result is returned right-justified in the buffer.
854 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
855 const bool __dec = (__basefield != ios_base::oct
856 && __basefield != ios_base::hex);
857 const __unsigned_type __u = ((__v > 0 || !__dec)
858 ? __unsigned_type(__v)
859 : -__unsigned_type(__v));
860 int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec);
861 __cs += __ilen - __len;
863 // Add grouping, if necessary.
864 if (__lc->_M_use_grouping)
866 // Grouping can add (almost) as many separators as the number
867 // of digits + space is reserved for numeric base or sign.
868 _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
871 _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size,
872 __lc->_M_thousands_sep, __io, __cs2 + 2, __cs, __len);
876 // Complete Stage 1, prepend numeric base or sign.
877 if (__builtin_expect(__dec, true))
882 if (bool(__flags & ios_base::showpos)
883 && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
884 *--__cs = __lit[__num_base::_S_oplus], ++__len;
887 *--__cs = __lit[__num_base::_S_ominus], ++__len;
889 else if (bool(__flags & ios_base::showbase) && __v)
891 if (__basefield == ios_base::oct)
892 *--__cs = __lit[__num_base::_S_odigits], ++__len;
896 const bool __uppercase = __flags & ios_base::uppercase;
897 *--__cs = __lit[__num_base::_S_ox + __uppercase];
899 *--__cs = __lit[__num_base::_S_odigits];
905 const streamsize __w = __io.width();
906 if (__w > static_cast<streamsize>(__len))
908 _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
910 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
915 // [22.2.2.2.2] Stage 4.
916 // Write resulting, fully-formatted string to output iterator.
917 return std::__write(__s, __cs, __len);
920 template<typename _CharT, typename _OutIter>
922 num_put<_CharT, _OutIter>::
923 _M_group_float(const char* __grouping, size_t __grouping_size,
924 _CharT __sep, const _CharT* __p, _CharT* __new,
925 _CharT* __cs, int& __len) const
927 // _GLIBCXX_RESOLVE_LIB_DEFECTS
928 // 282. What types does numpunct grouping refer to?
929 // Add grouping, if necessary.
930 const int __declen = __p ? __p - __cs : __len;
931 _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping,
933 __cs, __cs + __declen);
935 // Tack on decimal part.
936 int __newlen = __p2 - __new;
939 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
940 __newlen += __len - __declen;
945 // The following code uses vsnprintf (or vsprintf(), when
946 // _GLIBCXX_USE_C99 is not defined) to convert floating point values
947 // for insertion into a stream. An optimization would be to replace
948 // them with code that works directly on a wide buffer and then use
949 // __pad to do the padding. It would be good to replace them anyway
950 // to gain back the efficiency that C++ provides by knowing up front
951 // the type of the values to insert. Also, sprintf is dangerous
952 // since may lead to accidental buffer overruns. This
953 // implementation follows the C++ standard fairly directly as
954 // outlined in 22.2.2.2 [lib.locale.num.put]
955 template<typename _CharT, typename _OutIter>
956 template<typename _ValueT>
958 num_put<_CharT, _OutIter>::
959 _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
962 typedef __numpunct_cache<_CharT> __cache_type;
963 __use_cache<__cache_type> __uc;
964 const locale& __loc = __io._M_getloc();
965 const __cache_type* __lc = __uc(__loc);
967 // Use default precision if out of range.
968 const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision();
970 const int __max_digits =
971 __gnu_cxx::__numeric_traits<_ValueT>::__digits10;
973 // [22.2.2.2.2] Stage 1, numeric conversion to character.
975 // Long enough for the max format spec.
977 __num_base::_S_format_float(__io, __fbuf, __mod);
979 #ifdef _GLIBCXX_USE_C99
980 // First try a buffer perhaps big enough (most probably sufficient
981 // for non-ios_base::fixed outputs)
982 int __cs_size = __max_digits * 3;
983 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
984 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
985 __fbuf, __prec, __v);
987 // If the buffer was not large enough, try again with the correct size.
988 if (__len >= __cs_size)
990 __cs_size = __len + 1;
991 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
992 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
993 __fbuf, __prec, __v);
996 // Consider the possibility of long ios_base::fixed outputs
997 const bool __fixed = __io.flags() & ios_base::fixed;
998 const int __max_exp =
999 __gnu_cxx::__numeric_traits<_ValueT>::__max_exponent10;
1001 // The size of the output string is computed as follows.
1002 // ios_base::fixed outputs may need up to __max_exp + 1 chars
1003 // for the integer part + __prec chars for the fractional part
1004 // + 3 chars for sign, decimal point, '\0'. On the other hand,
1005 // for non-fixed outputs __max_digits * 2 + __prec chars are
1006 // largely sufficient.
1007 const int __cs_size = __fixed ? __max_exp + __prec + 4
1008 : __max_digits * 2 + __prec;
1009 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1010 __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf,
1014 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
1015 // numpunct.decimal_point() values for '.' and adding grouping.
1016 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1018 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1020 __ctype.widen(__cs, __cs + __len, __ws);
1022 // Replace decimal point.
1024 const char* __p = char_traits<char>::find(__cs, __len, '.');
1027 __wp = __ws + (__p - __cs);
1028 *__wp = __lc->_M_decimal_point;
1031 // Add grouping, if necessary.
1032 // N.B. Make sure to not group things like 2e20, i.e., no decimal
1033 // point, scientific notation.
1034 if (__lc->_M_use_grouping
1035 && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9'
1036 && __cs[1] >= '0' && __cs[2] >= '0')))
1038 // Grouping can add (almost) as many separators as the
1039 // number of digits, but no more.
1040 _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1043 streamsize __off = 0;
1044 if (__cs[0] == '-' || __cs[0] == '+')
1051 _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size,
1052 __lc->_M_thousands_sep, __wp, __ws2 + __off,
1053 __ws + __off, __len);
1060 const streamsize __w = __io.width();
1061 if (__w > static_cast<streamsize>(__len))
1063 _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1065 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
1070 // [22.2.2.2.2] Stage 4.
1071 // Write resulting, fully-formatted string to output iterator.
1072 return std::__write(__s, __ws, __len);
1075 template<typename _CharT, typename _OutIter>
1077 num_put<_CharT, _OutIter>::
1078 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
1080 const ios_base::fmtflags __flags = __io.flags();
1081 if ((__flags & ios_base::boolalpha) == 0)
1083 const long __l = __v;
1084 __s = _M_insert_int(__s, __io, __fill, __l);
1088 typedef __numpunct_cache<_CharT> __cache_type;
1089 __use_cache<__cache_type> __uc;
1090 const locale& __loc = __io._M_getloc();
1091 const __cache_type* __lc = __uc(__loc);
1093 const _CharT* __name = __v ? __lc->_M_truename
1094 : __lc->_M_falsename;
1095 int __len = __v ? __lc->_M_truename_size
1096 : __lc->_M_falsename_size;
1098 const streamsize __w = __io.width();
1099 if (__w > static_cast<streamsize>(__len))
1101 const streamsize __plen = __w - __len;
1103 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1106 char_traits<_CharT>::assign(__ps, __plen, __fill);
1109 if ((__flags & ios_base::adjustfield) == ios_base::left)
1111 __s = std::__write(__s, __name, __len);
1112 __s = std::__write(__s, __ps, __plen);
1116 __s = std::__write(__s, __ps, __plen);
1117 __s = std::__write(__s, __name, __len);
1122 __s = std::__write(__s, __name, __len);
1127 template<typename _CharT, typename _OutIter>
1129 num_put<_CharT, _OutIter>::
1130 do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1131 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1133 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1134 template<typename _CharT, typename _OutIter>
1136 num_put<_CharT, _OutIter>::
1137 __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1138 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1141 template<typename _CharT, typename _OutIter>
1143 num_put<_CharT, _OutIter>::
1144 do_put(iter_type __s, ios_base& __io, char_type __fill,
1145 long double __v) const
1146 { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1148 template<typename _CharT, typename _OutIter>
1150 num_put<_CharT, _OutIter>::
1151 do_put(iter_type __s, ios_base& __io, char_type __fill,
1152 const void* __v) const
1154 const ios_base::fmtflags __flags = __io.flags();
1155 const ios_base::fmtflags __fmt = ~(ios_base::basefield
1156 | ios_base::uppercase);
1157 __io.flags((__flags & __fmt) | (ios_base::hex | ios_base::showbase));
1159 typedef __gnu_cxx::__conditional_type<(sizeof(const void*)
1160 <= sizeof(unsigned long)),
1161 unsigned long, unsigned long long>::__type _UIntPtrType;
1163 __s = _M_insert_int(__s, __io, __fill,
1164 reinterpret_cast<_UIntPtrType>(__v));
1165 __io.flags(__flags);
1169 _GLIBCXX_END_LDBL_NAMESPACE
1171 // Construct correctly padded string, as per 22.2.2.2.2
1173 // __newlen > __oldlen
1174 // __news is allocated for __newlen size
1176 // NB: Of the two parameters, _CharT can be deduced from the
1177 // function arguments. The other (_Traits) has to be explicitly specified.
1178 template<typename _CharT, typename _Traits>
1180 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
1181 _CharT* __news, const _CharT* __olds,
1182 streamsize __newlen, streamsize __oldlen)
1184 const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
1185 const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
1188 if (__adjust == ios_base::left)
1190 _Traits::copy(__news, __olds, __oldlen);
1191 _Traits::assign(__news + __oldlen, __plen, __fill);
1196 if (__adjust == ios_base::internal)
1198 // Pad after the sign, if there is one.
1199 // Pad after 0[xX], if there is one.
1200 // Who came up with these rules, anyway? Jeeze.
1201 const locale& __loc = __io._M_getloc();
1202 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1204 if (__ctype.widen('-') == __olds[0]
1205 || __ctype.widen('+') == __olds[0])
1207 __news[0] = __olds[0];
1211 else if (__ctype.widen('0') == __olds[0]
1213 && (__ctype.widen('x') == __olds[1]
1214 || __ctype.widen('X') == __olds[1]))
1216 __news[0] = __olds[0];
1217 __news[1] = __olds[1];
1221 // else Padding first.
1223 _Traits::assign(__news, __plen, __fill);
1224 _Traits::copy(__news + __plen, __olds + __mod, __oldlen - __mod);
1227 template<typename _CharT>
1229 __add_grouping(_CharT* __s, _CharT __sep,
1230 const char* __gbeg, size_t __gsize,
1231 const _CharT* __first, const _CharT* __last)
1236 while (__last - __first > __gbeg[__idx]
1237 && static_cast<signed char>(__gbeg[__idx]) > 0
1238 && __gbeg[__idx] != __gnu_cxx::__numeric_traits<char>::__max)
1240 __last -= __gbeg[__idx];
1241 __idx < __gsize - 1 ? ++__idx : ++__ctr;
1244 while (__first != __last)
1245 *__s++ = *__first++;
1250 for (char __i = __gbeg[__idx]; __i > 0; --__i)
1251 *__s++ = *__first++;
1257 for (char __i = __gbeg[__idx]; __i > 0; --__i)
1258 *__s++ = *__first++;
1264 // Inhibit implicit instantiations for required instantiations,
1265 // which are defined via explicit instantiations elsewhere.
1266 // NB: This syntax is a GNU extension.
1267 #if _GLIBCXX_EXTERN_TEMPLATE
1268 extern template class numpunct<char>;
1269 extern template class numpunct_byname<char>;
1270 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<char>;
1271 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<char>;
1272 extern template class ctype_byname<char>;
1276 use_facet<ctype<char> >(const locale&);
1279 const numpunct<char>&
1280 use_facet<numpunct<char> >(const locale&);
1283 const num_put<char>&
1284 use_facet<num_put<char> >(const locale&);
1287 const num_get<char>&
1288 use_facet<num_get<char> >(const locale&);
1292 has_facet<ctype<char> >(const locale&);
1296 has_facet<numpunct<char> >(const locale&);
1300 has_facet<num_put<char> >(const locale&);
1304 has_facet<num_get<char> >(const locale&);
1306 #ifdef _GLIBCXX_USE_WCHAR_T
1307 extern template class numpunct<wchar_t>;
1308 extern template class numpunct_byname<wchar_t>;
1309 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<wchar_t>;
1310 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<wchar_t>;
1311 extern template class ctype_byname<wchar_t>;
1314 const ctype<wchar_t>&
1315 use_facet<ctype<wchar_t> >(const locale&);
1318 const numpunct<wchar_t>&
1319 use_facet<numpunct<wchar_t> >(const locale&);
1322 const num_put<wchar_t>&
1323 use_facet<num_put<wchar_t> >(const locale&);
1326 const num_get<wchar_t>&
1327 use_facet<num_get<wchar_t> >(const locale&);
1331 has_facet<ctype<wchar_t> >(const locale&);
1335 has_facet<numpunct<wchar_t> >(const locale&);
1339 has_facet<num_put<wchar_t> >(const locale&);
1343 has_facet<num_get<wchar_t> >(const locale&);
1347 _GLIBCXX_END_NAMESPACE