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 template<typename _Facet>
46 locale::combine(const locale& __other) const
48 _Impl* __tmp = new _Impl(*_M_impl, 1);
51 __tmp->_M_replace_facet(__other._M_impl, &_Facet::id);
55 __tmp->_M_remove_reference();
56 __throw_exception_again;
61 template<typename _CharT, typename _Traits, typename _Alloc>
63 locale::operator()(const basic_string<_CharT, _Traits, _Alloc>& __s1,
64 const basic_string<_CharT, _Traits, _Alloc>& __s2) const
66 typedef std::collate<_CharT> __collate_type;
67 const __collate_type& __collate = use_facet<__collate_type>(*this);
68 return (__collate.compare(__s1.data(), __s1.data() + __s1.length(),
69 __s2.data(), __s2.data() + __s2.length()) < 0);
73 * @brief Test for the presence of a facet.
75 * has_facet tests the locale argument for the presence of the facet type
76 * provided as the template parameter. Facets derived from the facet
77 * parameter will also return true.
79 * @param Facet The facet type to test the presence of.
80 * @param locale The locale to test.
81 * @return true if locale contains a facet of type Facet, else false.
83 template<typename _Facet>
85 has_facet(const locale& __loc) throw()
87 const size_t __i = _Facet::id._M_id();
88 const locale::facet** __facets = __loc._M_impl->_M_facets;
89 return (__i < __loc._M_impl->_M_facets_size && __facets[__i]);
93 * @brief Return a facet.
95 * use_facet looks for and returns a reference to a facet of type Facet
96 * where Facet is the template parameter. If has_facet(locale) is true,
97 * there is a suitable facet to return. It throws std::bad_cast if the
98 * locale doesn't contain a facet of type Facet.
100 * @param Facet The facet type to access.
101 * @param locale The locale to use.
102 * @return Reference to facet of type Facet.
103 * @throw std::bad_cast if locale doesn't contain a facet of type Facet.
105 template<typename _Facet>
107 use_facet(const locale& __loc)
109 const size_t __i = _Facet::id._M_id();
110 const locale::facet** __facets = __loc._M_impl->_M_facets;
111 if (!(__i < __loc._M_impl->_M_facets_size && __facets[__i]))
113 return static_cast<const _Facet&>(*__facets[__i]);
116 // Routine to access a cache for the facet. If the cache didn't
117 // exist before, it gets constructed on the fly.
118 template<typename _Facet>
122 operator() (const locale& __loc) const;
126 template<typename _CharT>
127 struct __use_cache<__numpunct_cache<_CharT> >
129 const __numpunct_cache<_CharT>*
130 operator() (const locale& __loc) const
132 const size_t __i = numpunct<_CharT>::id._M_id();
133 const locale::facet** __caches = __loc._M_impl->_M_caches;
136 __numpunct_cache<_CharT>* __tmp = NULL;
139 __tmp = new __numpunct_cache<_CharT>;
140 __tmp->_M_cache(__loc);
145 __throw_exception_again;
147 __loc._M_impl->_M_install_cache(__tmp, __i);
149 return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]);
153 template<typename _CharT>
155 __numpunct_cache<_CharT>::_M_cache(const locale& __loc)
159 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
161 _M_grouping_size = __np.grouping().size();
162 char* __grouping = new char[_M_grouping_size];
163 __np.grouping().copy(__grouping, _M_grouping_size);
164 _M_grouping = __grouping;
165 _M_use_grouping = (_M_grouping_size
166 && static_cast<signed char>(__np.grouping()[0]) > 0);
168 _M_truename_size = __np.truename().size();
169 _CharT* __truename = new _CharT[_M_truename_size];
170 __np.truename().copy(__truename, _M_truename_size);
171 _M_truename = __truename;
173 _M_falsename_size = __np.falsename().size();
174 _CharT* __falsename = new _CharT[_M_falsename_size];
175 __np.falsename().copy(__falsename, _M_falsename_size);
176 _M_falsename = __falsename;
178 _M_decimal_point = __np.decimal_point();
179 _M_thousands_sep = __np.thousands_sep();
181 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
182 __ct.widen(__num_base::_S_atoms_out,
183 __num_base::_S_atoms_out + __num_base::_S_oend, _M_atoms_out);
184 __ct.widen(__num_base::_S_atoms_in,
185 __num_base::_S_atoms_in + __num_base::_S_iend, _M_atoms_in);
188 // Used by both numeric and monetary facets.
189 // Check to make sure that the __grouping_tmp string constructed in
190 // money_get or num_get matches the canonical grouping for a given
192 // __grouping_tmp is parsed L to R
193 // 1,222,444 == __grouping_tmp of "\1\3\3"
194 // __grouping is parsed R to L
195 // 1,222,444 == __grouping of "\3" == "\3\3\3"
197 __verify_grouping(const char* __grouping, size_t __grouping_size,
198 const string& __grouping_tmp);
200 _GLIBCXX_BEGIN_LDBL_NAMESPACE
202 template<typename _CharT, typename _InIter>
204 num_get<_CharT, _InIter>::
205 _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
206 ios_base::iostate& __err, string& __xtrc) const
208 typedef char_traits<_CharT> __traits_type;
209 typedef __numpunct_cache<_CharT> __cache_type;
210 __use_cache<__cache_type> __uc;
211 const locale& __loc = __io._M_getloc();
212 const __cache_type* __lc = __uc(__loc);
213 const _CharT* __lit = __lc->_M_atoms_in;
214 char_type __c = char_type();
216 // True if __beg becomes equal to __end.
217 bool __testeof = __beg == __end;
219 // First check for sign.
223 const bool __plus = __c == __lit[__num_base::_S_iplus];
224 if ((__plus || __c == __lit[__num_base::_S_iminus])
225 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
226 && !(__c == __lc->_M_decimal_point))
228 __xtrc += __plus ? '+' : '-';
229 if (++__beg != __end)
236 // Next, look for leading zeros.
237 bool __found_mantissa = false;
241 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
242 || __c == __lc->_M_decimal_point)
244 else if (__c == __lit[__num_base::_S_izero])
246 if (!__found_mantissa)
249 __found_mantissa = true;
253 if (++__beg != __end)
262 // Only need acceptable digits for floating point numbers.
263 bool __found_dec = false;
264 bool __found_sci = false;
265 string __found_grouping;
266 if (__lc->_M_use_grouping)
267 __found_grouping.reserve(32);
268 const char_type* __lit_zero = __lit + __num_base::_S_izero;
270 if (!__lc->_M_allocated)
274 const int __digit = _M_find(__lit_zero, 10, __c);
277 __xtrc += '0' + __digit;
278 __found_mantissa = true;
280 else if (__c == __lc->_M_decimal_point
281 && !__found_dec && !__found_sci)
286 else if ((__c == __lit[__num_base::_S_ie]
287 || __c == __lit[__num_base::_S_iE])
288 && !__found_sci && __found_mantissa)
290 // Scientific notation.
294 // Remove optional plus or minus sign, if they exist.
295 if (++__beg != __end)
298 const bool __plus = __c == __lit[__num_base::_S_iplus];
299 if (__plus || __c == __lit[__num_base::_S_iminus])
300 __xtrc += __plus ? '+' : '-';
313 if (++__beg != __end)
321 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
322 // and decimal_point.
323 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
325 if (!__found_dec && !__found_sci)
327 // NB: Thousands separator at the beginning of a string
328 // is a no-no, as is two consecutive thousands separators.
331 __found_grouping += static_cast<char>(__sep_pos);
336 // NB: __convert_to_v will not assign __v and will
345 else if (__c == __lc->_M_decimal_point)
347 if (!__found_dec && !__found_sci)
349 // If no grouping chars are seen, no grouping check
350 // is applied. Therefore __found_grouping is adjusted
351 // only if decimal_point comes after some thousands_sep.
352 if (__found_grouping.size())
353 __found_grouping += static_cast<char>(__sep_pos);
362 const char_type* __q =
363 __traits_type::find(__lit_zero, 10, __c);
366 __xtrc += '0' + (__q - __lit_zero);
367 __found_mantissa = true;
370 else if ((__c == __lit[__num_base::_S_ie]
371 || __c == __lit[__num_base::_S_iE])
372 && !__found_sci && __found_mantissa)
374 // Scientific notation.
375 if (__found_grouping.size() && !__found_dec)
376 __found_grouping += static_cast<char>(__sep_pos);
380 // Remove optional plus or minus sign, if they exist.
381 if (++__beg != __end)
384 const bool __plus = __c == __lit[__num_base::_S_iplus];
385 if ((__plus || __c == __lit[__num_base::_S_iminus])
386 && !(__lc->_M_use_grouping
387 && __c == __lc->_M_thousands_sep)
388 && !(__c == __lc->_M_decimal_point))
389 __xtrc += __plus ? '+' : '-';
403 if (++__beg != __end)
409 // Digit grouping is checked. If grouping and found_grouping don't
410 // match, then get very very upset, and set failbit.
411 if (__found_grouping.size())
413 // Add the ending grouping if a decimal or 'e'/'E' wasn't found.
414 if (!__found_dec && !__found_sci)
415 __found_grouping += static_cast<char>(__sep_pos);
417 if (!std::__verify_grouping(__lc->_M_grouping,
418 __lc->_M_grouping_size,
420 __err |= ios_base::failbit;
425 __err |= ios_base::eofbit;
429 template<typename _CharT, typename _InIter>
430 template<typename _ValueT>
432 num_get<_CharT, _InIter>::
433 _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
434 ios_base::iostate& __err, _ValueT& __v) const
436 typedef char_traits<_CharT> __traits_type;
437 using __gnu_cxx::__add_unsigned;
438 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
439 typedef __numpunct_cache<_CharT> __cache_type;
440 __use_cache<__cache_type> __uc;
441 const locale& __loc = __io._M_getloc();
442 const __cache_type* __lc = __uc(__loc);
443 const _CharT* __lit = __lc->_M_atoms_in;
444 char_type __c = char_type();
446 // NB: Iff __basefield == 0, __base can change based on contents.
447 const ios_base::fmtflags __basefield = __io.flags()
448 & ios_base::basefield;
449 const bool __oct = __basefield == ios_base::oct;
450 int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10);
452 // True if __beg becomes equal to __end.
453 bool __testeof = __beg == __end;
455 // First check for sign.
456 bool __negative = false;
460 if (__gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
461 __negative = __c == __lit[__num_base::_S_iminus];
462 if ((__negative || __c == __lit[__num_base::_S_iplus])
463 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
464 && !(__c == __lc->_M_decimal_point))
466 if (++__beg != __end)
473 // Next, look for leading zeros and check required digits
475 bool __found_zero = false;
479 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
480 || __c == __lc->_M_decimal_point)
482 else if (__c == __lit[__num_base::_S_izero]
483 && (!__found_zero || __base == 10))
487 if (__basefield == 0)
492 else if (__found_zero
493 && (__c == __lit[__num_base::_S_ix]
494 || __c == __lit[__num_base::_S_iX]))
496 if (__basefield == 0)
500 __found_zero = false;
509 if (++__beg != __end)
519 // At this point, base is determined. If not hex, only allow
520 // base digits as valid input.
521 const size_t __len = (__base == 16 ? __num_base::_S_iend
522 - __num_base::_S_izero : __base);
525 string __found_grouping;
526 if (__lc->_M_use_grouping)
527 __found_grouping.reserve(32);
528 bool __testfail = false;
529 const __unsigned_type __max = __negative
530 ? -__gnu_cxx::__numeric_traits<_ValueT>::__min
531 : __gnu_cxx::__numeric_traits<_ValueT>::__max;
532 const __unsigned_type __smax = __max / __base;
533 __unsigned_type __result = 0;
535 const char_type* __lit_zero = __lit + __num_base::_S_izero;
537 if (!__lc->_M_allocated)
541 __digit = _M_find(__lit_zero, __len, __c);
545 if (__result > __smax)
550 __testfail |= __result > __max - __digit;
555 if (++__beg != __end)
563 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
564 // and decimal_point.
565 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
567 // NB: Thousands separator at the beginning of a string
568 // is a no-no, as is two consecutive thousands separators.
571 __found_grouping += static_cast<char>(__sep_pos);
580 else if (__c == __lc->_M_decimal_point)
584 const char_type* __q =
585 __traits_type::find(__lit_zero, __len, __c);
589 __digit = __q - __lit_zero;
592 if (__result > __smax)
597 __testfail |= __result > __max - __digit;
603 if (++__beg != __end)
609 // Digit grouping is checked. If grouping and found_grouping don't
610 // match, then get very very upset, and set failbit.
611 if (__found_grouping.size())
613 // Add the ending grouping.
614 __found_grouping += static_cast<char>(__sep_pos);
616 if (!std::__verify_grouping(__lc->_M_grouping,
617 __lc->_M_grouping_size,
619 __err |= ios_base::failbit;
622 if (!__testfail && (__sep_pos || __found_zero
623 || __found_grouping.size()))
624 __v = __negative ? -__result : __result;
626 __err |= ios_base::failbit;
629 __err |= ios_base::eofbit;
633 // _GLIBCXX_RESOLVE_LIB_DEFECTS
634 // 17. Bad bool parsing
635 template<typename _CharT, typename _InIter>
637 num_get<_CharT, _InIter>::
638 do_get(iter_type __beg, iter_type __end, ios_base& __io,
639 ios_base::iostate& __err, bool& __v) const
641 if (!(__io.flags() & ios_base::boolalpha))
643 // Parse bool values as long.
644 // NB: We can't just call do_get(long) here, as it might
645 // refer to a derived class.
647 __beg = _M_extract_int(__beg, __end, __io, __err, __l);
648 if (__l == 0 || __l == 1)
651 __err |= ios_base::failbit;
655 // Parse bool values as alphanumeric.
656 typedef __numpunct_cache<_CharT> __cache_type;
657 __use_cache<__cache_type> __uc;
658 const locale& __loc = __io._M_getloc();
659 const __cache_type* __lc = __uc(__loc);
664 bool __testeof = __beg == __end;
665 for (__n = 0; !__testeof; ++__n)
667 const char_type __c = *__beg;
671 if (__n < __lc->_M_falsename_size)
672 __testf = __c == __lc->_M_falsename[__n];
679 if (__n < __lc->_M_truename_size)
680 __testt = __c == __lc->_M_truename[__n];
685 if (!__testf && !__testt)
688 if (++__beg == __end)
691 if (__testf && __n == __lc->_M_falsename_size)
693 else if (__testt && __n == __lc->_M_truename_size)
696 __err |= ios_base::failbit;
699 __err |= ios_base::eofbit;
704 template<typename _CharT, typename _InIter>
706 num_get<_CharT, _InIter>::
707 do_get(iter_type __beg, iter_type __end, ios_base& __io,
708 ios_base::iostate& __err, long& __v) const
709 { return _M_extract_int(__beg, __end, __io, __err, __v); }
711 template<typename _CharT, typename _InIter>
713 num_get<_CharT, _InIter>::
714 do_get(iter_type __beg, iter_type __end, ios_base& __io,
715 ios_base::iostate& __err, unsigned short& __v) const
716 { return _M_extract_int(__beg, __end, __io, __err, __v); }
718 template<typename _CharT, typename _InIter>
720 num_get<_CharT, _InIter>::
721 do_get(iter_type __beg, iter_type __end, ios_base& __io,
722 ios_base::iostate& __err, unsigned int& __v) const
723 { return _M_extract_int(__beg, __end, __io, __err, __v); }
725 template<typename _CharT, typename _InIter>
727 num_get<_CharT, _InIter>::
728 do_get(iter_type __beg, iter_type __end, ios_base& __io,
729 ios_base::iostate& __err, unsigned long& __v) const
730 { return _M_extract_int(__beg, __end, __io, __err, __v); }
732 #ifdef _GLIBCXX_USE_LONG_LONG
733 template<typename _CharT, typename _InIter>
735 num_get<_CharT, _InIter>::
736 do_get(iter_type __beg, iter_type __end, ios_base& __io,
737 ios_base::iostate& __err, long long& __v) const
738 { return _M_extract_int(__beg, __end, __io, __err, __v); }
740 template<typename _CharT, typename _InIter>
742 num_get<_CharT, _InIter>::
743 do_get(iter_type __beg, iter_type __end, ios_base& __io,
744 ios_base::iostate& __err, unsigned long long& __v) const
745 { return _M_extract_int(__beg, __end, __io, __err, __v); }
748 template<typename _CharT, typename _InIter>
750 num_get<_CharT, _InIter>::
751 do_get(iter_type __beg, iter_type __end, ios_base& __io,
752 ios_base::iostate& __err, float& __v) const
756 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
757 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
761 template<typename _CharT, typename _InIter>
763 num_get<_CharT, _InIter>::
764 do_get(iter_type __beg, iter_type __end, ios_base& __io,
765 ios_base::iostate& __err, double& __v) const
769 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
770 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
774 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
775 template<typename _CharT, typename _InIter>
777 num_get<_CharT, _InIter>::
778 __do_get(iter_type __beg, iter_type __end, ios_base& __io,
779 ios_base::iostate& __err, double& __v) const
783 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
784 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
789 template<typename _CharT, typename _InIter>
791 num_get<_CharT, _InIter>::
792 do_get(iter_type __beg, iter_type __end, ios_base& __io,
793 ios_base::iostate& __err, long double& __v) const
797 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
798 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
802 template<typename _CharT, typename _InIter>
804 num_get<_CharT, _InIter>::
805 do_get(iter_type __beg, iter_type __end, ios_base& __io,
806 ios_base::iostate& __err, void*& __v) const
808 // Prepare for hex formatted input.
809 typedef ios_base::fmtflags fmtflags;
810 const fmtflags __fmt = __io.flags();
811 __io.flags((__fmt & ~ios_base::basefield) | ios_base::hex);
813 typedef __gnu_cxx::__conditional_type<(sizeof(void*)
814 <= sizeof(unsigned long)),
815 unsigned long, unsigned long long>::__type _UIntPtrType;
818 __beg = _M_extract_int(__beg, __end, __io, __err, __ul);
820 // Reset from hex formatted input.
823 if (!(__err & ios_base::failbit))
824 __v = reinterpret_cast<void*>(__ul);
828 // For use by integer and floating-point types after they have been
829 // converted into a char_type string.
830 template<typename _CharT, typename _OutIter>
832 num_put<_CharT, _OutIter>::
833 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
834 _CharT* __new, const _CharT* __cs, int& __len) const
836 // [22.2.2.2.2] Stage 3.
837 // If necessary, pad.
838 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new,
840 __len = static_cast<int>(__w);
843 _GLIBCXX_END_LDBL_NAMESPACE
845 template<typename _CharT, typename _ValueT>
847 __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
848 ios_base::fmtflags __flags, bool __dec)
850 _CharT* __buf = __bufend;
851 if (__builtin_expect(__dec, true))
856 *--__buf = __lit[(__v % 10) + __num_base::_S_odigits];
861 else if ((__flags & ios_base::basefield) == ios_base::oct)
866 *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits];
874 const bool __uppercase = __flags & ios_base::uppercase;
875 const int __case_offset = __uppercase ? __num_base::_S_oudigits
876 : __num_base::_S_odigits;
879 *--__buf = __lit[(__v & 0xf) + __case_offset];
884 return __bufend - __buf;
887 _GLIBCXX_BEGIN_LDBL_NAMESPACE
889 template<typename _CharT, typename _OutIter>
891 num_put<_CharT, _OutIter>::
892 _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep,
893 ios_base&, _CharT* __new, _CharT* __cs, int& __len) const
895 _CharT* __p = std::__add_grouping(__new, __sep, __grouping,
896 __grouping_size, __cs, __cs + __len);
900 template<typename _CharT, typename _OutIter>
901 template<typename _ValueT>
903 num_put<_CharT, _OutIter>::
904 _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
907 using __gnu_cxx::__add_unsigned;
908 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
909 typedef __numpunct_cache<_CharT> __cache_type;
910 __use_cache<__cache_type> __uc;
911 const locale& __loc = __io._M_getloc();
912 const __cache_type* __lc = __uc(__loc);
913 const _CharT* __lit = __lc->_M_atoms_out;
914 const ios_base::fmtflags __flags = __io.flags();
916 // Long enough to hold hex, dec, and octal representations.
917 const int __ilen = 5 * sizeof(_ValueT);
918 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
921 // [22.2.2.2.2] Stage 1, numeric conversion to character.
922 // Result is returned right-justified in the buffer.
923 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
924 const bool __dec = (__basefield != ios_base::oct
925 && __basefield != ios_base::hex);
926 const __unsigned_type __u = ((__v > 0 || !__dec)
927 ? __unsigned_type(__v)
928 : -__unsigned_type(__v));
929 int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec);
930 __cs += __ilen - __len;
932 // Add grouping, if necessary.
933 if (__lc->_M_use_grouping)
935 // Grouping can add (almost) as many separators as the number
936 // of digits + space is reserved for numeric base or sign.
937 _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
940 _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size,
941 __lc->_M_thousands_sep, __io, __cs2 + 2, __cs, __len);
945 // Complete Stage 1, prepend numeric base or sign.
946 if (__builtin_expect(__dec, true))
951 if (__flags & ios_base::showpos
952 && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
953 *--__cs = __lit[__num_base::_S_oplus], ++__len;
956 *--__cs = __lit[__num_base::_S_ominus], ++__len;
958 else if (__flags & ios_base::showbase && __v)
960 if (__basefield == ios_base::oct)
961 *--__cs = __lit[__num_base::_S_odigits], ++__len;
965 const bool __uppercase = __flags & ios_base::uppercase;
966 *--__cs = __lit[__num_base::_S_ox + __uppercase];
968 *--__cs = __lit[__num_base::_S_odigits];
974 const streamsize __w = __io.width();
975 if (__w > static_cast<streamsize>(__len))
977 _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
979 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
984 // [22.2.2.2.2] Stage 4.
985 // Write resulting, fully-formatted string to output iterator.
986 return std::__write(__s, __cs, __len);
989 template<typename _CharT, typename _OutIter>
991 num_put<_CharT, _OutIter>::
992 _M_group_float(const char* __grouping, size_t __grouping_size,
993 _CharT __sep, const _CharT* __p, _CharT* __new,
994 _CharT* __cs, int& __len) const
996 // _GLIBCXX_RESOLVE_LIB_DEFECTS
997 // 282. What types does numpunct grouping refer to?
998 // Add grouping, if necessary.
999 const int __declen = __p ? __p - __cs : __len;
1000 _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping,
1002 __cs, __cs + __declen);
1004 // Tack on decimal part.
1005 int __newlen = __p2 - __new;
1008 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
1009 __newlen += __len - __declen;
1014 // The following code uses vsnprintf (or vsprintf(), when
1015 // _GLIBCXX_USE_C99 is not defined) to convert floating point values
1016 // for insertion into a stream. An optimization would be to replace
1017 // them with code that works directly on a wide buffer and then use
1018 // __pad to do the padding. It would be good to replace them anyway
1019 // to gain back the efficiency that C++ provides by knowing up front
1020 // the type of the values to insert. Also, sprintf is dangerous
1021 // since may lead to accidental buffer overruns. This
1022 // implementation follows the C++ standard fairly directly as
1023 // outlined in 22.2.2.2 [lib.locale.num.put]
1024 template<typename _CharT, typename _OutIter>
1025 template<typename _ValueT>
1027 num_put<_CharT, _OutIter>::
1028 _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
1031 typedef __numpunct_cache<_CharT> __cache_type;
1032 __use_cache<__cache_type> __uc;
1033 const locale& __loc = __io._M_getloc();
1034 const __cache_type* __lc = __uc(__loc);
1036 // Use default precision if out of range.
1037 const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision();
1039 const int __max_digits =
1040 __gnu_cxx::__numeric_traits<_ValueT>::__digits10;
1042 // [22.2.2.2.2] Stage 1, numeric conversion to character.
1044 // Long enough for the max format spec.
1046 __num_base::_S_format_float(__io, __fbuf, __mod);
1048 #ifdef _GLIBCXX_USE_C99
1049 // First try a buffer perhaps big enough (most probably sufficient
1050 // for non-ios_base::fixed outputs)
1051 int __cs_size = __max_digits * 3;
1052 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1053 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1054 __fbuf, __prec, __v);
1056 // If the buffer was not large enough, try again with the correct size.
1057 if (__len >= __cs_size)
1059 __cs_size = __len + 1;
1060 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1061 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1062 __fbuf, __prec, __v);
1065 // Consider the possibility of long ios_base::fixed outputs
1066 const bool __fixed = __io.flags() & ios_base::fixed;
1067 const int __max_exp =
1068 __gnu_cxx::__numeric_traits<_ValueT>::__max_exponent10;
1070 // The size of the output string is computed as follows.
1071 // ios_base::fixed outputs may need up to __max_exp + 1 chars
1072 // for the integer part + __prec chars for the fractional part
1073 // + 3 chars for sign, decimal point, '\0'. On the other hand,
1074 // for non-fixed outputs __max_digits * 2 + __prec chars are
1075 // largely sufficient.
1076 const int __cs_size = __fixed ? __max_exp + __prec + 4
1077 : __max_digits * 2 + __prec;
1078 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1079 __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf,
1083 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
1084 // numpunct.decimal_point() values for '.' and adding grouping.
1085 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1087 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1089 __ctype.widen(__cs, __cs + __len, __ws);
1091 // Replace decimal point.
1093 const char* __p = char_traits<char>::find(__cs, __len, '.');
1096 __wp = __ws + (__p - __cs);
1097 *__wp = __lc->_M_decimal_point;
1100 // Add grouping, if necessary.
1101 // N.B. Make sure to not group things like 2e20, i.e., no decimal
1102 // point, scientific notation.
1103 if (__lc->_M_use_grouping
1104 && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9'
1105 && __cs[1] >= '0' && __cs[2] >= '0')))
1107 // Grouping can add (almost) as many separators as the
1108 // number of digits, but no more.
1109 _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1112 streamsize __off = 0;
1113 if (__cs[0] == '-' || __cs[0] == '+')
1120 _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size,
1121 __lc->_M_thousands_sep, __wp, __ws2 + __off,
1122 __ws + __off, __len);
1129 const streamsize __w = __io.width();
1130 if (__w > static_cast<streamsize>(__len))
1132 _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1134 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
1139 // [22.2.2.2.2] Stage 4.
1140 // Write resulting, fully-formatted string to output iterator.
1141 return std::__write(__s, __ws, __len);
1144 template<typename _CharT, typename _OutIter>
1146 num_put<_CharT, _OutIter>::
1147 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
1149 const ios_base::fmtflags __flags = __io.flags();
1150 if ((__flags & ios_base::boolalpha) == 0)
1152 const long __l = __v;
1153 __s = _M_insert_int(__s, __io, __fill, __l);
1157 typedef __numpunct_cache<_CharT> __cache_type;
1158 __use_cache<__cache_type> __uc;
1159 const locale& __loc = __io._M_getloc();
1160 const __cache_type* __lc = __uc(__loc);
1162 const _CharT* __name = __v ? __lc->_M_truename
1163 : __lc->_M_falsename;
1164 int __len = __v ? __lc->_M_truename_size
1165 : __lc->_M_falsename_size;
1167 const streamsize __w = __io.width();
1168 if (__w > static_cast<streamsize>(__len))
1171 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1173 _M_pad(__fill, __w, __io, __cs, __name, __len);
1177 __s = std::__write(__s, __name, __len);
1182 template<typename _CharT, typename _OutIter>
1184 num_put<_CharT, _OutIter>::
1185 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
1186 { return _M_insert_int(__s, __io, __fill, __v); }
1188 template<typename _CharT, typename _OutIter>
1190 num_put<_CharT, _OutIter>::
1191 do_put(iter_type __s, ios_base& __io, char_type __fill,
1192 unsigned long __v) const
1193 { return _M_insert_int(__s, __io, __fill, __v); }
1195 #ifdef _GLIBCXX_USE_LONG_LONG
1196 template<typename _CharT, typename _OutIter>
1198 num_put<_CharT, _OutIter>::
1199 do_put(iter_type __s, ios_base& __io, char_type __fill, long long __v) const
1200 { return _M_insert_int(__s, __io, __fill, __v); }
1202 template<typename _CharT, typename _OutIter>
1204 num_put<_CharT, _OutIter>::
1205 do_put(iter_type __s, ios_base& __io, char_type __fill,
1206 unsigned long long __v) const
1207 { return _M_insert_int(__s, __io, __fill, __v); }
1210 template<typename _CharT, typename _OutIter>
1212 num_put<_CharT, _OutIter>::
1213 do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1214 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1216 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1217 template<typename _CharT, typename _OutIter>
1219 num_put<_CharT, _OutIter>::
1220 __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1221 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1224 template<typename _CharT, typename _OutIter>
1226 num_put<_CharT, _OutIter>::
1227 do_put(iter_type __s, ios_base& __io, char_type __fill,
1228 long double __v) const
1229 { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1231 template<typename _CharT, typename _OutIter>
1233 num_put<_CharT, _OutIter>::
1234 do_put(iter_type __s, ios_base& __io, char_type __fill,
1235 const void* __v) const
1237 const ios_base::fmtflags __flags = __io.flags();
1238 const ios_base::fmtflags __fmt = ~(ios_base::basefield
1239 | ios_base::uppercase
1240 | ios_base::internal);
1241 __io.flags((__flags & __fmt) | (ios_base::hex | ios_base::showbase));
1243 typedef __gnu_cxx::__conditional_type<(sizeof(const void*)
1244 <= sizeof(unsigned long)),
1245 unsigned long, unsigned long long>::__type _UIntPtrType;
1247 __s = _M_insert_int(__s, __io, __fill,
1248 reinterpret_cast<_UIntPtrType>(__v));
1249 __io.flags(__flags);
1253 _GLIBCXX_END_LDBL_NAMESPACE
1255 // Generic version does nothing.
1256 template<typename _CharT>
1258 collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
1261 // Generic version does nothing.
1262 template<typename _CharT>
1264 collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
1267 template<typename _CharT>
1270 do_compare(const _CharT* __lo1, const _CharT* __hi1,
1271 const _CharT* __lo2, const _CharT* __hi2) const
1273 // strcoll assumes zero-terminated strings so we make a copy
1274 // and then put a zero at the end.
1275 const string_type __one(__lo1, __hi1);
1276 const string_type __two(__lo2, __hi2);
1278 const _CharT* __p = __one.c_str();
1279 const _CharT* __pend = __one.data() + __one.length();
1280 const _CharT* __q = __two.c_str();
1281 const _CharT* __qend = __two.data() + __two.length();
1283 // strcoll stops when it sees a nul character so we break
1284 // the strings into zero-terminated substrings and pass those
1288 const int __res = _M_compare(__p, __q);
1292 __p += char_traits<_CharT>::length(__p);
1293 __q += char_traits<_CharT>::length(__q);
1294 if (__p == __pend && __q == __qend)
1296 else if (__p == __pend)
1298 else if (__q == __qend)
1306 template<typename _CharT>
1307 typename collate<_CharT>::string_type
1309 do_transform(const _CharT* __lo, const _CharT* __hi) const
1313 // strxfrm assumes zero-terminated strings so we make a copy
1314 const string_type __str(__lo, __hi);
1316 const _CharT* __p = __str.c_str();
1317 const _CharT* __pend = __str.data() + __str.length();
1319 size_t __len = (__hi - __lo) * 2;
1321 _CharT* __c = new _CharT[__len];
1325 // strxfrm stops when it sees a nul character so we break
1326 // the string into zero-terminated substrings and pass those
1330 // First try a buffer perhaps big enough.
1331 size_t __res = _M_transform(__c, __p, __len);
1332 // If the buffer was not large enough, try again with the
1337 delete [] __c, __c = 0;
1338 __c = new _CharT[__len];
1339 __res = _M_transform(__c, __p, __len);
1342 __ret.append(__c, __res);
1343 __p += char_traits<_CharT>::length(__p);
1348 __ret.push_back(_CharT());
1354 __throw_exception_again;
1362 template<typename _CharT>
1365 do_hash(const _CharT* __lo, const _CharT* __hi) const
1367 unsigned long __val = 0;
1368 for (; __lo < __hi; ++__lo)
1370 *__lo + ((__val << 7)
1371 | (__val >> (__gnu_cxx::__numeric_traits<unsigned long>::
1373 return static_cast<long>(__val);
1376 // Construct correctly padded string, as per 22.2.2.2.2
1378 // __newlen > __oldlen
1379 // __news is allocated for __newlen size
1381 // NB: Of the two parameters, _CharT can be deduced from the
1382 // function arguments. The other (_Traits) has to be explicitly specified.
1383 template<typename _CharT, typename _Traits>
1385 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
1386 _CharT* __news, const _CharT* __olds,
1387 const streamsize __newlen,
1388 const streamsize __oldlen)
1390 const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
1391 const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
1394 if (__adjust == ios_base::left)
1396 _Traits::copy(__news, const_cast<_CharT*>(__olds), __oldlen);
1397 _Traits::assign(__news + __oldlen, __plen, __fill);
1402 if (__adjust == ios_base::internal)
1404 // Pad after the sign, if there is one.
1405 // Pad after 0[xX], if there is one.
1406 // Who came up with these rules, anyway? Jeeze.
1407 const locale& __loc = __io._M_getloc();
1408 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1410 const bool __testsign = (__ctype.widen('-') == __olds[0]
1411 || __ctype.widen('+') == __olds[0]);
1412 const bool __testhex = (__ctype.widen('0') == __olds[0]
1414 && (__ctype.widen('x') == __olds[1]
1415 || __ctype.widen('X') == __olds[1]));
1418 __news[0] = __olds[0];
1419 __news[1] = __olds[1];
1423 else if (__testsign)
1425 __news[0] = __olds[0];
1429 // else Padding first.
1431 _Traits::assign(__news, __plen, __fill);
1432 _Traits::copy(__news + __plen, const_cast<_CharT*>(__olds + __mod),
1437 __verify_grouping(const char* __grouping, size_t __grouping_size,
1438 const string& __grouping_tmp)
1440 const size_t __n = __grouping_tmp.size() - 1;
1441 const size_t __min = std::min(__n, size_t(__grouping_size - 1));
1445 // Parsed number groupings have to match the
1446 // numpunct::grouping string exactly, starting at the
1447 // right-most point of the parsed sequence of elements ...
1448 for (size_t __j = 0; __j < __min && __test; --__i, ++__j)
1449 __test = __grouping_tmp[__i] == __grouping[__j];
1450 for (; __i && __test; --__i)
1451 __test = __grouping_tmp[__i] == __grouping[__min];
1452 // ... but the first parsed grouping can be <= numpunct
1453 // grouping (only do the check if the numpunct char is > 0
1454 // because <= 0 means any size is ok).
1455 if (static_cast<signed char>(__grouping[__min]) > 0)
1456 __test &= __grouping_tmp[0] <= __grouping[__min];
1460 template<typename _CharT>
1462 __add_grouping(_CharT* __s, _CharT __sep,
1463 const char* __gbeg, size_t __gsize,
1464 const _CharT* __first, const _CharT* __last)
1469 while (__last - __first > __gbeg[__idx]
1470 && static_cast<signed char>(__gbeg[__idx]) > 0)
1472 __last -= __gbeg[__idx];
1473 __idx < __gsize - 1 ? ++__idx : ++__ctr;
1476 while (__first != __last)
1477 *__s++ = *__first++;
1482 for (char __i = __gbeg[__idx]; __i > 0; --__i)
1483 *__s++ = *__first++;
1489 for (char __i = __gbeg[__idx]; __i > 0; --__i)
1490 *__s++ = *__first++;
1496 // Inhibit implicit instantiations for required instantiations,
1497 // which are defined via explicit instantiations elsewhere.
1498 // NB: This syntax is a GNU extension.
1499 #if _GLIBCXX_EXTERN_TEMPLATE
1500 extern template class numpunct<char>;
1501 extern template class numpunct_byname<char>;
1502 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<char>;
1503 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<char>;
1504 extern template class ctype_byname<char>;
1505 extern template class codecvt_byname<char, char, mbstate_t>;
1506 extern template class collate<char>;
1507 extern template class collate_byname<char>;
1510 const codecvt<char, char, mbstate_t>&
1511 use_facet<codecvt<char, char, mbstate_t> >(const locale&);
1514 const collate<char>&
1515 use_facet<collate<char> >(const locale&);
1518 const numpunct<char>&
1519 use_facet<numpunct<char> >(const locale&);
1522 const num_put<char>&
1523 use_facet<num_put<char> >(const locale&);
1526 const num_get<char>&
1527 use_facet<num_get<char> >(const locale&);
1531 has_facet<ctype<char> >(const locale&);
1535 has_facet<codecvt<char, char, mbstate_t> >(const locale&);
1539 has_facet<collate<char> >(const locale&);
1543 has_facet<numpunct<char> >(const locale&);
1547 has_facet<num_put<char> >(const locale&);
1551 has_facet<num_get<char> >(const locale&);
1553 #ifdef _GLIBCXX_USE_WCHAR_T
1554 extern template class numpunct<wchar_t>;
1555 extern template class numpunct_byname<wchar_t>;
1556 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<wchar_t>;
1557 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<wchar_t>;
1558 extern template class ctype_byname<wchar_t>;
1559 extern template class codecvt_byname<wchar_t, char, mbstate_t>;
1560 extern template class collate<wchar_t>;
1561 extern template class collate_byname<wchar_t>;
1564 const codecvt<wchar_t, char, mbstate_t>&
1565 use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&);
1568 const collate<wchar_t>&
1569 use_facet<collate<wchar_t> >(const locale&);
1572 const numpunct<wchar_t>&
1573 use_facet<numpunct<wchar_t> >(const locale&);
1576 const num_put<wchar_t>&
1577 use_facet<num_put<wchar_t> >(const locale&);
1580 const num_get<wchar_t>&
1581 use_facet<num_get<wchar_t> >(const locale&);
1585 has_facet<ctype<wchar_t> >(const locale&);
1589 has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
1593 has_facet<collate<wchar_t> >(const locale&);
1597 has_facet<numpunct<wchar_t> >(const locale&);
1601 has_facet<num_put<wchar_t> >(const locale&);
1605 has_facet<num_get<wchar_t> >(const locale&);
1609 _GLIBCXX_END_NAMESPACE