1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 // Warning: this file is not meant for user inclusion. Use <locale>.
33 #ifndef _LOCALE_FACETS_TCC
34 #define _LOCALE_FACETS_TCC 1
36 #pragma GCC system_header
39 #include <clocale> // For localeconv
40 #include <cstdlib> // For strof, strtold
41 #include <cmath> // For ceil
42 #include <cctype> // For isspace
43 #include <limits> // For numeric_limits
44 #include <typeinfo> // For bad_cast.
45 #include <bits/streambuf_iterator.h>
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);
77 template<typename _Facet>
79 has_facet(const locale& __loc) throw()
81 size_t __i = _Facet::id._M_id();
82 const locale::facet** __facets = __loc._M_impl->_M_facets;
83 return (__i < __loc._M_impl->_M_facets_size && __facets[__i]);
86 template<typename _Facet>
88 use_facet(const locale& __loc)
90 size_t __i = _Facet::id._M_id();
91 const locale::facet** __facets = __loc._M_impl->_M_facets;
92 if (!(__i < __loc._M_impl->_M_facets_size && __facets[__i]))
94 return static_cast<const _Facet&>(*__facets[__i]);
97 // Routine to access a cache for the facet. If the cache didn't
98 // exist before, it gets constructed on the fly.
99 template<typename _Facet>
103 operator() (const locale& __loc) const;
106 template<typename _CharT>
107 struct __use_cache<__numpunct_cache<_CharT> >
109 const __numpunct_cache<_CharT>*
110 operator() (const locale& __loc) const
112 size_t __i = numpunct<_CharT>::id._M_id();
113 const locale::facet** __caches = __loc._M_impl->_M_caches;
116 __numpunct_cache<_CharT>* __tmp = NULL;
119 __tmp = new __numpunct_cache<_CharT>;
120 __tmp->_M_cache(__loc);
125 __throw_exception_again;
127 __loc._M_impl->_M_install_cache(__tmp, __i);
129 return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]);
133 template<typename _CharT, typename _InIter>
135 num_get<_CharT, _InIter>::
136 _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
137 ios_base::iostate& __err, string& __xtrc) const
139 typedef char_traits<_CharT> __traits_type;
140 typedef typename numpunct<_CharT>::__cache_type __cache_type;
141 __use_cache<__cache_type> __uc;
142 const locale& __loc = __io._M_getloc();
143 const __cache_type* __lc = __uc(__loc);
144 const _CharT* __lit = __lc->_M_atoms_in;
146 // First check for sign.
148 char_type __c = *__beg;
149 const bool __plus = __traits_type::eq(__c, __lit[_S_iplus]);
150 if ((__plus || __traits_type::eq(__c, __lit[_S_iminus]))
153 __xtrc += __plus ? _S_atoms_in[_S_iplus] : _S_atoms_in[_S_iminus];
158 // Next, strip leading zeros.
159 bool __found_zero = false;
160 while (__traits_type::eq(__c, __lit[_S_izero]) && __beg != __end)
167 __xtrc += _S_atoms_in[_S_izero];
171 // Only need acceptable digits for floating point numbers.
172 bool __found_dec = false;
173 bool __found_sci = false;
174 string __found_grouping;
175 const size_t __len = _S_iE - _S_izero + 1;
178 while (__beg != __end)
180 // Only look in digits.
181 const char_type* __p = __traits_type::find(__lit + _S_izero, 10,
184 // NB: strchr returns true for __c == 0x0
185 if (__p && !__traits_type::eq(__c, char_type()))
187 // Try first for acceptable digit; record it if found.
189 __xtrc += _S_atoms_in[__p - __lit];
193 else if (__traits_type::eq(__c, __lc->_M_thousands_sep)
194 && __lc->_M_use_grouping && !__found_dec)
196 // NB: Thousands separator at the beginning of a string
197 // is a no-no, as is two consecutive thousands separators.
200 __found_grouping += static_cast<char>(__sep_pos);
206 __err |= ios_base::failbit;
210 else if (__traits_type::eq(__c, __lc->_M_decimal_point)
213 // According to the standard, if no grouping chars are seen,
214 // no grouping check is applied. Therefore __found_grouping
215 // must be adjusted only if __dec comes after some __sep.
216 if (__found_grouping.size())
217 __found_grouping += static_cast<char>(__sep_pos);
223 else if ((__e = __traits_type::eq(__c, __lit[_S_ie])
224 || __traits_type::eq(__c, __lit[_S_iE]))
225 && !__found_sci && __pos)
227 // Scientific notation.
229 __xtrc += __e ? _S_atoms_in[_S_ie] : _S_atoms_in[_S_iE];
232 // Remove optional plus or minus sign, if they exist.
233 const bool __plus = __traits_type::eq(__c, __lit[_S_iplus]);
234 if (__plus || __traits_type::eq(__c, __lit[_S_iminus]))
237 __xtrc += __plus ? _S_atoms_in[_S_iplus]
238 : _S_atoms_in[_S_iminus];
244 // Not a valid input item.
248 // Digit grouping is checked. If grouping and found_grouping don't
249 // match, then get very very upset, and set failbit.
250 if (__lc->_M_use_grouping && __found_grouping.size())
252 // Add the ending grouping if a decimal wasn't found.
254 __found_grouping += static_cast<char>(__sep_pos);
256 const string __grouping = __lc->_M_grouping;
257 if (!std::__verify_grouping(__grouping, __found_grouping))
258 __err |= ios_base::failbit;
264 __err |= ios_base::eofbit;
268 template<typename _CharT, typename _InIter>
270 num_get<_CharT, _InIter>::
271 _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
272 ios_base::iostate& __err, string& __xtrc, int& __base) const
274 typedef char_traits<_CharT> __traits_type;
275 typedef typename numpunct<_CharT>::__cache_type __cache_type;
276 __use_cache<__cache_type> __uc;
277 const locale& __loc = __io._M_getloc();
278 const __cache_type* __lc = __uc(__loc);
279 const _CharT* __lit = __lc->_M_atoms_in;
281 // NB: Iff __basefield == 0, this can change based on contents.
282 ios_base::fmtflags __basefield = __io.flags() & ios_base::basefield;
283 if (__basefield == ios_base::oct)
285 else if (__basefield == ios_base::hex)
290 // First check for sign.
292 char_type __c = *__beg;
293 const bool __plus = __traits_type::eq(__c, __lit[_S_iplus]);
294 if ((__plus || __traits_type::eq(__c, __lit[_S_iminus]))
297 __xtrc += __plus ? _S_atoms_in[_S_iplus] : _S_atoms_in[_S_iminus];
302 // Next, strip leading zeros and check required digits for base formats.
305 bool __found_zero = false;
306 while (__traits_type::eq(__c, __lit[_S_izero]) && __beg != __end)
313 __xtrc += _S_atoms_in[_S_izero];
315 if (__basefield == 0)
317 const bool __x = __traits_type::eq(__c, __lit[_S_ix]);
318 const bool __X = __traits_type::eq(__c, __lit[_S_iX]);
319 if ((__x || __X) && __beg != __end)
321 __xtrc += __x ? _S_atoms_in[_S_ix] : _S_atoms_in[_S_iX];
331 else if (__base == 16)
333 if (__traits_type::eq(__c, __lit[_S_izero]) && __beg != __end)
335 __xtrc += _S_atoms_in[_S_izero];
339 const bool __x = __traits_type::eq(__c, __lit[_S_ix]);
340 const bool __X = __traits_type::eq(__c, __lit[_S_iX]);
341 if ((__x || __X) && __beg != __end)
343 __xtrc += __x ? _S_atoms_in[_S_ix] : _S_atoms_in[_S_iX];
350 // At this point, base is determined. If not hex, only allow
351 // base digits as valid input.
359 string __found_grouping;
360 const char_type __sep = __lc->_M_thousands_sep;
362 while (__beg != __end)
364 const char_type* __p = __traits_type::find(__lit + _S_izero,
367 // NB: strchr returns true for __c == 0x0
368 if (__p && !__traits_type::eq(__c, char_type()))
370 // Try first for acceptable digit; record it if found.
371 __xtrc += _S_atoms_in[__p - __lit];
376 else if (__traits_type::eq(__c, __sep) && __lc->_M_use_grouping)
378 // NB: Thousands separator at the beginning of a string
379 // is a no-no, as is two consecutive thousands separators.
382 __found_grouping += static_cast<char>(__sep_pos);
388 __err |= ios_base::failbit;
393 // Not a valid input item.
397 // Digit grouping is checked. If grouping and found_grouping don't
398 // match, then get very very upset, and set failbit.
399 if (__lc->_M_use_grouping && __found_grouping.size())
401 // Add the ending grouping.
402 __found_grouping += static_cast<char>(__sep_pos);
404 const string __grouping = __lc->_M_grouping;
405 if (!std::__verify_grouping(__grouping, __found_grouping))
406 __err |= ios_base::failbit;
412 __err |= ios_base::eofbit;
416 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
417 //17. Bad bool parsing
418 template<typename _CharT, typename _InIter>
420 num_get<_CharT, _InIter>::
421 do_get(iter_type __beg, iter_type __end, ios_base& __io,
422 ios_base::iostate& __err, bool& __v) const
424 if (!(__io.flags() & ios_base::boolalpha))
426 // Parse bool values as unsigned long.
427 // NB: We can't just call do_get(long) here, as it might
428 // refer to a derived class.
431 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
434 std::__convert_to_v(__xtrc.c_str(), __ul, __err,
435 _S_get_c_locale(), __base);
436 if (!(__err & ios_base::failbit) && __ul <= 1)
439 __err |= ios_base::failbit;
443 // Parse bool values as alphanumeric.
444 typedef char_traits<_CharT> __traits_type;
445 typedef typename numpunct<_CharT>::__cache_type __cache_type;
446 __use_cache<__cache_type> __uc;
447 const locale& __loc = __io._M_getloc();
448 const __cache_type* __lc = __uc(__loc);
449 const size_t __tn = __traits_type::length(__lc->_M_truename) - 1;
450 const size_t __fn = __traits_type::length(__lc->_M_falsename) - 1;
452 bool __testf = false;
453 bool __testt = false;
454 for (size_t __n = 0; __beg != __end; ++__n)
456 const char_type __c = *__beg;
460 __testf = __traits_type::eq(__c, __lc->_M_falsename[__n]);
463 __testt = __traits_type::eq(__c, __lc->_M_truename[__n]);
465 if (!(__testf || __testt))
467 __err |= ios_base::failbit;
470 else if (__testf && __n == __fn)
475 else if (__testt && __n == __tn)
482 __err |= ios_base::eofbit;
488 template<typename _CharT, typename _InIter>
490 num_get<_CharT, _InIter>::
491 do_get(iter_type __beg, iter_type __end, ios_base& __io,
492 ios_base::iostate& __err, long& __v) const
496 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
497 std::__convert_to_v(__xtrc.c_str(), __v, __err,
498 _S_get_c_locale(), __base);
502 template<typename _CharT, typename _InIter>
504 num_get<_CharT, _InIter>::
505 do_get(iter_type __beg, iter_type __end, ios_base& __io,
506 ios_base::iostate& __err, unsigned short& __v) const
510 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
512 std::__convert_to_v(__xtrc.c_str(), __ul, __err,
513 _S_get_c_locale(), __base);
514 if (!(__err & ios_base::failbit)
515 && __ul <= numeric_limits<unsigned short>::max())
516 __v = static_cast<unsigned short>(__ul);
518 __err |= ios_base::failbit;
522 template<typename _CharT, typename _InIter>
524 num_get<_CharT, _InIter>::
525 do_get(iter_type __beg, iter_type __end, ios_base& __io,
526 ios_base::iostate& __err, unsigned int& __v) const
530 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
532 std::__convert_to_v(__xtrc.c_str(), __ul, __err,
533 _S_get_c_locale(), __base);
534 if (!(__err & ios_base::failbit)
535 && __ul <= numeric_limits<unsigned int>::max())
536 __v = static_cast<unsigned int>(__ul);
538 __err |= ios_base::failbit;
542 template<typename _CharT, typename _InIter>
544 num_get<_CharT, _InIter>::
545 do_get(iter_type __beg, iter_type __end, ios_base& __io,
546 ios_base::iostate& __err, unsigned long& __v) const
550 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
551 std::__convert_to_v(__xtrc.c_str(), __v, __err,
552 _S_get_c_locale(), __base);
556 #ifdef _GLIBCXX_USE_LONG_LONG
557 template<typename _CharT, typename _InIter>
559 num_get<_CharT, _InIter>::
560 do_get(iter_type __beg, iter_type __end, ios_base& __io,
561 ios_base::iostate& __err, long long& __v) const
565 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
566 std::__convert_to_v(__xtrc.c_str(), __v, __err,
567 _S_get_c_locale(), __base);
571 template<typename _CharT, typename _InIter>
573 num_get<_CharT, _InIter>::
574 do_get(iter_type __beg, iter_type __end, ios_base& __io,
575 ios_base::iostate& __err, unsigned long long& __v) const
579 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
580 std::__convert_to_v(__xtrc.c_str(), __v, __err,
581 _S_get_c_locale(), __base);
586 template<typename _CharT, typename _InIter>
588 num_get<_CharT, _InIter>::
589 do_get(iter_type __beg, iter_type __end, ios_base& __io,
590 ios_base::iostate& __err, float& __v) const
594 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
595 std::__convert_to_v(__xtrc.c_str(), __v, __err,
600 template<typename _CharT, typename _InIter>
602 num_get<_CharT, _InIter>::
603 do_get(iter_type __beg, iter_type __end, ios_base& __io,
604 ios_base::iostate& __err, double& __v) const
608 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
609 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
613 template<typename _CharT, typename _InIter>
615 num_get<_CharT, _InIter>::
616 do_get(iter_type __beg, iter_type __end, ios_base& __io,
617 ios_base::iostate& __err, long double& __v) const
621 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
622 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
626 template<typename _CharT, typename _InIter>
628 num_get<_CharT, _InIter>::
629 do_get(iter_type __beg, iter_type __end, ios_base& __io,
630 ios_base::iostate& __err, void*& __v) const
632 // Prepare for hex formatted input.
633 typedef ios_base::fmtflags fmtflags;
634 fmtflags __fmt = __io.flags();
635 fmtflags __fmtmask = ~(ios_base::showpos | ios_base::basefield
636 | ios_base::uppercase | ios_base::internal);
637 __io.flags(__fmt & __fmtmask | (ios_base::hex | ios_base::showbase));
641 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
643 // Reset from hex formatted input.
647 std::__convert_to_v(__xtrc.c_str(), __ul, __err,
648 _S_get_c_locale(), __base);
649 if (!(__err & ios_base::failbit))
650 __v = reinterpret_cast<void*>(__ul);
652 __err |= ios_base::failbit;
656 // For use by integer and floating-point types after they have been
657 // converted into a char_type string.
658 template<typename _CharT, typename _OutIter>
660 num_put<_CharT, _OutIter>::
661 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
662 _CharT* __new, const _CharT* __cs, int& __len) const
664 // [22.2.2.2.2] Stage 3.
665 // If necessary, pad.
666 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new, __cs,
668 __len = static_cast<int>(__w);
671 // Forwarding functions to peel signed from unsigned integer types.
672 template<typename _CharT>
674 __int_to_char(_CharT* __out, const int __size, long __v,
675 const _CharT* __lit, ios_base::fmtflags __flags)
677 unsigned long __ul = static_cast<unsigned long>(__v);
684 return __int_to_char(__out, __size, __ul, __lit, __flags, __neg);
687 template<typename _CharT>
689 __int_to_char(_CharT* __out, const int __size, unsigned long __v,
690 const _CharT* __lit, ios_base::fmtflags __flags)
691 { return __int_to_char(__out, __size, __v, __lit, __flags, false); }
693 #ifdef _GLIBCXX_USE_LONG_LONG
694 template<typename _CharT>
696 __int_to_char(_CharT* __out, const int __size, long long __v,
697 const _CharT* __lit, ios_base::fmtflags __flags)
699 unsigned long long __ull = static_cast<unsigned long long>(__v);
706 return __int_to_char(__out, __size, __ull, __lit, __flags, __neg);
709 template<typename _CharT>
711 __int_to_char(_CharT* __out, const int __size, unsigned long long __v,
712 const _CharT* __lit, ios_base::fmtflags __flags)
713 { return __int_to_char(__out, __size, __v, __lit, __flags, false); }
716 template<typename _CharT, typename _ValueT>
718 __int_to_char(_CharT* __out, const int __size, _ValueT __v,
719 const _CharT* __lit, ios_base::fmtflags __flags, bool __neg)
721 // Don't write base if already 0.
722 const bool __showbase = (__flags & ios_base::showbase) && __v;
723 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
724 _CharT* __buf = __out + __size - 1;
725 _CharT* __bufend = __out + __size;
727 if (__builtin_expect(__basefield != ios_base::oct &&
728 __basefield != ios_base::hex, true))
733 *__buf-- = __lit[(__v % 10) + __num_base::_S_odigits];
738 *__buf-- = __lit[__num_base::_S_ominus];
739 else if (__flags & ios_base::showpos)
740 *__buf-- = __lit[__num_base::_S_oplus];
742 else if (__basefield == ios_base::oct)
747 *__buf-- = __lit[(__v & 0x7) + __num_base::_S_odigits];
752 *__buf-- = __lit[__num_base::_S_odigits];
757 const bool __uppercase = __flags & ios_base::uppercase;
758 int __case_offset = __uppercase ? __num_base::_S_oudigits
759 : __num_base::_S_odigits;
762 *__buf-- = __lit[(__v & 0xf) + __case_offset];
769 *__buf-- = __lit[__num_base::_S_ox + __uppercase];
771 *__buf-- = __lit[__num_base::_S_odigits];
774 int __ret = __bufend - __buf - 1;
778 template<typename _CharT, typename _OutIter>
780 num_put<_CharT, _OutIter>::
781 _M_group_int(const string& __grouping, _CharT __sep, ios_base& __io,
782 _CharT* __new, _CharT* __cs, int& __len) const
784 // By itself __add_grouping cannot deal correctly with __ws when
785 // ios::showbase is set and ios_base::oct || ios_base::hex.
786 // Therefore we take care "by hand" of the initial 0, 0x or 0X.
787 // However, remember that the latter do not occur if the number
788 // printed is '0' (__len == 1).
789 streamsize __off = 0;
790 const ios_base::fmtflags __basefield = __io.flags()
791 & ios_base::basefield;
792 if ((__io.flags() & ios_base::showbase) && __len > 1)
793 if (__basefield == ios_base::oct)
798 else if (__basefield == ios_base::hex)
805 __p = std::__add_grouping(__new + __off, __sep, __grouping.c_str(),
806 __grouping.c_str() + __grouping.size(),
807 __cs + __off, __cs + __len);
811 template<typename _CharT, typename _OutIter>
812 template<typename _ValueT>
814 num_put<_CharT, _OutIter>::
815 _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
818 typedef typename numpunct<_CharT>::__cache_type __cache_type;
819 __use_cache<__cache_type> __uc;
820 const locale& __loc = __io._M_getloc();
821 const __cache_type* __lc = __uc(__loc);
822 const _CharT* __lit = __lc->_M_atoms_out;
824 // Long enough to hold hex, dec, and octal representations.
825 int __ilen = 4 * sizeof(_ValueT);
826 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
828 // [22.2.2.2.2] Stage 1, numeric conversion to character.
829 // Result is returned right-justified in the buffer.
831 __len = __int_to_char(&__cs[0], __ilen, __v, __lit, __io.flags());
832 __cs = __cs + __ilen - __len;
834 // Add grouping, if necessary.
836 if (__lc->_M_use_grouping)
838 // Grouping can add (almost) as many separators as the
839 // number of digits, but no more.
840 __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
842 _M_group_int(__lc->_M_grouping, __lc->_M_thousands_sep, __io,
849 streamsize __w = __io.width();
850 if (__w > static_cast<streamsize>(__len))
852 __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
854 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
859 // [22.2.2.2.2] Stage 4.
860 // Write resulting, fully-formatted string to output iterator.
861 return std::__write(__s, __cs, __len);
864 template<typename _CharT, typename _OutIter>
866 num_put<_CharT, _OutIter>::
867 _M_group_float(const string& __grouping, _CharT __sep, const _CharT* __p,
868 _CharT* __new, _CharT* __cs, int& __len) const
870 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
871 //282. What types does numpunct grouping refer to?
872 // Add grouping, if necessary.
874 int __declen = __p ? __p - __cs : __len;
875 __p2 = std::__add_grouping(__new, __sep, __grouping.c_str(),
876 __grouping.c_str() + __grouping.size(),
877 __cs, __cs + __declen);
879 // Tack on decimal part.
880 int __newlen = __p2 - __new;
883 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
884 __newlen += __len - __declen;
890 // The following code uses snprintf (or sprintf(), when
891 // _GLIBCXX_USE_C99 is not defined) to convert floating point values
892 // for insertion into a stream. An optimization would be to replace
893 // them with code that works directly on a wide buffer and then use
894 // __pad to do the padding. It would be good to replace them anyway
895 // to gain back the efficiency that C++ provides by knowing up front
896 // the type of the values to insert. Also, sprintf is dangerous
897 // since may lead to accidental buffer overruns. This
898 // implementation follows the C++ standard fairly directly as
899 // outlined in 22.2.2.2 [lib.locale.num.put]
900 template<typename _CharT, typename _OutIter>
901 template<typename _ValueT>
903 num_put<_CharT, _OutIter>::
904 _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
907 typedef typename numpunct<_CharT>::__cache_type __cache_type;
908 __use_cache<__cache_type> __uc;
909 const locale& __loc = __io._M_getloc();
910 const __cache_type* __lc = __uc(__loc);
912 // Note: digits10 is rounded down: add 1 to ensure the maximum
913 // available precision. Then, in general, one more 1 needs to
914 // be added since, when the %{g,G} conversion specifiers are
915 // chosen inside _S_format_float, the precision field is "the
916 // maximum number of significant digits", *not* the "number of
917 // digits to appear after the decimal point", as happens for
918 // %{e,E,f,F} (C99, 7.19.6.1,4).
919 const int __max_digits = numeric_limits<_ValueT>::digits10 + 2;
921 // Use default precision if out of range.
922 streamsize __prec = __io.precision();
923 if (__prec > static_cast<streamsize>(__max_digits))
924 __prec = static_cast<streamsize>(__max_digits);
925 else if (__prec < static_cast<streamsize>(0))
926 __prec = static_cast<streamsize>(6);
928 // [22.2.2.2.2] Stage 1, numeric conversion to character.
930 // Long enough for the max format spec.
933 #ifdef _GLIBCXX_USE_C99
934 // First try a buffer perhaps big enough (for sure sufficient
935 // for non-ios_base::fixed outputs)
936 int __cs_size = __max_digits * 3;
937 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
939 _S_format_float(__io, __fbuf, __mod);
940 __len = std::__convert_from_v(__cs, __cs_size, __fbuf, __v,
941 _S_get_c_locale(), __prec);
943 // If the buffer was not large enough, try again with the correct size.
944 if (__len >= __cs_size)
946 __cs_size = __len + 1;
947 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
948 __len = std::__convert_from_v(__cs, __cs_size, __fbuf, __v,
949 _S_get_c_locale(), __prec);
952 // Consider the possibility of long ios_base::fixed outputs
953 const bool __fixed = __io.flags() & ios_base::fixed;
954 const int __max_exp = numeric_limits<_ValueT>::max_exponent10;
956 // The size of the output string is computed as follows.
957 // ios_base::fixed outputs may need up to __max_exp+1 chars
958 // for the integer part + up to __max_digits chars for the
959 // fractional part + 3 chars for sign, decimal point, '\0'. On
960 // the other hand, for non-fixed outputs __max_digits*3 chars
961 // are largely sufficient.
962 const int __cs_size = __fixed ? __max_exp + __max_digits + 4
964 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
966 _S_format_float(__io, __fbuf, __mod);
967 __len = std::__convert_from_v(__cs, 0, __fbuf, __v,
968 _S_get_c_locale(), __prec);
971 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
972 // numpunct.decimal_point() values for '.' and adding grouping.
973 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
975 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
977 __ctype.widen(__cs, __cs + __len, __ws);
979 // Replace decimal point.
980 const _CharT __cdec = __ctype.widen('.');
981 const _CharT __dec = __lc->_M_decimal_point;
983 if (__p = char_traits<_CharT>::find(__ws, __len, __cdec))
984 __ws[__p - __ws] = __dec;
986 // Add grouping, if necessary.
988 if (__lc->_M_use_grouping)
990 // Grouping can add (almost) as many separators as the
991 // number of digits, but no more.
992 __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
994 _M_group_float(__lc->_M_grouping, __lc->_M_thousands_sep, __p,
1001 streamsize __w = __io.width();
1002 if (__w > static_cast<streamsize>(__len))
1004 __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
1005 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
1010 // [22.2.2.2.2] Stage 4.
1011 // Write resulting, fully-formatted string to output iterator.
1012 return std::__write(__s, __ws, __len);
1015 template<typename _CharT, typename _OutIter>
1017 num_put<_CharT, _OutIter>::
1018 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
1020 ios_base::fmtflags __flags = __io.flags();
1021 if ((__flags & ios_base::boolalpha) == 0)
1023 unsigned long __uv = __v;
1024 __s = _M_insert_int(__s, __io, __fill, __uv);
1028 typedef typename numpunct<_CharT>::__cache_type __cache_type;
1029 __use_cache<__cache_type> __uc;
1030 const locale& __loc = __io._M_getloc();
1031 const __cache_type* __lc = __uc(__loc);
1033 const _CharT* __name;
1034 __name = __v ? __lc->_M_truename : __lc->_M_falsename;
1035 int __len = char_traits<_CharT>::length(__name);
1038 streamsize __w = __io.width();
1039 if (__w > static_cast<streamsize>(__len))
1041 __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1043 _M_pad(__fill, __w, __io, __cs, __name, __len);
1047 __s = std::__write(__s, __name, __len);
1052 template<typename _CharT, typename _OutIter>
1054 num_put<_CharT, _OutIter>::
1055 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
1056 { return _M_insert_int(__s, __io, __fill, __v); }
1058 template<typename _CharT, typename _OutIter>
1060 num_put<_CharT, _OutIter>::
1061 do_put(iter_type __s, ios_base& __io, char_type __fill,
1062 unsigned long __v) const
1063 { return _M_insert_int(__s, __io, __fill, __v); }
1065 #ifdef _GLIBCXX_USE_LONG_LONG
1066 template<typename _CharT, typename _OutIter>
1068 num_put<_CharT, _OutIter>::
1069 do_put(iter_type __s, ios_base& __b, char_type __fill, long long __v) const
1070 { return _M_insert_int(__s, __b, __fill, __v); }
1072 template<typename _CharT, typename _OutIter>
1074 num_put<_CharT, _OutIter>::
1075 do_put(iter_type __s, ios_base& __io, char_type __fill,
1076 unsigned long long __v) const
1077 { return _M_insert_int(__s, __io, __fill, __v); }
1080 template<typename _CharT, typename _OutIter>
1082 num_put<_CharT, _OutIter>::
1083 do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1084 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1086 template<typename _CharT, typename _OutIter>
1088 num_put<_CharT, _OutIter>::
1089 do_put(iter_type __s, ios_base& __io, char_type __fill,
1090 long double __v) const
1091 { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1093 template<typename _CharT, typename _OutIter>
1095 num_put<_CharT, _OutIter>::
1096 do_put(iter_type __s, ios_base& __io, char_type __fill,
1097 const void* __v) const
1099 ios_base::fmtflags __flags = __io.flags();
1100 ios_base::fmtflags __fmt = ~(ios_base::showpos | ios_base::basefield
1101 | ios_base::uppercase | ios_base::internal);
1102 __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase));
1105 __s = _M_insert_int(__s, __io, __fill,
1106 reinterpret_cast<unsigned long>(__v));
1107 __io.flags(__flags);
1111 __io.flags(__flags);
1112 __throw_exception_again;
1118 template<typename _CharT, typename _InIter>
1120 money_get<_CharT, _InIter>::
1121 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1122 ios_base::iostate& __err, long double& __units) const
1125 __beg = this->do_get(__beg, __end, __intl, __io, __err, __str);
1127 const int __n = numeric_limits<long double>::digits10;
1128 char* __cs = static_cast<char*>(__builtin_alloca(__n));
1129 const locale __loc = __io.getloc();
1130 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1131 const _CharT* __wcs = __str.c_str();
1132 __ctype.narrow(__wcs, __wcs + __str.size() + 1, char(), __cs);
1133 std::__convert_to_v(__cs, __units, __err, _S_get_c_locale());
1137 template<typename _CharT, typename _InIter>
1139 money_get<_CharT, _InIter>::
1140 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1141 ios_base::iostate& __err, string_type& __units) const
1143 // These contortions are quite unfortunate.
1144 typedef moneypunct<_CharT, true> __money_true;
1145 typedef moneypunct<_CharT, false> __money_false;
1146 typedef money_base::part part;
1147 typedef typename string_type::size_type size_type;
1149 const locale __loc = __io.getloc();
1150 const __money_true& __mpt = use_facet<__money_true>(__loc);
1151 const __money_false& __mpf = use_facet<__money_false>(__loc);
1152 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1154 const money_base::pattern __p = __intl ? __mpt.neg_format()
1155 : __mpf.neg_format();
1157 const string_type __pos_sign =__intl ? __mpt.positive_sign()
1158 : __mpf.positive_sign();
1159 const string_type __neg_sign =__intl ? __mpt.negative_sign()
1160 : __mpf.negative_sign();
1161 const char_type __d = __intl ? __mpt.decimal_point()
1162 : __mpf.decimal_point();
1163 const char_type __sep = __intl ? __mpt.thousands_sep()
1164 : __mpf.thousands_sep();
1166 const string __grouping = __intl ? __mpt.grouping() : __mpf.grouping();
1168 // Set to deduced positive or negative sign, depending.
1170 // String of grouping info from thousands_sep plucked from __units.
1171 string __grouping_tmp;
1172 // Marker for thousands_sep position.
1174 // If input iterator is in a valid state.
1175 bool __testvalid = true;
1176 // Flag marking when a decimal point is found.
1177 bool __testdecfound = false;
1179 // The tentative returned string is stored here.
1180 string_type __tmp_units;
1182 char_type __c = *__beg;
1183 char_type __eof = static_cast<char_type>(char_traits<char_type>::eof());
1184 for (int __i = 0; __beg != __end && __i < 4 && __testvalid; ++__i)
1186 part __which = static_cast<part>(__p.field[__i]);
1189 case money_base::symbol:
1190 if (__io.flags() & ios_base::showbase
1191 || __i < 2 || __sign.size() > 1
1192 || ((static_cast<part>(__p.field[3]) != money_base::none)
1195 // According to 22.2.6.1.2.2, symbol is required
1196 // if (__io.flags() & ios_base::showbase),
1197 // otherwise is optional and consumed only if
1198 // other characters are needed to complete the
1200 const string_type __symbol = __intl ? __mpt.curr_symbol()
1201 : __mpf.curr_symbol();
1202 size_type __len = __symbol.size();
1204 while (__beg != __end
1205 && __j < __len && __symbol[__j] == __c)
1210 // When (__io.flags() & ios_base::showbase)
1211 // symbol is required.
1212 if (__j != __len && (__io.flags() & ios_base::showbase))
1213 __testvalid = false;
1216 case money_base::sign:
1217 // Sign might not exist, or be more than one character long.
1218 if (__pos_sign.size() && __neg_sign.size())
1220 // Sign is mandatory.
1221 if (__c == __pos_sign[0])
1223 __sign = __pos_sign;
1226 else if (__c == __neg_sign[0])
1228 __sign = __neg_sign;
1232 __testvalid = false;
1234 else if (__pos_sign.size() && __c == __pos_sign[0])
1236 __sign = __pos_sign;
1239 else if (__neg_sign.size() && __c == __neg_sign[0])
1241 __sign = __neg_sign;
1245 case money_base::value:
1246 // Extract digits, remove and stash away the
1247 // grouping of found thousands separators.
1248 while (__beg != __end
1249 && (__ctype.is(ctype_base::digit, __c)
1250 || (__c == __d && !__testdecfound)
1255 __grouping_tmp += static_cast<char>(__sep_pos);
1257 __testdecfound = true;
1259 else if (__c == __sep)
1261 if (__grouping.size())
1263 // Mark position for later analysis.
1264 __grouping_tmp += static_cast<char>(__sep_pos);
1269 __testvalid = false;
1281 case money_base::space:
1282 case money_base::none:
1283 // Only if not at the end of the pattern.
1285 while (__beg != __end
1286 && __ctype.is(ctype_base::space, __c))
1292 // Need to get the rest of the sign characters, if they exist.
1293 if (__sign.size() > 1)
1295 size_type __len = __sign.size();
1297 for (; __c != __eof && __i < __len; ++__i)
1298 while (__beg != __end && __c != __sign[__i])
1302 __testvalid = false;
1305 // Strip leading zeros.
1306 while (__tmp_units.size() > 1 && __tmp_units[0] == __ctype.widen('0'))
1307 __tmp_units.erase(__tmp_units.begin());
1309 if (__sign.size() && __sign == __neg_sign)
1310 __tmp_units.insert(__tmp_units.begin(), __ctype.widen('-'));
1312 // Test for grouping fidelity.
1313 if (__grouping.size() && __grouping_tmp.size())
1315 if (!std::__verify_grouping(__grouping, __grouping_tmp))
1316 __testvalid = false;
1319 // Iff no more characters are available.
1321 __err |= ios_base::eofbit;
1323 // Iff not enough digits were supplied after the decimal-point.
1326 const int __frac = __intl ? __mpt.frac_digits()
1327 : __mpf.frac_digits();
1330 if (__sep_pos != __frac)
1331 __testvalid = false;
1335 // Iff valid sequence is not recognized.
1336 if (!__testvalid || !__tmp_units.size())
1337 __err |= ios_base::failbit;
1339 // Use the "swap trick" to copy __tmp_units into __units.
1340 __tmp_units.swap(__units);
1345 template<typename _CharT, typename _OutIter>
1347 money_put<_CharT, _OutIter>::
1348 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1349 long double __units) const
1351 const locale __loc = __io.getloc();
1352 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1353 #ifdef _GLIBCXX_USE_C99
1354 // First try a buffer perhaps big enough.
1356 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1357 int __len = std::__convert_from_v(__cs, __cs_size, "%.01Lf", __units,
1359 // If the buffer was not large enough, try again with the correct size.
1360 if (__len >= __cs_size)
1362 __cs_size = __len + 1;
1363 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1364 __len = std::__convert_from_v(__cs, __cs_size, "%.01Lf", __units,
1368 // max_exponent10 + 1 for the integer part, + 4 for sign, decimal point,
1369 // decimal digit, '\0'.
1370 const int __cs_size = numeric_limits<long double>::max_exponent10 + 5;
1371 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1372 int __len = std::__convert_from_v(__cs, 0, "%.01Lf", __units,
1375 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1377 __ctype.widen(__cs, __cs + __len, __ws);
1378 string_type __digits(__ws);
1379 return this->do_put(__s, __intl, __io, __fill, __digits);
1382 template<typename _CharT, typename _OutIter>
1384 money_put<_CharT, _OutIter>::
1385 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1386 const string_type& __digits) const
1388 typedef typename string_type::size_type size_type;
1389 typedef money_base::part part;
1391 const locale __loc = __io.getloc();
1392 const size_type __width = static_cast<size_type>(__io.width());
1394 // These contortions are quite unfortunate.
1395 typedef moneypunct<_CharT, true> __money_true;
1396 typedef moneypunct<_CharT, false> __money_false;
1397 const __money_true& __mpt = use_facet<__money_true>(__loc);
1398 const __money_false& __mpf = use_facet<__money_false>(__loc);
1399 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1401 // Determine if negative or positive formats are to be used, and
1402 // discard leading negative_sign if it is present.
1403 const char_type* __beg = __digits.data();
1404 const char_type* __end = __beg + __digits.size();
1405 money_base::pattern __p;
1407 if (*__beg != __ctype.widen('-'))
1409 __p = __intl ? __mpt.pos_format() : __mpf.pos_format();
1410 __sign =__intl ? __mpt.positive_sign() : __mpf.positive_sign();
1414 __p = __intl ? __mpt.neg_format() : __mpf.neg_format();
1415 __sign =__intl ? __mpt.negative_sign() : __mpf.negative_sign();
1419 // Look for valid numbers in the current ctype facet within input digits.
1420 __end = __ctype.scan_not(ctype_base::digit, __beg, __end);
1423 // Assume valid input, and attempt to format.
1424 // Break down input numbers into base components, as follows:
1425 // final_value = grouped units + (decimal point) + (digits)
1427 string_type __value;
1428 const string_type __symbol = __intl ? __mpt.curr_symbol()
1429 : __mpf.curr_symbol();
1431 // Deal with decimal point, decimal digits.
1432 const int __frac = __intl ? __mpt.frac_digits()
1433 : __mpf.frac_digits();
1436 const char_type __d = __intl ? __mpt.decimal_point()
1437 : __mpf.decimal_point();
1438 if (__end - __beg >= __frac)
1440 __value = string_type(__end - __frac, __end);
1441 __value.insert(__value.begin(), __d);
1446 // Have to pad zeros in the decimal position.
1447 __value = string_type(__beg, __end);
1448 int __paddec = __frac - (__end - __beg);
1449 char_type __zero = __ctype.widen('0');
1450 __value.insert(__value.begin(), __paddec, __zero);
1451 __value.insert(__value.begin(), __d);
1456 // Add thousands separators to non-decimal digits, per
1460 const string __grouping = __intl ? __mpt.grouping()
1462 if (__grouping.size())
1464 const char_type __sep = __intl ? __mpt.thousands_sep()
1465 : __mpf.thousands_sep();
1466 const char* __gbeg = __grouping.c_str();
1467 const char* __gend = __gbeg + __grouping.size();
1468 const int __n = (__end - __beg) * 2;
1470 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __n));
1471 _CharT* __ws_end = std::__add_grouping(__ws2, __sep, __gbeg,
1472 __gend, __beg, __end);
1473 __value.insert(0, __ws2, __ws_end - __ws2);
1476 __value.insert(0, string_type(__beg, __end));
1479 // Calculate length of resulting string.
1480 ios_base::fmtflags __f = __io.flags() & ios_base::adjustfield;
1481 size_type __len = __value.size() + __sign.size();
1482 __len += (__io.flags() & ios_base::showbase) ? __symbol.size() : 0;
1483 bool __testipad = __f == ios_base::internal && __len < __width;
1485 // Fit formatted digits into the required pattern.
1486 for (int __i = 0; __i < 4; ++__i)
1488 part __which = static_cast<part>(__p.field[__i]);
1491 case money_base::symbol:
1492 if (__io.flags() & ios_base::showbase)
1495 case money_base::sign:
1496 // Sign might not exist, or be more than one
1497 // charater long. In that case, add in the rest
1502 case money_base::value:
1505 case money_base::space:
1506 // At least one space is required, but if internal
1507 // formatting is required, an arbitrary number of
1508 // fill spaces will be necessary.
1510 __res += string_type(__width - __len, __fill);
1512 __res += __ctype.widen(__fill);
1514 case money_base::none:
1516 __res += string_type(__width - __len, __fill);
1521 // Special case of multi-part sign parts.
1522 if (__sign.size() > 1)
1523 __res += string_type(__sign.begin() + 1, __sign.end());
1525 // Pad, if still necessary.
1526 __len = __res.size();
1527 if (__width > __len)
1529 if (__f == ios_base::left)
1531 __res.append(__width - __len, __fill);
1534 __res.insert(0, string_type(__width - __len, __fill));
1538 // Write resulting, fully-formatted string to output iterator.
1539 __s = std::__write(__s, __res.c_str(), __len);
1546 // NB: Not especially useful. Without an ios_base object or some
1547 // kind of locale reference, we are left clawing at the air where
1548 // the side of the mountain used to be...
1549 template<typename _CharT, typename _InIter>
1550 time_base::dateorder
1551 time_get<_CharT, _InIter>::do_date_order() const
1552 { return time_base::no_order; }
1554 template<typename _CharT, typename _InIter>
1556 time_get<_CharT, _InIter>::
1557 _M_extract_via_format(iter_type& __beg, iter_type& __end, ios_base& __io,
1558 ios_base::iostate& __err, tm* __tm,
1559 const _CharT* __format) const
1561 locale __loc = __io.getloc();
1562 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1563 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1564 size_t __len = char_traits<_CharT>::length(__format);
1566 for (size_t __i = 0; __beg != __end && __i < __len && !__err; ++__i)
1568 char __c = __format[__i];
1571 // Verify valid formatting code, attempt to extract.
1572 __c = __format[++__i];
1575 if (__c == 'E' || __c == 'O')
1578 __c = __format[++__i];
1585 // Abbreviated weekday name [tm_wday]
1586 const char_type* __days1[7];
1587 __tp._M_days_abbreviated(__days1);
1588 _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 7,
1592 // Weekday name [tm_wday].
1593 const char_type* __days2[7];
1594 __tp._M_days(__days2);
1595 _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 7,
1600 // Abbreviated month name [tm_mon]
1601 const char_type* __months1[12];
1602 __tp._M_months_abbreviated(__months1);
1603 _M_extract_name(__beg, __end, __tm->tm_mon, __months1, 12,
1607 // Month name [tm_mon].
1608 const char_type* __months2[12];
1609 __tp._M_months(__months2);
1610 _M_extract_name(__beg, __end, __tm->tm_mon, __months2, 12,
1614 // Default time and date representation.
1615 const char_type* __dt[2];
1616 __tp._M_date_time_formats(__dt);
1617 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1621 // Day [01, 31]. [tm_mday]
1622 _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2,
1626 // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
1628 __ctype.widen(__cs, __cs + 9, __wcs);
1629 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1633 // Hour [00, 23]. [tm_hour]
1634 _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
1638 // Hour [01, 12]. [tm_hour]
1639 _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2,
1643 // Month [01, 12]. [tm_mon]
1644 _M_extract_num(__beg, __end, __mem, 1, 12, 2, __ctype,
1647 __tm->tm_mon = __mem - 1;
1650 // Minute [00, 59]. [tm_min]
1651 _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
1655 if (__ctype.narrow(*__beg, 0) == '\n')
1658 __err |= ios_base::failbit;
1661 // Equivalent to (%H:%M).
1663 __ctype.widen(__cs, __cs + 6, __wcs);
1664 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1669 _M_extract_num(__beg, __end, __tm->tm_sec, 0, 59, 2,
1673 if (__ctype.narrow(*__beg, 0) == '\t')
1676 __err |= ios_base::failbit;
1679 // Equivalent to (%H:%M:%S).
1681 __ctype.widen(__cs, __cs + 9, __wcs);
1682 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1687 const char_type* __dates[2];
1688 __tp._M_date_formats(__dates);
1689 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1694 const char_type* __times[2];
1695 __tp._M_time_formats(__times);
1696 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1700 // Two digit year. [tm_year]
1701 _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2,
1705 // Year [1900). [tm_year]
1706 _M_extract_num(__beg, __end, __mem, 0,
1707 numeric_limits<int>::max(), 4,
1710 __tm->tm_year = __mem - 1900;
1714 if (__ctype.is(ctype_base::upper, *__beg))
1717 _M_extract_name(__beg, __end, __tmp,
1718 __timepunct_cache<_CharT>::_S_timezones,
1721 // GMT requires special effort.
1722 char_type __c = *__beg;
1723 if (!__err && __tmp == 0
1724 && (__c == __ctype.widen('-')
1725 || __c == __ctype.widen('+')))
1727 _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
1729 _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
1734 __err |= ios_base::failbit;
1738 __err |= ios_base::failbit;
1743 // Verify format and input match, extract and discard.
1744 if (__c == __ctype.narrow(*__beg, 0))
1747 __err |= ios_base::failbit;
1752 template<typename _CharT, typename _InIter>
1754 time_get<_CharT, _InIter>::
1755 _M_extract_num(iter_type& __beg, iter_type& __end, int& __member,
1756 int __min, int __max, size_t __len,
1757 const ctype<_CharT>& __ctype,
1758 ios_base::iostate& __err) const
1762 bool __testvalid = true;
1763 char_type __c = *__beg;
1764 while (__beg != __end && __i < __len
1765 && __ctype.is(ctype_base::digit, __c))
1767 __digits += __ctype.narrow(__c, 0);
1773 int __value = atoi(__digits.c_str());
1774 if (__min <= __value && __value <= __max)
1777 __testvalid = false;
1780 __testvalid = false;
1782 __err |= ios_base::failbit;
1786 // All elements in __names are unique.
1787 template<typename _CharT, typename _InIter>
1789 time_get<_CharT, _InIter>::
1790 _M_extract_name(iter_type& __beg, iter_type& __end, int& __member,
1791 const _CharT** __names, size_t __indexlen,
1792 ios_base::iostate& __err) const
1794 typedef char_traits<_CharT> __traits_type;
1795 int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int)
1797 size_t __nmatches = 0;
1799 bool __testvalid = true;
1800 const char_type* __name;
1802 char_type __c = *__beg;
1803 // Look for initial matches.
1804 for (size_t __i1 = 0; __i1 < __indexlen; ++__i1)
1805 if (__c == __names[__i1][0])
1806 __matches[__nmatches++] = __i1;
1808 while (__nmatches > 1)
1810 // Find smallest matching string.
1811 size_t __minlen = 10;
1812 for (size_t __i2 = 0; __i2 < __nmatches; ++__i2)
1813 __minlen = std::min(__minlen,
1814 __traits_type::length(__names[__matches[__i2]]));
1816 if (__pos < __minlen && __beg != __end)
1820 for (size_t __i3 = 0; __i3 < __nmatches; ++__i3)
1822 __name = __names[__matches[__i3]];
1823 if (__name[__pos] != __c)
1824 __matches[__i3] = __matches[--__nmatches];
1831 if (__nmatches == 1)
1833 // Make sure found name is completely extracted.
1834 __name = __names[__matches[0]];
1835 const size_t __len = __traits_type::length(__name);
1836 while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
1840 __member = __matches[0];
1842 __testvalid = false;
1845 __testvalid = false;
1847 __err |= ios_base::failbit;
1850 template<typename _CharT, typename _InIter>
1852 time_get<_CharT, _InIter>::
1853 do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
1854 ios_base::iostate& __err, tm* __tm) const
1857 const char* __cs = "%X";
1858 locale __loc = __io.getloc();
1859 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1860 __ctype.widen(__cs, __cs + 3, __wcs);
1861 _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1863 __err |= ios_base::eofbit;
1867 template<typename _CharT, typename _InIter>
1869 time_get<_CharT, _InIter>::
1870 do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
1871 ios_base::iostate& __err, tm* __tm) const
1874 const char* __cs = "%x";
1875 locale __loc = __io.getloc();
1876 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1877 __ctype.widen(__cs, __cs + 3, __wcs);
1878 _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1880 __err |= ios_base::eofbit;
1884 template<typename _CharT, typename _InIter>
1886 time_get<_CharT, _InIter>::
1887 do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
1888 ios_base::iostate& __err, tm* __tm) const
1890 typedef char_traits<_CharT> __traits_type;
1891 locale __loc = __io.getloc();
1892 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1893 const char_type* __days[7];
1894 __tp._M_days_abbreviated(__days);
1896 _M_extract_name(__beg, __end, __tmpwday, __days, 7, __err);
1898 // Check to see if non-abbreviated name exists, and extract.
1899 // NB: Assumes both _M_days and _M_days_abbreviated organized in
1900 // exact same order, first to last, such that the resulting
1901 // __days array with the same index points to a day, and that
1902 // day's abbreviated form.
1903 // NB: Also assumes that an abbreviated name is a subset of the name.
1906 size_t __pos = __traits_type::length(__days[__tmpwday]);
1907 __tp._M_days(__days);
1908 const char_type* __name = __days[__tmpwday];
1909 if (__name[__pos] == *__beg)
1911 // Extract the rest of it.
1912 const size_t __len = __traits_type::length(__name);
1913 while (__pos < __len && __beg != __end
1914 && __name[__pos] == *__beg)
1917 __err |= ios_base::failbit;
1920 __tm->tm_wday = __tmpwday;
1923 __err |= ios_base::eofbit;
1927 template<typename _CharT, typename _InIter>
1929 time_get<_CharT, _InIter>::
1930 do_get_monthname(iter_type __beg, iter_type __end,
1931 ios_base& __io, ios_base::iostate& __err, tm* __tm) const
1933 typedef char_traits<_CharT> __traits_type;
1934 locale __loc = __io.getloc();
1935 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1936 const char_type* __months[12];
1937 __tp._M_months_abbreviated(__months);
1939 _M_extract_name(__beg, __end, __tmpmon, __months, 12, __err);
1941 // Check to see if non-abbreviated name exists, and extract.
1942 // NB: Assumes both _M_months and _M_months_abbreviated organized in
1943 // exact same order, first to last, such that the resulting
1944 // __months array with the same index points to a month, and that
1945 // month's abbreviated form.
1946 // NB: Also assumes that an abbreviated name is a subset of the name.
1949 size_t __pos = __traits_type::length(__months[__tmpmon]);
1950 __tp._M_months(__months);
1951 const char_type* __name = __months[__tmpmon];
1952 if (__name[__pos] == *__beg)
1954 // Extract the rest of it.
1955 const size_t __len = __traits_type::length(__name);
1956 while (__pos < __len && __beg != __end
1957 && __name[__pos] == *__beg)
1960 __err |= ios_base::failbit;
1963 __tm->tm_mon = __tmpmon;
1967 __err |= ios_base::eofbit;
1971 template<typename _CharT, typename _InIter>
1973 time_get<_CharT, _InIter>::
1974 do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
1975 ios_base::iostate& __err, tm* __tm) const
1977 locale __loc = __io.getloc();
1978 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1980 char_type __c = *__beg;
1983 while (__i < 4 && __beg != __end && __ctype.is(ctype_base::digit, __c))
1985 __digits += __ctype.narrow(__c, 0);
1989 if (__i == 2 || __i == 4)
1992 std::__convert_to_v(__digits.c_str(), __l, __err,
1994 if (!(__err & ios_base::failbit) && __l <= INT_MAX)
1996 __l = __i == 2 ? __l : __l - 1900;
1997 __tm->tm_year = static_cast<int>(__l);
2001 __err |= ios_base::failbit;
2003 __err |= ios_base::eofbit;
2007 template<typename _CharT, typename _OutIter>
2009 time_put<_CharT, _OutIter>::
2010 put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
2011 const _CharT* __beg, const _CharT* __end) const
2013 locale __loc = __io.getloc();
2014 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2015 while (__beg != __end)
2017 const _CharT __tmp = *__beg;
2019 if (__ctype.narrow(__tmp, 0) == '%' && __beg != __end)
2023 const char __c = __ctype.narrow(*__beg, 0);
2025 if (__c == 'E' || __c == 'O')
2028 __format = __ctype.narrow(*__beg, 0);
2033 __s = this->do_put(__s, __io, __fill, __tm, __format, __mod);
2044 template<typename _CharT, typename _OutIter>
2046 time_put<_CharT, _OutIter>::
2047 do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
2048 char __format, char __mod) const
2050 locale __loc = __io.getloc();
2051 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2052 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
2054 // NB: This size is arbitrary. Should this be a data member,
2055 // initialized at construction?
2056 const size_t __maxlen = 64;
2057 char_type* __res = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen));
2059 // NB: In IEE 1003.1-200x, and perhaps other locale models, it
2060 // is possible that the format character will be longer than one
2061 // character. Possibilities include 'E' or 'O' followed by a
2062 // format character: if __mod is not the default argument, assume
2063 // it's a valid modifier.
2065 __fmt[0] = __ctype.widen('%');
2068 __fmt[1] = __format;
2069 __fmt[2] = char_type();
2074 __fmt[2] = __format;
2075 __fmt[3] = char_type();
2078 __tp._M_put(__res, __maxlen, __fmt, __tm);
2080 // Write resulting, fully-formatted string to output iterator.
2081 return std::__write(__s, __res, char_traits<char_type>::length(__res));
2085 // Generic version does nothing.
2086 template<typename _CharT>
2088 collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
2091 // Generic version does nothing.
2092 template<typename _CharT>
2094 collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
2097 template<typename _CharT>
2100 do_compare(const _CharT* __lo1, const _CharT* __hi1,
2101 const _CharT* __lo2, const _CharT* __hi2) const
2103 // strcoll assumes zero-terminated strings so we make a copy
2104 // and then put a zero at the end.
2105 const string_type __one(__lo1, __hi1);
2106 const string_type __two(__lo2, __hi2);
2108 const _CharT* __p = __one.c_str();
2109 const _CharT* __pend = __one.c_str() + __one.length();
2110 const _CharT* __q = __two.c_str();
2111 const _CharT* __qend = __two.c_str() + __two.length();
2113 // strcoll stops when it sees a nul character so we break
2114 // the strings into zero-terminated substrings and pass those
2118 int __res = _M_compare(__p, __q);
2122 __p += char_traits<_CharT>::length(__p);
2123 __q += char_traits<_CharT>::length(__q);
2124 if (__p == __pend && __q == __qend)
2126 else if (__p == __pend)
2128 else if (__q == __qend)
2136 template<typename _CharT>
2137 typename collate<_CharT>::string_type
2139 do_transform(const _CharT* __lo, const _CharT* __hi) const
2141 // strxfrm assumes zero-terminated strings so we make a copy
2142 string_type __str(__lo, __hi);
2144 const _CharT* __p = __str.c_str();
2145 const _CharT* __pend = __str.c_str() + __str.length();
2147 size_t __len = (__hi - __lo) * 2;
2151 // strxfrm stops when it sees a nul character so we break
2152 // the string into zero-terminated substrings and pass those
2156 // First try a buffer perhaps big enough.
2158 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len));
2159 size_t __res = _M_transform(__c, __p, __len);
2160 // If the buffer was not large enough, try again with the
2165 __c = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
2167 __res = _M_transform(__c, __p, __res + 1);
2170 __ret.append(__c, __res);
2171 __p += char_traits<_CharT>::length(__p);
2176 __ret.push_back(_CharT());
2180 template<typename _CharT>
2183 do_hash(const _CharT* __lo, const _CharT* __hi) const
2185 unsigned long __val = 0;
2186 for (; __lo < __hi; ++__lo)
2187 __val = *__lo + ((__val << 7) |
2188 (__val >> (numeric_limits<unsigned long>::digits - 7)));
2189 return static_cast<long>(__val);
2192 // Construct correctly padded string, as per 22.2.2.2.2
2194 // __newlen > __oldlen
2195 // __news is allocated for __newlen size
2196 // Used by both num_put and ostream inserters: if __num,
2197 // internal-adjusted objects are padded according to the rules below
2198 // concerning 0[xX] and +-, otherwise, exactly as right-adjusted
2201 // NB: Of the two parameters, _CharT can be deduced from the
2202 // function arguments. The other (_Traits) has to be explicitly specified.
2203 template<typename _CharT, typename _Traits>
2205 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
2206 _CharT* __news, const _CharT* __olds,
2207 const streamsize __newlen,
2208 const streamsize __oldlen, const bool __num)
2210 const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
2211 const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
2214 if (__adjust == ios_base::left)
2216 _Traits::copy(__news, const_cast<_CharT*>(__olds), __oldlen);
2217 _Traits::assign(__news + __oldlen, __plen, __fill);
2222 if (__adjust == ios_base::internal && __num)
2224 // Pad after the sign, if there is one.
2225 // Pad after 0[xX], if there is one.
2226 // Who came up with these rules, anyway? Jeeze.
2227 const locale& __loc = __io._M_getloc();
2228 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2229 const _CharT __minus = __ctype.widen('-');
2230 const _CharT __plus = __ctype.widen('+');
2231 const bool __testsign = _Traits::eq(__olds[0], __minus)
2232 || _Traits::eq(__olds[0], __plus);
2234 const bool __testhex = _Traits::eq(__ctype.widen('0'), __olds[0])
2235 && (_Traits::eq(__ctype.widen('x'), __olds[1])
2236 || _Traits::eq(__ctype.widen('X'), __olds[1]));
2239 __news[0] = __olds[0];
2240 __news[1] = __olds[1];
2244 else if (__testsign)
2246 __news[0] = __olds[0];
2250 // else Padding first.
2252 _Traits::assign(__news, __plen, __fill);
2253 _Traits::copy(__news + __plen, const_cast<_CharT*>(__olds + __mod),
2257 template<typename _CharT>
2259 __verify_grouping(const basic_string<_CharT>& __grouping,
2260 basic_string<_CharT>& __grouping_tmp)
2264 const size_t __len = __grouping.size();
2265 const size_t __n = __grouping_tmp.size();
2268 // Parsed number groupings have to match the
2269 // numpunct::grouping string exactly, starting at the
2270 // right-most point of the parsed sequence of elements ...
2271 while (__test && __i < __n - 1)
2272 for (__j = 0; __test && __j < __len && __i < __n - 1; ++__j,++__i)
2273 __test &= __grouping[__j] == __grouping_tmp[__n - __i - 1];
2274 // ... but the last parsed grouping can be <= numpunct
2276 __j == __len ? __j = 0 : __j;
2277 __test &= __grouping[__j] >= __grouping_tmp[__n - __i - 1];
2281 template<typename _CharT>
2283 __add_grouping(_CharT* __s, _CharT __sep,
2284 const char* __gbeg, const char* __gend,
2285 const _CharT* __first, const _CharT* __last)
2287 if (__last - __first > *__gbeg)
2289 const bool __bump = __gbeg + 1 != __gend;
2290 __s = std::__add_grouping(__s, __sep, __gbeg + __bump,
2291 __gend, __first, __last - *__gbeg);
2292 __first = __last - *__gbeg;
2296 *__s++ = *__first++;
2297 while (__first != __last);
2301 // Inhibit implicit instantiations for required instantiations,
2302 // which are defined via explicit instantiations elsewhere.
2303 // NB: This syntax is a GNU extension.
2304 #if _GLIBCXX_EXTERN_TEMPLATE
2305 extern template class moneypunct<char, false>;
2306 extern template class moneypunct<char, true>;
2307 extern template class moneypunct_byname<char, false>;
2308 extern template class moneypunct_byname<char, true>;
2309 extern template class money_get<char>;
2310 extern template class money_put<char>;
2311 extern template class numpunct<char>;
2312 extern template class numpunct_byname<char>;
2313 extern template class num_get<char>;
2314 extern template class num_put<char>;
2315 extern template class __timepunct<char>;
2316 extern template class time_put<char>;
2317 extern template class time_put_byname<char>;
2318 extern template class time_get<char>;
2319 extern template class time_get_byname<char>;
2320 extern template class messages<char>;
2321 extern template class messages_byname<char>;
2322 extern template class ctype_byname<char>;
2323 extern template class codecvt_byname<char, char, mbstate_t>;
2324 extern template class collate<char>;
2325 extern template class collate_byname<char>;
2328 const codecvt<char, char, mbstate_t>&
2329 use_facet<codecvt<char, char, mbstate_t> >(const locale&);
2332 const collate<char>&
2333 use_facet<collate<char> >(const locale&);
2336 const numpunct<char>&
2337 use_facet<numpunct<char> >(const locale&);
2340 const num_put<char>&
2341 use_facet<num_put<char> >(const locale&);
2344 const num_get<char>&
2345 use_facet<num_get<char> >(const locale&);
2348 const moneypunct<char, true>&
2349 use_facet<moneypunct<char, true> >(const locale&);
2352 const moneypunct<char, false>&
2353 use_facet<moneypunct<char, false> >(const locale&);
2356 const money_put<char>&
2357 use_facet<money_put<char> >(const locale&);
2360 const money_get<char>&
2361 use_facet<money_get<char> >(const locale&);
2364 const __timepunct<char>&
2365 use_facet<__timepunct<char> >(const locale&);
2368 const time_put<char>&
2369 use_facet<time_put<char> >(const locale&);
2372 const time_get<char>&
2373 use_facet<time_get<char> >(const locale&);
2376 const messages<char>&
2377 use_facet<messages<char> >(const locale&);
2381 has_facet<ctype<char> >(const locale&);
2385 has_facet<codecvt<char, char, mbstate_t> >(const locale&);
2389 has_facet<collate<char> >(const locale&);
2393 has_facet<numpunct<char> >(const locale&);
2397 has_facet<num_put<char> >(const locale&);
2401 has_facet<num_get<char> >(const locale&);
2405 has_facet<moneypunct<char> >(const locale&);
2409 has_facet<money_put<char> >(const locale&);
2413 has_facet<money_get<char> >(const locale&);
2417 has_facet<__timepunct<char> >(const locale&);
2421 has_facet<time_put<char> >(const locale&);
2425 has_facet<time_get<char> >(const locale&);
2429 has_facet<messages<char> >(const locale&);
2431 #ifdef _GLIBCXX_USE_WCHAR_T
2432 extern template class moneypunct<wchar_t, false>;
2433 extern template class moneypunct<wchar_t, true>;
2434 extern template class moneypunct_byname<wchar_t, false>;
2435 extern template class moneypunct_byname<wchar_t, true>;
2436 extern template class money_get<wchar_t>;
2437 extern template class money_put<wchar_t>;
2438 extern template class numpunct<wchar_t>;
2439 extern template class numpunct_byname<wchar_t>;
2440 extern template class num_get<wchar_t>;
2441 extern template class num_put<wchar_t>;
2442 extern template class __timepunct<wchar_t>;
2443 extern template class time_put<wchar_t>;
2444 extern template class time_put_byname<wchar_t>;
2445 extern template class time_get<wchar_t>;
2446 extern template class time_get_byname<wchar_t>;
2447 extern template class messages<wchar_t>;
2448 extern template class messages_byname<wchar_t>;
2449 extern template class ctype_byname<wchar_t>;
2450 extern template class codecvt_byname<wchar_t, char, mbstate_t>;
2451 extern template class collate<wchar_t>;
2452 extern template class collate_byname<wchar_t>;
2455 const codecvt<wchar_t, char, mbstate_t>&
2456 use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&);
2459 const collate<wchar_t>&
2460 use_facet<collate<wchar_t> >(const locale&);
2463 const numpunct<wchar_t>&
2464 use_facet<numpunct<wchar_t> >(const locale&);
2467 const num_put<wchar_t>&
2468 use_facet<num_put<wchar_t> >(const locale&);
2471 const num_get<wchar_t>&
2472 use_facet<num_get<wchar_t> >(const locale&);
2475 const moneypunct<wchar_t, true>&
2476 use_facet<moneypunct<wchar_t, true> >(const locale&);
2479 const moneypunct<wchar_t, false>&
2480 use_facet<moneypunct<wchar_t, false> >(const locale&);
2483 const money_put<wchar_t>&
2484 use_facet<money_put<wchar_t> >(const locale&);
2487 const money_get<wchar_t>&
2488 use_facet<money_get<wchar_t> >(const locale&);
2491 const __timepunct<wchar_t>&
2492 use_facet<__timepunct<wchar_t> >(const locale&);
2495 const time_put<wchar_t>&
2496 use_facet<time_put<wchar_t> >(const locale&);
2499 const time_get<wchar_t>&
2500 use_facet<time_get<wchar_t> >(const locale&);
2503 const messages<wchar_t>&
2504 use_facet<messages<wchar_t> >(const locale&);
2508 has_facet<ctype<wchar_t> >(const locale&);
2512 has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
2516 has_facet<collate<wchar_t> >(const locale&);
2520 has_facet<numpunct<wchar_t> >(const locale&);
2524 has_facet<num_put<wchar_t> >(const locale&);
2528 has_facet<num_get<wchar_t> >(const locale&);
2532 has_facet<moneypunct<wchar_t> >(const locale&);
2536 has_facet<money_put<wchar_t> >(const locale&);
2540 has_facet<money_get<wchar_t> >(const locale&);
2544 has_facet<__timepunct<wchar_t> >(const locale&);
2548 has_facet<time_put<wchar_t> >(const locale&);
2552 has_facet<time_get<wchar_t> >(const locale&);
2556 has_facet<messages<wchar_t> >(const locale&);