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 #include <ext/numeric_traits.h>
43 #include <typeinfo> // For bad_cast.
44 #include <bits/streambuf_iterator.h>
45 #include <ext/type_traits.h>
47 _GLIBCXX_BEGIN_NAMESPACE(std)
49 template<typename _Facet>
51 locale::combine(const locale& __other) const
53 _Impl* __tmp = new _Impl(*_M_impl, 1);
56 __tmp->_M_replace_facet(__other._M_impl, &_Facet::id);
60 __tmp->_M_remove_reference();
61 __throw_exception_again;
66 template<typename _CharT, typename _Traits, typename _Alloc>
68 locale::operator()(const basic_string<_CharT, _Traits, _Alloc>& __s1,
69 const basic_string<_CharT, _Traits, _Alloc>& __s2) const
71 typedef std::collate<_CharT> __collate_type;
72 const __collate_type& __collate = use_facet<__collate_type>(*this);
73 return (__collate.compare(__s1.data(), __s1.data() + __s1.length(),
74 __s2.data(), __s2.data() + __s2.length()) < 0);
78 * @brief Test for the presence of a facet.
80 * has_facet tests the locale argument for the presence of the facet type
81 * provided as the template parameter. Facets derived from the facet
82 * parameter will also return true.
84 * @param Facet The facet type to test the presence of.
85 * @param locale The locale to test.
86 * @return true if locale contains a facet of type Facet, else false.
88 template<typename _Facet>
90 has_facet(const locale& __loc) throw()
92 const size_t __i = _Facet::id._M_id();
93 const locale::facet** __facets = __loc._M_impl->_M_facets;
94 return (__i < __loc._M_impl->_M_facets_size && __facets[__i]);
98 * @brief Return a facet.
100 * use_facet looks for and returns a reference to a facet of type Facet
101 * where Facet is the template parameter. If has_facet(locale) is true,
102 * there is a suitable facet to return. It throws std::bad_cast if the
103 * locale doesn't contain a facet of type Facet.
105 * @param Facet The facet type to access.
106 * @param locale The locale to use.
107 * @return Reference to facet of type Facet.
108 * @throw std::bad_cast if locale doesn't contain a facet of type Facet.
110 template<typename _Facet>
112 use_facet(const locale& __loc)
114 const size_t __i = _Facet::id._M_id();
115 const locale::facet** __facets = __loc._M_impl->_M_facets;
116 if (!(__i < __loc._M_impl->_M_facets_size && __facets[__i]))
118 return static_cast<const _Facet&>(*__facets[__i]);
121 // Routine to access a cache for the facet. If the cache didn't
122 // exist before, it gets constructed on the fly.
123 template<typename _Facet>
127 operator() (const locale& __loc) const;
131 template<typename _CharT>
132 struct __use_cache<__numpunct_cache<_CharT> >
134 const __numpunct_cache<_CharT>*
135 operator() (const locale& __loc) const
137 const size_t __i = numpunct<_CharT>::id._M_id();
138 const locale::facet** __caches = __loc._M_impl->_M_caches;
141 __numpunct_cache<_CharT>* __tmp = NULL;
144 __tmp = new __numpunct_cache<_CharT>;
145 __tmp->_M_cache(__loc);
150 __throw_exception_again;
152 __loc._M_impl->_M_install_cache(__tmp, __i);
154 return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]);
158 template<typename _CharT, bool _Intl>
159 struct __use_cache<__moneypunct_cache<_CharT, _Intl> >
161 const __moneypunct_cache<_CharT, _Intl>*
162 operator() (const locale& __loc) const
164 const size_t __i = moneypunct<_CharT, _Intl>::id._M_id();
165 const locale::facet** __caches = __loc._M_impl->_M_caches;
168 __moneypunct_cache<_CharT, _Intl>* __tmp = NULL;
171 __tmp = new __moneypunct_cache<_CharT, _Intl>;
172 __tmp->_M_cache(__loc);
177 __throw_exception_again;
179 __loc._M_impl->_M_install_cache(__tmp, __i);
182 const __moneypunct_cache<_CharT, _Intl>*>(__caches[__i]);
186 template<typename _CharT>
188 __numpunct_cache<_CharT>::_M_cache(const locale& __loc)
192 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
194 _M_grouping_size = __np.grouping().size();
195 char* __grouping = new char[_M_grouping_size];
196 __np.grouping().copy(__grouping, _M_grouping_size);
197 _M_grouping = __grouping;
198 _M_use_grouping = (_M_grouping_size
199 && static_cast<signed char>(__np.grouping()[0]) > 0);
201 _M_truename_size = __np.truename().size();
202 _CharT* __truename = new _CharT[_M_truename_size];
203 __np.truename().copy(__truename, _M_truename_size);
204 _M_truename = __truename;
206 _M_falsename_size = __np.falsename().size();
207 _CharT* __falsename = new _CharT[_M_falsename_size];
208 __np.falsename().copy(__falsename, _M_falsename_size);
209 _M_falsename = __falsename;
211 _M_decimal_point = __np.decimal_point();
212 _M_thousands_sep = __np.thousands_sep();
214 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
215 __ct.widen(__num_base::_S_atoms_out,
216 __num_base::_S_atoms_out + __num_base::_S_oend, _M_atoms_out);
217 __ct.widen(__num_base::_S_atoms_in,
218 __num_base::_S_atoms_in + __num_base::_S_iend, _M_atoms_in);
221 template<typename _CharT, bool _Intl>
223 __moneypunct_cache<_CharT, _Intl>::_M_cache(const locale& __loc)
227 const moneypunct<_CharT, _Intl>& __mp =
228 use_facet<moneypunct<_CharT, _Intl> >(__loc);
230 _M_grouping_size = __mp.grouping().size();
231 char* __grouping = new char[_M_grouping_size];
232 __mp.grouping().copy(__grouping, _M_grouping_size);
233 _M_grouping = __grouping;
234 _M_use_grouping = (_M_grouping_size
235 && static_cast<signed char>(__mp.grouping()[0]) > 0);
237 _M_decimal_point = __mp.decimal_point();
238 _M_thousands_sep = __mp.thousands_sep();
239 _M_frac_digits = __mp.frac_digits();
241 _M_curr_symbol_size = __mp.curr_symbol().size();
242 _CharT* __curr_symbol = new _CharT[_M_curr_symbol_size];
243 __mp.curr_symbol().copy(__curr_symbol, _M_curr_symbol_size);
244 _M_curr_symbol = __curr_symbol;
246 _M_positive_sign_size = __mp.positive_sign().size();
247 _CharT* __positive_sign = new _CharT[_M_positive_sign_size];
248 __mp.positive_sign().copy(__positive_sign, _M_positive_sign_size);
249 _M_positive_sign = __positive_sign;
251 _M_negative_sign_size = __mp.negative_sign().size();
252 _CharT* __negative_sign = new _CharT[_M_negative_sign_size];
253 __mp.negative_sign().copy(__negative_sign, _M_negative_sign_size);
254 _M_negative_sign = __negative_sign;
256 _M_pos_format = __mp.pos_format();
257 _M_neg_format = __mp.neg_format();
259 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
260 __ct.widen(money_base::_S_atoms,
261 money_base::_S_atoms + money_base::_S_end, _M_atoms);
265 // Used by both numeric and monetary facets.
266 // Check to make sure that the __grouping_tmp string constructed in
267 // money_get or num_get matches the canonical grouping for a given
269 // __grouping_tmp is parsed L to R
270 // 1,222,444 == __grouping_tmp of "\1\3\3"
271 // __grouping is parsed R to L
272 // 1,222,444 == __grouping of "\3" == "\3\3\3"
274 __verify_grouping(const char* __grouping, size_t __grouping_size,
275 const string& __grouping_tmp);
277 _GLIBCXX_BEGIN_LDBL_NAMESPACE
279 template<typename _CharT, typename _InIter>
281 num_get<_CharT, _InIter>::
282 _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
283 ios_base::iostate& __err, string& __xtrc) const
285 typedef char_traits<_CharT> __traits_type;
286 typedef __numpunct_cache<_CharT> __cache_type;
287 __use_cache<__cache_type> __uc;
288 const locale& __loc = __io._M_getloc();
289 const __cache_type* __lc = __uc(__loc);
290 const _CharT* __lit = __lc->_M_atoms_in;
291 char_type __c = char_type();
293 // True if __beg becomes equal to __end.
294 bool __testeof = __beg == __end;
296 // First check for sign.
300 const bool __plus = __c == __lit[__num_base::_S_iplus];
301 if ((__plus || __c == __lit[__num_base::_S_iminus])
302 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
303 && !(__c == __lc->_M_decimal_point))
305 __xtrc += __plus ? '+' : '-';
306 if (++__beg != __end)
313 // Next, look for leading zeros.
314 bool __found_mantissa = false;
318 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
319 || __c == __lc->_M_decimal_point)
321 else if (__c == __lit[__num_base::_S_izero])
323 if (!__found_mantissa)
326 __found_mantissa = true;
330 if (++__beg != __end)
339 // Only need acceptable digits for floating point numbers.
340 bool __found_dec = false;
341 bool __found_sci = false;
342 string __found_grouping;
343 if (__lc->_M_use_grouping)
344 __found_grouping.reserve(32);
345 const char_type* __lit_zero = __lit + __num_base::_S_izero;
347 if (!__lc->_M_allocated)
351 const int __digit = _M_find(__lit_zero, 10, __c);
354 __xtrc += '0' + __digit;
355 __found_mantissa = true;
357 else if (__c == __lc->_M_decimal_point
358 && !__found_dec && !__found_sci)
363 else if ((__c == __lit[__num_base::_S_ie]
364 || __c == __lit[__num_base::_S_iE])
365 && !__found_sci && __found_mantissa)
367 // Scientific notation.
371 // Remove optional plus or minus sign, if they exist.
372 if (++__beg != __end)
375 const bool __plus = __c == __lit[__num_base::_S_iplus];
376 if (__plus || __c == __lit[__num_base::_S_iminus])
377 __xtrc += __plus ? '+' : '-';
390 if (++__beg != __end)
398 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
399 // and decimal_point.
400 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
402 if (!__found_dec && !__found_sci)
404 // NB: Thousands separator at the beginning of a string
405 // is a no-no, as is two consecutive thousands separators.
408 __found_grouping += static_cast<char>(__sep_pos);
413 // NB: __convert_to_v will not assign __v and will
422 else if (__c == __lc->_M_decimal_point)
424 if (!__found_dec && !__found_sci)
426 // If no grouping chars are seen, no grouping check
427 // is applied. Therefore __found_grouping is adjusted
428 // only if decimal_point comes after some thousands_sep.
429 if (__found_grouping.size())
430 __found_grouping += static_cast<char>(__sep_pos);
439 const char_type* __q =
440 __traits_type::find(__lit_zero, 10, __c);
443 __xtrc += '0' + (__q - __lit_zero);
444 __found_mantissa = true;
447 else if ((__c == __lit[__num_base::_S_ie]
448 || __c == __lit[__num_base::_S_iE])
449 && !__found_sci && __found_mantissa)
451 // Scientific notation.
452 if (__found_grouping.size() && !__found_dec)
453 __found_grouping += static_cast<char>(__sep_pos);
457 // Remove optional plus or minus sign, if they exist.
458 if (++__beg != __end)
461 const bool __plus = __c == __lit[__num_base::_S_iplus];
462 if ((__plus || __c == __lit[__num_base::_S_iminus])
463 && !(__lc->_M_use_grouping
464 && __c == __lc->_M_thousands_sep)
465 && !(__c == __lc->_M_decimal_point))
466 __xtrc += __plus ? '+' : '-';
480 if (++__beg != __end)
486 // Digit grouping is checked. If grouping and found_grouping don't
487 // match, then get very very upset, and set failbit.
488 if (__found_grouping.size())
490 // Add the ending grouping if a decimal or 'e'/'E' wasn't found.
491 if (!__found_dec && !__found_sci)
492 __found_grouping += static_cast<char>(__sep_pos);
494 if (!std::__verify_grouping(__lc->_M_grouping,
495 __lc->_M_grouping_size,
497 __err |= ios_base::failbit;
502 __err |= ios_base::eofbit;
506 _GLIBCXX_END_LDBL_NAMESPACE
508 _GLIBCXX_BEGIN_LDBL_NAMESPACE
510 template<typename _CharT, typename _InIter>
511 template<typename _ValueT>
513 num_get<_CharT, _InIter>::
514 _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
515 ios_base::iostate& __err, _ValueT& __v) const
517 typedef char_traits<_CharT> __traits_type;
518 using __gnu_cxx::__add_unsigned;
519 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
520 typedef __numpunct_cache<_CharT> __cache_type;
521 __use_cache<__cache_type> __uc;
522 const locale& __loc = __io._M_getloc();
523 const __cache_type* __lc = __uc(__loc);
524 const _CharT* __lit = __lc->_M_atoms_in;
525 char_type __c = char_type();
527 // NB: Iff __basefield == 0, __base can change based on contents.
528 const ios_base::fmtflags __basefield = __io.flags()
529 & ios_base::basefield;
530 const bool __oct = __basefield == ios_base::oct;
531 int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10);
533 // True if __beg becomes equal to __end.
534 bool __testeof = __beg == __end;
536 // First check for sign.
537 bool __negative = false;
541 if (__gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
542 __negative = __c == __lit[__num_base::_S_iminus];
543 if ((__negative || __c == __lit[__num_base::_S_iplus])
544 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
545 && !(__c == __lc->_M_decimal_point))
547 if (++__beg != __end)
554 // Next, look for leading zeros and check required digits
556 bool __found_zero = false;
560 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
561 || __c == __lc->_M_decimal_point)
563 else if (__c == __lit[__num_base::_S_izero]
564 && (!__found_zero || __base == 10))
568 if (__basefield == 0)
573 else if (__found_zero
574 && (__c == __lit[__num_base::_S_ix]
575 || __c == __lit[__num_base::_S_iX]))
577 if (__basefield == 0)
581 __found_zero = false;
590 if (++__beg != __end)
600 // At this point, base is determined. If not hex, only allow
601 // base digits as valid input.
602 const size_t __len = (__base == 16 ? __num_base::_S_iend
603 - __num_base::_S_izero : __base);
606 string __found_grouping;
607 if (__lc->_M_use_grouping)
608 __found_grouping.reserve(32);
609 bool __testfail = false;
610 const __unsigned_type __max = __negative
611 ? -__gnu_cxx::__numeric_traits<_ValueT>::__min
612 : __gnu_cxx::__numeric_traits<_ValueT>::__max;
613 const __unsigned_type __smax = __max / __base;
614 __unsigned_type __result = 0;
616 const char_type* __lit_zero = __lit + __num_base::_S_izero;
618 if (!__lc->_M_allocated)
622 __digit = _M_find(__lit_zero, __len, __c);
626 if (__result > __smax)
631 __testfail |= __result > __max - __digit;
636 if (++__beg != __end)
644 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
645 // and decimal_point.
646 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
648 // NB: Thousands separator at the beginning of a string
649 // is a no-no, as is two consecutive thousands separators.
652 __found_grouping += static_cast<char>(__sep_pos);
661 else if (__c == __lc->_M_decimal_point)
665 const char_type* __q =
666 __traits_type::find(__lit_zero, __len, __c);
670 __digit = __q - __lit_zero;
673 if (__result > __smax)
678 __testfail |= __result > __max - __digit;
684 if (++__beg != __end)
690 // Digit grouping is checked. If grouping and found_grouping don't
691 // match, then get very very upset, and set failbit.
692 if (__found_grouping.size())
694 // Add the ending grouping.
695 __found_grouping += static_cast<char>(__sep_pos);
697 if (!std::__verify_grouping(__lc->_M_grouping,
698 __lc->_M_grouping_size,
700 __err |= ios_base::failbit;
703 if (!__testfail && (__sep_pos || __found_zero
704 || __found_grouping.size()))
705 __v = __negative ? -__result : __result;
707 __err |= ios_base::failbit;
710 __err |= ios_base::eofbit;
714 // _GLIBCXX_RESOLVE_LIB_DEFECTS
715 // 17. Bad bool parsing
716 template<typename _CharT, typename _InIter>
718 num_get<_CharT, _InIter>::
719 do_get(iter_type __beg, iter_type __end, ios_base& __io,
720 ios_base::iostate& __err, bool& __v) const
722 if (!(__io.flags() & ios_base::boolalpha))
724 // Parse bool values as long.
725 // NB: We can't just call do_get(long) here, as it might
726 // refer to a derived class.
728 __beg = _M_extract_int(__beg, __end, __io, __err, __l);
729 if (__l == 0 || __l == 1)
732 __err |= ios_base::failbit;
736 // Parse bool values as alphanumeric.
737 typedef __numpunct_cache<_CharT> __cache_type;
738 __use_cache<__cache_type> __uc;
739 const locale& __loc = __io._M_getloc();
740 const __cache_type* __lc = __uc(__loc);
745 bool __testeof = __beg == __end;
746 for (__n = 0; !__testeof; ++__n)
748 const char_type __c = *__beg;
752 if (__n < __lc->_M_falsename_size)
753 __testf = __c == __lc->_M_falsename[__n];
760 if (__n < __lc->_M_truename_size)
761 __testt = __c == __lc->_M_truename[__n];
766 if (!__testf && !__testt)
769 if (++__beg == __end)
772 if (__testf && __n == __lc->_M_falsename_size)
774 else if (__testt && __n == __lc->_M_truename_size)
777 __err |= ios_base::failbit;
780 __err |= ios_base::eofbit;
785 template<typename _CharT, typename _InIter>
787 num_get<_CharT, _InIter>::
788 do_get(iter_type __beg, iter_type __end, ios_base& __io,
789 ios_base::iostate& __err, long& __v) const
790 { return _M_extract_int(__beg, __end, __io, __err, __v); }
792 template<typename _CharT, typename _InIter>
794 num_get<_CharT, _InIter>::
795 do_get(iter_type __beg, iter_type __end, ios_base& __io,
796 ios_base::iostate& __err, unsigned short& __v) const
797 { return _M_extract_int(__beg, __end, __io, __err, __v); }
799 template<typename _CharT, typename _InIter>
801 num_get<_CharT, _InIter>::
802 do_get(iter_type __beg, iter_type __end, ios_base& __io,
803 ios_base::iostate& __err, unsigned int& __v) const
804 { return _M_extract_int(__beg, __end, __io, __err, __v); }
806 template<typename _CharT, typename _InIter>
808 num_get<_CharT, _InIter>::
809 do_get(iter_type __beg, iter_type __end, ios_base& __io,
810 ios_base::iostate& __err, unsigned long& __v) const
811 { return _M_extract_int(__beg, __end, __io, __err, __v); }
813 #ifdef _GLIBCXX_USE_LONG_LONG
814 template<typename _CharT, typename _InIter>
816 num_get<_CharT, _InIter>::
817 do_get(iter_type __beg, iter_type __end, ios_base& __io,
818 ios_base::iostate& __err, long long& __v) const
819 { return _M_extract_int(__beg, __end, __io, __err, __v); }
821 template<typename _CharT, typename _InIter>
823 num_get<_CharT, _InIter>::
824 do_get(iter_type __beg, iter_type __end, ios_base& __io,
825 ios_base::iostate& __err, unsigned long long& __v) const
826 { return _M_extract_int(__beg, __end, __io, __err, __v); }
829 template<typename _CharT, typename _InIter>
831 num_get<_CharT, _InIter>::
832 do_get(iter_type __beg, iter_type __end, ios_base& __io,
833 ios_base::iostate& __err, float& __v) const
837 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
838 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
842 template<typename _CharT, typename _InIter>
844 num_get<_CharT, _InIter>::
845 do_get(iter_type __beg, iter_type __end, ios_base& __io,
846 ios_base::iostate& __err, double& __v) const
850 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
851 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
855 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
856 template<typename _CharT, typename _InIter>
858 num_get<_CharT, _InIter>::
859 __do_get(iter_type __beg, iter_type __end, ios_base& __io,
860 ios_base::iostate& __err, double& __v) const
864 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
865 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
870 template<typename _CharT, typename _InIter>
872 num_get<_CharT, _InIter>::
873 do_get(iter_type __beg, iter_type __end, ios_base& __io,
874 ios_base::iostate& __err, long double& __v) const
878 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
879 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
883 template<typename _CharT, typename _InIter>
885 num_get<_CharT, _InIter>::
886 do_get(iter_type __beg, iter_type __end, ios_base& __io,
887 ios_base::iostate& __err, void*& __v) const
889 // Prepare for hex formatted input.
890 typedef ios_base::fmtflags fmtflags;
891 const fmtflags __fmt = __io.flags();
892 __io.flags((__fmt & ~ios_base::basefield) | ios_base::hex);
894 typedef __gnu_cxx::__conditional_type<(sizeof(void*)
895 <= sizeof(unsigned long)),
896 unsigned long, unsigned long long>::__type _UIntPtrType;
899 __beg = _M_extract_int(__beg, __end, __io, __err, __ul);
901 // Reset from hex formatted input.
904 if (!(__err & ios_base::failbit))
905 __v = reinterpret_cast<void*>(__ul);
909 // For use by integer and floating-point types after they have been
910 // converted into a char_type string.
911 template<typename _CharT, typename _OutIter>
913 num_put<_CharT, _OutIter>::
914 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
915 _CharT* __new, const _CharT* __cs, int& __len) const
917 // [22.2.2.2.2] Stage 3.
918 // If necessary, pad.
919 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new,
921 __len = static_cast<int>(__w);
924 _GLIBCXX_END_LDBL_NAMESPACE
926 template<typename _CharT, typename _ValueT>
928 __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
929 ios_base::fmtflags __flags, bool __dec)
931 _CharT* __buf = __bufend;
932 if (__builtin_expect(__dec, true))
937 *--__buf = __lit[(__v % 10) + __num_base::_S_odigits];
942 else if ((__flags & ios_base::basefield) == ios_base::oct)
947 *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits];
955 const bool __uppercase = __flags & ios_base::uppercase;
956 const int __case_offset = __uppercase ? __num_base::_S_oudigits
957 : __num_base::_S_odigits;
960 *--__buf = __lit[(__v & 0xf) + __case_offset];
965 return __bufend - __buf;
968 _GLIBCXX_BEGIN_LDBL_NAMESPACE
970 template<typename _CharT, typename _OutIter>
972 num_put<_CharT, _OutIter>::
973 _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep,
974 ios_base&, _CharT* __new, _CharT* __cs, int& __len) const
976 _CharT* __p = std::__add_grouping(__new, __sep, __grouping,
977 __grouping_size, __cs, __cs + __len);
981 template<typename _CharT, typename _OutIter>
982 template<typename _ValueT>
984 num_put<_CharT, _OutIter>::
985 _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
988 using __gnu_cxx::__add_unsigned;
989 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
990 typedef __numpunct_cache<_CharT> __cache_type;
991 __use_cache<__cache_type> __uc;
992 const locale& __loc = __io._M_getloc();
993 const __cache_type* __lc = __uc(__loc);
994 const _CharT* __lit = __lc->_M_atoms_out;
995 const ios_base::fmtflags __flags = __io.flags();
997 // Long enough to hold hex, dec, and octal representations.
998 const int __ilen = 5 * sizeof(_ValueT);
999 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1002 // [22.2.2.2.2] Stage 1, numeric conversion to character.
1003 // Result is returned right-justified in the buffer.
1004 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
1005 const bool __dec = (__basefield != ios_base::oct
1006 && __basefield != ios_base::hex);
1007 const __unsigned_type __u = ((__v > 0 || !__dec)
1008 ? __unsigned_type(__v)
1009 : -__unsigned_type(__v));
1010 int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec);
1011 __cs += __ilen - __len;
1013 // Add grouping, if necessary.
1014 if (__lc->_M_use_grouping)
1016 // Grouping can add (almost) as many separators as the number
1017 // of digits + space is reserved for numeric base or sign.
1018 _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1021 _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size,
1022 __lc->_M_thousands_sep, __io, __cs2 + 2, __cs, __len);
1026 // Complete Stage 1, prepend numeric base or sign.
1027 if (__builtin_expect(__dec, true))
1032 if (__flags & ios_base::showpos
1033 && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
1034 *--__cs = __lit[__num_base::_S_oplus], ++__len;
1037 *--__cs = __lit[__num_base::_S_ominus], ++__len;
1039 else if (__flags & ios_base::showbase && __v)
1041 if (__basefield == ios_base::oct)
1042 *--__cs = __lit[__num_base::_S_odigits], ++__len;
1046 const bool __uppercase = __flags & ios_base::uppercase;
1047 *--__cs = __lit[__num_base::_S_ox + __uppercase];
1049 *--__cs = __lit[__num_base::_S_odigits];
1055 const streamsize __w = __io.width();
1056 if (__w > static_cast<streamsize>(__len))
1058 _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1060 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
1065 // [22.2.2.2.2] Stage 4.
1066 // Write resulting, fully-formatted string to output iterator.
1067 return std::__write(__s, __cs, __len);
1070 template<typename _CharT, typename _OutIter>
1072 num_put<_CharT, _OutIter>::
1073 _M_group_float(const char* __grouping, size_t __grouping_size,
1074 _CharT __sep, const _CharT* __p, _CharT* __new,
1075 _CharT* __cs, int& __len) const
1077 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1078 // 282. What types does numpunct grouping refer to?
1079 // Add grouping, if necessary.
1080 const int __declen = __p ? __p - __cs : __len;
1081 _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping,
1083 __cs, __cs + __declen);
1085 // Tack on decimal part.
1086 int __newlen = __p2 - __new;
1089 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
1090 __newlen += __len - __declen;
1095 // The following code uses vsnprintf (or vsprintf(), when
1096 // _GLIBCXX_USE_C99 is not defined) to convert floating point values
1097 // for insertion into a stream. An optimization would be to replace
1098 // them with code that works directly on a wide buffer and then use
1099 // __pad to do the padding. It would be good to replace them anyway
1100 // to gain back the efficiency that C++ provides by knowing up front
1101 // the type of the values to insert. Also, sprintf is dangerous
1102 // since may lead to accidental buffer overruns. This
1103 // implementation follows the C++ standard fairly directly as
1104 // outlined in 22.2.2.2 [lib.locale.num.put]
1105 template<typename _CharT, typename _OutIter>
1106 template<typename _ValueT>
1108 num_put<_CharT, _OutIter>::
1109 _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
1112 typedef __numpunct_cache<_CharT> __cache_type;
1113 __use_cache<__cache_type> __uc;
1114 const locale& __loc = __io._M_getloc();
1115 const __cache_type* __lc = __uc(__loc);
1117 // Use default precision if out of range.
1118 const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision();
1120 const int __max_digits =
1121 __gnu_cxx::__numeric_traits<_ValueT>::__digits10;
1123 // [22.2.2.2.2] Stage 1, numeric conversion to character.
1125 // Long enough for the max format spec.
1127 __num_base::_S_format_float(__io, __fbuf, __mod);
1129 #ifdef _GLIBCXX_USE_C99
1130 // First try a buffer perhaps big enough (most probably sufficient
1131 // for non-ios_base::fixed outputs)
1132 int __cs_size = __max_digits * 3;
1133 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1134 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1135 __fbuf, __prec, __v);
1137 // If the buffer was not large enough, try again with the correct size.
1138 if (__len >= __cs_size)
1140 __cs_size = __len + 1;
1141 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1142 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1143 __fbuf, __prec, __v);
1146 // Consider the possibility of long ios_base::fixed outputs
1147 const bool __fixed = __io.flags() & ios_base::fixed;
1148 const int __max_exp =
1149 __gnu_cxx::__numeric_traits<_ValueT>::__max_exponent10;
1151 // The size of the output string is computed as follows.
1152 // ios_base::fixed outputs may need up to __max_exp + 1 chars
1153 // for the integer part + __prec chars for the fractional part
1154 // + 3 chars for sign, decimal point, '\0'. On the other hand,
1155 // for non-fixed outputs __max_digits * 2 + __prec chars are
1156 // largely sufficient.
1157 const int __cs_size = __fixed ? __max_exp + __prec + 4
1158 : __max_digits * 2 + __prec;
1159 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1160 __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf,
1164 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
1165 // numpunct.decimal_point() values for '.' and adding grouping.
1166 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1168 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1170 __ctype.widen(__cs, __cs + __len, __ws);
1172 // Replace decimal point.
1174 const char* __p = char_traits<char>::find(__cs, __len, '.');
1177 __wp = __ws + (__p - __cs);
1178 *__wp = __lc->_M_decimal_point;
1181 // Add grouping, if necessary.
1182 // N.B. Make sure to not group things like 2e20, i.e., no decimal
1183 // point, scientific notation.
1184 if (__lc->_M_use_grouping
1185 && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9'
1186 && __cs[1] >= '0' && __cs[2] >= '0')))
1188 // Grouping can add (almost) as many separators as the
1189 // number of digits, but no more.
1190 _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1193 streamsize __off = 0;
1194 if (__cs[0] == '-' || __cs[0] == '+')
1201 _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size,
1202 __lc->_M_thousands_sep, __wp, __ws2 + __off,
1203 __ws + __off, __len);
1210 const streamsize __w = __io.width();
1211 if (__w > static_cast<streamsize>(__len))
1213 _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1215 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
1220 // [22.2.2.2.2] Stage 4.
1221 // Write resulting, fully-formatted string to output iterator.
1222 return std::__write(__s, __ws, __len);
1225 template<typename _CharT, typename _OutIter>
1227 num_put<_CharT, _OutIter>::
1228 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
1230 const ios_base::fmtflags __flags = __io.flags();
1231 if ((__flags & ios_base::boolalpha) == 0)
1233 const long __l = __v;
1234 __s = _M_insert_int(__s, __io, __fill, __l);
1238 typedef __numpunct_cache<_CharT> __cache_type;
1239 __use_cache<__cache_type> __uc;
1240 const locale& __loc = __io._M_getloc();
1241 const __cache_type* __lc = __uc(__loc);
1243 const _CharT* __name = __v ? __lc->_M_truename
1244 : __lc->_M_falsename;
1245 int __len = __v ? __lc->_M_truename_size
1246 : __lc->_M_falsename_size;
1248 const streamsize __w = __io.width();
1249 if (__w > static_cast<streamsize>(__len))
1252 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1254 _M_pad(__fill, __w, __io, __cs, __name, __len);
1258 __s = std::__write(__s, __name, __len);
1263 template<typename _CharT, typename _OutIter>
1265 num_put<_CharT, _OutIter>::
1266 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
1267 { return _M_insert_int(__s, __io, __fill, __v); }
1269 template<typename _CharT, typename _OutIter>
1271 num_put<_CharT, _OutIter>::
1272 do_put(iter_type __s, ios_base& __io, char_type __fill,
1273 unsigned long __v) const
1274 { return _M_insert_int(__s, __io, __fill, __v); }
1276 #ifdef _GLIBCXX_USE_LONG_LONG
1277 template<typename _CharT, typename _OutIter>
1279 num_put<_CharT, _OutIter>::
1280 do_put(iter_type __s, ios_base& __io, char_type __fill, long long __v) const
1281 { return _M_insert_int(__s, __io, __fill, __v); }
1283 template<typename _CharT, typename _OutIter>
1285 num_put<_CharT, _OutIter>::
1286 do_put(iter_type __s, ios_base& __io, char_type __fill,
1287 unsigned long long __v) const
1288 { return _M_insert_int(__s, __io, __fill, __v); }
1291 template<typename _CharT, typename _OutIter>
1293 num_put<_CharT, _OutIter>::
1294 do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1295 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1297 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1298 template<typename _CharT, typename _OutIter>
1300 num_put<_CharT, _OutIter>::
1301 __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1302 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1305 template<typename _CharT, typename _OutIter>
1307 num_put<_CharT, _OutIter>::
1308 do_put(iter_type __s, ios_base& __io, char_type __fill,
1309 long double __v) const
1310 { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1312 template<typename _CharT, typename _OutIter>
1314 num_put<_CharT, _OutIter>::
1315 do_put(iter_type __s, ios_base& __io, char_type __fill,
1316 const void* __v) const
1318 const ios_base::fmtflags __flags = __io.flags();
1319 const ios_base::fmtflags __fmt = ~(ios_base::basefield
1320 | ios_base::uppercase
1321 | ios_base::internal);
1322 __io.flags((__flags & __fmt) | (ios_base::hex | ios_base::showbase));
1324 typedef __gnu_cxx::__conditional_type<(sizeof(const void*)
1325 <= sizeof(unsigned long)),
1326 unsigned long, unsigned long long>::__type _UIntPtrType;
1328 __s = _M_insert_int(__s, __io, __fill,
1329 reinterpret_cast<_UIntPtrType>(__v));
1330 __io.flags(__flags);
1334 template<typename _CharT, typename _InIter>
1335 template<bool _Intl>
1337 money_get<_CharT, _InIter>::
1338 _M_extract(iter_type __beg, iter_type __end, ios_base& __io,
1339 ios_base::iostate& __err, string& __units) const
1341 typedef char_traits<_CharT> __traits_type;
1342 typedef typename string_type::size_type size_type;
1343 typedef money_base::part part;
1344 typedef __moneypunct_cache<_CharT, _Intl> __cache_type;
1346 const locale& __loc = __io._M_getloc();
1347 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1349 __use_cache<__cache_type> __uc;
1350 const __cache_type* __lc = __uc(__loc);
1351 const char_type* __lit = __lc->_M_atoms;
1354 bool __negative = false;
1356 size_type __sign_size = 0;
1357 // True if sign is mandatory.
1358 const bool __mandatory_sign = (__lc->_M_positive_sign_size
1359 && __lc->_M_negative_sign_size);
1360 // String of grouping info from thousands_sep plucked from __units.
1361 string __grouping_tmp;
1362 if (__lc->_M_use_grouping)
1363 __grouping_tmp.reserve(32);
1364 // Last position before the decimal point.
1366 // Separator positions, then, possibly, fractional digits.
1368 // If input iterator is in a valid state.
1369 bool __testvalid = true;
1370 // Flag marking when a decimal point is found.
1371 bool __testdecfound = false;
1373 // The tentative returned string is stored here.
1377 const char_type* __lit_zero = __lit + money_base::_S_zero;
1378 const money_base::pattern __p = __lc->_M_neg_format;
1379 for (int __i = 0; __i < 4 && __testvalid; ++__i)
1381 const part __which = static_cast<part>(__p.field[__i]);
1384 case money_base::symbol:
1385 // According to 22.2.6.1.2, p2, symbol is required
1386 // if (__io.flags() & ios_base::showbase), otherwise
1387 // is optional and consumed only if other characters
1388 // are needed to complete the format.
1389 if (__io.flags() & ios_base::showbase || __sign_size > 1
1391 || (__i == 1 && (__mandatory_sign
1392 || (static_cast<part>(__p.field[0])
1393 == money_base::sign)
1394 || (static_cast<part>(__p.field[2])
1395 == money_base::space)))
1396 || (__i == 2 && ((static_cast<part>(__p.field[3])
1397 == money_base::value)
1398 || (__mandatory_sign
1399 && (static_cast<part>(__p.field[3])
1400 == money_base::sign)))))
1402 const size_type __len = __lc->_M_curr_symbol_size;
1404 for (; __beg != __end && __j < __len
1405 && *__beg == __lc->_M_curr_symbol[__j];
1408 && (__j || __io.flags() & ios_base::showbase))
1409 __testvalid = false;
1412 case money_base::sign:
1413 // Sign might not exist, or be more than one character long.
1414 if (__lc->_M_positive_sign_size && __beg != __end
1415 && *__beg == __lc->_M_positive_sign[0])
1417 __sign_size = __lc->_M_positive_sign_size;
1420 else if (__lc->_M_negative_sign_size && __beg != __end
1421 && *__beg == __lc->_M_negative_sign[0])
1424 __sign_size = __lc->_M_negative_sign_size;
1427 else if (__lc->_M_positive_sign_size
1428 && !__lc->_M_negative_sign_size)
1429 // "... if no sign is detected, the result is given the sign
1430 // that corresponds to the source of the empty string"
1432 else if (__mandatory_sign)
1433 __testvalid = false;
1435 case money_base::value:
1436 // Extract digits, remove and stash away the
1437 // grouping of found thousands separators.
1438 for (; __beg != __end; ++__beg)
1440 const char_type __c = *__beg;
1441 const char_type* __q = __traits_type::find(__lit_zero,
1445 __res += money_base::_S_atoms[__q - __lit];
1448 else if (__c == __lc->_M_decimal_point
1453 __testdecfound = true;
1455 else if (__lc->_M_use_grouping
1456 && __c == __lc->_M_thousands_sep
1461 // Mark position for later analysis.
1462 __grouping_tmp += static_cast<char>(__n);
1467 __testvalid = false;
1475 __testvalid = false;
1477 case money_base::space:
1478 // At least one space is required.
1479 if (__beg != __end && __ctype.is(ctype_base::space, *__beg))
1482 __testvalid = false;
1483 case money_base::none:
1484 // Only if not at the end of the pattern.
1486 for (; __beg != __end
1487 && __ctype.is(ctype_base::space, *__beg); ++__beg);
1492 // Need to get the rest of the sign characters, if they exist.
1493 if (__sign_size > 1 && __testvalid)
1495 const char_type* __sign = __negative ? __lc->_M_negative_sign
1496 : __lc->_M_positive_sign;
1498 for (; __beg != __end && __i < __sign_size
1499 && *__beg == __sign[__i]; ++__beg, ++__i);
1501 if (__i != __sign_size)
1502 __testvalid = false;
1507 // Strip leading zeros.
1508 if (__res.size() > 1)
1510 const size_type __first = __res.find_first_not_of('0');
1511 const bool __only_zeros = __first == string::npos;
1513 __res.erase(0, __only_zeros ? __res.size() - 1 : __first);
1517 if (__negative && __res[0] != '0')
1518 __res.insert(__res.begin(), '-');
1520 // Test for grouping fidelity.
1521 if (__grouping_tmp.size())
1523 // Add the ending grouping.
1524 __grouping_tmp += static_cast<char>(__testdecfound ? __last_pos
1526 if (!std::__verify_grouping(__lc->_M_grouping,
1527 __lc->_M_grouping_size,
1529 __err |= ios_base::failbit;
1532 // Iff not enough digits were supplied after the decimal-point.
1533 if (__testdecfound && __lc->_M_frac_digits > 0
1534 && __n != __lc->_M_frac_digits)
1535 __testvalid = false;
1538 // Iff valid sequence is not recognized.
1540 __err |= ios_base::failbit;
1542 __units.swap(__res);
1544 // Iff no more characters are available.
1546 __err |= ios_base::eofbit;
1550 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1551 template<typename _CharT, typename _InIter>
1553 money_get<_CharT, _InIter>::
1554 __do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1555 ios_base::iostate& __err, double& __units) const
1558 __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
1559 : _M_extract<false>(__beg, __end, __io, __err, __str);
1560 std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale());
1565 template<typename _CharT, typename _InIter>
1567 money_get<_CharT, _InIter>::
1568 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1569 ios_base::iostate& __err, long double& __units) const
1572 __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
1573 : _M_extract<false>(__beg, __end, __io, __err, __str);
1574 std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale());
1578 template<typename _CharT, typename _InIter>
1580 money_get<_CharT, _InIter>::
1581 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1582 ios_base::iostate& __err, string_type& __digits) const
1584 typedef typename string::size_type size_type;
1586 const locale& __loc = __io._M_getloc();
1587 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1590 __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
1591 : _M_extract<false>(__beg, __end, __io, __err, __str);
1592 const size_type __len = __str.size();
1595 __digits.resize(__len);
1596 __ctype.widen(__str.data(), __str.data() + __len, &__digits[0]);
1601 template<typename _CharT, typename _OutIter>
1602 template<bool _Intl>
1604 money_put<_CharT, _OutIter>::
1605 _M_insert(iter_type __s, ios_base& __io, char_type __fill,
1606 const string_type& __digits) const
1608 typedef typename string_type::size_type size_type;
1609 typedef money_base::part part;
1610 typedef __moneypunct_cache<_CharT, _Intl> __cache_type;
1612 const locale& __loc = __io._M_getloc();
1613 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1615 __use_cache<__cache_type> __uc;
1616 const __cache_type* __lc = __uc(__loc);
1617 const char_type* __lit = __lc->_M_atoms;
1619 // Determine if negative or positive formats are to be used, and
1620 // discard leading negative_sign if it is present.
1621 const char_type* __beg = __digits.data();
1623 money_base::pattern __p;
1624 const char_type* __sign;
1625 size_type __sign_size;
1626 if (!(*__beg == __lit[money_base::_S_minus]))
1628 __p = __lc->_M_pos_format;
1629 __sign = __lc->_M_positive_sign;
1630 __sign_size = __lc->_M_positive_sign_size;
1634 __p = __lc->_M_neg_format;
1635 __sign = __lc->_M_negative_sign;
1636 __sign_size = __lc->_M_negative_sign_size;
1637 if (__digits.size())
1641 // Look for valid numbers in the ctype facet within input digits.
1642 size_type __len = __ctype.scan_not(ctype_base::digit, __beg,
1643 __beg + __digits.size()) - __beg;
1646 // Assume valid input, and attempt to format.
1647 // Break down input numbers into base components, as follows:
1648 // final_value = grouped units + (decimal point) + (digits)
1649 string_type __value;
1650 __value.reserve(2 * __len);
1652 // Add thousands separators to non-decimal digits, per
1654 long __paddec = __len - __lc->_M_frac_digits;
1657 if (__lc->_M_frac_digits < 0)
1659 if (__lc->_M_grouping_size)
1661 __value.assign(2 * __paddec, char_type());
1663 std::__add_grouping(&__value[0], __lc->_M_thousands_sep,
1665 __lc->_M_grouping_size,
1666 __beg, __beg + __paddec);
1667 __value.erase(__vend - &__value[0]);
1670 __value.assign(__beg, __paddec);
1673 // Deal with decimal point, decimal digits.
1674 if (__lc->_M_frac_digits > 0)
1676 __value += __lc->_M_decimal_point;
1678 __value.append(__beg + __paddec, __lc->_M_frac_digits);
1681 // Have to pad zeros in the decimal position.
1682 __value.append(-__paddec, __lit[money_base::_S_zero]);
1683 __value.append(__beg, __len);
1687 // Calculate length of resulting string.
1688 const ios_base::fmtflags __f = __io.flags()
1689 & ios_base::adjustfield;
1690 __len = __value.size() + __sign_size;
1691 __len += ((__io.flags() & ios_base::showbase)
1692 ? __lc->_M_curr_symbol_size : 0);
1695 __res.reserve(2 * __len);
1697 const size_type __width = static_cast<size_type>(__io.width());
1698 const bool __testipad = (__f == ios_base::internal
1699 && __len < __width);
1700 // Fit formatted digits into the required pattern.
1701 for (int __i = 0; __i < 4; ++__i)
1703 const part __which = static_cast<part>(__p.field[__i]);
1706 case money_base::symbol:
1707 if (__io.flags() & ios_base::showbase)
1708 __res.append(__lc->_M_curr_symbol,
1709 __lc->_M_curr_symbol_size);
1711 case money_base::sign:
1712 // Sign might not exist, or be more than one
1713 // charater long. In that case, add in the rest
1718 case money_base::value:
1721 case money_base::space:
1722 // At least one space is required, but if internal
1723 // formatting is required, an arbitrary number of
1724 // fill spaces will be necessary.
1726 __res.append(__width - __len, __fill);
1730 case money_base::none:
1732 __res.append(__width - __len, __fill);
1737 // Special case of multi-part sign parts.
1738 if (__sign_size > 1)
1739 __res.append(__sign + 1, __sign_size - 1);
1741 // Pad, if still necessary.
1742 __len = __res.size();
1743 if (__width > __len)
1745 if (__f == ios_base::left)
1747 __res.append(__width - __len, __fill);
1750 __res.insert(0, __width - __len, __fill);
1754 // Write resulting, fully-formatted string to output iterator.
1755 __s = std::__write(__s, __res.data(), __len);
1761 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1762 template<typename _CharT, typename _OutIter>
1764 money_put<_CharT, _OutIter>::
1765 __do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1766 double __units) const
1767 { return this->do_put(__s, __intl, __io, __fill, (long double) __units); }
1770 template<typename _CharT, typename _OutIter>
1772 money_put<_CharT, _OutIter>::
1773 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1774 long double __units) const
1776 const locale __loc = __io.getloc();
1777 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1778 #ifdef _GLIBCXX_USE_C99
1779 // First try a buffer perhaps big enough.
1781 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1782 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1783 // 328. Bad sprintf format modifier in money_put<>::do_put()
1784 int __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1785 "%.*Lf", 0, __units);
1786 // If the buffer was not large enough, try again with the correct size.
1787 if (__len >= __cs_size)
1789 __cs_size = __len + 1;
1790 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1791 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1792 "%.*Lf", 0, __units);
1795 // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'.
1796 const int __cs_size =
1797 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 3;
1798 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1799 int __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, "%.*Lf",
1802 string_type __digits(__len, char_type());
1803 __ctype.widen(__cs, __cs + __len, &__digits[0]);
1804 return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
1805 : _M_insert<false>(__s, __io, __fill, __digits);
1808 template<typename _CharT, typename _OutIter>
1810 money_put<_CharT, _OutIter>::
1811 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1812 const string_type& __digits) const
1813 { return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
1814 : _M_insert<false>(__s, __io, __fill, __digits); }
1816 _GLIBCXX_END_LDBL_NAMESPACE
1818 // NB: Not especially useful. Without an ios_base object or some
1819 // kind of locale reference, we are left clawing at the air where
1820 // the side of the mountain used to be...
1821 template<typename _CharT, typename _InIter>
1822 time_base::dateorder
1823 time_get<_CharT, _InIter>::do_date_order() const
1824 { return time_base::no_order; }
1826 // Expand a strftime format string and parse it. E.g., do_get_date() may
1827 // pass %m/%d/%Y => extracted characters.
1828 template<typename _CharT, typename _InIter>
1830 time_get<_CharT, _InIter>::
1831 _M_extract_via_format(iter_type __beg, iter_type __end, ios_base& __io,
1832 ios_base::iostate& __err, tm* __tm,
1833 const _CharT* __format) const
1835 const locale& __loc = __io._M_getloc();
1836 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
1837 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1838 const size_t __len = char_traits<_CharT>::length(__format);
1840 ios_base::iostate __tmperr = ios_base::goodbit;
1841 for (size_t __i = 0; __beg != __end && __i < __len && !__tmperr; ++__i)
1843 if (__ctype.narrow(__format[__i], 0) == '%')
1845 // Verify valid formatting code, attempt to extract.
1846 char __c = __ctype.narrow(__format[++__i], 0);
1848 if (__c == 'E' || __c == 'O')
1849 __c = __ctype.narrow(__format[++__i], 0);
1855 // Abbreviated weekday name [tm_wday]
1856 const char_type* __days1[7];
1857 __tp._M_days_abbreviated(__days1);
1858 __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days1,
1862 // Weekday name [tm_wday].
1863 const char_type* __days2[7];
1864 __tp._M_days(__days2);
1865 __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days2,
1870 // Abbreviated month name [tm_mon]
1871 const char_type* __months1[12];
1872 __tp._M_months_abbreviated(__months1);
1873 __beg = _M_extract_name(__beg, __end, __tm->tm_mon,
1874 __months1, 12, __io, __tmperr);
1877 // Month name [tm_mon].
1878 const char_type* __months2[12];
1879 __tp._M_months(__months2);
1880 __beg = _M_extract_name(__beg, __end, __tm->tm_mon,
1881 __months2, 12, __io, __tmperr);
1884 // Default time and date representation.
1885 const char_type* __dt[2];
1886 __tp._M_date_time_formats(__dt);
1887 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1891 // Day [01, 31]. [tm_mday]
1892 __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2,
1896 // Day [1, 31], with single digits preceded by
1898 if (__ctype.is(ctype_base::space, *__beg))
1899 __beg = _M_extract_num(++__beg, __end, __tm->tm_mday, 1, 9,
1902 __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 10, 31,
1906 // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
1908 __ctype.widen(__cs, __cs + 9, __wcs);
1909 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1913 // Hour [00, 23]. [tm_hour]
1914 __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
1918 // Hour [01, 12]. [tm_hour]
1919 __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2,
1923 // Month [01, 12]. [tm_mon]
1924 __beg = _M_extract_num(__beg, __end, __mem, 1, 12, 2,
1927 __tm->tm_mon = __mem - 1;
1930 // Minute [00, 59]. [tm_min]
1931 __beg = _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
1935 if (__ctype.narrow(*__beg, 0) == '\n')
1938 __tmperr |= ios_base::failbit;
1941 // Equivalent to (%H:%M).
1943 __ctype.widen(__cs, __cs + 6, __wcs);
1944 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1948 // Seconds. [tm_sec]
1949 // [00, 60] in C99 (one leap-second), [00, 61] in C89.
1950 #ifdef _GLIBCXX_USE_C99
1951 __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 60, 2,
1953 __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 61, 2,
1958 if (__ctype.narrow(*__beg, 0) == '\t')
1961 __tmperr |= ios_base::failbit;
1964 // Equivalent to (%H:%M:%S).
1966 __ctype.widen(__cs, __cs + 9, __wcs);
1967 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1972 const char_type* __dates[2];
1973 __tp._M_date_formats(__dates);
1974 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1979 const char_type* __times[2];
1980 __tp._M_time_formats(__times);
1981 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1986 // Two digit year. [tm_year]
1987 __beg = _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2,
1991 // Year [1900). [tm_year]
1992 __beg = _M_extract_num(__beg, __end, __mem, 0, 9999, 4,
1995 __tm->tm_year = __mem - 1900;
1999 if (__ctype.is(ctype_base::upper, *__beg))
2002 __beg = _M_extract_name(__beg, __end, __tmp,
2003 __timepunct_cache<_CharT>::_S_timezones,
2004 14, __io, __tmperr);
2006 // GMT requires special effort.
2007 if (__beg != __end && !__tmperr && __tmp == 0
2008 && (*__beg == __ctype.widen('-')
2009 || *__beg == __ctype.widen('+')))
2011 __beg = _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
2013 __beg = _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
2018 __tmperr |= ios_base::failbit;
2022 __tmperr |= ios_base::failbit;
2027 // Verify format and input match, extract and discard.
2028 if (__format[__i] == *__beg)
2031 __tmperr |= ios_base::failbit;
2036 __err |= ios_base::failbit;
2041 template<typename _CharT, typename _InIter>
2043 time_get<_CharT, _InIter>::
2044 _M_extract_num(iter_type __beg, iter_type __end, int& __member,
2045 int __min, int __max, size_t __len,
2046 ios_base& __io, ios_base::iostate& __err) const
2048 const locale& __loc = __io._M_getloc();
2049 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2051 // As-is works for __len = 1, 2, 4, the values actually used.
2052 int __mult = __len == 2 ? 10 : (__len == 4 ? 1000 : 1);
2057 for (; __beg != __end && __i < __len; ++__beg, ++__i)
2059 const char __c = __ctype.narrow(*__beg, '*');
2060 if (__c >= '0' && __c <= '9')
2062 __value = __value * 10 + (__c - '0');
2063 const int __valuec = __value * __mult;
2064 if (__valuec > __max || __valuec + __mult < __min)
2074 __err |= ios_base::failbit;
2080 // All elements in __names are unique.
2081 template<typename _CharT, typename _InIter>
2083 time_get<_CharT, _InIter>::
2084 _M_extract_name(iter_type __beg, iter_type __end, int& __member,
2085 const _CharT** __names, size_t __indexlen,
2086 ios_base& __io, ios_base::iostate& __err) const
2088 typedef char_traits<_CharT> __traits_type;
2089 const locale& __loc = __io._M_getloc();
2090 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2092 int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int)
2094 size_t __nmatches = 0;
2096 bool __testvalid = true;
2097 const char_type* __name;
2099 // Look for initial matches.
2100 // NB: Some of the locale data is in the form of all lowercase
2101 // names, and some is in the form of initially-capitalized
2102 // names. Look for both.
2105 const char_type __c = *__beg;
2106 for (size_t __i1 = 0; __i1 < __indexlen; ++__i1)
2107 if (__c == __names[__i1][0]
2108 || __c == __ctype.toupper(__names[__i1][0]))
2109 __matches[__nmatches++] = __i1;
2112 while (__nmatches > 1)
2114 // Find smallest matching string.
2115 size_t __minlen = __traits_type::length(__names[__matches[0]]);
2116 for (size_t __i2 = 1; __i2 < __nmatches; ++__i2)
2117 __minlen = std::min(__minlen,
2118 __traits_type::length(__names[__matches[__i2]]));
2120 if (__pos < __minlen && __beg != __end)
2121 for (size_t __i3 = 0; __i3 < __nmatches;)
2123 __name = __names[__matches[__i3]];
2124 if (!(__name[__pos] == *__beg))
2125 __matches[__i3] = __matches[--__nmatches];
2133 if (__nmatches == 1)
2135 // Make sure found name is completely extracted.
2137 __name = __names[__matches[0]];
2138 const size_t __len = __traits_type::length(__name);
2139 while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
2143 __member = __matches[0];
2145 __testvalid = false;
2148 __testvalid = false;
2150 __err |= ios_base::failbit;
2155 template<typename _CharT, typename _InIter>
2157 time_get<_CharT, _InIter>::
2158 do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
2159 ios_base::iostate& __err, tm* __tm) const
2161 const locale& __loc = __io._M_getloc();
2162 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
2163 const char_type* __times[2];
2164 __tp._M_time_formats(__times);
2165 __beg = _M_extract_via_format(__beg, __end, __io, __err,
2168 __err |= ios_base::eofbit;
2172 template<typename _CharT, typename _InIter>
2174 time_get<_CharT, _InIter>::
2175 do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
2176 ios_base::iostate& __err, tm* __tm) const
2178 const locale& __loc = __io._M_getloc();
2179 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
2180 const char_type* __dates[2];
2181 __tp._M_date_formats(__dates);
2182 __beg = _M_extract_via_format(__beg, __end, __io, __err,
2185 __err |= ios_base::eofbit;
2189 template<typename _CharT, typename _InIter>
2191 time_get<_CharT, _InIter>::
2192 do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
2193 ios_base::iostate& __err, tm* __tm) const
2195 typedef char_traits<_CharT> __traits_type;
2196 const locale& __loc = __io._M_getloc();
2197 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
2198 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2199 const char_type* __days[7];
2200 __tp._M_days_abbreviated(__days);
2202 ios_base::iostate __tmperr = ios_base::goodbit;
2203 __beg = _M_extract_name(__beg, __end, __tmpwday, __days, 7,
2206 // Check to see if non-abbreviated name exists, and extract.
2207 // NB: Assumes both _M_days and _M_days_abbreviated organized in
2208 // exact same order, first to last, such that the resulting
2209 // __days array with the same index points to a day, and that
2210 // day's abbreviated form.
2211 // NB: Also assumes that an abbreviated name is a subset of the name.
2212 if (!__tmperr && __beg != __end)
2214 size_t __pos = __traits_type::length(__days[__tmpwday]);
2215 __tp._M_days(__days);
2216 const char_type* __name = __days[__tmpwday];
2217 if (__name[__pos] == *__beg)
2219 // Extract the rest of it.
2220 const size_t __len = __traits_type::length(__name);
2221 while (__pos < __len && __beg != __end
2222 && __name[__pos] == *__beg)
2225 __tmperr |= ios_base::failbit;
2229 __tm->tm_wday = __tmpwday;
2231 __err |= ios_base::failbit;
2234 __err |= ios_base::eofbit;
2238 template<typename _CharT, typename _InIter>
2240 time_get<_CharT, _InIter>::
2241 do_get_monthname(iter_type __beg, iter_type __end,
2242 ios_base& __io, ios_base::iostate& __err, tm* __tm) const
2244 typedef char_traits<_CharT> __traits_type;
2245 const locale& __loc = __io._M_getloc();
2246 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
2247 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2248 const char_type* __months[12];
2249 __tp._M_months_abbreviated(__months);
2251 ios_base::iostate __tmperr = ios_base::goodbit;
2252 __beg = _M_extract_name(__beg, __end, __tmpmon, __months, 12,
2255 // Check to see if non-abbreviated name exists, and extract.
2256 // NB: Assumes both _M_months and _M_months_abbreviated organized in
2257 // exact same order, first to last, such that the resulting
2258 // __months array with the same index points to a month, and that
2259 // month's abbreviated form.
2260 // NB: Also assumes that an abbreviated name is a subset of the name.
2261 if (!__tmperr && __beg != __end)
2263 size_t __pos = __traits_type::length(__months[__tmpmon]);
2264 __tp._M_months(__months);
2265 const char_type* __name = __months[__tmpmon];
2266 if (__name[__pos] == *__beg)
2268 // Extract the rest of it.
2269 const size_t __len = __traits_type::length(__name);
2270 while (__pos < __len && __beg != __end
2271 && __name[__pos] == *__beg)
2274 __tmperr |= ios_base::failbit;
2278 __tm->tm_mon = __tmpmon;
2280 __err |= ios_base::failbit;
2283 __err |= ios_base::eofbit;
2287 template<typename _CharT, typename _InIter>
2289 time_get<_CharT, _InIter>::
2290 do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
2291 ios_base::iostate& __err, tm* __tm) const
2293 const locale& __loc = __io._M_getloc();
2294 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2298 for (; __beg != __end && __i < 4; ++__beg, ++__i)
2300 const char __c = __ctype.narrow(*__beg, '*');
2301 if (__c >= '0' && __c <= '9')
2302 __value = __value * 10 + (__c - '0');
2306 if (__i == 2 || __i == 4)
2307 __tm->tm_year = __i == 2 ? __value : __value - 1900;
2309 __err |= ios_base::failbit;
2312 __err |= ios_base::eofbit;
2316 template<typename _CharT, typename _OutIter>
2318 time_put<_CharT, _OutIter>::
2319 put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
2320 const _CharT* __beg, const _CharT* __end) const
2322 const locale& __loc = __io._M_getloc();
2323 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2324 for (; __beg != __end; ++__beg)
2325 if (__ctype.narrow(*__beg, 0) != '%')
2330 else if (++__beg != __end)
2334 const char __c = __ctype.narrow(*__beg, 0);
2335 if (__c != 'E' && __c != 'O')
2337 else if (++__beg != __end)
2340 __format = __ctype.narrow(*__beg, 0);
2344 __s = this->do_put(__s, __io, __fill, __tm, __format, __mod);
2351 template<typename _CharT, typename _OutIter>
2353 time_put<_CharT, _OutIter>::
2354 do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
2355 char __format, char __mod) const
2357 const locale& __loc = __io._M_getloc();
2358 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2359 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
2361 // NB: This size is arbitrary. Should this be a data member,
2362 // initialized at construction?
2363 const size_t __maxlen = 128;
2365 static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen));
2367 // NB: In IEE 1003.1-200x, and perhaps other locale models, it
2368 // is possible that the format character will be longer than one
2369 // character. Possibilities include 'E' or 'O' followed by a
2370 // format character: if __mod is not the default argument, assume
2371 // it's a valid modifier.
2373 __fmt[0] = __ctype.widen('%');
2376 __fmt[1] = __format;
2377 __fmt[2] = char_type();
2382 __fmt[2] = __format;
2383 __fmt[3] = char_type();
2386 __tp._M_put(__res, __maxlen, __fmt, __tm);
2388 // Write resulting, fully-formatted string to output iterator.
2389 return std::__write(__s, __res, char_traits<char_type>::length(__res));
2392 // Generic version does nothing.
2393 template<typename _CharT>
2395 collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
2398 // Generic version does nothing.
2399 template<typename _CharT>
2401 collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
2404 template<typename _CharT>
2407 do_compare(const _CharT* __lo1, const _CharT* __hi1,
2408 const _CharT* __lo2, const _CharT* __hi2) const
2410 // strcoll assumes zero-terminated strings so we make a copy
2411 // and then put a zero at the end.
2412 const string_type __one(__lo1, __hi1);
2413 const string_type __two(__lo2, __hi2);
2415 const _CharT* __p = __one.c_str();
2416 const _CharT* __pend = __one.data() + __one.length();
2417 const _CharT* __q = __two.c_str();
2418 const _CharT* __qend = __two.data() + __two.length();
2420 // strcoll stops when it sees a nul character so we break
2421 // the strings into zero-terminated substrings and pass those
2425 const int __res = _M_compare(__p, __q);
2429 __p += char_traits<_CharT>::length(__p);
2430 __q += char_traits<_CharT>::length(__q);
2431 if (__p == __pend && __q == __qend)
2433 else if (__p == __pend)
2435 else if (__q == __qend)
2443 template<typename _CharT>
2444 typename collate<_CharT>::string_type
2446 do_transform(const _CharT* __lo, const _CharT* __hi) const
2450 // strxfrm assumes zero-terminated strings so we make a copy
2451 const string_type __str(__lo, __hi);
2453 const _CharT* __p = __str.c_str();
2454 const _CharT* __pend = __str.data() + __str.length();
2456 size_t __len = (__hi - __lo) * 2;
2458 _CharT* __c = new _CharT[__len];
2462 // strxfrm stops when it sees a nul character so we break
2463 // the string into zero-terminated substrings and pass those
2467 // First try a buffer perhaps big enough.
2468 size_t __res = _M_transform(__c, __p, __len);
2469 // If the buffer was not large enough, try again with the
2474 delete [] __c, __c = 0;
2475 __c = new _CharT[__len];
2476 __res = _M_transform(__c, __p, __len);
2479 __ret.append(__c, __res);
2480 __p += char_traits<_CharT>::length(__p);
2485 __ret.push_back(_CharT());
2491 __throw_exception_again;
2499 template<typename _CharT>
2502 do_hash(const _CharT* __lo, const _CharT* __hi) const
2504 unsigned long __val = 0;
2505 for (; __lo < __hi; ++__lo)
2507 *__lo + ((__val << 7)
2508 | (__val >> (__gnu_cxx::__numeric_traits<unsigned long>::
2510 return static_cast<long>(__val);
2513 // Construct correctly padded string, as per 22.2.2.2.2
2515 // __newlen > __oldlen
2516 // __news is allocated for __newlen size
2518 // NB: Of the two parameters, _CharT can be deduced from the
2519 // function arguments. The other (_Traits) has to be explicitly specified.
2520 template<typename _CharT, typename _Traits>
2522 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
2523 _CharT* __news, const _CharT* __olds,
2524 const streamsize __newlen,
2525 const streamsize __oldlen)
2527 const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
2528 const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
2531 if (__adjust == ios_base::left)
2533 _Traits::copy(__news, const_cast<_CharT*>(__olds), __oldlen);
2534 _Traits::assign(__news + __oldlen, __plen, __fill);
2539 if (__adjust == ios_base::internal)
2541 // Pad after the sign, if there is one.
2542 // Pad after 0[xX], if there is one.
2543 // Who came up with these rules, anyway? Jeeze.
2544 const locale& __loc = __io._M_getloc();
2545 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2547 const bool __testsign = (__ctype.widen('-') == __olds[0]
2548 || __ctype.widen('+') == __olds[0]);
2549 const bool __testhex = (__ctype.widen('0') == __olds[0]
2551 && (__ctype.widen('x') == __olds[1]
2552 || __ctype.widen('X') == __olds[1]));
2555 __news[0] = __olds[0];
2556 __news[1] = __olds[1];
2560 else if (__testsign)
2562 __news[0] = __olds[0];
2566 // else Padding first.
2568 _Traits::assign(__news, __plen, __fill);
2569 _Traits::copy(__news + __plen, const_cast<_CharT*>(__olds + __mod),
2574 __verify_grouping(const char* __grouping, size_t __grouping_size,
2575 const string& __grouping_tmp)
2577 const size_t __n = __grouping_tmp.size() - 1;
2578 const size_t __min = std::min(__n, size_t(__grouping_size - 1));
2582 // Parsed number groupings have to match the
2583 // numpunct::grouping string exactly, starting at the
2584 // right-most point of the parsed sequence of elements ...
2585 for (size_t __j = 0; __j < __min && __test; --__i, ++__j)
2586 __test = __grouping_tmp[__i] == __grouping[__j];
2587 for (; __i && __test; --__i)
2588 __test = __grouping_tmp[__i] == __grouping[__min];
2589 // ... but the first parsed grouping can be <= numpunct
2590 // grouping (only do the check if the numpunct char is > 0
2591 // because <= 0 means any size is ok).
2592 if (static_cast<signed char>(__grouping[__min]) > 0)
2593 __test &= __grouping_tmp[0] <= __grouping[__min];
2597 template<typename _CharT>
2599 __add_grouping(_CharT* __s, _CharT __sep,
2600 const char* __gbeg, size_t __gsize,
2601 const _CharT* __first, const _CharT* __last)
2606 while (__last - __first > __gbeg[__idx]
2607 && static_cast<signed char>(__gbeg[__idx]) > 0)
2609 __last -= __gbeg[__idx];
2610 __idx < __gsize - 1 ? ++__idx : ++__ctr;
2613 while (__first != __last)
2614 *__s++ = *__first++;
2619 for (char __i = __gbeg[__idx]; __i > 0; --__i)
2620 *__s++ = *__first++;
2626 for (char __i = __gbeg[__idx]; __i > 0; --__i)
2627 *__s++ = *__first++;
2633 // Inhibit implicit instantiations for required instantiations,
2634 // which are defined via explicit instantiations elsewhere.
2635 // NB: This syntax is a GNU extension.
2636 #if _GLIBCXX_EXTERN_TEMPLATE
2637 extern template class moneypunct<char, false>;
2638 extern template class moneypunct<char, true>;
2639 extern template class moneypunct_byname<char, false>;
2640 extern template class moneypunct_byname<char, true>;
2641 extern template class _GLIBCXX_LDBL_NAMESPACE money_get<char>;
2642 extern template class _GLIBCXX_LDBL_NAMESPACE money_put<char>;
2643 extern template class numpunct<char>;
2644 extern template class numpunct_byname<char>;
2645 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<char>;
2646 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<char>;
2647 extern template class __timepunct<char>;
2648 extern template class time_put<char>;
2649 extern template class time_put_byname<char>;
2650 extern template class time_get<char>;
2651 extern template class time_get_byname<char>;
2652 extern template class messages<char>;
2653 extern template class messages_byname<char>;
2654 extern template class ctype_byname<char>;
2655 extern template class codecvt_byname<char, char, mbstate_t>;
2656 extern template class collate<char>;
2657 extern template class collate_byname<char>;
2660 const codecvt<char, char, mbstate_t>&
2661 use_facet<codecvt<char, char, mbstate_t> >(const locale&);
2664 const collate<char>&
2665 use_facet<collate<char> >(const locale&);
2668 const numpunct<char>&
2669 use_facet<numpunct<char> >(const locale&);
2672 const num_put<char>&
2673 use_facet<num_put<char> >(const locale&);
2676 const num_get<char>&
2677 use_facet<num_get<char> >(const locale&);
2680 const moneypunct<char, true>&
2681 use_facet<moneypunct<char, true> >(const locale&);
2684 const moneypunct<char, false>&
2685 use_facet<moneypunct<char, false> >(const locale&);
2688 const money_put<char>&
2689 use_facet<money_put<char> >(const locale&);
2692 const money_get<char>&
2693 use_facet<money_get<char> >(const locale&);
2696 const __timepunct<char>&
2697 use_facet<__timepunct<char> >(const locale&);
2700 const time_put<char>&
2701 use_facet<time_put<char> >(const locale&);
2704 const time_get<char>&
2705 use_facet<time_get<char> >(const locale&);
2708 const messages<char>&
2709 use_facet<messages<char> >(const locale&);
2713 has_facet<ctype<char> >(const locale&);
2717 has_facet<codecvt<char, char, mbstate_t> >(const locale&);
2721 has_facet<collate<char> >(const locale&);
2725 has_facet<numpunct<char> >(const locale&);
2729 has_facet<num_put<char> >(const locale&);
2733 has_facet<num_get<char> >(const locale&);
2737 has_facet<moneypunct<char> >(const locale&);
2741 has_facet<money_put<char> >(const locale&);
2745 has_facet<money_get<char> >(const locale&);
2749 has_facet<__timepunct<char> >(const locale&);
2753 has_facet<time_put<char> >(const locale&);
2757 has_facet<time_get<char> >(const locale&);
2761 has_facet<messages<char> >(const locale&);
2763 #ifdef _GLIBCXX_USE_WCHAR_T
2764 extern template class moneypunct<wchar_t, false>;
2765 extern template class moneypunct<wchar_t, true>;
2766 extern template class moneypunct_byname<wchar_t, false>;
2767 extern template class moneypunct_byname<wchar_t, true>;
2768 extern template class _GLIBCXX_LDBL_NAMESPACE money_get<wchar_t>;
2769 extern template class _GLIBCXX_LDBL_NAMESPACE money_put<wchar_t>;
2770 extern template class numpunct<wchar_t>;
2771 extern template class numpunct_byname<wchar_t>;
2772 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<wchar_t>;
2773 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<wchar_t>;
2774 extern template class __timepunct<wchar_t>;
2775 extern template class time_put<wchar_t>;
2776 extern template class time_put_byname<wchar_t>;
2777 extern template class time_get<wchar_t>;
2778 extern template class time_get_byname<wchar_t>;
2779 extern template class messages<wchar_t>;
2780 extern template class messages_byname<wchar_t>;
2781 extern template class ctype_byname<wchar_t>;
2782 extern template class codecvt_byname<wchar_t, char, mbstate_t>;
2783 extern template class collate<wchar_t>;
2784 extern template class collate_byname<wchar_t>;
2787 const codecvt<wchar_t, char, mbstate_t>&
2788 use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&);
2791 const collate<wchar_t>&
2792 use_facet<collate<wchar_t> >(const locale&);
2795 const numpunct<wchar_t>&
2796 use_facet<numpunct<wchar_t> >(const locale&);
2799 const num_put<wchar_t>&
2800 use_facet<num_put<wchar_t> >(const locale&);
2803 const num_get<wchar_t>&
2804 use_facet<num_get<wchar_t> >(const locale&);
2807 const moneypunct<wchar_t, true>&
2808 use_facet<moneypunct<wchar_t, true> >(const locale&);
2811 const moneypunct<wchar_t, false>&
2812 use_facet<moneypunct<wchar_t, false> >(const locale&);
2815 const money_put<wchar_t>&
2816 use_facet<money_put<wchar_t> >(const locale&);
2819 const money_get<wchar_t>&
2820 use_facet<money_get<wchar_t> >(const locale&);
2823 const __timepunct<wchar_t>&
2824 use_facet<__timepunct<wchar_t> >(const locale&);
2827 const time_put<wchar_t>&
2828 use_facet<time_put<wchar_t> >(const locale&);
2831 const time_get<wchar_t>&
2832 use_facet<time_get<wchar_t> >(const locale&);
2835 const messages<wchar_t>&
2836 use_facet<messages<wchar_t> >(const locale&);
2840 has_facet<ctype<wchar_t> >(const locale&);
2844 has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
2848 has_facet<collate<wchar_t> >(const locale&);
2852 has_facet<numpunct<wchar_t> >(const locale&);
2856 has_facet<num_put<wchar_t> >(const locale&);
2860 has_facet<num_get<wchar_t> >(const locale&);
2864 has_facet<moneypunct<wchar_t> >(const locale&);
2868 has_facet<money_put<wchar_t> >(const locale&);
2872 has_facet<money_get<wchar_t> >(const locale&);
2876 has_facet<__timepunct<wchar_t> >(const locale&);
2880 has_facet<time_put<wchar_t> >(const locale&);
2884 has_facet<time_get<wchar_t> >(const locale&);
2888 has_facet<messages<wchar_t> >(const locale&);
2892 _GLIBCXX_END_NAMESPACE