1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
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>(__np.grouping()[0]) > 0);
96 _M_truename_size = __np.truename().size();
97 _CharT* __truename = new _CharT[_M_truename_size];
98 __np.truename().copy(__truename, _M_truename_size);
99 _M_truename = __truename;
101 _M_falsename_size = __np.falsename().size();
102 _CharT* __falsename = new _CharT[_M_falsename_size];
103 __np.falsename().copy(__falsename, _M_falsename_size);
104 _M_falsename = __falsename;
106 _M_decimal_point = __np.decimal_point();
107 _M_thousands_sep = __np.thousands_sep();
109 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
110 __ct.widen(__num_base::_S_atoms_out,
111 __num_base::_S_atoms_out + __num_base::_S_oend, _M_atoms_out);
112 __ct.widen(__num_base::_S_atoms_in,
113 __num_base::_S_atoms_in + __num_base::_S_iend, _M_atoms_in);
116 // Used by both numeric and monetary facets.
117 // Check to make sure that the __grouping_tmp string constructed in
118 // money_get or num_get matches the canonical grouping for a given
120 // __grouping_tmp is parsed L to R
121 // 1,222,444 == __grouping_tmp of "\1\3\3"
122 // __grouping is parsed R to L
123 // 1,222,444 == __grouping of "\3" == "\3\3\3"
125 __verify_grouping(const char* __grouping, size_t __grouping_size,
126 const string& __grouping_tmp);
128 _GLIBCXX_BEGIN_LDBL_NAMESPACE
130 template<typename _CharT, typename _InIter>
132 num_get<_CharT, _InIter>::
133 _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
134 ios_base::iostate& __err, string& __xtrc) const
136 typedef char_traits<_CharT> __traits_type;
137 typedef __numpunct_cache<_CharT> __cache_type;
138 __use_cache<__cache_type> __uc;
139 const locale& __loc = __io._M_getloc();
140 const __cache_type* __lc = __uc(__loc);
141 const _CharT* __lit = __lc->_M_atoms_in;
142 char_type __c = char_type();
144 // True if __beg becomes equal to __end.
145 bool __testeof = __beg == __end;
147 // First check for sign.
151 const bool __plus = __c == __lit[__num_base::_S_iplus];
152 if ((__plus || __c == __lit[__num_base::_S_iminus])
153 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
154 && !(__c == __lc->_M_decimal_point))
156 __xtrc += __plus ? '+' : '-';
157 if (++__beg != __end)
164 // Next, look for leading zeros.
165 bool __found_mantissa = false;
169 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
170 || __c == __lc->_M_decimal_point)
172 else if (__c == __lit[__num_base::_S_izero])
174 if (!__found_mantissa)
177 __found_mantissa = true;
181 if (++__beg != __end)
190 // Only need acceptable digits for floating point numbers.
191 bool __found_dec = false;
192 bool __found_sci = false;
193 string __found_grouping;
194 if (__lc->_M_use_grouping)
195 __found_grouping.reserve(32);
196 const char_type* __lit_zero = __lit + __num_base::_S_izero;
198 if (!__lc->_M_allocated)
202 const int __digit = _M_find(__lit_zero, 10, __c);
205 __xtrc += '0' + __digit;
206 __found_mantissa = true;
208 else if (__c == __lc->_M_decimal_point
209 && !__found_dec && !__found_sci)
214 else if ((__c == __lit[__num_base::_S_ie]
215 || __c == __lit[__num_base::_S_iE])
216 && !__found_sci && __found_mantissa)
218 // Scientific notation.
222 // Remove optional plus or minus sign, if they exist.
223 if (++__beg != __end)
226 const bool __plus = __c == __lit[__num_base::_S_iplus];
227 if (__plus || __c == __lit[__num_base::_S_iminus])
228 __xtrc += __plus ? '+' : '-';
241 if (++__beg != __end)
249 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
250 // and decimal_point.
251 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
253 if (!__found_dec && !__found_sci)
255 // NB: Thousands separator at the beginning of a string
256 // is a no-no, as is two consecutive thousands separators.
259 __found_grouping += static_cast<char>(__sep_pos);
264 // NB: __convert_to_v will not assign __v and will
273 else if (__c == __lc->_M_decimal_point)
275 if (!__found_dec && !__found_sci)
277 // If no grouping chars are seen, no grouping check
278 // is applied. Therefore __found_grouping is adjusted
279 // only if decimal_point comes after some thousands_sep.
280 if (__found_grouping.size())
281 __found_grouping += static_cast<char>(__sep_pos);
290 const char_type* __q =
291 __traits_type::find(__lit_zero, 10, __c);
294 __xtrc += '0' + (__q - __lit_zero);
295 __found_mantissa = true;
298 else if ((__c == __lit[__num_base::_S_ie]
299 || __c == __lit[__num_base::_S_iE])
300 && !__found_sci && __found_mantissa)
302 // Scientific notation.
303 if (__found_grouping.size() && !__found_dec)
304 __found_grouping += static_cast<char>(__sep_pos);
308 // Remove optional plus or minus sign, if they exist.
309 if (++__beg != __end)
312 const bool __plus = __c == __lit[__num_base::_S_iplus];
313 if ((__plus || __c == __lit[__num_base::_S_iminus])
314 && !(__lc->_M_use_grouping
315 && __c == __lc->_M_thousands_sep)
316 && !(__c == __lc->_M_decimal_point))
317 __xtrc += __plus ? '+' : '-';
331 if (++__beg != __end)
337 // Digit grouping is checked. If grouping and found_grouping don't
338 // match, then get very very upset, and set failbit.
339 if (__found_grouping.size())
341 // Add the ending grouping if a decimal or 'e'/'E' wasn't found.
342 if (!__found_dec && !__found_sci)
343 __found_grouping += static_cast<char>(__sep_pos);
345 if (!std::__verify_grouping(__lc->_M_grouping,
346 __lc->_M_grouping_size,
348 __err = ios_base::failbit;
354 template<typename _CharT, typename _InIter>
355 template<typename _ValueT>
357 num_get<_CharT, _InIter>::
358 _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
359 ios_base::iostate& __err, _ValueT& __v) const
361 typedef char_traits<_CharT> __traits_type;
362 using __gnu_cxx::__add_unsigned;
363 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
364 typedef __numpunct_cache<_CharT> __cache_type;
365 __use_cache<__cache_type> __uc;
366 const locale& __loc = __io._M_getloc();
367 const __cache_type* __lc = __uc(__loc);
368 const _CharT* __lit = __lc->_M_atoms_in;
369 char_type __c = char_type();
371 // NB: Iff __basefield == 0, __base can change based on contents.
372 const ios_base::fmtflags __basefield = __io.flags()
373 & ios_base::basefield;
374 const bool __oct = __basefield == ios_base::oct;
375 int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10);
377 // True if __beg becomes equal to __end.
378 bool __testeof = __beg == __end;
380 // First check for sign.
381 bool __negative = false;
385 if (__gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
386 __negative = __c == __lit[__num_base::_S_iminus];
387 if ((__negative || __c == __lit[__num_base::_S_iplus])
388 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
389 && !(__c == __lc->_M_decimal_point))
391 if (++__beg != __end)
398 // Next, look for leading zeros and check required digits
400 bool __found_zero = false;
404 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
405 || __c == __lc->_M_decimal_point)
407 else if (__c == __lit[__num_base::_S_izero]
408 && (!__found_zero || __base == 10))
412 if (__basefield == 0)
417 else if (__found_zero
418 && (__c == __lit[__num_base::_S_ix]
419 || __c == __lit[__num_base::_S_iX]))
421 if (__basefield == 0)
425 __found_zero = false;
434 if (++__beg != __end)
444 // At this point, base is determined. If not hex, only allow
445 // base digits as valid input.
446 const size_t __len = (__base == 16 ? __num_base::_S_iend
447 - __num_base::_S_izero : __base);
450 string __found_grouping;
451 if (__lc->_M_use_grouping)
452 __found_grouping.reserve(32);
453 bool __testfail = false;
454 bool __testoverflow = false;
455 const __unsigned_type __max = __negative
456 ? -__gnu_cxx::__numeric_traits<_ValueT>::__min
457 : __gnu_cxx::__numeric_traits<_ValueT>::__max;
458 const __unsigned_type __smax = __max / __base;
459 __unsigned_type __result = 0;
461 const char_type* __lit_zero = __lit + __num_base::_S_izero;
463 if (!__lc->_M_allocated)
467 __digit = _M_find(__lit_zero, __len, __c);
471 if (__result > __smax)
472 __testoverflow = true;
476 __testoverflow |= __result > __max - __digit;
481 if (++__beg != __end)
489 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
490 // and decimal_point.
491 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
493 // NB: Thousands separator at the beginning of a string
494 // is a no-no, as is two consecutive thousands separators.
497 __found_grouping += static_cast<char>(__sep_pos);
506 else if (__c == __lc->_M_decimal_point)
510 const char_type* __q =
511 __traits_type::find(__lit_zero, __len, __c);
515 __digit = __q - __lit_zero;
518 if (__result > __smax)
519 __testoverflow = true;
523 __testoverflow |= __result > __max - __digit;
529 if (++__beg != __end)
535 // Digit grouping is checked. If grouping and found_grouping don't
536 // match, then get very very upset, and set failbit.
537 if (__found_grouping.size())
539 // Add the ending grouping.
540 __found_grouping += static_cast<char>(__sep_pos);
542 if (!std::__verify_grouping(__lc->_M_grouping,
543 __lc->_M_grouping_size,
545 __err = ios_base::failbit;
548 // _GLIBCXX_RESOLVE_LIB_DEFECTS
549 // 23. Num_get overflow result.
550 if ((!__sep_pos && !__found_zero && !__found_grouping.size())
554 __err = ios_base::failbit;
556 else if (__testoverflow)
559 __v = __gnu_cxx::__numeric_traits<_ValueT>::__min;
561 __v = __gnu_cxx::__numeric_traits<_ValueT>::__max;
562 __err = ios_base::failbit;
565 __v = __negative ? -__result : __result;
568 __err |= ios_base::eofbit;
572 // _GLIBCXX_RESOLVE_LIB_DEFECTS
573 // 17. Bad bool parsing
574 template<typename _CharT, typename _InIter>
576 num_get<_CharT, _InIter>::
577 do_get(iter_type __beg, iter_type __end, ios_base& __io,
578 ios_base::iostate& __err, bool& __v) const
580 if (!(__io.flags() & ios_base::boolalpha))
582 // Parse bool values as long.
583 // NB: We can't just call do_get(long) here, as it might
584 // refer to a derived class.
586 __beg = _M_extract_int(__beg, __end, __io, __err, __l);
587 if (__l == 0 || __l == 1)
591 // _GLIBCXX_RESOLVE_LIB_DEFECTS
592 // 23. Num_get overflow result.
594 __err = ios_base::failbit;
596 __err |= ios_base::eofbit;
601 // Parse bool values as alphanumeric.
602 typedef __numpunct_cache<_CharT> __cache_type;
603 __use_cache<__cache_type> __uc;
604 const locale& __loc = __io._M_getloc();
605 const __cache_type* __lc = __uc(__loc);
609 bool __donef = __lc->_M_falsename_size == 0;
610 bool __donet = __lc->_M_truename_size == 0;
611 bool __testeof = false;
613 while (!__donef || !__donet)
621 const char_type __c = *__beg;
624 __testf = __c == __lc->_M_falsename[__n];
626 if (!__testf && __donet)
630 __testt = __c == __lc->_M_truename[__n];
632 if (!__testt && __donef)
635 if (!__testt && !__testf)
641 __donef = !__testf || __n >= __lc->_M_falsename_size;
642 __donet = !__testt || __n >= __lc->_M_truename_size;
644 if (__testf && __n == __lc->_M_falsename_size && __n)
647 if (__testt && __n == __lc->_M_truename_size)
648 __err = ios_base::failbit;
650 __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
652 else if (__testt && __n == __lc->_M_truename_size && __n)
655 __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
659 // _GLIBCXX_RESOLVE_LIB_DEFECTS
660 // 23. Num_get overflow result.
662 __err = ios_base::failbit;
664 __err |= ios_base::eofbit;
670 template<typename _CharT, typename _InIter>
672 num_get<_CharT, _InIter>::
673 do_get(iter_type __beg, iter_type __end, ios_base& __io,
674 ios_base::iostate& __err, float& __v) const
678 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
679 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
681 __err |= ios_base::eofbit;
685 template<typename _CharT, typename _InIter>
687 num_get<_CharT, _InIter>::
688 do_get(iter_type __beg, iter_type __end, ios_base& __io,
689 ios_base::iostate& __err, double& __v) const
693 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
694 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
696 __err |= ios_base::eofbit;
700 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
701 template<typename _CharT, typename _InIter>
703 num_get<_CharT, _InIter>::
704 __do_get(iter_type __beg, iter_type __end, ios_base& __io,
705 ios_base::iostate& __err, double& __v) const
709 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
710 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
712 __err |= ios_base::eofbit;
717 template<typename _CharT, typename _InIter>
719 num_get<_CharT, _InIter>::
720 do_get(iter_type __beg, iter_type __end, ios_base& __io,
721 ios_base::iostate& __err, long double& __v) const
725 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
726 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
728 __err |= ios_base::eofbit;
732 template<typename _CharT, typename _InIter>
734 num_get<_CharT, _InIter>::
735 do_get(iter_type __beg, iter_type __end, ios_base& __io,
736 ios_base::iostate& __err, void*& __v) const
738 // Prepare for hex formatted input.
739 typedef ios_base::fmtflags fmtflags;
740 const fmtflags __fmt = __io.flags();
741 __io.flags((__fmt & ~ios_base::basefield) | ios_base::hex);
743 typedef __gnu_cxx::__conditional_type<(sizeof(void*)
744 <= sizeof(unsigned long)),
745 unsigned long, unsigned long long>::__type _UIntPtrType;
748 __beg = _M_extract_int(__beg, __end, __io, __err, __ul);
750 // Reset from hex formatted input.
753 __v = reinterpret_cast<void*>(__ul);
757 // For use by integer and floating-point types after they have been
758 // converted into a char_type string.
759 template<typename _CharT, typename _OutIter>
761 num_put<_CharT, _OutIter>::
762 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
763 _CharT* __new, const _CharT* __cs, int& __len) const
765 // [22.2.2.2.2] Stage 3.
766 // If necessary, pad.
767 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new,
769 __len = static_cast<int>(__w);
772 _GLIBCXX_END_LDBL_NAMESPACE
774 template<typename _CharT, typename _ValueT>
776 __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
777 ios_base::fmtflags __flags, bool __dec)
779 _CharT* __buf = __bufend;
780 if (__builtin_expect(__dec, true))
785 *--__buf = __lit[(__v % 10) + __num_base::_S_odigits];
790 else if ((__flags & ios_base::basefield) == ios_base::oct)
795 *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits];
803 const bool __uppercase = __flags & ios_base::uppercase;
804 const int __case_offset = __uppercase ? __num_base::_S_oudigits
805 : __num_base::_S_odigits;
808 *--__buf = __lit[(__v & 0xf) + __case_offset];
813 return __bufend - __buf;
816 _GLIBCXX_BEGIN_LDBL_NAMESPACE
818 template<typename _CharT, typename _OutIter>
820 num_put<_CharT, _OutIter>::
821 _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep,
822 ios_base&, _CharT* __new, _CharT* __cs, int& __len) const
824 _CharT* __p = std::__add_grouping(__new, __sep, __grouping,
825 __grouping_size, __cs, __cs + __len);
829 template<typename _CharT, typename _OutIter>
830 template<typename _ValueT>
832 num_put<_CharT, _OutIter>::
833 _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
836 using __gnu_cxx::__add_unsigned;
837 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
838 typedef __numpunct_cache<_CharT> __cache_type;
839 __use_cache<__cache_type> __uc;
840 const locale& __loc = __io._M_getloc();
841 const __cache_type* __lc = __uc(__loc);
842 const _CharT* __lit = __lc->_M_atoms_out;
843 const ios_base::fmtflags __flags = __io.flags();
845 // Long enough to hold hex, dec, and octal representations.
846 const int __ilen = 5 * sizeof(_ValueT);
847 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
850 // [22.2.2.2.2] Stage 1, numeric conversion to character.
851 // Result is returned right-justified in the buffer.
852 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
853 const bool __dec = (__basefield != ios_base::oct
854 && __basefield != ios_base::hex);
855 const __unsigned_type __u = ((__v > 0 || !__dec)
856 ? __unsigned_type(__v)
857 : -__unsigned_type(__v));
858 int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec);
859 __cs += __ilen - __len;
861 // Add grouping, if necessary.
862 if (__lc->_M_use_grouping)
864 // Grouping can add (almost) as many separators as the number
865 // of digits + space is reserved for numeric base or sign.
866 _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
869 _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size,
870 __lc->_M_thousands_sep, __io, __cs2 + 2, __cs, __len);
874 // Complete Stage 1, prepend numeric base or sign.
875 if (__builtin_expect(__dec, true))
880 if (bool(__flags & ios_base::showpos)
881 && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
882 *--__cs = __lit[__num_base::_S_oplus], ++__len;
885 *--__cs = __lit[__num_base::_S_ominus], ++__len;
887 else if (bool(__flags & ios_base::showbase) && __v)
889 if (__basefield == ios_base::oct)
890 *--__cs = __lit[__num_base::_S_odigits], ++__len;
894 const bool __uppercase = __flags & ios_base::uppercase;
895 *--__cs = __lit[__num_base::_S_ox + __uppercase];
897 *--__cs = __lit[__num_base::_S_odigits];
903 const streamsize __w = __io.width();
904 if (__w > static_cast<streamsize>(__len))
906 _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
908 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
913 // [22.2.2.2.2] Stage 4.
914 // Write resulting, fully-formatted string to output iterator.
915 return std::__write(__s, __cs, __len);
918 template<typename _CharT, typename _OutIter>
920 num_put<_CharT, _OutIter>::
921 _M_group_float(const char* __grouping, size_t __grouping_size,
922 _CharT __sep, const _CharT* __p, _CharT* __new,
923 _CharT* __cs, int& __len) const
925 // _GLIBCXX_RESOLVE_LIB_DEFECTS
926 // 282. What types does numpunct grouping refer to?
927 // Add grouping, if necessary.
928 const int __declen = __p ? __p - __cs : __len;
929 _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping,
931 __cs, __cs + __declen);
933 // Tack on decimal part.
934 int __newlen = __p2 - __new;
937 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
938 __newlen += __len - __declen;
943 // The following code uses vsnprintf (or vsprintf(), when
944 // _GLIBCXX_USE_C99 is not defined) to convert floating point values
945 // for insertion into a stream. An optimization would be to replace
946 // them with code that works directly on a wide buffer and then use
947 // __pad to do the padding. It would be good to replace them anyway
948 // to gain back the efficiency that C++ provides by knowing up front
949 // the type of the values to insert. Also, sprintf is dangerous
950 // since may lead to accidental buffer overruns. This
951 // implementation follows the C++ standard fairly directly as
952 // outlined in 22.2.2.2 [lib.locale.num.put]
953 template<typename _CharT, typename _OutIter>
954 template<typename _ValueT>
956 num_put<_CharT, _OutIter>::
957 _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
960 typedef __numpunct_cache<_CharT> __cache_type;
961 __use_cache<__cache_type> __uc;
962 const locale& __loc = __io._M_getloc();
963 const __cache_type* __lc = __uc(__loc);
965 // Use default precision if out of range.
966 const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision();
968 const int __max_digits =
969 __gnu_cxx::__numeric_traits<_ValueT>::__digits10;
971 // [22.2.2.2.2] Stage 1, numeric conversion to character.
973 // Long enough for the max format spec.
975 __num_base::_S_format_float(__io, __fbuf, __mod);
977 #ifdef _GLIBCXX_USE_C99
978 // First try a buffer perhaps big enough (most probably sufficient
979 // for non-ios_base::fixed outputs)
980 int __cs_size = __max_digits * 3;
981 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
982 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
983 __fbuf, __prec, __v);
985 // If the buffer was not large enough, try again with the correct size.
986 if (__len >= __cs_size)
988 __cs_size = __len + 1;
989 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
990 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
991 __fbuf, __prec, __v);
994 // Consider the possibility of long ios_base::fixed outputs
995 const bool __fixed = __io.flags() & ios_base::fixed;
996 const int __max_exp =
997 __gnu_cxx::__numeric_traits<_ValueT>::__max_exponent10;
999 // The size of the output string is computed as follows.
1000 // ios_base::fixed outputs may need up to __max_exp + 1 chars
1001 // for the integer part + __prec chars for the fractional part
1002 // + 3 chars for sign, decimal point, '\0'. On the other hand,
1003 // for non-fixed outputs __max_digits * 2 + __prec chars are
1004 // largely sufficient.
1005 const int __cs_size = __fixed ? __max_exp + __prec + 4
1006 : __max_digits * 2 + __prec;
1007 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1008 __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf,
1012 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
1013 // numpunct.decimal_point() values for '.' and adding grouping.
1014 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1016 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1018 __ctype.widen(__cs, __cs + __len, __ws);
1020 // Replace decimal point.
1022 const char* __p = char_traits<char>::find(__cs, __len, '.');
1025 __wp = __ws + (__p - __cs);
1026 *__wp = __lc->_M_decimal_point;
1029 // Add grouping, if necessary.
1030 // N.B. Make sure to not group things like 2e20, i.e., no decimal
1031 // point, scientific notation.
1032 if (__lc->_M_use_grouping
1033 && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9'
1034 && __cs[1] >= '0' && __cs[2] >= '0')))
1036 // Grouping can add (almost) as many separators as the
1037 // number of digits, but no more.
1038 _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1041 streamsize __off = 0;
1042 if (__cs[0] == '-' || __cs[0] == '+')
1049 _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size,
1050 __lc->_M_thousands_sep, __wp, __ws2 + __off,
1051 __ws + __off, __len);
1058 const streamsize __w = __io.width();
1059 if (__w > static_cast<streamsize>(__len))
1061 _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1063 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
1068 // [22.2.2.2.2] Stage 4.
1069 // Write resulting, fully-formatted string to output iterator.
1070 return std::__write(__s, __ws, __len);
1073 template<typename _CharT, typename _OutIter>
1075 num_put<_CharT, _OutIter>::
1076 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
1078 const ios_base::fmtflags __flags = __io.flags();
1079 if ((__flags & ios_base::boolalpha) == 0)
1081 const long __l = __v;
1082 __s = _M_insert_int(__s, __io, __fill, __l);
1086 typedef __numpunct_cache<_CharT> __cache_type;
1087 __use_cache<__cache_type> __uc;
1088 const locale& __loc = __io._M_getloc();
1089 const __cache_type* __lc = __uc(__loc);
1091 const _CharT* __name = __v ? __lc->_M_truename
1092 : __lc->_M_falsename;
1093 int __len = __v ? __lc->_M_truename_size
1094 : __lc->_M_falsename_size;
1096 const streamsize __w = __io.width();
1097 if (__w > static_cast<streamsize>(__len))
1100 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1102 _M_pad(__fill, __w, __io, __cs, __name, __len);
1106 __s = std::__write(__s, __name, __len);
1111 template<typename _CharT, typename _OutIter>
1113 num_put<_CharT, _OutIter>::
1114 do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1115 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1117 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1118 template<typename _CharT, typename _OutIter>
1120 num_put<_CharT, _OutIter>::
1121 __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1122 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1125 template<typename _CharT, typename _OutIter>
1127 num_put<_CharT, _OutIter>::
1128 do_put(iter_type __s, ios_base& __io, char_type __fill,
1129 long double __v) const
1130 { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1132 template<typename _CharT, typename _OutIter>
1134 num_put<_CharT, _OutIter>::
1135 do_put(iter_type __s, ios_base& __io, char_type __fill,
1136 const void* __v) const
1138 const ios_base::fmtflags __flags = __io.flags();
1139 const ios_base::fmtflags __fmt = ~(ios_base::basefield
1140 | ios_base::uppercase
1141 | ios_base::internal);
1142 __io.flags((__flags & __fmt) | (ios_base::hex | ios_base::showbase));
1144 typedef __gnu_cxx::__conditional_type<(sizeof(const void*)
1145 <= sizeof(unsigned long)),
1146 unsigned long, unsigned long long>::__type _UIntPtrType;
1148 __s = _M_insert_int(__s, __io, __fill,
1149 reinterpret_cast<_UIntPtrType>(__v));
1150 __io.flags(__flags);
1154 _GLIBCXX_END_LDBL_NAMESPACE
1156 // Construct correctly padded string, as per 22.2.2.2.2
1158 // __newlen > __oldlen
1159 // __news is allocated for __newlen size
1161 // NB: Of the two parameters, _CharT can be deduced from the
1162 // function arguments. The other (_Traits) has to be explicitly specified.
1163 template<typename _CharT, typename _Traits>
1165 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
1166 _CharT* __news, const _CharT* __olds,
1167 streamsize __newlen, streamsize __oldlen)
1169 const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
1170 const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
1173 if (__adjust == ios_base::left)
1175 _Traits::copy(__news, __olds, __oldlen);
1176 _Traits::assign(__news + __oldlen, __plen, __fill);
1181 if (__adjust == ios_base::internal)
1183 // Pad after the sign, if there is one.
1184 // Pad after 0[xX], if there is one.
1185 // Who came up with these rules, anyway? Jeeze.
1186 const locale& __loc = __io._M_getloc();
1187 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1189 if (__ctype.widen('-') == __olds[0]
1190 || __ctype.widen('+') == __olds[0])
1192 __news[0] = __olds[0];
1196 else if (__ctype.widen('0') == __olds[0]
1198 && (__ctype.widen('x') == __olds[1]
1199 || __ctype.widen('X') == __olds[1]))
1201 __news[0] = __olds[0];
1202 __news[1] = __olds[1];
1206 // else Padding first.
1208 _Traits::assign(__news, __plen, __fill);
1209 _Traits::copy(__news + __plen, __olds + __mod, __oldlen - __mod);
1212 template<typename _CharT>
1214 __add_grouping(_CharT* __s, _CharT __sep,
1215 const char* __gbeg, size_t __gsize,
1216 const _CharT* __first, const _CharT* __last)
1221 while (__last - __first > __gbeg[__idx]
1222 && static_cast<signed char>(__gbeg[__idx]) > 0)
1224 __last -= __gbeg[__idx];
1225 __idx < __gsize - 1 ? ++__idx : ++__ctr;
1228 while (__first != __last)
1229 *__s++ = *__first++;
1234 for (char __i = __gbeg[__idx]; __i > 0; --__i)
1235 *__s++ = *__first++;
1241 for (char __i = __gbeg[__idx]; __i > 0; --__i)
1242 *__s++ = *__first++;
1248 // Inhibit implicit instantiations for required instantiations,
1249 // which are defined via explicit instantiations elsewhere.
1250 // NB: This syntax is a GNU extension.
1251 #if _GLIBCXX_EXTERN_TEMPLATE
1252 extern template class numpunct<char>;
1253 extern template class numpunct_byname<char>;
1254 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<char>;
1255 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<char>;
1256 extern template class ctype_byname<char>;
1260 use_facet<ctype<char> >(const locale&);
1263 const numpunct<char>&
1264 use_facet<numpunct<char> >(const locale&);
1267 const num_put<char>&
1268 use_facet<num_put<char> >(const locale&);
1271 const num_get<char>&
1272 use_facet<num_get<char> >(const locale&);
1276 has_facet<ctype<char> >(const locale&);
1280 has_facet<numpunct<char> >(const locale&);
1284 has_facet<num_put<char> >(const locale&);
1288 has_facet<num_get<char> >(const locale&);
1290 #ifdef _GLIBCXX_USE_WCHAR_T
1291 extern template class numpunct<wchar_t>;
1292 extern template class numpunct_byname<wchar_t>;
1293 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<wchar_t>;
1294 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<wchar_t>;
1295 extern template class ctype_byname<wchar_t>;
1298 const ctype<wchar_t>&
1299 use_facet<ctype<wchar_t> >(const locale&);
1302 const numpunct<wchar_t>&
1303 use_facet<numpunct<wchar_t> >(const locale&);
1306 const num_put<wchar_t>&
1307 use_facet<num_put<wchar_t> >(const locale&);
1310 const num_get<wchar_t>&
1311 use_facet<num_get<wchar_t> >(const locale&);
1315 has_facet<ctype<wchar_t> >(const locale&);
1319 has_facet<numpunct<wchar_t> >(const locale&);
1323 has_facet<num_put<wchar_t> >(const locale&);
1327 has_facet<num_get<wchar_t> >(const locale&);
1331 _GLIBCXX_END_NAMESPACE