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 <limits> // For numeric_limits
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 (numeric_limits<_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 -numeric_limits<_ValueT>::min() : numeric_limits<_ValueT>::max();
612 const __unsigned_type __smax = __max / __base;
613 __unsigned_type __result = 0;
615 const char_type* __lit_zero = __lit + __num_base::_S_izero;
617 if (!__lc->_M_allocated)
621 __digit = _M_find(__lit_zero, __len, __c);
625 if (__result > __smax)
630 __testfail |= __result > __max - __digit;
635 if (++__beg != __end)
643 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
644 // and decimal_point.
645 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
647 // NB: Thousands separator at the beginning of a string
648 // is a no-no, as is two consecutive thousands separators.
651 __found_grouping += static_cast<char>(__sep_pos);
660 else if (__c == __lc->_M_decimal_point)
664 const char_type* __q =
665 __traits_type::find(__lit_zero, __len, __c);
669 __digit = __q - __lit_zero;
672 if (__result > __smax)
677 __testfail |= __result > __max - __digit;
683 if (++__beg != __end)
689 // Digit grouping is checked. If grouping and found_grouping don't
690 // match, then get very very upset, and set failbit.
691 if (__found_grouping.size())
693 // Add the ending grouping.
694 __found_grouping += static_cast<char>(__sep_pos);
696 if (!std::__verify_grouping(__lc->_M_grouping,
697 __lc->_M_grouping_size,
699 __err |= ios_base::failbit;
702 if (!__testfail && (__sep_pos || __found_zero
703 || __found_grouping.size()))
704 __v = __negative ? -__result : __result;
706 __err |= ios_base::failbit;
709 __err |= ios_base::eofbit;
713 // _GLIBCXX_RESOLVE_LIB_DEFECTS
714 // 17. Bad bool parsing
715 template<typename _CharT, typename _InIter>
717 num_get<_CharT, _InIter>::
718 do_get(iter_type __beg, iter_type __end, ios_base& __io,
719 ios_base::iostate& __err, bool& __v) const
721 if (!(__io.flags() & ios_base::boolalpha))
723 // Parse bool values as long.
724 // NB: We can't just call do_get(long) here, as it might
725 // refer to a derived class.
727 __beg = _M_extract_int(__beg, __end, __io, __err, __l);
728 if (__l == 0 || __l == 1)
731 __err |= ios_base::failbit;
735 // Parse bool values as alphanumeric.
736 typedef __numpunct_cache<_CharT> __cache_type;
737 __use_cache<__cache_type> __uc;
738 const locale& __loc = __io._M_getloc();
739 const __cache_type* __lc = __uc(__loc);
744 bool __testeof = __beg == __end;
745 for (__n = 0; !__testeof; ++__n)
747 const char_type __c = *__beg;
751 if (__n < __lc->_M_falsename_size)
752 __testf = __c == __lc->_M_falsename[__n];
759 if (__n < __lc->_M_truename_size)
760 __testt = __c == __lc->_M_truename[__n];
765 if (!__testf && !__testt)
768 if (++__beg == __end)
771 if (__testf && __n == __lc->_M_falsename_size)
773 else if (__testt && __n == __lc->_M_truename_size)
776 __err |= ios_base::failbit;
779 __err |= ios_base::eofbit;
784 template<typename _CharT, typename _InIter>
786 num_get<_CharT, _InIter>::
787 do_get(iter_type __beg, iter_type __end, ios_base& __io,
788 ios_base::iostate& __err, long& __v) const
789 { return _M_extract_int(__beg, __end, __io, __err, __v); }
791 template<typename _CharT, typename _InIter>
793 num_get<_CharT, _InIter>::
794 do_get(iter_type __beg, iter_type __end, ios_base& __io,
795 ios_base::iostate& __err, unsigned short& __v) const
796 { return _M_extract_int(__beg, __end, __io, __err, __v); }
798 template<typename _CharT, typename _InIter>
800 num_get<_CharT, _InIter>::
801 do_get(iter_type __beg, iter_type __end, ios_base& __io,
802 ios_base::iostate& __err, unsigned int& __v) const
803 { return _M_extract_int(__beg, __end, __io, __err, __v); }
805 template<typename _CharT, typename _InIter>
807 num_get<_CharT, _InIter>::
808 do_get(iter_type __beg, iter_type __end, ios_base& __io,
809 ios_base::iostate& __err, unsigned long& __v) const
810 { return _M_extract_int(__beg, __end, __io, __err, __v); }
812 #ifdef _GLIBCXX_USE_LONG_LONG
813 template<typename _CharT, typename _InIter>
815 num_get<_CharT, _InIter>::
816 do_get(iter_type __beg, iter_type __end, ios_base& __io,
817 ios_base::iostate& __err, long long& __v) const
818 { return _M_extract_int(__beg, __end, __io, __err, __v); }
820 template<typename _CharT, typename _InIter>
822 num_get<_CharT, _InIter>::
823 do_get(iter_type __beg, iter_type __end, ios_base& __io,
824 ios_base::iostate& __err, unsigned long long& __v) const
825 { return _M_extract_int(__beg, __end, __io, __err, __v); }
828 template<typename _CharT, typename _InIter>
830 num_get<_CharT, _InIter>::
831 do_get(iter_type __beg, iter_type __end, ios_base& __io,
832 ios_base::iostate& __err, float& __v) const
836 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
837 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
841 template<typename _CharT, typename _InIter>
843 num_get<_CharT, _InIter>::
844 do_get(iter_type __beg, iter_type __end, ios_base& __io,
845 ios_base::iostate& __err, double& __v) const
849 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
850 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
854 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
855 template<typename _CharT, typename _InIter>
857 num_get<_CharT, _InIter>::
858 __do_get(iter_type __beg, iter_type __end, ios_base& __io,
859 ios_base::iostate& __err, double& __v) const
863 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
864 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
869 template<typename _CharT, typename _InIter>
871 num_get<_CharT, _InIter>::
872 do_get(iter_type __beg, iter_type __end, ios_base& __io,
873 ios_base::iostate& __err, long double& __v) const
877 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
878 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
882 template<typename _CharT, typename _InIter>
884 num_get<_CharT, _InIter>::
885 do_get(iter_type __beg, iter_type __end, ios_base& __io,
886 ios_base::iostate& __err, void*& __v) const
888 // Prepare for hex formatted input.
889 typedef ios_base::fmtflags fmtflags;
890 const fmtflags __fmt = __io.flags();
891 __io.flags((__fmt & ~ios_base::basefield) | ios_base::hex);
893 typedef __gnu_cxx::__conditional_type<(sizeof(void*)
894 <= sizeof(unsigned long)),
895 unsigned long, unsigned long long>::__type _UIntPtrType;
898 __beg = _M_extract_int(__beg, __end, __io, __err, __ul);
900 // Reset from hex formatted input.
903 if (!(__err & ios_base::failbit))
904 __v = reinterpret_cast<void*>(__ul);
908 // For use by integer and floating-point types after they have been
909 // converted into a char_type string.
910 template<typename _CharT, typename _OutIter>
912 num_put<_CharT, _OutIter>::
913 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
914 _CharT* __new, const _CharT* __cs, int& __len) const
916 // [22.2.2.2.2] Stage 3.
917 // If necessary, pad.
918 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new,
920 __len = static_cast<int>(__w);
923 _GLIBCXX_END_LDBL_NAMESPACE
925 template<typename _CharT, typename _ValueT>
927 __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
928 ios_base::fmtflags __flags, bool __dec)
930 _CharT* __buf = __bufend;
931 if (__builtin_expect(__dec, true))
936 *--__buf = __lit[(__v % 10) + __num_base::_S_odigits];
941 else if ((__flags & ios_base::basefield) == ios_base::oct)
946 *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits];
954 const bool __uppercase = __flags & ios_base::uppercase;
955 const int __case_offset = __uppercase ? __num_base::_S_oudigits
956 : __num_base::_S_odigits;
959 *--__buf = __lit[(__v & 0xf) + __case_offset];
964 return __bufend - __buf;
967 _GLIBCXX_BEGIN_LDBL_NAMESPACE
969 template<typename _CharT, typename _OutIter>
971 num_put<_CharT, _OutIter>::
972 _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep,
973 ios_base&, _CharT* __new, _CharT* __cs, int& __len) const
975 _CharT* __p = std::__add_grouping(__new, __sep, __grouping,
976 __grouping_size, __cs, __cs + __len);
980 template<typename _CharT, typename _OutIter>
981 template<typename _ValueT>
983 num_put<_CharT, _OutIter>::
984 _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
987 using __gnu_cxx::__add_unsigned;
988 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
989 typedef __numpunct_cache<_CharT> __cache_type;
990 __use_cache<__cache_type> __uc;
991 const locale& __loc = __io._M_getloc();
992 const __cache_type* __lc = __uc(__loc);
993 const _CharT* __lit = __lc->_M_atoms_out;
994 const ios_base::fmtflags __flags = __io.flags();
996 // Long enough to hold hex, dec, and octal representations.
997 const int __ilen = 5 * sizeof(_ValueT);
998 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1001 // [22.2.2.2.2] Stage 1, numeric conversion to character.
1002 // Result is returned right-justified in the buffer.
1003 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
1004 const bool __dec = (__basefield != ios_base::oct
1005 && __basefield != ios_base::hex);
1006 const __unsigned_type __u = ((__v > 0 || !__dec)
1007 ? __unsigned_type(__v)
1008 : -__unsigned_type(__v));
1009 int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec);
1010 __cs += __ilen - __len;
1012 // Add grouping, if necessary.
1013 if (__lc->_M_use_grouping)
1015 // Grouping can add (almost) as many separators as the number
1016 // of digits + space is reserved for numeric base or sign.
1017 _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1020 _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size,
1021 __lc->_M_thousands_sep, __io, __cs2 + 2, __cs, __len);
1025 // Complete Stage 1, prepend numeric base or sign.
1026 if (__builtin_expect(__dec, true))
1031 if (__flags & ios_base::showpos
1032 && numeric_limits<_ValueT>::is_signed)
1033 *--__cs = __lit[__num_base::_S_oplus], ++__len;
1036 *--__cs = __lit[__num_base::_S_ominus], ++__len;
1038 else if (__flags & ios_base::showbase && __v)
1040 if (__basefield == ios_base::oct)
1041 *--__cs = __lit[__num_base::_S_odigits], ++__len;
1045 const bool __uppercase = __flags & ios_base::uppercase;
1046 *--__cs = __lit[__num_base::_S_ox + __uppercase];
1048 *--__cs = __lit[__num_base::_S_odigits];
1054 const streamsize __w = __io.width();
1055 if (__w > static_cast<streamsize>(__len))
1057 _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1059 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
1064 // [22.2.2.2.2] Stage 4.
1065 // Write resulting, fully-formatted string to output iterator.
1066 return std::__write(__s, __cs, __len);
1069 template<typename _CharT, typename _OutIter>
1071 num_put<_CharT, _OutIter>::
1072 _M_group_float(const char* __grouping, size_t __grouping_size,
1073 _CharT __sep, const _CharT* __p, _CharT* __new,
1074 _CharT* __cs, int& __len) const
1076 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1077 // 282. What types does numpunct grouping refer to?
1078 // Add grouping, if necessary.
1079 const int __declen = __p ? __p - __cs : __len;
1080 _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping,
1082 __cs, __cs + __declen);
1084 // Tack on decimal part.
1085 int __newlen = __p2 - __new;
1088 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
1089 __newlen += __len - __declen;
1094 // The following code uses vsnprintf (or vsprintf(), when
1095 // _GLIBCXX_USE_C99 is not defined) to convert floating point values
1096 // for insertion into a stream. An optimization would be to replace
1097 // them with code that works directly on a wide buffer and then use
1098 // __pad to do the padding. It would be good to replace them anyway
1099 // to gain back the efficiency that C++ provides by knowing up front
1100 // the type of the values to insert. Also, sprintf is dangerous
1101 // since may lead to accidental buffer overruns. This
1102 // implementation follows the C++ standard fairly directly as
1103 // outlined in 22.2.2.2 [lib.locale.num.put]
1104 template<typename _CharT, typename _OutIter>
1105 template<typename _ValueT>
1107 num_put<_CharT, _OutIter>::
1108 _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
1111 typedef __numpunct_cache<_CharT> __cache_type;
1112 __use_cache<__cache_type> __uc;
1113 const locale& __loc = __io._M_getloc();
1114 const __cache_type* __lc = __uc(__loc);
1116 // Use default precision if out of range.
1117 const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision();
1119 const int __max_digits = numeric_limits<_ValueT>::digits10;
1121 // [22.2.2.2.2] Stage 1, numeric conversion to character.
1123 // Long enough for the max format spec.
1125 __num_base::_S_format_float(__io, __fbuf, __mod);
1127 #ifdef _GLIBCXX_USE_C99
1128 // First try a buffer perhaps big enough (most probably sufficient
1129 // for non-ios_base::fixed outputs)
1130 int __cs_size = __max_digits * 3;
1131 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1132 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1133 __fbuf, __prec, __v);
1135 // If the buffer was not large enough, try again with the correct size.
1136 if (__len >= __cs_size)
1138 __cs_size = __len + 1;
1139 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1140 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1141 __fbuf, __prec, __v);
1144 // Consider the possibility of long ios_base::fixed outputs
1145 const bool __fixed = __io.flags() & ios_base::fixed;
1146 const int __max_exp = numeric_limits<_ValueT>::max_exponent10;
1148 // The size of the output string is computed as follows.
1149 // ios_base::fixed outputs may need up to __max_exp + 1 chars
1150 // for the integer part + __prec chars for the fractional part
1151 // + 3 chars for sign, decimal point, '\0'. On the other hand,
1152 // for non-fixed outputs __max_digits * 2 + __prec chars are
1153 // largely sufficient.
1154 const int __cs_size = __fixed ? __max_exp + __prec + 4
1155 : __max_digits * 2 + __prec;
1156 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1157 __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf,
1161 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
1162 // numpunct.decimal_point() values for '.' and adding grouping.
1163 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1165 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1167 __ctype.widen(__cs, __cs + __len, __ws);
1169 // Replace decimal point.
1171 const char* __p = char_traits<char>::find(__cs, __len, '.');
1174 __wp = __ws + (__p - __cs);
1175 *__wp = __lc->_M_decimal_point;
1178 // Add grouping, if necessary.
1179 // N.B. Make sure to not group things like 2e20, i.e., no decimal
1180 // point, scientific notation.
1181 if (__lc->_M_use_grouping
1182 && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9'
1183 && __cs[1] >= '0' && __cs[2] >= '0')))
1185 // Grouping can add (almost) as many separators as the
1186 // number of digits, but no more.
1187 _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1190 streamsize __off = 0;
1191 if (__cs[0] == '-' || __cs[0] == '+')
1198 _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size,
1199 __lc->_M_thousands_sep, __wp, __ws2 + __off,
1200 __ws + __off, __len);
1207 const streamsize __w = __io.width();
1208 if (__w > static_cast<streamsize>(__len))
1210 _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1212 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
1217 // [22.2.2.2.2] Stage 4.
1218 // Write resulting, fully-formatted string to output iterator.
1219 return std::__write(__s, __ws, __len);
1222 template<typename _CharT, typename _OutIter>
1224 num_put<_CharT, _OutIter>::
1225 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
1227 const ios_base::fmtflags __flags = __io.flags();
1228 if ((__flags & ios_base::boolalpha) == 0)
1230 const long __l = __v;
1231 __s = _M_insert_int(__s, __io, __fill, __l);
1235 typedef __numpunct_cache<_CharT> __cache_type;
1236 __use_cache<__cache_type> __uc;
1237 const locale& __loc = __io._M_getloc();
1238 const __cache_type* __lc = __uc(__loc);
1240 const _CharT* __name = __v ? __lc->_M_truename
1241 : __lc->_M_falsename;
1242 int __len = __v ? __lc->_M_truename_size
1243 : __lc->_M_falsename_size;
1245 const streamsize __w = __io.width();
1246 if (__w > static_cast<streamsize>(__len))
1249 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1251 _M_pad(__fill, __w, __io, __cs, __name, __len);
1255 __s = std::__write(__s, __name, __len);
1260 template<typename _CharT, typename _OutIter>
1262 num_put<_CharT, _OutIter>::
1263 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
1264 { return _M_insert_int(__s, __io, __fill, __v); }
1266 template<typename _CharT, typename _OutIter>
1268 num_put<_CharT, _OutIter>::
1269 do_put(iter_type __s, ios_base& __io, char_type __fill,
1270 unsigned long __v) const
1271 { return _M_insert_int(__s, __io, __fill, __v); }
1273 #ifdef _GLIBCXX_USE_LONG_LONG
1274 template<typename _CharT, typename _OutIter>
1276 num_put<_CharT, _OutIter>::
1277 do_put(iter_type __s, ios_base& __io, char_type __fill, long long __v) const
1278 { return _M_insert_int(__s, __io, __fill, __v); }
1280 template<typename _CharT, typename _OutIter>
1282 num_put<_CharT, _OutIter>::
1283 do_put(iter_type __s, ios_base& __io, char_type __fill,
1284 unsigned long long __v) const
1285 { return _M_insert_int(__s, __io, __fill, __v); }
1288 template<typename _CharT, typename _OutIter>
1290 num_put<_CharT, _OutIter>::
1291 do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1292 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1294 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1295 template<typename _CharT, typename _OutIter>
1297 num_put<_CharT, _OutIter>::
1298 __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1299 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1302 template<typename _CharT, typename _OutIter>
1304 num_put<_CharT, _OutIter>::
1305 do_put(iter_type __s, ios_base& __io, char_type __fill,
1306 long double __v) const
1307 { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1309 template<typename _CharT, typename _OutIter>
1311 num_put<_CharT, _OutIter>::
1312 do_put(iter_type __s, ios_base& __io, char_type __fill,
1313 const void* __v) const
1315 const ios_base::fmtflags __flags = __io.flags();
1316 const ios_base::fmtflags __fmt = ~(ios_base::basefield
1317 | ios_base::uppercase
1318 | ios_base::internal);
1319 __io.flags((__flags & __fmt) | (ios_base::hex | ios_base::showbase));
1321 typedef __gnu_cxx::__conditional_type<(sizeof(const void*)
1322 <= sizeof(unsigned long)),
1323 unsigned long, unsigned long long>::__type _UIntPtrType;
1325 __s = _M_insert_int(__s, __io, __fill,
1326 reinterpret_cast<_UIntPtrType>(__v));
1327 __io.flags(__flags);
1331 template<typename _CharT, typename _InIter>
1332 template<bool _Intl>
1334 money_get<_CharT, _InIter>::
1335 _M_extract(iter_type __beg, iter_type __end, ios_base& __io,
1336 ios_base::iostate& __err, string& __units) const
1338 typedef char_traits<_CharT> __traits_type;
1339 typedef typename string_type::size_type size_type;
1340 typedef money_base::part part;
1341 typedef __moneypunct_cache<_CharT, _Intl> __cache_type;
1343 const locale& __loc = __io._M_getloc();
1344 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1346 __use_cache<__cache_type> __uc;
1347 const __cache_type* __lc = __uc(__loc);
1348 const char_type* __lit = __lc->_M_atoms;
1351 bool __negative = false;
1353 size_type __sign_size = 0;
1354 // True if sign is mandatory.
1355 const bool __mandatory_sign = (__lc->_M_positive_sign_size
1356 && __lc->_M_negative_sign_size);
1357 // String of grouping info from thousands_sep plucked from __units.
1358 string __grouping_tmp;
1359 if (__lc->_M_use_grouping)
1360 __grouping_tmp.reserve(32);
1361 // Last position before the decimal point.
1363 // Separator positions, then, possibly, fractional digits.
1365 // If input iterator is in a valid state.
1366 bool __testvalid = true;
1367 // Flag marking when a decimal point is found.
1368 bool __testdecfound = false;
1370 // The tentative returned string is stored here.
1374 const char_type* __lit_zero = __lit + money_base::_S_zero;
1375 const money_base::pattern __p = __lc->_M_neg_format;
1376 for (int __i = 0; __i < 4 && __testvalid; ++__i)
1378 const part __which = static_cast<part>(__p.field[__i]);
1381 case money_base::symbol:
1382 // According to 22.2.6.1.2, p2, symbol is required
1383 // if (__io.flags() & ios_base::showbase), otherwise
1384 // is optional and consumed only if other characters
1385 // are needed to complete the format.
1386 if (__io.flags() & ios_base::showbase || __sign_size > 1
1388 || (__i == 1 && (__mandatory_sign
1389 || (static_cast<part>(__p.field[0])
1390 == money_base::sign)
1391 || (static_cast<part>(__p.field[2])
1392 == money_base::space)))
1393 || (__i == 2 && ((static_cast<part>(__p.field[3])
1394 == money_base::value)
1395 || (__mandatory_sign
1396 && (static_cast<part>(__p.field[3])
1397 == money_base::sign)))))
1399 const size_type __len = __lc->_M_curr_symbol_size;
1401 for (; __beg != __end && __j < __len
1402 && *__beg == __lc->_M_curr_symbol[__j];
1405 && (__j || __io.flags() & ios_base::showbase))
1406 __testvalid = false;
1409 case money_base::sign:
1410 // Sign might not exist, or be more than one character long.
1411 if (__lc->_M_positive_sign_size && __beg != __end
1412 && *__beg == __lc->_M_positive_sign[0])
1414 __sign_size = __lc->_M_positive_sign_size;
1417 else if (__lc->_M_negative_sign_size && __beg != __end
1418 && *__beg == __lc->_M_negative_sign[0])
1421 __sign_size = __lc->_M_negative_sign_size;
1424 else if (__lc->_M_positive_sign_size
1425 && !__lc->_M_negative_sign_size)
1426 // "... if no sign is detected, the result is given the sign
1427 // that corresponds to the source of the empty string"
1429 else if (__mandatory_sign)
1430 __testvalid = false;
1432 case money_base::value:
1433 // Extract digits, remove and stash away the
1434 // grouping of found thousands separators.
1435 for (; __beg != __end; ++__beg)
1437 const char_type __c = *__beg;
1438 const char_type* __q = __traits_type::find(__lit_zero,
1442 __res += money_base::_S_atoms[__q - __lit];
1445 else if (__c == __lc->_M_decimal_point
1450 __testdecfound = true;
1452 else if (__lc->_M_use_grouping
1453 && __c == __lc->_M_thousands_sep
1458 // Mark position for later analysis.
1459 __grouping_tmp += static_cast<char>(__n);
1464 __testvalid = false;
1472 __testvalid = false;
1474 case money_base::space:
1475 // At least one space is required.
1476 if (__beg != __end && __ctype.is(ctype_base::space, *__beg))
1479 __testvalid = false;
1480 case money_base::none:
1481 // Only if not at the end of the pattern.
1483 for (; __beg != __end
1484 && __ctype.is(ctype_base::space, *__beg); ++__beg);
1489 // Need to get the rest of the sign characters, if they exist.
1490 if (__sign_size > 1 && __testvalid)
1492 const char_type* __sign = __negative ? __lc->_M_negative_sign
1493 : __lc->_M_positive_sign;
1495 for (; __beg != __end && __i < __sign_size
1496 && *__beg == __sign[__i]; ++__beg, ++__i);
1498 if (__i != __sign_size)
1499 __testvalid = false;
1504 // Strip leading zeros.
1505 if (__res.size() > 1)
1507 const size_type __first = __res.find_first_not_of('0');
1508 const bool __only_zeros = __first == string::npos;
1510 __res.erase(0, __only_zeros ? __res.size() - 1 : __first);
1514 if (__negative && __res[0] != '0')
1515 __res.insert(__res.begin(), '-');
1517 // Test for grouping fidelity.
1518 if (__grouping_tmp.size())
1520 // Add the ending grouping.
1521 __grouping_tmp += static_cast<char>(__testdecfound ? __last_pos
1523 if (!std::__verify_grouping(__lc->_M_grouping,
1524 __lc->_M_grouping_size,
1526 __err |= ios_base::failbit;
1529 // Iff not enough digits were supplied after the decimal-point.
1530 if (__testdecfound && __lc->_M_frac_digits > 0
1531 && __n != __lc->_M_frac_digits)
1532 __testvalid = false;
1535 // Iff valid sequence is not recognized.
1537 __err |= ios_base::failbit;
1539 __units.swap(__res);
1541 // Iff no more characters are available.
1543 __err |= ios_base::eofbit;
1547 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1548 template<typename _CharT, typename _InIter>
1550 money_get<_CharT, _InIter>::
1551 __do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1552 ios_base::iostate& __err, double& __units) const
1555 __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
1556 : _M_extract<false>(__beg, __end, __io, __err, __str);
1557 std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale());
1562 template<typename _CharT, typename _InIter>
1564 money_get<_CharT, _InIter>::
1565 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1566 ios_base::iostate& __err, long double& __units) const
1569 __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
1570 : _M_extract<false>(__beg, __end, __io, __err, __str);
1571 std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale());
1575 template<typename _CharT, typename _InIter>
1577 money_get<_CharT, _InIter>::
1578 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1579 ios_base::iostate& __err, string_type& __digits) const
1581 typedef typename string::size_type size_type;
1583 const locale& __loc = __io._M_getloc();
1584 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1587 __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
1588 : _M_extract<false>(__beg, __end, __io, __err, __str);
1589 const size_type __len = __str.size();
1592 __digits.resize(__len);
1593 __ctype.widen(__str.data(), __str.data() + __len, &__digits[0]);
1598 template<typename _CharT, typename _OutIter>
1599 template<bool _Intl>
1601 money_put<_CharT, _OutIter>::
1602 _M_insert(iter_type __s, ios_base& __io, char_type __fill,
1603 const string_type& __digits) const
1605 typedef typename string_type::size_type size_type;
1606 typedef money_base::part part;
1607 typedef __moneypunct_cache<_CharT, _Intl> __cache_type;
1609 const locale& __loc = __io._M_getloc();
1610 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1612 __use_cache<__cache_type> __uc;
1613 const __cache_type* __lc = __uc(__loc);
1614 const char_type* __lit = __lc->_M_atoms;
1616 // Determine if negative or positive formats are to be used, and
1617 // discard leading negative_sign if it is present.
1618 const char_type* __beg = __digits.data();
1620 money_base::pattern __p;
1621 const char_type* __sign;
1622 size_type __sign_size;
1623 if (!(*__beg == __lit[money_base::_S_minus]))
1625 __p = __lc->_M_pos_format;
1626 __sign = __lc->_M_positive_sign;
1627 __sign_size = __lc->_M_positive_sign_size;
1631 __p = __lc->_M_neg_format;
1632 __sign = __lc->_M_negative_sign;
1633 __sign_size = __lc->_M_negative_sign_size;
1634 if (__digits.size())
1638 // Look for valid numbers in the ctype facet within input digits.
1639 size_type __len = __ctype.scan_not(ctype_base::digit, __beg,
1640 __beg + __digits.size()) - __beg;
1643 // Assume valid input, and attempt to format.
1644 // Break down input numbers into base components, as follows:
1645 // final_value = grouped units + (decimal point) + (digits)
1646 string_type __value;
1647 __value.reserve(2 * __len);
1649 // Add thousands separators to non-decimal digits, per
1651 long __paddec = __len - __lc->_M_frac_digits;
1654 if (__lc->_M_frac_digits < 0)
1656 if (__lc->_M_grouping_size)
1658 __value.assign(2 * __paddec, char_type());
1660 std::__add_grouping(&__value[0], __lc->_M_thousands_sep,
1662 __lc->_M_grouping_size,
1663 __beg, __beg + __paddec);
1664 __value.erase(__vend - &__value[0]);
1667 __value.assign(__beg, __paddec);
1670 // Deal with decimal point, decimal digits.
1671 if (__lc->_M_frac_digits > 0)
1673 __value += __lc->_M_decimal_point;
1675 __value.append(__beg + __paddec, __lc->_M_frac_digits);
1678 // Have to pad zeros in the decimal position.
1679 __value.append(-__paddec, __lit[money_base::_S_zero]);
1680 __value.append(__beg, __len);
1684 // Calculate length of resulting string.
1685 const ios_base::fmtflags __f = __io.flags()
1686 & ios_base::adjustfield;
1687 __len = __value.size() + __sign_size;
1688 __len += ((__io.flags() & ios_base::showbase)
1689 ? __lc->_M_curr_symbol_size : 0);
1692 __res.reserve(2 * __len);
1694 const size_type __width = static_cast<size_type>(__io.width());
1695 const bool __testipad = (__f == ios_base::internal
1696 && __len < __width);
1697 // Fit formatted digits into the required pattern.
1698 for (int __i = 0; __i < 4; ++__i)
1700 const part __which = static_cast<part>(__p.field[__i]);
1703 case money_base::symbol:
1704 if (__io.flags() & ios_base::showbase)
1705 __res.append(__lc->_M_curr_symbol,
1706 __lc->_M_curr_symbol_size);
1708 case money_base::sign:
1709 // Sign might not exist, or be more than one
1710 // charater long. In that case, add in the rest
1715 case money_base::value:
1718 case money_base::space:
1719 // At least one space is required, but if internal
1720 // formatting is required, an arbitrary number of
1721 // fill spaces will be necessary.
1723 __res.append(__width - __len, __fill);
1727 case money_base::none:
1729 __res.append(__width - __len, __fill);
1734 // Special case of multi-part sign parts.
1735 if (__sign_size > 1)
1736 __res.append(__sign + 1, __sign_size - 1);
1738 // Pad, if still necessary.
1739 __len = __res.size();
1740 if (__width > __len)
1742 if (__f == ios_base::left)
1744 __res.append(__width - __len, __fill);
1747 __res.insert(0, __width - __len, __fill);
1751 // Write resulting, fully-formatted string to output iterator.
1752 __s = std::__write(__s, __res.data(), __len);
1758 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1759 template<typename _CharT, typename _OutIter>
1761 money_put<_CharT, _OutIter>::
1762 __do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1763 double __units) const
1764 { return this->do_put(__s, __intl, __io, __fill, (long double) __units); }
1767 template<typename _CharT, typename _OutIter>
1769 money_put<_CharT, _OutIter>::
1770 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1771 long double __units) const
1773 const locale __loc = __io.getloc();
1774 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1775 #ifdef _GLIBCXX_USE_C99
1776 // First try a buffer perhaps big enough.
1778 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1779 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1780 // 328. Bad sprintf format modifier in money_put<>::do_put()
1781 int __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1782 "%.*Lf", 0, __units);
1783 // If the buffer was not large enough, try again with the correct size.
1784 if (__len >= __cs_size)
1786 __cs_size = __len + 1;
1787 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1788 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1789 "%.*Lf", 0, __units);
1792 // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'.
1793 const int __cs_size = numeric_limits<long double>::max_exponent10 + 3;
1794 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1795 int __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, "%.*Lf",
1798 string_type __digits(__len, char_type());
1799 __ctype.widen(__cs, __cs + __len, &__digits[0]);
1800 return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
1801 : _M_insert<false>(__s, __io, __fill, __digits);
1804 template<typename _CharT, typename _OutIter>
1806 money_put<_CharT, _OutIter>::
1807 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1808 const string_type& __digits) const
1809 { return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
1810 : _M_insert<false>(__s, __io, __fill, __digits); }
1812 _GLIBCXX_END_LDBL_NAMESPACE
1814 // NB: Not especially useful. Without an ios_base object or some
1815 // kind of locale reference, we are left clawing at the air where
1816 // the side of the mountain used to be...
1817 template<typename _CharT, typename _InIter>
1818 time_base::dateorder
1819 time_get<_CharT, _InIter>::do_date_order() const
1820 { return time_base::no_order; }
1822 // Expand a strftime format string and parse it. E.g., do_get_date() may
1823 // pass %m/%d/%Y => extracted characters.
1824 template<typename _CharT, typename _InIter>
1826 time_get<_CharT, _InIter>::
1827 _M_extract_via_format(iter_type __beg, iter_type __end, ios_base& __io,
1828 ios_base::iostate& __err, tm* __tm,
1829 const _CharT* __format) const
1831 const locale& __loc = __io._M_getloc();
1832 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
1833 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1834 const size_t __len = char_traits<_CharT>::length(__format);
1836 ios_base::iostate __tmperr = ios_base::goodbit;
1837 for (size_t __i = 0; __beg != __end && __i < __len && !__tmperr; ++__i)
1839 if (__ctype.narrow(__format[__i], 0) == '%')
1841 // Verify valid formatting code, attempt to extract.
1842 char __c = __ctype.narrow(__format[++__i], 0);
1844 if (__c == 'E' || __c == 'O')
1845 __c = __ctype.narrow(__format[++__i], 0);
1851 // Abbreviated weekday name [tm_wday]
1852 const char_type* __days1[7];
1853 __tp._M_days_abbreviated(__days1);
1854 __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days1,
1858 // Weekday name [tm_wday].
1859 const char_type* __days2[7];
1860 __tp._M_days(__days2);
1861 __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days2,
1866 // Abbreviated month name [tm_mon]
1867 const char_type* __months1[12];
1868 __tp._M_months_abbreviated(__months1);
1869 __beg = _M_extract_name(__beg, __end, __tm->tm_mon,
1870 __months1, 12, __io, __tmperr);
1873 // Month name [tm_mon].
1874 const char_type* __months2[12];
1875 __tp._M_months(__months2);
1876 __beg = _M_extract_name(__beg, __end, __tm->tm_mon,
1877 __months2, 12, __io, __tmperr);
1880 // Default time and date representation.
1881 const char_type* __dt[2];
1882 __tp._M_date_time_formats(__dt);
1883 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1887 // Day [01, 31]. [tm_mday]
1888 __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2,
1892 // Day [1, 31], with single digits preceded by
1894 if (__ctype.is(ctype_base::space, *__beg))
1895 __beg = _M_extract_num(++__beg, __end, __tm->tm_mday, 1, 9,
1898 __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 10, 31,
1902 // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
1904 __ctype.widen(__cs, __cs + 9, __wcs);
1905 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1909 // Hour [00, 23]. [tm_hour]
1910 __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
1914 // Hour [01, 12]. [tm_hour]
1915 __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2,
1919 // Month [01, 12]. [tm_mon]
1920 __beg = _M_extract_num(__beg, __end, __mem, 1, 12, 2,
1923 __tm->tm_mon = __mem - 1;
1926 // Minute [00, 59]. [tm_min]
1927 __beg = _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
1931 if (__ctype.narrow(*__beg, 0) == '\n')
1934 __tmperr |= ios_base::failbit;
1937 // Equivalent to (%H:%M).
1939 __ctype.widen(__cs, __cs + 6, __wcs);
1940 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1944 // Seconds. [tm_sec]
1945 // [00, 60] in C99 (one leap-second), [00, 61] in C89.
1946 #ifdef _GLIBCXX_USE_C99
1947 __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 60, 2,
1949 __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 61, 2,
1954 if (__ctype.narrow(*__beg, 0) == '\t')
1957 __tmperr |= ios_base::failbit;
1960 // Equivalent to (%H:%M:%S).
1962 __ctype.widen(__cs, __cs + 9, __wcs);
1963 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1968 const char_type* __dates[2];
1969 __tp._M_date_formats(__dates);
1970 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1975 const char_type* __times[2];
1976 __tp._M_time_formats(__times);
1977 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
1982 // Two digit year. [tm_year]
1983 __beg = _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2,
1987 // Year [1900). [tm_year]
1988 __beg = _M_extract_num(__beg, __end, __mem, 0, 9999, 4,
1991 __tm->tm_year = __mem - 1900;
1995 if (__ctype.is(ctype_base::upper, *__beg))
1998 __beg = _M_extract_name(__beg, __end, __tmp,
1999 __timepunct_cache<_CharT>::_S_timezones,
2000 14, __io, __tmperr);
2002 // GMT requires special effort.
2003 if (__beg != __end && !__tmperr && __tmp == 0
2004 && (*__beg == __ctype.widen('-')
2005 || *__beg == __ctype.widen('+')))
2007 __beg = _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
2009 __beg = _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
2014 __tmperr |= ios_base::failbit;
2018 __tmperr |= ios_base::failbit;
2023 // Verify format and input match, extract and discard.
2024 if (__format[__i] == *__beg)
2027 __tmperr |= ios_base::failbit;
2032 __err |= ios_base::failbit;
2037 template<typename _CharT, typename _InIter>
2039 time_get<_CharT, _InIter>::
2040 _M_extract_num(iter_type __beg, iter_type __end, int& __member,
2041 int __min, int __max, size_t __len,
2042 ios_base& __io, ios_base::iostate& __err) const
2044 const locale& __loc = __io._M_getloc();
2045 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2047 // As-is works for __len = 1, 2, 4, the values actually used.
2048 int __mult = __len == 2 ? 10 : (__len == 4 ? 1000 : 1);
2053 for (; __beg != __end && __i < __len; ++__beg, ++__i)
2055 const char __c = __ctype.narrow(*__beg, '*');
2056 if (__c >= '0' && __c <= '9')
2058 __value = __value * 10 + (__c - '0');
2059 const int __valuec = __value * __mult;
2060 if (__valuec > __max || __valuec + __mult < __min)
2070 __err |= ios_base::failbit;
2076 // All elements in __names are unique.
2077 template<typename _CharT, typename _InIter>
2079 time_get<_CharT, _InIter>::
2080 _M_extract_name(iter_type __beg, iter_type __end, int& __member,
2081 const _CharT** __names, size_t __indexlen,
2082 ios_base& __io, ios_base::iostate& __err) const
2084 typedef char_traits<_CharT> __traits_type;
2085 const locale& __loc = __io._M_getloc();
2086 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2088 int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int)
2090 size_t __nmatches = 0;
2092 bool __testvalid = true;
2093 const char_type* __name;
2095 // Look for initial matches.
2096 // NB: Some of the locale data is in the form of all lowercase
2097 // names, and some is in the form of initially-capitalized
2098 // names. Look for both.
2101 const char_type __c = *__beg;
2102 for (size_t __i1 = 0; __i1 < __indexlen; ++__i1)
2103 if (__c == __names[__i1][0]
2104 || __c == __ctype.toupper(__names[__i1][0]))
2105 __matches[__nmatches++] = __i1;
2108 while (__nmatches > 1)
2110 // Find smallest matching string.
2111 size_t __minlen = __traits_type::length(__names[__matches[0]]);
2112 for (size_t __i2 = 1; __i2 < __nmatches; ++__i2)
2113 __minlen = std::min(__minlen,
2114 __traits_type::length(__names[__matches[__i2]]));
2116 if (__pos < __minlen && __beg != __end)
2117 for (size_t __i3 = 0; __i3 < __nmatches;)
2119 __name = __names[__matches[__i3]];
2120 if (!(__name[__pos] == *__beg))
2121 __matches[__i3] = __matches[--__nmatches];
2129 if (__nmatches == 1)
2131 // Make sure found name is completely extracted.
2133 __name = __names[__matches[0]];
2134 const size_t __len = __traits_type::length(__name);
2135 while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
2139 __member = __matches[0];
2141 __testvalid = false;
2144 __testvalid = false;
2146 __err |= ios_base::failbit;
2151 template<typename _CharT, typename _InIter>
2153 time_get<_CharT, _InIter>::
2154 do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
2155 ios_base::iostate& __err, tm* __tm) const
2157 const locale& __loc = __io._M_getloc();
2158 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
2159 const char_type* __times[2];
2160 __tp._M_time_formats(__times);
2161 __beg = _M_extract_via_format(__beg, __end, __io, __err,
2164 __err |= ios_base::eofbit;
2168 template<typename _CharT, typename _InIter>
2170 time_get<_CharT, _InIter>::
2171 do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
2172 ios_base::iostate& __err, tm* __tm) const
2174 const locale& __loc = __io._M_getloc();
2175 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
2176 const char_type* __dates[2];
2177 __tp._M_date_formats(__dates);
2178 __beg = _M_extract_via_format(__beg, __end, __io, __err,
2181 __err |= ios_base::eofbit;
2185 template<typename _CharT, typename _InIter>
2187 time_get<_CharT, _InIter>::
2188 do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
2189 ios_base::iostate& __err, tm* __tm) const
2191 typedef char_traits<_CharT> __traits_type;
2192 const locale& __loc = __io._M_getloc();
2193 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
2194 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2195 const char_type* __days[7];
2196 __tp._M_days_abbreviated(__days);
2198 ios_base::iostate __tmperr = ios_base::goodbit;
2199 __beg = _M_extract_name(__beg, __end, __tmpwday, __days, 7,
2202 // Check to see if non-abbreviated name exists, and extract.
2203 // NB: Assumes both _M_days and _M_days_abbreviated organized in
2204 // exact same order, first to last, such that the resulting
2205 // __days array with the same index points to a day, and that
2206 // day's abbreviated form.
2207 // NB: Also assumes that an abbreviated name is a subset of the name.
2208 if (!__tmperr && __beg != __end)
2210 size_t __pos = __traits_type::length(__days[__tmpwday]);
2211 __tp._M_days(__days);
2212 const char_type* __name = __days[__tmpwday];
2213 if (__name[__pos] == *__beg)
2215 // Extract the rest of it.
2216 const size_t __len = __traits_type::length(__name);
2217 while (__pos < __len && __beg != __end
2218 && __name[__pos] == *__beg)
2221 __tmperr |= ios_base::failbit;
2225 __tm->tm_wday = __tmpwday;
2227 __err |= ios_base::failbit;
2230 __err |= ios_base::eofbit;
2234 template<typename _CharT, typename _InIter>
2236 time_get<_CharT, _InIter>::
2237 do_get_monthname(iter_type __beg, iter_type __end,
2238 ios_base& __io, ios_base::iostate& __err, tm* __tm) const
2240 typedef char_traits<_CharT> __traits_type;
2241 const locale& __loc = __io._M_getloc();
2242 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
2243 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2244 const char_type* __months[12];
2245 __tp._M_months_abbreviated(__months);
2247 ios_base::iostate __tmperr = ios_base::goodbit;
2248 __beg = _M_extract_name(__beg, __end, __tmpmon, __months, 12,
2251 // Check to see if non-abbreviated name exists, and extract.
2252 // NB: Assumes both _M_months and _M_months_abbreviated organized in
2253 // exact same order, first to last, such that the resulting
2254 // __months array with the same index points to a month, and that
2255 // month's abbreviated form.
2256 // NB: Also assumes that an abbreviated name is a subset of the name.
2257 if (!__tmperr && __beg != __end)
2259 size_t __pos = __traits_type::length(__months[__tmpmon]);
2260 __tp._M_months(__months);
2261 const char_type* __name = __months[__tmpmon];
2262 if (__name[__pos] == *__beg)
2264 // Extract the rest of it.
2265 const size_t __len = __traits_type::length(__name);
2266 while (__pos < __len && __beg != __end
2267 && __name[__pos] == *__beg)
2270 __tmperr |= ios_base::failbit;
2274 __tm->tm_mon = __tmpmon;
2276 __err |= ios_base::failbit;
2279 __err |= ios_base::eofbit;
2283 template<typename _CharT, typename _InIter>
2285 time_get<_CharT, _InIter>::
2286 do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
2287 ios_base::iostate& __err, tm* __tm) const
2289 const locale& __loc = __io._M_getloc();
2290 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2294 for (; __beg != __end && __i < 4; ++__beg, ++__i)
2296 const char __c = __ctype.narrow(*__beg, '*');
2297 if (__c >= '0' && __c <= '9')
2298 __value = __value * 10 + (__c - '0');
2302 if (__i == 2 || __i == 4)
2303 __tm->tm_year = __i == 2 ? __value : __value - 1900;
2305 __err |= ios_base::failbit;
2308 __err |= ios_base::eofbit;
2312 template<typename _CharT, typename _OutIter>
2314 time_put<_CharT, _OutIter>::
2315 put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
2316 const _CharT* __beg, const _CharT* __end) const
2318 const locale& __loc = __io._M_getloc();
2319 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2320 for (; __beg != __end; ++__beg)
2321 if (__ctype.narrow(*__beg, 0) != '%')
2326 else if (++__beg != __end)
2330 const char __c = __ctype.narrow(*__beg, 0);
2331 if (__c != 'E' && __c != 'O')
2333 else if (++__beg != __end)
2336 __format = __ctype.narrow(*__beg, 0);
2340 __s = this->do_put(__s, __io, __fill, __tm, __format, __mod);
2347 template<typename _CharT, typename _OutIter>
2349 time_put<_CharT, _OutIter>::
2350 do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
2351 char __format, char __mod) const
2353 const locale& __loc = __io._M_getloc();
2354 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2355 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
2357 // NB: This size is arbitrary. Should this be a data member,
2358 // initialized at construction?
2359 const size_t __maxlen = 128;
2361 static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen));
2363 // NB: In IEE 1003.1-200x, and perhaps other locale models, it
2364 // is possible that the format character will be longer than one
2365 // character. Possibilities include 'E' or 'O' followed by a
2366 // format character: if __mod is not the default argument, assume
2367 // it's a valid modifier.
2369 __fmt[0] = __ctype.widen('%');
2372 __fmt[1] = __format;
2373 __fmt[2] = char_type();
2378 __fmt[2] = __format;
2379 __fmt[3] = char_type();
2382 __tp._M_put(__res, __maxlen, __fmt, __tm);
2384 // Write resulting, fully-formatted string to output iterator.
2385 return std::__write(__s, __res, char_traits<char_type>::length(__res));
2388 // Generic version does nothing.
2389 template<typename _CharT>
2391 collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
2394 // Generic version does nothing.
2395 template<typename _CharT>
2397 collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
2400 template<typename _CharT>
2403 do_compare(const _CharT* __lo1, const _CharT* __hi1,
2404 const _CharT* __lo2, const _CharT* __hi2) const
2406 // strcoll assumes zero-terminated strings so we make a copy
2407 // and then put a zero at the end.
2408 const string_type __one(__lo1, __hi1);
2409 const string_type __two(__lo2, __hi2);
2411 const _CharT* __p = __one.c_str();
2412 const _CharT* __pend = __one.data() + __one.length();
2413 const _CharT* __q = __two.c_str();
2414 const _CharT* __qend = __two.data() + __two.length();
2416 // strcoll stops when it sees a nul character so we break
2417 // the strings into zero-terminated substrings and pass those
2421 const int __res = _M_compare(__p, __q);
2425 __p += char_traits<_CharT>::length(__p);
2426 __q += char_traits<_CharT>::length(__q);
2427 if (__p == __pend && __q == __qend)
2429 else if (__p == __pend)
2431 else if (__q == __qend)
2439 template<typename _CharT>
2440 typename collate<_CharT>::string_type
2442 do_transform(const _CharT* __lo, const _CharT* __hi) const
2446 // strxfrm assumes zero-terminated strings so we make a copy
2447 const string_type __str(__lo, __hi);
2449 const _CharT* __p = __str.c_str();
2450 const _CharT* __pend = __str.data() + __str.length();
2452 size_t __len = (__hi - __lo) * 2;
2454 _CharT* __c = new _CharT[__len];
2458 // strxfrm stops when it sees a nul character so we break
2459 // the string into zero-terminated substrings and pass those
2463 // First try a buffer perhaps big enough.
2464 size_t __res = _M_transform(__c, __p, __len);
2465 // If the buffer was not large enough, try again with the
2470 delete [] __c, __c = 0;
2471 __c = new _CharT[__len];
2472 __res = _M_transform(__c, __p, __len);
2475 __ret.append(__c, __res);
2476 __p += char_traits<_CharT>::length(__p);
2481 __ret.push_back(_CharT());
2487 __throw_exception_again;
2495 template<typename _CharT>
2498 do_hash(const _CharT* __lo, const _CharT* __hi) const
2500 unsigned long __val = 0;
2501 for (; __lo < __hi; ++__lo)
2502 __val = *__lo + ((__val << 7) |
2503 (__val >> (numeric_limits<unsigned long>::digits - 7)));
2504 return static_cast<long>(__val);
2507 // Construct correctly padded string, as per 22.2.2.2.2
2509 // __newlen > __oldlen
2510 // __news is allocated for __newlen size
2512 // NB: Of the two parameters, _CharT can be deduced from the
2513 // function arguments. The other (_Traits) has to be explicitly specified.
2514 template<typename _CharT, typename _Traits>
2516 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
2517 _CharT* __news, const _CharT* __olds,
2518 const streamsize __newlen,
2519 const streamsize __oldlen)
2521 const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
2522 const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
2525 if (__adjust == ios_base::left)
2527 _Traits::copy(__news, const_cast<_CharT*>(__olds), __oldlen);
2528 _Traits::assign(__news + __oldlen, __plen, __fill);
2533 if (__adjust == ios_base::internal)
2535 // Pad after the sign, if there is one.
2536 // Pad after 0[xX], if there is one.
2537 // Who came up with these rules, anyway? Jeeze.
2538 const locale& __loc = __io._M_getloc();
2539 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2541 const bool __testsign = (__ctype.widen('-') == __olds[0]
2542 || __ctype.widen('+') == __olds[0]);
2543 const bool __testhex = (__ctype.widen('0') == __olds[0]
2545 && (__ctype.widen('x') == __olds[1]
2546 || __ctype.widen('X') == __olds[1]));
2549 __news[0] = __olds[0];
2550 __news[1] = __olds[1];
2554 else if (__testsign)
2556 __news[0] = __olds[0];
2560 // else Padding first.
2562 _Traits::assign(__news, __plen, __fill);
2563 _Traits::copy(__news + __plen, const_cast<_CharT*>(__olds + __mod),
2568 __verify_grouping(const char* __grouping, size_t __grouping_size,
2569 const string& __grouping_tmp)
2571 const size_t __n = __grouping_tmp.size() - 1;
2572 const size_t __min = std::min(__n, size_t(__grouping_size - 1));
2576 // Parsed number groupings have to match the
2577 // numpunct::grouping string exactly, starting at the
2578 // right-most point of the parsed sequence of elements ...
2579 for (size_t __j = 0; __j < __min && __test; --__i, ++__j)
2580 __test = __grouping_tmp[__i] == __grouping[__j];
2581 for (; __i && __test; --__i)
2582 __test = __grouping_tmp[__i] == __grouping[__min];
2583 // ... but the first parsed grouping can be <= numpunct
2584 // grouping (only do the check if the numpunct char is > 0
2585 // because <= 0 means any size is ok).
2586 if (static_cast<signed char>(__grouping[__min]) > 0)
2587 __test &= __grouping_tmp[0] <= __grouping[__min];
2591 template<typename _CharT>
2593 __add_grouping(_CharT* __s, _CharT __sep,
2594 const char* __gbeg, size_t __gsize,
2595 const _CharT* __first, const _CharT* __last)
2600 while (__last - __first > __gbeg[__idx]
2601 && static_cast<signed char>(__gbeg[__idx]) > 0)
2603 __last -= __gbeg[__idx];
2604 __idx < __gsize - 1 ? ++__idx : ++__ctr;
2607 while (__first != __last)
2608 *__s++ = *__first++;
2613 for (char __i = __gbeg[__idx]; __i > 0; --__i)
2614 *__s++ = *__first++;
2620 for (char __i = __gbeg[__idx]; __i > 0; --__i)
2621 *__s++ = *__first++;
2627 // Inhibit implicit instantiations for required instantiations,
2628 // which are defined via explicit instantiations elsewhere.
2629 // NB: This syntax is a GNU extension.
2630 #if _GLIBCXX_EXTERN_TEMPLATE
2631 extern template class moneypunct<char, false>;
2632 extern template class moneypunct<char, true>;
2633 extern template class moneypunct_byname<char, false>;
2634 extern template class moneypunct_byname<char, true>;
2635 extern template class _GLIBCXX_LDBL_NAMESPACE money_get<char>;
2636 extern template class _GLIBCXX_LDBL_NAMESPACE money_put<char>;
2637 extern template class numpunct<char>;
2638 extern template class numpunct_byname<char>;
2639 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<char>;
2640 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<char>;
2641 extern template class __timepunct<char>;
2642 extern template class time_put<char>;
2643 extern template class time_put_byname<char>;
2644 extern template class time_get<char>;
2645 extern template class time_get_byname<char>;
2646 extern template class messages<char>;
2647 extern template class messages_byname<char>;
2648 extern template class ctype_byname<char>;
2649 extern template class codecvt_byname<char, char, mbstate_t>;
2650 extern template class collate<char>;
2651 extern template class collate_byname<char>;
2654 const codecvt<char, char, mbstate_t>&
2655 use_facet<codecvt<char, char, mbstate_t> >(const locale&);
2658 const collate<char>&
2659 use_facet<collate<char> >(const locale&);
2662 const numpunct<char>&
2663 use_facet<numpunct<char> >(const locale&);
2666 const num_put<char>&
2667 use_facet<num_put<char> >(const locale&);
2670 const num_get<char>&
2671 use_facet<num_get<char> >(const locale&);
2674 const moneypunct<char, true>&
2675 use_facet<moneypunct<char, true> >(const locale&);
2678 const moneypunct<char, false>&
2679 use_facet<moneypunct<char, false> >(const locale&);
2682 const money_put<char>&
2683 use_facet<money_put<char> >(const locale&);
2686 const money_get<char>&
2687 use_facet<money_get<char> >(const locale&);
2690 const __timepunct<char>&
2691 use_facet<__timepunct<char> >(const locale&);
2694 const time_put<char>&
2695 use_facet<time_put<char> >(const locale&);
2698 const time_get<char>&
2699 use_facet<time_get<char> >(const locale&);
2702 const messages<char>&
2703 use_facet<messages<char> >(const locale&);
2707 has_facet<ctype<char> >(const locale&);
2711 has_facet<codecvt<char, char, mbstate_t> >(const locale&);
2715 has_facet<collate<char> >(const locale&);
2719 has_facet<numpunct<char> >(const locale&);
2723 has_facet<num_put<char> >(const locale&);
2727 has_facet<num_get<char> >(const locale&);
2731 has_facet<moneypunct<char> >(const locale&);
2735 has_facet<money_put<char> >(const locale&);
2739 has_facet<money_get<char> >(const locale&);
2743 has_facet<__timepunct<char> >(const locale&);
2747 has_facet<time_put<char> >(const locale&);
2751 has_facet<time_get<char> >(const locale&);
2755 has_facet<messages<char> >(const locale&);
2757 #ifdef _GLIBCXX_USE_WCHAR_T
2758 extern template class moneypunct<wchar_t, false>;
2759 extern template class moneypunct<wchar_t, true>;
2760 extern template class moneypunct_byname<wchar_t, false>;
2761 extern template class moneypunct_byname<wchar_t, true>;
2762 extern template class _GLIBCXX_LDBL_NAMESPACE money_get<wchar_t>;
2763 extern template class _GLIBCXX_LDBL_NAMESPACE money_put<wchar_t>;
2764 extern template class numpunct<wchar_t>;
2765 extern template class numpunct_byname<wchar_t>;
2766 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<wchar_t>;
2767 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<wchar_t>;
2768 extern template class __timepunct<wchar_t>;
2769 extern template class time_put<wchar_t>;
2770 extern template class time_put_byname<wchar_t>;
2771 extern template class time_get<wchar_t>;
2772 extern template class time_get_byname<wchar_t>;
2773 extern template class messages<wchar_t>;
2774 extern template class messages_byname<wchar_t>;
2775 extern template class ctype_byname<wchar_t>;
2776 extern template class codecvt_byname<wchar_t, char, mbstate_t>;
2777 extern template class collate<wchar_t>;
2778 extern template class collate_byname<wchar_t>;
2781 const codecvt<wchar_t, char, mbstate_t>&
2782 use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&);
2785 const collate<wchar_t>&
2786 use_facet<collate<wchar_t> >(const locale&);
2789 const numpunct<wchar_t>&
2790 use_facet<numpunct<wchar_t> >(const locale&);
2793 const num_put<wchar_t>&
2794 use_facet<num_put<wchar_t> >(const locale&);
2797 const num_get<wchar_t>&
2798 use_facet<num_get<wchar_t> >(const locale&);
2801 const moneypunct<wchar_t, true>&
2802 use_facet<moneypunct<wchar_t, true> >(const locale&);
2805 const moneypunct<wchar_t, false>&
2806 use_facet<moneypunct<wchar_t, false> >(const locale&);
2809 const money_put<wchar_t>&
2810 use_facet<money_put<wchar_t> >(const locale&);
2813 const money_get<wchar_t>&
2814 use_facet<money_get<wchar_t> >(const locale&);
2817 const __timepunct<wchar_t>&
2818 use_facet<__timepunct<wchar_t> >(const locale&);
2821 const time_put<wchar_t>&
2822 use_facet<time_put<wchar_t> >(const locale&);
2825 const time_get<wchar_t>&
2826 use_facet<time_get<wchar_t> >(const locale&);
2829 const messages<wchar_t>&
2830 use_facet<messages<wchar_t> >(const locale&);
2834 has_facet<ctype<wchar_t> >(const locale&);
2838 has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
2842 has_facet<collate<wchar_t> >(const locale&);
2846 has_facet<numpunct<wchar_t> >(const locale&);
2850 has_facet<num_put<wchar_t> >(const locale&);
2854 has_facet<num_get<wchar_t> >(const locale&);
2858 has_facet<moneypunct<wchar_t> >(const locale&);
2862 has_facet<money_put<wchar_t> >(const locale&);
2866 has_facet<money_get<wchar_t> >(const locale&);
2870 has_facet<__timepunct<wchar_t> >(const locale&);
2874 has_facet<time_put<wchar_t> >(const locale&);
2878 has_facet<time_get<wchar_t> >(const locale&);
2882 has_facet<messages<wchar_t> >(const locale&);
2886 _GLIBCXX_END_NAMESPACE