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);
54 __tmp->_M_replace_facet(__other._M_impl, &_Facet::id);
58 template<typename _CharT, typename _Traits, typename _Alloc>
60 locale::operator()(const basic_string<_CharT, _Traits, _Alloc>& __s1,
61 const basic_string<_CharT, _Traits, _Alloc>& __s2) const
63 typedef std::collate<_CharT> __collate_type;
64 const __collate_type& __collate = use_facet<__collate_type>(*this);
65 return (__collate.compare(__s1.data(), __s1.data() + __s1.length(),
66 __s2.data(), __s2.data() + __s2.length()) < 0);
69 template<typename _Facet>
71 has_facet(const locale& __loc) throw()
73 size_t __i = _Facet::id._M_id();
74 const locale::facet** __facets = __loc._M_impl->_M_facets;
75 return (__i < __loc._M_impl->_M_facets_size && __facets[__i]);
78 template<typename _Facet>
80 use_facet(const locale& __loc)
82 size_t __i = _Facet::id._M_id();
83 const locale::facet** __facets = __loc._M_impl->_M_facets;
84 if (!(__i < __loc._M_impl->_M_facets_size && __facets[__i]))
86 return static_cast<const _Facet&>(*__facets[__i]);
89 // Routine to access a cache for the facet. If the cache didn't
90 // exist before, it gets constructed on the fly.
91 template<typename _Facet>
93 __use_cache(const locale& __loc);
96 const __numpunct_cache<char>&
97 __use_cache(const locale& __loc);
99 #ifdef _GLIBCXX_USE_WCHAR_T
101 const __numpunct_cache<wchar_t>&
102 __use_cache(const locale& __loc);
105 // Stage 1: Determine a conversion specifier.
106 template<typename _CharT, typename _InIter>
108 num_get<_CharT, _InIter>::
109 _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
110 ios_base::iostate& __err, string& __xtrc) const
112 typedef char_traits<_CharT> __traits_type;
113 const locale __loc = __io.getloc();
114 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
115 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
117 // First check for sign.
118 const char_type __plus = __ctype.widen('+');
119 const char_type __minus = __ctype.widen('-');
121 char_type __c = *__beg;
122 if ((__traits_type::eq(__c, __plus) || __traits_type::eq(__c, __minus))
125 __xtrc += __ctype.narrow(__c, char());
130 // Next, strip leading zeros.
131 const char_type __zero = __ctype.widen(_S_atoms_in[_S_izero]);
132 bool __found_zero = false;
133 while (__traits_type::eq(__c, __zero) && __beg != __end)
140 __xtrc += _S_atoms_in[_S_izero];
144 // Only need acceptable digits for floating point numbers.
145 const size_t __len = _S_iE - _S_izero + 1;
146 char_type __watoms[__len];
147 __ctype.widen(_S_atoms_in, _S_atoms_in + __len, __watoms);
148 bool __found_dec = false;
149 bool __found_sci = false;
150 const char_type __dec = __np.decimal_point();
152 string __found_grouping;
153 const string __grouping = __np.grouping();
154 bool __check_grouping = __grouping.size();
156 const char_type __sep = __np.thousands_sep();
158 while (__beg != __end)
160 // Only look in digits.
161 const char_type* __p = __traits_type::find(__watoms, 10, __c);
163 // NB: strchr returns true for __c == 0x0
164 if (__p && !__traits_type::eq(__c, char_type()))
166 // Try first for acceptable digit; record it if found.
168 __xtrc += _S_atoms_in[__p - __watoms];
172 else if (__traits_type::eq(__c, __sep)
173 && __check_grouping && !__found_dec)
175 // NB: Thousands separator at the beginning of a string
176 // is a no-no, as is two consecutive thousands separators.
179 __found_grouping += static_cast<char>(__sep_pos);
185 __err |= ios_base::failbit;
189 else if (__traits_type::eq(__c, __dec) && !__found_dec)
191 // According to the standard, if no grouping chars are seen,
192 // no grouping check is applied. Therefore __found_grouping
193 // must be adjusted only if __dec comes after some __sep.
194 if (__found_grouping.size())
195 __found_grouping += static_cast<char>(__sep_pos);
201 else if ((__traits_type::eq(__c, __watoms[_S_ie])
202 || __traits_type::eq(__c, __watoms[_S_iE]))
203 && !__found_sci && __pos)
205 // Scientific notation.
207 __xtrc += __ctype.narrow(__c, char());
210 // Remove optional plus or minus sign, if they exist.
211 if (__traits_type::eq(__c, __plus)
212 || __traits_type::eq(__c, __minus))
215 __xtrc += __ctype.narrow(__c, char());
221 // Not a valid input item.
225 // Digit grouping is checked. If grouping and found_grouping don't
226 // match, then get very very upset, and set failbit.
227 if (__check_grouping && __found_grouping.size())
229 // Add the ending grouping if a decimal wasn't found.
231 __found_grouping += static_cast<char>(__sep_pos);
232 if (!__verify_grouping(__grouping, __found_grouping))
233 __err |= ios_base::failbit;
239 __err |= ios_base::eofbit;
243 // Stage 1: Determine a conversion specifier.
244 template<typename _CharT, typename _InIter>
246 num_get<_CharT, _InIter>::
247 _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
248 ios_base::iostate& __err, string& __xtrc, int& __base) const
250 typedef char_traits<_CharT> __traits_type;
251 const locale __loc = __io.getloc();
252 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
253 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
255 // NB: Iff __basefield == 0, this can change based on contents.
256 ios_base::fmtflags __basefield = __io.flags() & ios_base::basefield;
257 if (__basefield == ios_base::oct)
259 else if (__basefield == ios_base::hex)
264 // First check for sign.
266 char_type __c = *__beg;
267 const char_type __plus = __ctype.widen('+');
268 const char_type __minus = __ctype.widen('-');
270 if ((__traits_type::eq(__c, __plus) || __traits_type::eq(__c, __minus))
273 __xtrc += __ctype.narrow(__c, char());
278 // Next, strip leading zeros and check required digits for base formats.
279 const char_type __zero = __ctype.widen(_S_atoms_in[_S_izero]);
280 const char_type __x = __ctype.widen('x');
281 const char_type __X = __ctype.widen('X');
284 bool __found_zero = false;
285 while (__traits_type::eq(__c, __zero) && __beg != __end)
292 __xtrc += _S_atoms_in[_S_izero];
294 if (__basefield == 0)
296 if ((__traits_type::eq(__c, __x)
297 || __traits_type::eq(__c, __X))
300 __xtrc += __ctype.narrow(__c, char());
310 else if (__base == 16)
312 if (__traits_type::eq(__c, __zero) && __beg != __end)
314 __xtrc += _S_atoms_in[_S_izero];
317 if ((__traits_type::eq(__c, __x) || __traits_type::eq(__c, __X))
320 __xtrc += __ctype.narrow(__c, char());
327 // At this point, base is determined. If not hex, only allow
328 // base digits as valid input.
336 char_type __watoms[_S_iend];
337 __ctype.widen(_S_atoms_in, _S_atoms_in + __len, __watoms);
338 string __found_grouping;
339 const string __grouping = __np.grouping();
340 bool __check_grouping = __grouping.size();
342 const char_type __sep = __np.thousands_sep();
343 while (__beg != __end)
345 const char_type* __p = __traits_type::find(__watoms, __len, __c);
347 // NB: strchr returns true for __c == 0x0
348 if (__p && !__traits_type::eq(__c, char_type()))
350 // Try first for acceptable digit; record it if found.
351 __xtrc += _S_atoms_in[__p - __watoms];
356 else if (__traits_type::eq(__c, __sep) && __check_grouping)
358 // NB: Thousands separator at the beginning of a string
359 // is a no-no, as is two consecutive thousands separators.
362 __found_grouping += static_cast<char>(__sep_pos);
368 __err |= ios_base::failbit;
373 // Not a valid input item.
377 // Digit grouping is checked. If grouping and found_grouping don't
378 // match, then get very very upset, and set failbit.
379 if (__check_grouping && __found_grouping.size())
381 // Add the ending grouping.
382 __found_grouping += static_cast<char>(__sep_pos);
383 if (!__verify_grouping(__grouping, __found_grouping))
384 __err |= ios_base::failbit;
390 __err |= ios_base::eofbit;
394 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
395 //17. Bad bool parsing
396 template<typename _CharT, typename _InIter>
398 num_get<_CharT, _InIter>::
399 do_get(iter_type __beg, iter_type __end, ios_base& __io,
400 ios_base::iostate& __err, bool& __v) const
402 // Parse bool values as unsigned long
403 if (!(__io.flags() & ios_base::boolalpha))
405 // NB: We can't just call do_get(long) here, as it might
406 // refer to a derived class.
409 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
412 __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
413 if (!(__err & ios_base::failbit) && __ul <= 1)
416 __err |= ios_base::failbit;
419 // Parse bool values as alphanumeric
422 typedef char_traits<_CharT> __traits_type;
423 typedef basic_string<_CharT> __string_type;
425 locale __loc = __io.getloc();
426 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
427 const __string_type __true = __np.truename();
428 const __string_type __false = __np.falsename();
429 const char_type* __trues = __true.c_str();
430 const char_type* __falses = __false.c_str();
431 const size_t __truen = __true.size() - 1;
432 const size_t __falsen = __false.size() - 1;
434 for (size_t __n = 0; __beg != __end; ++__n)
436 char_type __c = *__beg++;
437 bool __testf = __n <= __falsen
438 ? __traits_type::eq(__c, __falses[__n]) : false;
439 bool __testt = __n <= __truen
440 ? __traits_type::eq(__c, __trues[__n]) : false;
441 if (!(__testf || __testt))
443 __err |= ios_base::failbit;
446 else if (__testf && __n == __falsen)
451 else if (__testt && __n == __truen)
458 __err |= ios_base::eofbit;
464 template<typename _CharT, typename _InIter>
466 num_get<_CharT, _InIter>::
467 do_get(iter_type __beg, iter_type __end, ios_base& __io,
468 ios_base::iostate& __err, long& __v) const
472 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
473 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
477 template<typename _CharT, typename _InIter>
479 num_get<_CharT, _InIter>::
480 do_get(iter_type __beg, iter_type __end, ios_base& __io,
481 ios_base::iostate& __err, unsigned short& __v) const
485 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
487 __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
488 if (!(__err & ios_base::failbit)
489 && __ul <= numeric_limits<unsigned short>::max())
490 __v = static_cast<unsigned short>(__ul);
492 __err |= ios_base::failbit;
496 template<typename _CharT, typename _InIter>
498 num_get<_CharT, _InIter>::
499 do_get(iter_type __beg, iter_type __end, ios_base& __io,
500 ios_base::iostate& __err, unsigned int& __v) const
504 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
506 __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
507 if (!(__err & ios_base::failbit)
508 && __ul <= numeric_limits<unsigned int>::max())
509 __v = static_cast<unsigned int>(__ul);
511 __err |= ios_base::failbit;
515 template<typename _CharT, typename _InIter>
517 num_get<_CharT, _InIter>::
518 do_get(iter_type __beg, iter_type __end, ios_base& __io,
519 ios_base::iostate& __err, unsigned long& __v) const
523 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
524 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
528 #ifdef _GLIBCXX_USE_LONG_LONG
529 template<typename _CharT, typename _InIter>
531 num_get<_CharT, _InIter>::
532 do_get(iter_type __beg, iter_type __end, ios_base& __io,
533 ios_base::iostate& __err, long long& __v) const
537 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
538 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
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 long& __v) const
550 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
551 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
556 template<typename _CharT, typename _InIter>
558 num_get<_CharT, _InIter>::
559 do_get(iter_type __beg, iter_type __end, ios_base& __io,
560 ios_base::iostate& __err, float& __v) const
564 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
565 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale);
569 template<typename _CharT, typename _InIter>
571 num_get<_CharT, _InIter>::
572 do_get(iter_type __beg, iter_type __end, ios_base& __io,
573 ios_base::iostate& __err, double& __v) const
577 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
578 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale);
582 template<typename _CharT, typename _InIter>
584 num_get<_CharT, _InIter>::
585 do_get(iter_type __beg, iter_type __end, ios_base& __io,
586 ios_base::iostate& __err, long double& __v) const
590 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
591 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale);
595 template<typename _CharT, typename _InIter>
597 num_get<_CharT, _InIter>::
598 do_get(iter_type __beg, iter_type __end, ios_base& __io,
599 ios_base::iostate& __err, void*& __v) const
601 // Prepare for hex formatted input
602 typedef ios_base::fmtflags fmtflags;
603 fmtflags __fmt = __io.flags();
604 fmtflags __fmtmask = ~(ios_base::showpos | ios_base::basefield
605 | ios_base::uppercase | ios_base::internal);
606 __io.flags(__fmt & __fmtmask | (ios_base::hex | ios_base::showbase));
610 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
612 // Reset from hex formatted input
616 __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
617 if (!(__err & ios_base::failbit))
618 __v = reinterpret_cast<void*>(__ul);
620 __err |= ios_base::failbit;
624 // For use by integer and floating-point types after they have been
625 // converted into a char_type string.
626 template<typename _CharT, typename _OutIter>
628 num_put<_CharT, _OutIter>::
629 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
630 _CharT* __new, const _CharT* __cs, int& __len) const
632 // [22.2.2.2.2] Stage 3.
633 // If necessary, pad.
634 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new, __cs,
636 __len = static_cast<int>(__w);
639 // Forwarding functions to peel signed from unsigned integer types.
640 template<typename _CharT>
642 __int_to_char(_CharT* __out, const int __size, long __v,
643 const _CharT* __lit, ios_base::fmtflags __flags)
645 unsigned long __ul = static_cast<unsigned long>(__v);
652 return __int_to_char(__out, __size, __ul, __lit, __flags, __neg);
655 template<typename _CharT>
657 __int_to_char(_CharT* __out, const int __size, unsigned long __v,
658 const _CharT* __lit, ios_base::fmtflags __flags)
659 { return __int_to_char(__out, __size, __v, __lit, __flags, false); }
661 #ifdef _GLIBCXX_USE_LONG_LONG
662 template<typename _CharT>
664 __int_to_char(_CharT* __out, const int __size, long long __v,
665 const _CharT* __lit, ios_base::fmtflags __flags)
667 unsigned long long __ull = static_cast<unsigned long long>(__v);
674 return __int_to_char(__out, __size, __ull, __lit, __flags, __neg);
677 template<typename _CharT>
679 __int_to_char(_CharT* __out, const int __size, unsigned long long __v,
680 const _CharT* __lit, ios_base::fmtflags __flags)
681 { return __int_to_char(__out, __size, __v, __lit, __flags, false); }
684 template<typename _CharT, typename _ValueT>
686 __int_to_char(_CharT* __out, const int __size, _ValueT __v,
687 const _CharT* __lit, ios_base::fmtflags __flags, bool __neg)
689 // Don't write base if already 0.
690 const bool __showbase = (__flags & ios_base::showbase) && __v;
691 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
692 _CharT* __buf = __out + __size - 1;
693 _CharT* __bufend = __out + __size;
695 if (__builtin_expect(__basefield != ios_base::oct &&
696 __basefield != ios_base::hex, true))
701 *__buf-- = __lit[(__v % 10) + __num_base::_S_odigits];
706 *__buf-- = __lit[__num_base::_S_ominus];
707 else if (__flags & ios_base::showpos)
708 *__buf-- = __lit[__num_base::_S_oplus];
710 else if (__basefield == ios_base::oct)
715 *__buf-- = __lit[(__v & 0x7) + __num_base::_S_odigits];
720 *__buf-- = __lit[__num_base::_S_odigits];
725 const bool __uppercase = __flags & ios_base::uppercase;
726 int __case_offset = __uppercase ? __num_base::_S_oudigits
727 : __num_base::_S_odigits;
730 *__buf-- = __lit[(__v & 0xf) + __case_offset];
737 *__buf-- = __lit[__num_base::_S_ox + __uppercase];
739 *__buf-- = __lit[__num_base::_S_odigits];
742 int __ret = __bufend - __buf - 1;
746 template<typename _CharT, typename _OutIter>
748 num_put<_CharT, _OutIter>::
749 _M_group_int(const string& __grouping, _CharT __sep, ios_base& __io,
750 _CharT* __new, _CharT* __cs, int& __len) const
752 // By itself __add_grouping cannot deal correctly with __ws when
753 // ios::showbase is set and ios_base::oct || ios_base::hex.
754 // Therefore we take care "by hand" of the initial 0, 0x or 0X.
755 // However, remember that the latter do not occur if the number
756 // printed is '0' (__len == 1).
757 streamsize __off = 0;
758 const ios_base::fmtflags __basefield = __io.flags()
759 & ios_base::basefield;
760 if ((__io.flags() & ios_base::showbase) && __len > 1)
761 if (__basefield == ios_base::oct)
766 else if (__basefield == ios_base::hex)
770 *(__new + 1) = *(__cs + 1);
773 __p = __add_grouping(__new + __off, __sep,
775 __grouping.c_str() + __grouping.size(),
776 __cs + __off, __cs + __len);
780 template<typename _CharT, typename _OutIter>
781 template<typename _ValueT>
783 num_put<_CharT, _OutIter>::
784 _M_convert_int(_OutIter __s, ios_base& __io, _CharT __fill,
787 typedef typename numpunct<_CharT>::__cache_type __cache_type;
788 const locale& __loc = __io._M_getloc();
789 const __cache_type& __lc = __use_cache<__cache_type>(__loc);
790 const _CharT* __lit = __lc._M_atoms_out;
792 // Long enough to hold hex, dec, and octal representations.
793 int __ilen = 4 * sizeof(_ValueT);
794 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
796 // [22.2.2.2.2] Stage 1, numeric conversion to character.
797 // Result is returned right-justified in the buffer.
799 __len = __int_to_char(&__cs[0], __ilen, __v, __lit, __io.flags());
800 __cs = __cs + __ilen - __len;
802 // Add grouping, if necessary.
804 if (__lc._M_use_grouping)
806 // Grouping can add (almost) as many separators as the
807 // number of digits, but no more.
808 __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
810 _M_group_int(__lc._M_grouping, __lc._M_thousands_sep, __io,
817 streamsize __w = __io.width();
818 if (__w > static_cast<streamsize>(__len))
820 __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
822 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
827 // [22.2.2.2.2] Stage 4.
828 // Write resulting, fully-formatted string to output iterator.
829 return __write(__s, __cs, __len);
832 template<typename _CharT, typename _OutIter>
834 num_put<_CharT, _OutIter>::
835 _M_group_float(const string& __grouping, _CharT __sep, const _CharT* __p,
836 _CharT* __new, _CharT* __cs, int& __len) const
838 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
839 //282. What types does numpunct grouping refer to?
840 // Add grouping, if necessary.
842 int __declen = __p ? __p - __cs : __len;
843 __p2 = __add_grouping(__new, __sep,
845 __grouping.c_str() + __grouping.size(),
846 __cs, __cs + __declen);
848 // Tack on decimal part.
849 int __newlen = __p2 - __new;
852 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
853 __newlen += __len - __declen;
859 // The following code uses snprintf (or sprintf(), when
860 // _GLIBCXX_USE_C99 is not defined) to convert floating point values
861 // for insertion into a stream. An optimization would be to replace
862 // them with code that works directly on a wide buffer and then use
863 // __pad to do the padding. It would be good to replace them anyway
864 // to gain back the efficiency that C++ provides by knowing up front
865 // the type of the values to insert. Also, sprintf is dangerous
866 // since may lead to accidental buffer overruns. This
867 // implementation follows the C++ standard fairly directly as
868 // outlined in 22.2.2.2 [lib.locale.num.put]
869 template<typename _CharT, typename _OutIter>
870 template<typename _ValueT>
872 num_put<_CharT, _OutIter>::
873 _M_convert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
876 // Note: digits10 is rounded down: add 1 to ensure the maximum
877 // available precision. Then, in general, one more 1 needs to
878 // be added since, when the %{g,G} conversion specifiers are
879 // chosen inside _S_format_float, the precision field is "the
880 // maximum number of significant digits", *not* the "number of
881 // digits to appear after the decimal point", as happens for
882 // %{e,E,f,F} (C99, 7.19.6.1,4).
883 const int __max_digits = numeric_limits<_ValueT>::digits10 + 2;
885 // Use default precision if out of range.
886 streamsize __prec = __io.precision();
887 if (__prec > static_cast<streamsize>(__max_digits))
888 __prec = static_cast<streamsize>(__max_digits);
889 else if (__prec < static_cast<streamsize>(0))
890 __prec = static_cast<streamsize>(6);
892 typedef typename numpunct<_CharT>::__cache_type __cache_type;
893 const locale& __loc = __io._M_getloc();
894 const __cache_type& __lc = __use_cache<__cache_type>(__loc);
896 // [22.2.2.2.2] Stage 1, numeric conversion to character.
898 // Long enough for the max format spec.
901 #ifdef _GLIBCXX_USE_C99
902 // First try a buffer perhaps big enough (for sure sufficient
903 // for non-ios_base::fixed outputs)
904 int __cs_size = __max_digits * 3;
905 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
907 _S_format_float(__io, __fbuf, __mod);
908 __len = __convert_from_v(__cs, __cs_size, __fbuf, __v,
909 _S_c_locale, __prec);
911 // If the buffer was not large enough, try again with the correct size.
912 if (__len >= __cs_size)
914 __cs_size = __len + 1;
915 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
916 __len = __convert_from_v(__cs, __cs_size, __fbuf, __v,
917 _S_c_locale, __prec);
920 // Consider the possibility of long ios_base::fixed outputs
921 const bool __fixed = __io.flags() & ios_base::fixed;
922 const int __max_exp = numeric_limits<_ValueT>::max_exponent10;
924 // ios_base::fixed outputs may need up to __max_exp+1 chars
925 // for the integer part + up to __max_digits chars for the
926 // fractional part + 3 chars for sign, decimal point, '\0'. On
927 // the other hand, for non-fixed outputs __max_digits*3 chars
928 // are largely sufficient.
929 const int __cs_size = __fixed ? __max_exp + __max_digits + 4
931 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
933 _S_format_float(__io, __fbuf, __mod);
934 __len = __convert_from_v(__cs, 0, __fbuf, __v, _S_c_locale, __prec);
937 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
938 // numpunct.decimal_point() values for '.' and adding grouping.
939 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
941 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
943 __ctype.widen(__cs, __cs + __len, __ws);
945 // Replace decimal point.
946 const _CharT __cdec = __ctype.widen('.');
947 const _CharT __dec = __lc._M_decimal_point;
949 if (__p = char_traits<_CharT>::find(__ws, __len, __cdec))
950 __ws[__p - __ws] = __dec;
952 // Add grouping, if necessary.
954 if (__lc._M_use_grouping)
956 // Grouping can add (almost) as many separators as the
957 // number of digits, but no more.
958 __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
960 _M_group_float(__lc._M_grouping, __lc._M_thousands_sep, __p,
967 streamsize __w = __io.width();
968 if (__w > static_cast<streamsize>(__len))
970 __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
971 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
976 // [22.2.2.2.2] Stage 4.
977 // Write resulting, fully-formatted string to output iterator.
978 return __write(__s, __ws, __len);
981 template<typename _CharT, typename _OutIter>
983 num_put<_CharT, _OutIter>::
984 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
986 ios_base::fmtflags __flags = __io.flags();
987 if ((__flags & ios_base::boolalpha) == 0)
989 unsigned long __uv = __v;
990 __s = _M_convert_int(__s, __io, __fill, __uv);
994 typedef typename numpunct<_CharT>::__cache_type __cache_type;
995 const locale& __loc = __io._M_getloc();
996 const __cache_type& __lc = __use_cache<__cache_type>(__loc);
998 typedef basic_string<_CharT> __string_type;
999 __string_type __name;
1001 __name = __lc._M_truename;
1003 __name = __lc._M_falsename;
1005 const _CharT* __cs = __name.c_str();
1006 int __len = __name.size();
1008 streamsize __w = __io.width();
1009 if (__w > static_cast<streamsize>(__len))
1011 __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1013 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
1017 __s = __write(__s, __cs, __len);
1022 template<typename _CharT, typename _OutIter>
1024 num_put<_CharT, _OutIter>::
1025 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
1026 { return _M_convert_int(__s, __io, __fill, __v); }
1028 template<typename _CharT, typename _OutIter>
1030 num_put<_CharT, _OutIter>::
1031 do_put(iter_type __s, ios_base& __io, char_type __fill,
1032 unsigned long __v) const
1033 { return _M_convert_int(__s, __io, __fill, __v); }
1035 #ifdef _GLIBCXX_USE_LONG_LONG
1036 template<typename _CharT, typename _OutIter>
1038 num_put<_CharT, _OutIter>::
1039 do_put(iter_type __s, ios_base& __b, char_type __fill, long long __v) const
1040 { return _M_convert_int(__s, __b, __fill, __v); }
1042 template<typename _CharT, typename _OutIter>
1044 num_put<_CharT, _OutIter>::
1045 do_put(iter_type __s, ios_base& __io, char_type __fill,
1046 unsigned long long __v) const
1047 { return _M_convert_int(__s, __io, __fill, __v); }
1050 template<typename _CharT, typename _OutIter>
1052 num_put<_CharT, _OutIter>::
1053 do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1054 { return _M_convert_float(__s, __io, __fill, char(), __v); }
1056 template<typename _CharT, typename _OutIter>
1058 num_put<_CharT, _OutIter>::
1059 do_put(iter_type __s, ios_base& __io, char_type __fill,
1060 long double __v) const
1061 { return _M_convert_float(__s, __io, __fill, 'L', __v); }
1063 template<typename _CharT, typename _OutIter>
1065 num_put<_CharT, _OutIter>::
1066 do_put(iter_type __s, ios_base& __io, char_type __fill,
1067 const void* __v) const
1069 ios_base::fmtflags __flags = __io.flags();
1070 ios_base::fmtflags __fmt = ~(ios_base::showpos | ios_base::basefield
1071 | ios_base::uppercase | ios_base::internal);
1072 __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase));
1075 __s = _M_convert_int(__s, __io, __fill,
1076 reinterpret_cast<unsigned long>(__v));
1077 __io.flags(__flags);
1081 __io.flags(__flags);
1082 __throw_exception_again;
1088 template<typename _CharT, typename _InIter>
1090 money_get<_CharT, _InIter>::
1091 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1092 ios_base::iostate& __err, long double& __units) const
1095 __beg = this->do_get(__beg, __end, __intl, __io, __err, __str);
1097 const int __n = numeric_limits<long double>::digits10;
1098 char* __cs = static_cast<char*>(__builtin_alloca(__n));
1099 const locale __loc = __io.getloc();
1100 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1101 const _CharT* __wcs = __str.c_str();
1102 __ctype.narrow(__wcs, __wcs + __str.size() + 1, char(), __cs);
1103 __convert_to_v(__cs, __units, __err, _S_c_locale);
1107 template<typename _CharT, typename _InIter>
1109 money_get<_CharT, _InIter>::
1110 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1111 ios_base::iostate& __err, string_type& __units) const
1113 // These contortions are quite unfortunate.
1114 typedef moneypunct<_CharT, true> __money_true;
1115 typedef moneypunct<_CharT, false> __money_false;
1116 typedef money_base::part part;
1117 typedef typename string_type::size_type size_type;
1119 const locale __loc = __io.getloc();
1120 const __money_true& __mpt = use_facet<__money_true>(__loc);
1121 const __money_false& __mpf = use_facet<__money_false>(__loc);
1122 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1124 const money_base::pattern __p = __intl ? __mpt.neg_format()
1125 : __mpf.neg_format();
1127 const string_type __pos_sign =__intl ? __mpt.positive_sign()
1128 : __mpf.positive_sign();
1129 const string_type __neg_sign =__intl ? __mpt.negative_sign()
1130 : __mpf.negative_sign();
1131 const char_type __d = __intl ? __mpt.decimal_point()
1132 : __mpf.decimal_point();
1133 const char_type __sep = __intl ? __mpt.thousands_sep()
1134 : __mpf.thousands_sep();
1136 const string __grouping = __intl ? __mpt.grouping() : __mpf.grouping();
1138 // Set to deduced positive or negative sign, depending.
1140 // String of grouping info from thousands_sep plucked from __units.
1141 string __grouping_tmp;
1142 // Marker for thousands_sep position.
1144 // If input iterator is in a valid state.
1145 bool __testvalid = true;
1146 // Flag marking when a decimal point is found.
1147 bool __testdecfound = false;
1149 // The tentative returned string is stored here.
1150 string_type __tmp_units;
1152 char_type __c = *__beg;
1153 char_type __eof = static_cast<char_type>(char_traits<char_type>::eof());
1154 for (int __i = 0; __beg != __end && __i < 4 && __testvalid; ++__i)
1156 part __which = static_cast<part>(__p.field[__i]);
1159 case money_base::symbol:
1160 if (__io.flags() & ios_base::showbase
1161 || __i < 2 || __sign.size() > 1
1162 || ((static_cast<part>(__p.field[3]) != money_base::none)
1165 // According to 22.2.6.1.2.2, symbol is required
1166 // if (__io.flags() & ios_base::showbase),
1167 // otherwise is optional and consumed only if
1168 // other characters are needed to complete the
1170 const string_type __symbol = __intl ? __mpt.curr_symbol()
1171 : __mpf.curr_symbol();
1172 size_type __len = __symbol.size();
1174 while (__beg != __end
1175 && __j < __len && __symbol[__j] == __c)
1180 // When (__io.flags() & ios_base::showbase)
1181 // symbol is required.
1182 if (__j != __len && (__io.flags() & ios_base::showbase))
1183 __testvalid = false;
1186 case money_base::sign:
1187 // Sign might not exist, or be more than one character long.
1188 if (__pos_sign.size() && __neg_sign.size())
1190 // Sign is mandatory.
1191 if (__c == __pos_sign[0])
1193 __sign = __pos_sign;
1196 else if (__c == __neg_sign[0])
1198 __sign = __neg_sign;
1202 __testvalid = false;
1204 else if (__pos_sign.size() && __c == __pos_sign[0])
1206 __sign = __pos_sign;
1209 else if (__neg_sign.size() && __c == __neg_sign[0])
1211 __sign = __neg_sign;
1215 case money_base::value:
1216 // Extract digits, remove and stash away the
1217 // grouping of found thousands separators.
1218 while (__beg != __end
1219 && (__ctype.is(ctype_base::digit, __c)
1220 || (__c == __d && !__testdecfound)
1225 __grouping_tmp += static_cast<char>(__sep_pos);
1227 __testdecfound = true;
1229 else if (__c == __sep)
1231 if (__grouping.size())
1233 // Mark position for later analysis.
1234 __grouping_tmp += static_cast<char>(__sep_pos);
1239 __testvalid = false;
1251 case money_base::space:
1252 case money_base::none:
1253 // Only if not at the end of the pattern.
1255 while (__beg != __end
1256 && __ctype.is(ctype_base::space, __c))
1262 // Need to get the rest of the sign characters, if they exist.
1263 if (__sign.size() > 1)
1265 size_type __len = __sign.size();
1267 for (; __c != __eof && __i < __len; ++__i)
1268 while (__beg != __end && __c != __sign[__i])
1272 __testvalid = false;
1275 // Strip leading zeros.
1276 while (!__tmp_units.empty() && __tmp_units[0] == __ctype.widen('0'))
1277 __tmp_units.erase(__tmp_units.begin());
1279 if (__sign.size() && __sign == __neg_sign)
1280 __tmp_units.insert(__tmp_units.begin(), __ctype.widen('-'));
1282 // Test for grouping fidelity.
1283 if (__grouping.size() && __grouping_tmp.size())
1285 if (!__verify_grouping(__grouping, __grouping_tmp))
1286 __testvalid = false;
1289 // Iff no more characters are available.
1291 __err |= ios_base::eofbit;
1293 // Iff not enough digits were supplied after the decimal-point.
1296 const int __frac = __intl ? __mpt.frac_digits()
1297 : __mpf.frac_digits();
1300 if (__sep_pos != __frac)
1301 __testvalid = false;
1305 // Iff valid sequence is not recognized.
1306 if (!__testvalid || !__tmp_units.size())
1307 __err |= ios_base::failbit;
1309 // Use the "swap trick" to copy __tmp_units into __units.
1310 __tmp_units.swap(__units);
1315 template<typename _CharT, typename _OutIter>
1317 money_put<_CharT, _OutIter>::
1318 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1319 long double __units) const
1321 const locale __loc = __io.getloc();
1322 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1323 #ifdef _GLIBCXX_USE_C99
1324 // First try a buffer perhaps big enough.
1326 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1327 int __len = __convert_from_v(__cs, __cs_size, "%.01Lf", __units,
1329 // If the buffer was not large enough, try again with the correct size.
1330 if (__len >= __cs_size)
1332 __cs_size = __len + 1;
1333 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1334 __len = __convert_from_v(__cs, __cs_size, "%.01Lf", __units,
1338 // max_exponent10 + 1 for the integer part, + 4 for sign, decimal point,
1339 // decimal digit, '\0'.
1340 const int __cs_size = numeric_limits<long double>::max_exponent10 + 5;
1341 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1342 int __len = __convert_from_v(__cs, 0, "%.01Lf", __units, _S_c_locale);
1344 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1346 __ctype.widen(__cs, __cs + __len, __ws);
1347 string_type __digits(__ws);
1348 return this->do_put(__s, __intl, __io, __fill, __digits);
1351 template<typename _CharT, typename _OutIter>
1353 money_put<_CharT, _OutIter>::
1354 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1355 const string_type& __digits) const
1357 typedef typename string_type::size_type size_type;
1358 typedef money_base::part part;
1360 const locale __loc = __io.getloc();
1361 const size_type __width = static_cast<size_type>(__io.width());
1363 // These contortions are quite unfortunate.
1364 typedef moneypunct<_CharT, true> __money_true;
1365 typedef moneypunct<_CharT, false> __money_false;
1366 const __money_true& __mpt = use_facet<__money_true>(__loc);
1367 const __money_false& __mpf = use_facet<__money_false>(__loc);
1368 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1370 // Determine if negative or positive formats are to be used, and
1371 // discard leading negative_sign if it is present.
1372 const char_type* __beg = __digits.data();
1373 const char_type* __end = __beg + __digits.size();
1374 money_base::pattern __p;
1376 if (*__beg != __ctype.widen('-'))
1378 __p = __intl ? __mpt.pos_format() : __mpf.pos_format();
1379 __sign =__intl ? __mpt.positive_sign() : __mpf.positive_sign();
1383 __p = __intl ? __mpt.neg_format() : __mpf.neg_format();
1384 __sign =__intl ? __mpt.negative_sign() : __mpf.negative_sign();
1388 // Look for valid numbers in the current ctype facet within input digits.
1389 __end = __ctype.scan_not(ctype_base::digit, __beg, __end);
1392 // Assume valid input, and attempt to format.
1393 // Break down input numbers into base components, as follows:
1394 // final_value = grouped units + (decimal point) + (digits)
1396 string_type __value;
1397 const string_type __symbol = __intl ? __mpt.curr_symbol()
1398 : __mpf.curr_symbol();
1400 // Deal with decimal point, decimal digits.
1401 const int __frac = __intl ? __mpt.frac_digits()
1402 : __mpf.frac_digits();
1405 const char_type __d = __intl ? __mpt.decimal_point()
1406 : __mpf.decimal_point();
1407 if (__end - __beg >= __frac)
1409 __value = string_type(__end - __frac, __end);
1410 __value.insert(__value.begin(), __d);
1415 // Have to pad zeros in the decimal position.
1416 __value = string_type(__beg, __end);
1417 int __paddec = __frac - (__end - __beg);
1418 char_type __zero = __ctype.widen('0');
1419 __value.insert(__value.begin(), __paddec, __zero);
1420 __value.insert(__value.begin(), __d);
1425 // Add thousands separators to non-decimal digits, per
1429 const string __grouping = __intl ? __mpt.grouping()
1431 if (__grouping.size())
1433 const char_type __sep = __intl ? __mpt.thousands_sep()
1434 : __mpf.thousands_sep();
1435 const char* __gbeg = __grouping.c_str();
1436 const char* __gend = __gbeg + __grouping.size();
1437 const int __n = (__end - __beg) * 2;
1439 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __n));
1440 _CharT* __ws_end = __add_grouping(__ws2, __sep, __gbeg,
1441 __gend, __beg, __end);
1442 __value.insert(0, __ws2, __ws_end - __ws2);
1445 __value.insert(0, string_type(__beg, __end));
1448 // Calculate length of resulting string.
1449 ios_base::fmtflags __f = __io.flags() & ios_base::adjustfield;
1450 size_type __len = __value.size() + __sign.size();
1451 __len += (__io.flags() & ios_base::showbase) ? __symbol.size() : 0;
1452 bool __testipad = __f == ios_base::internal && __len < __width;
1454 // Fit formatted digits into the required pattern.
1455 for (int __i = 0; __i < 4; ++__i)
1457 part __which = static_cast<part>(__p.field[__i]);
1460 case money_base::symbol:
1461 if (__io.flags() & ios_base::showbase)
1464 case money_base::sign:
1465 // Sign might not exist, or be more than one
1466 // charater long. In that case, add in the rest
1471 case money_base::value:
1474 case money_base::space:
1475 // At least one space is required, but if internal
1476 // formatting is required, an arbitrary number of
1477 // fill spaces will be necessary.
1479 __res += string_type(__width - __len, __fill);
1481 __res += __ctype.widen(__fill);
1483 case money_base::none:
1485 __res += string_type(__width - __len, __fill);
1490 // Special case of multi-part sign parts.
1491 if (__sign.size() > 1)
1492 __res += string_type(__sign.begin() + 1, __sign.end());
1494 // Pad, if still necessary.
1495 __len = __res.size();
1496 if (__width > __len)
1498 if (__f == ios_base::left)
1500 __res.append(__width - __len, __fill);
1503 __res.insert(0, string_type(__width - __len, __fill));
1507 // Write resulting, fully-formatted string to output iterator.
1508 __s = __write(__s, __res.c_str(), __len);
1515 // NB: Not especially useful. Without an ios_base object or some
1516 // kind of locale reference, we are left clawing at the air where
1517 // the side of the mountain used to be...
1518 template<typename _CharT, typename _InIter>
1519 time_base::dateorder
1520 time_get<_CharT, _InIter>::do_date_order() const
1521 { return time_base::no_order; }
1523 template<typename _CharT, typename _InIter>
1525 time_get<_CharT, _InIter>::
1526 _M_extract_via_format(iter_type& __beg, iter_type& __end, ios_base& __io,
1527 ios_base::iostate& __err, tm* __tm,
1528 const _CharT* __format) const
1530 locale __loc = __io.getloc();
1531 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1532 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1533 size_t __len = char_traits<_CharT>::length(__format);
1535 for (size_t __i = 0; __beg != __end && __i < __len && !__err; ++__i)
1537 char __c = __format[__i];
1540 // Verify valid formatting code, attempt to extract.
1541 __c = __format[++__i];
1544 if (__c == 'E' || __c == 'O')
1547 __c = __format[++__i];
1554 // Abbreviated weekday name [tm_wday]
1555 const char_type* __days1[7];
1556 __tp._M_days_abbreviated(__days1);
1557 _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 7,
1561 // Weekday name [tm_wday].
1562 const char_type* __days2[7];
1563 __tp._M_days(__days2);
1564 _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 7,
1569 // Abbreviated month name [tm_mon]
1570 const char_type* __months1[12];
1571 __tp._M_months_abbreviated(__months1);
1572 _M_extract_name(__beg, __end, __tm->tm_mon, __months1, 12,
1576 // Month name [tm_mon].
1577 const char_type* __months2[12];
1578 __tp._M_months(__months2);
1579 _M_extract_name(__beg, __end, __tm->tm_mon, __months2, 12,
1583 // Default time and date representation.
1584 const char_type* __dt[2];
1585 __tp._M_date_time_formats(__dt);
1586 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1590 // Day [01, 31]. [tm_mday]
1591 _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2,
1595 // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
1597 __ctype.widen(__cs, __cs + 9, __wcs);
1598 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1602 // Hour [00, 23]. [tm_hour]
1603 _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
1607 // Hour [01, 12]. [tm_hour]
1608 _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2,
1612 // Month [01, 12]. [tm_mon]
1613 _M_extract_num(__beg, __end, __mem, 1, 12, 2, __ctype,
1616 __tm->tm_mon = __mem - 1;
1619 // Minute [00, 59]. [tm_min]
1620 _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
1624 if (__ctype.narrow(*__beg, 0) == '\n')
1627 __err |= ios_base::failbit;
1630 // Equivalent to (%H:%M).
1632 __ctype.widen(__cs, __cs + 6, __wcs);
1633 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1638 _M_extract_num(__beg, __end, __tm->tm_sec, 0, 59, 2,
1642 if (__ctype.narrow(*__beg, 0) == '\t')
1645 __err |= ios_base::failbit;
1648 // Equivalent to (%H:%M:%S).
1650 __ctype.widen(__cs, __cs + 9, __wcs);
1651 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1656 const char_type* __dates[2];
1657 __tp._M_date_formats(__dates);
1658 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1663 const char_type* __times[2];
1664 __tp._M_time_formats(__times);
1665 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1669 // Two digit year. [tm_year]
1670 _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2,
1674 // Year [1900). [tm_year]
1675 _M_extract_num(__beg, __end, __mem, 0,
1676 numeric_limits<int>::max(), 4,
1679 __tm->tm_year = __mem - 1900;
1683 if (__ctype.is(ctype_base::upper, *__beg))
1686 _M_extract_name(__beg, __end, __tmp,
1687 __timepunct<_CharT>::_S_timezones,
1690 // GMT requires special effort.
1691 char_type __c = *__beg;
1692 if (!__err && __tmp == 0
1693 && (__c == __ctype.widen('-')
1694 || __c == __ctype.widen('+')))
1696 _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
1698 _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
1703 __err |= ios_base::failbit;
1707 __err |= ios_base::failbit;
1712 // Verify format and input match, extract and discard.
1713 if (__c == __ctype.narrow(*__beg, 0))
1716 __err |= ios_base::failbit;
1721 template<typename _CharT, typename _InIter>
1723 time_get<_CharT, _InIter>::
1724 _M_extract_num(iter_type& __beg, iter_type& __end, int& __member,
1725 int __min, int __max, size_t __len,
1726 const ctype<_CharT>& __ctype,
1727 ios_base::iostate& __err) const
1731 bool __testvalid = true;
1732 char_type __c = *__beg;
1733 while (__beg != __end && __i < __len
1734 && __ctype.is(ctype_base::digit, __c))
1736 __digits += __ctype.narrow(__c, 0);
1742 int __value = atoi(__digits.c_str());
1743 if (__min <= __value && __value <= __max)
1746 __testvalid = false;
1749 __testvalid = false;
1751 __err |= ios_base::failbit;
1755 // All elements in __names are unique.
1756 template<typename _CharT, typename _InIter>
1758 time_get<_CharT, _InIter>::
1759 _M_extract_name(iter_type& __beg, iter_type& __end, int& __member,
1760 const _CharT** __names, size_t __indexlen,
1761 ios_base::iostate& __err) const
1763 typedef char_traits<_CharT> __traits_type;
1764 int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int)
1766 size_t __nmatches = 0;
1768 bool __testvalid = true;
1769 const char_type* __name;
1771 char_type __c = *__beg;
1772 // Look for initial matches.
1773 for (size_t __i1 = 0; __i1 < __indexlen; ++__i1)
1774 if (__c == __names[__i1][0])
1775 __matches[__nmatches++] = __i1;
1777 while (__nmatches > 1)
1779 // Find smallest matching string.
1780 size_t __minlen = 10;
1781 for (size_t __i2 = 0; __i2 < __nmatches; ++__i2)
1782 __minlen = min(__minlen,
1783 __traits_type::length(__names[__matches[__i2]]));
1785 if (__pos < __minlen && __beg != __end)
1789 for (size_t __i3 = 0; __i3 < __nmatches; ++__i3)
1791 __name = __names[__matches[__i3]];
1792 if (__name[__pos] != __c)
1793 __matches[__i3] = __matches[--__nmatches];
1800 if (__nmatches == 1)
1802 // Make sure found name is completely extracted.
1803 __name = __names[__matches[0]];
1804 const size_t __len = __traits_type::length(__name);
1805 while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
1809 __member = __matches[0];
1811 __testvalid = false;
1814 __testvalid = false;
1816 __err |= ios_base::failbit;
1819 template<typename _CharT, typename _InIter>
1821 time_get<_CharT, _InIter>::
1822 do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
1823 ios_base::iostate& __err, tm* __tm) const
1826 const char* __cs = "%X";
1827 locale __loc = __io.getloc();
1828 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1829 __ctype.widen(__cs, __cs + 3, __wcs);
1830 _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1832 __err |= ios_base::eofbit;
1836 template<typename _CharT, typename _InIter>
1838 time_get<_CharT, _InIter>::
1839 do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
1840 ios_base::iostate& __err, tm* __tm) const
1843 const char* __cs = "%x";
1844 locale __loc = __io.getloc();
1845 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1846 __ctype.widen(__cs, __cs + 3, __wcs);
1847 _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1849 __err |= ios_base::eofbit;
1853 template<typename _CharT, typename _InIter>
1855 time_get<_CharT, _InIter>::
1856 do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
1857 ios_base::iostate& __err, tm* __tm) const
1859 typedef char_traits<_CharT> __traits_type;
1860 locale __loc = __io.getloc();
1861 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1862 const char_type* __days[7];
1863 __tp._M_days_abbreviated(__days);
1865 _M_extract_name(__beg, __end, __tmpwday, __days, 7, __err);
1867 // Check to see if non-abbreviated name exists, and extract.
1868 // NB: Assumes both _M_days and _M_days_abbreviated organized in
1869 // exact same order, first to last, such that the resulting
1870 // __days array with the same index points to a day, and that
1871 // day's abbreviated form.
1872 // NB: Also assumes that an abbreviated name is a subset of the name.
1875 size_t __pos = __traits_type::length(__days[__tmpwday]);
1876 __tp._M_days(__days);
1877 const char_type* __name = __days[__tmpwday];
1878 if (__name[__pos] == *__beg)
1880 // Extract the rest of it.
1881 const size_t __len = __traits_type::length(__name);
1882 while (__pos < __len && __beg != __end
1883 && __name[__pos] == *__beg)
1886 __err |= ios_base::failbit;
1889 __tm->tm_wday = __tmpwday;
1892 __err |= ios_base::eofbit;
1896 template<typename _CharT, typename _InIter>
1898 time_get<_CharT, _InIter>::
1899 do_get_monthname(iter_type __beg, iter_type __end,
1900 ios_base& __io, ios_base::iostate& __err, tm* __tm) const
1902 typedef char_traits<_CharT> __traits_type;
1903 locale __loc = __io.getloc();
1904 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1905 const char_type* __months[12];
1906 __tp._M_months_abbreviated(__months);
1908 _M_extract_name(__beg, __end, __tmpmon, __months, 12, __err);
1910 // Check to see if non-abbreviated name exists, and extract.
1911 // NB: Assumes both _M_months and _M_months_abbreviated organized in
1912 // exact same order, first to last, such that the resulting
1913 // __months array with the same index points to a month, and that
1914 // month's abbreviated form.
1915 // NB: Also assumes that an abbreviated name is a subset of the name.
1918 size_t __pos = __traits_type::length(__months[__tmpmon]);
1919 __tp._M_months(__months);
1920 const char_type* __name = __months[__tmpmon];
1921 if (__name[__pos] == *__beg)
1923 // Extract the rest of it.
1924 const size_t __len = __traits_type::length(__name);
1925 while (__pos < __len && __beg != __end
1926 && __name[__pos] == *__beg)
1929 __err |= ios_base::failbit;
1932 __tm->tm_mon = __tmpmon;
1936 __err |= ios_base::eofbit;
1940 template<typename _CharT, typename _InIter>
1942 time_get<_CharT, _InIter>::
1943 do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
1944 ios_base::iostate& __err, tm* __tm) const
1946 locale __loc = __io.getloc();
1947 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1949 char_type __c = *__beg;
1952 while (__i < 4 && __beg != __end && __ctype.is(ctype_base::digit, __c))
1954 __digits += __ctype.narrow(__c, 0);
1958 if (__i == 2 || __i == 4)
1961 __convert_to_v(__digits.c_str(), __l, __err, _S_c_locale);
1962 if (!(__err & ios_base::failbit) && __l <= INT_MAX)
1964 __l = __i == 2 ? __l : __l - 1900;
1965 __tm->tm_year = static_cast<int>(__l);
1969 __err |= ios_base::failbit;
1971 __err |= ios_base::eofbit;
1975 template<typename _CharT, typename _OutIter>
1977 time_put<_CharT, _OutIter>::
1978 put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
1979 const _CharT* __beg, const _CharT* __end) const
1981 locale __loc = __io.getloc();
1982 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1983 while (__beg != __end)
1985 char __c = __ctype.narrow(*__beg, 0);
1992 __c = __ctype.narrow(*__beg, 0);
1994 if (__c == 'E' || __c == 'O')
1997 __format = __ctype.narrow(*__beg, 0);
2002 __s = this->do_put(__s, __io, _CharT(), __tm, __format, __mod);
2013 template<typename _CharT, typename _OutIter>
2015 time_put<_CharT, _OutIter>::
2016 do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
2017 char __format, char __mod) const
2019 locale __loc = __io.getloc();
2020 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2021 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
2023 // NB: This size is arbitrary. Should this be a data member,
2024 // initialized at construction?
2025 const size_t __maxlen = 64;
2026 char_type* __res = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen));
2028 // NB: In IEE 1003.1-200x, and perhaps other locale models, it
2029 // is possible that the format character will be longer than one
2030 // character. Possibilities include 'E' or 'O' followed by a
2031 // format character: if __mod is not the default argument, assume
2032 // it's a valid modifier.
2034 __fmt[0] = __ctype.widen('%');
2037 __fmt[1] = __format;
2038 __fmt[2] = char_type();
2043 __fmt[2] = __format;
2044 __fmt[3] = char_type();
2047 __tp._M_put(__res, __maxlen, __fmt, __tm);
2049 // Write resulting, fully-formatted string to output iterator.
2050 return __write(__s, __res, char_traits<char_type>::length(__res));
2054 // Generic version does nothing.
2055 template<typename _CharT>
2057 collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
2060 // Generic version does nothing.
2061 template<typename _CharT>
2063 collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
2066 template<typename _CharT>
2069 do_compare(const _CharT* __lo1, const _CharT* __hi1,
2070 const _CharT* __lo2, const _CharT* __hi2) const
2072 // strcoll assumes zero-terminated strings so we make a copy
2073 // and then put a zero at the end.
2074 const string_type __one(__lo1, __hi1);
2075 const string_type __two(__lo2, __hi2);
2077 const _CharT* __p = __one.c_str();
2078 const _CharT* __pend = __one.c_str() + __one.length();
2079 const _CharT* __q = __two.c_str();
2080 const _CharT* __qend = __two.c_str() + __two.length();
2082 // strcoll stops when it sees a nul character so we break
2083 // the strings into zero-terminated substrings and pass those
2087 int __res = _M_compare(__p, __q);
2091 __p += char_traits<_CharT>::length(__p);
2092 __q += char_traits<_CharT>::length(__q);
2093 if (__p == __pend && __q == __qend)
2095 else if (__p == __pend)
2097 else if (__q == __qend)
2105 template<typename _CharT>
2106 typename collate<_CharT>::string_type
2108 do_transform(const _CharT* __lo, const _CharT* __hi) const
2110 // strxfrm assumes zero-terminated strings so we make a copy
2111 string_type __str(__lo, __hi);
2113 const _CharT* __p = __str.c_str();
2114 const _CharT* __pend = __str.c_str() + __str.length();
2116 size_t __len = (__hi - __lo) * 2;
2120 // strxfrm stops when it sees a nul character so we break
2121 // the string into zero-terminated substrings and pass those
2125 // First try a buffer perhaps big enough.
2127 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len));
2128 size_t __res = _M_transform(__c, __p, __len);
2129 // If the buffer was not large enough, try again with the
2134 __c = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
2136 __res = _M_transform(__c, __p, __res + 1);
2139 __ret.append(__c, __res);
2140 __p += char_traits<_CharT>::length(__p);
2145 __ret.push_back(_CharT());
2149 template<typename _CharT>
2152 do_hash(const _CharT* __lo, const _CharT* __hi) const
2154 unsigned long __val = 0;
2155 for (; __lo < __hi; ++__lo)
2156 __val = *__lo + ((__val << 7) |
2157 (__val >> (numeric_limits<unsigned long>::digits - 7)));
2158 return static_cast<long>(__val);
2161 // Construct correctly padded string, as per 22.2.2.2.2
2163 // __newlen > __oldlen
2164 // __news is allocated for __newlen size
2165 // Used by both num_put and ostream inserters: if __num,
2166 // internal-adjusted objects are padded according to the rules below
2167 // concerning 0[xX] and +-, otherwise, exactly as right-adjusted
2170 // NB: Of the two parameters, _CharT can be deduced from the
2171 // function arguments. The other (_Traits) has to be explicitly specified.
2172 template<typename _CharT, typename _Traits>
2174 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
2175 _CharT* __news, const _CharT* __olds,
2176 const streamsize __newlen,
2177 const streamsize __oldlen, const bool __num)
2179 size_t __plen = static_cast<size_t>(__newlen - __oldlen);
2180 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
2182 _Traits::assign(__pads, __plen, __fill);
2187 size_t __beglen; //either __plen or __oldlen
2188 ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
2190 if (__adjust == ios_base::left)
2193 __beg = const_cast<_CharT*>(__olds);
2194 __beglen = __oldlen;
2197 else if (__adjust == ios_base::internal && __num)
2199 // Pad after the sign, if there is one.
2200 // Pad after 0[xX], if there is one.
2201 // Who came up with these rules, anyway? Jeeze.
2202 locale __loc = __io.getloc();
2203 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2204 const _CharT __minus = __ctype.widen('-');
2205 const _CharT __plus = __ctype.widen('+');
2206 bool __testsign = _Traits::eq(__olds[0], __minus)
2207 || _Traits::eq(__olds[0], __plus);
2209 bool __testhex = _Traits::eq(__ctype.widen('0'), __olds[0])
2210 && (_Traits::eq(__ctype.widen('x'), __olds[1])
2211 || _Traits::eq(__ctype.widen('X'), __olds[1]));
2214 __news[0] = __olds[0];
2215 __news[1] = __olds[1];
2220 __end = const_cast<_CharT*>(__olds + __mod);
2222 else if (__testsign)
2224 _Traits::eq((__news[0] = __olds[0]), __plus) ? __plus : __minus;
2229 __end = const_cast<_CharT*>(__olds + __mod);
2236 __end = const_cast<_CharT*>(__olds);
2244 __end = const_cast<_CharT*>(__olds);
2246 _Traits::copy(__news, __beg, __beglen);
2247 _Traits::copy(__news + __beglen, __end,
2248 __newlen - __beglen - __mod);
2251 template<typename _CharT>
2253 __verify_grouping(const basic_string<_CharT>& __grouping,
2254 basic_string<_CharT>& __grouping_tmp)
2258 const int __len = __grouping.size();
2259 const int __n = __grouping_tmp.size();
2262 // Parsed number groupings have to match the
2263 // numpunct::grouping string exactly, starting at the
2264 // right-most point of the parsed sequence of elements ...
2265 while (__test && __i < __n - 1)
2266 for (__j = 0; __test && __j < __len && __i < __n - 1; ++__j,++__i)
2267 __test &= __grouping[__j] == __grouping_tmp[__n - __i - 1];
2268 // ... but the last parsed grouping can be <= numpunct
2270 __j == __len ? __j = 0 : __j;
2271 __test &= __grouping[__j] >= __grouping_tmp[__n - __i - 1];
2275 template<typename _CharT>
2277 __add_grouping(_CharT* __s, _CharT __sep,
2278 const char* __gbeg, const char* __gend,
2279 const _CharT* __first, const _CharT* __last)
2281 if (__last - __first > *__gbeg)
2283 __s = __add_grouping(__s, __sep,
2284 (__gbeg + 1 == __gend ? __gbeg : __gbeg + 1),
2285 __gend, __first, __last - *__gbeg);
2286 __first = __last - *__gbeg;
2290 *__s++ = *__first++;
2291 while (__first != __last);
2295 // Inhibit implicit instantiations for required instantiations,
2296 // which are defined via explicit instantiations elsewhere.
2297 // NB: This syntax is a GNU extension.
2298 #if _GLIBCXX_EXTERN_TEMPLATE
2299 extern template class moneypunct<char, false>;
2300 extern template class moneypunct<char, true>;
2301 extern template class moneypunct_byname<char, false>;
2302 extern template class moneypunct_byname<char, true>;
2303 extern template class money_get<char>;
2304 extern template class money_put<char>;
2305 extern template class numpunct<char>;
2306 extern template class numpunct_byname<char>;
2307 extern template class num_get<char>;
2308 extern template class num_put<char>;
2309 extern template class __timepunct<char>;
2310 extern template class time_put<char>;
2311 extern template class time_put_byname<char>;
2312 extern template class time_get<char>;
2313 extern template class time_get_byname<char>;
2314 extern template class messages<char>;
2315 extern template class messages_byname<char>;
2316 extern template class ctype_byname<char>;
2317 extern template class codecvt_byname<char, char, mbstate_t>;
2318 extern template class collate<char>;
2319 extern template class collate_byname<char>;
2322 const codecvt<char, char, mbstate_t>&
2323 use_facet<codecvt<char, char, mbstate_t> >(const locale&);
2326 const collate<char>&
2327 use_facet<collate<char> >(const locale&);
2330 const numpunct<char>&
2331 use_facet<numpunct<char> >(const locale&);
2334 const num_put<char>&
2335 use_facet<num_put<char> >(const locale&);
2338 const num_get<char>&
2339 use_facet<num_get<char> >(const locale&);
2342 const moneypunct<char, true>&
2343 use_facet<moneypunct<char, true> >(const locale&);
2346 const moneypunct<char, false>&
2347 use_facet<moneypunct<char, false> >(const locale&);
2350 const money_put<char>&
2351 use_facet<money_put<char> >(const locale&);
2354 const money_get<char>&
2355 use_facet<money_get<char> >(const locale&);
2358 const __timepunct<char>&
2359 use_facet<__timepunct<char> >(const locale&);
2362 const time_put<char>&
2363 use_facet<time_put<char> >(const locale&);
2366 const time_get<char>&
2367 use_facet<time_get<char> >(const locale&);
2370 const messages<char>&
2371 use_facet<messages<char> >(const locale&);
2375 has_facet<ctype<char> >(const locale&);
2379 has_facet<codecvt<char, char, mbstate_t> >(const locale&);
2383 has_facet<collate<char> >(const locale&);
2387 has_facet<numpunct<char> >(const locale&);
2391 has_facet<num_put<char> >(const locale&);
2395 has_facet<num_get<char> >(const locale&);
2399 has_facet<moneypunct<char> >(const locale&);
2403 has_facet<money_put<char> >(const locale&);
2407 has_facet<money_get<char> >(const locale&);
2411 has_facet<__timepunct<char> >(const locale&);
2415 has_facet<time_put<char> >(const locale&);
2419 has_facet<time_get<char> >(const locale&);
2423 has_facet<messages<char> >(const locale&);
2425 #ifdef _GLIBCXX_USE_WCHAR_T
2426 extern template class moneypunct<wchar_t, false>;
2427 extern template class moneypunct<wchar_t, true>;
2428 extern template class moneypunct_byname<wchar_t, false>;
2429 extern template class moneypunct_byname<wchar_t, true>;
2430 extern template class money_get<wchar_t>;
2431 extern template class money_put<wchar_t>;
2432 extern template class numpunct<wchar_t>;
2433 extern template class numpunct_byname<wchar_t>;
2434 extern template class num_get<wchar_t>;
2435 extern template class num_put<wchar_t>;
2436 extern template class __timepunct<wchar_t>;
2437 extern template class time_put<wchar_t>;
2438 extern template class time_put_byname<wchar_t>;
2439 extern template class time_get<wchar_t>;
2440 extern template class time_get_byname<wchar_t>;
2441 extern template class messages<wchar_t>;
2442 extern template class messages_byname<wchar_t>;
2443 extern template class ctype_byname<wchar_t>;
2444 extern template class codecvt_byname<wchar_t, char, mbstate_t>;
2445 extern template class collate<wchar_t>;
2446 extern template class collate_byname<wchar_t>;
2449 const codecvt<wchar_t, char, mbstate_t>&
2450 use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&);
2453 const collate<wchar_t>&
2454 use_facet<collate<wchar_t> >(const locale&);
2457 const numpunct<wchar_t>&
2458 use_facet<numpunct<wchar_t> >(const locale&);
2461 const num_put<wchar_t>&
2462 use_facet<num_put<wchar_t> >(const locale&);
2465 const num_get<wchar_t>&
2466 use_facet<num_get<wchar_t> >(const locale&);
2469 const moneypunct<wchar_t, true>&
2470 use_facet<moneypunct<wchar_t, true> >(const locale&);
2473 const moneypunct<wchar_t, false>&
2474 use_facet<moneypunct<wchar_t, false> >(const locale&);
2477 const money_put<wchar_t>&
2478 use_facet<money_put<wchar_t> >(const locale&);
2481 const money_get<wchar_t>&
2482 use_facet<money_get<wchar_t> >(const locale&);
2485 const __timepunct<wchar_t>&
2486 use_facet<__timepunct<wchar_t> >(const locale&);
2489 const time_put<wchar_t>&
2490 use_facet<time_put<wchar_t> >(const locale&);
2493 const time_get<wchar_t>&
2494 use_facet<time_get<wchar_t> >(const locale&);
2497 const messages<wchar_t>&
2498 use_facet<messages<wchar_t> >(const locale&);
2502 has_facet<ctype<wchar_t> >(const locale&);
2506 has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
2510 has_facet<collate<wchar_t> >(const locale&);
2514 has_facet<numpunct<wchar_t> >(const locale&);
2518 has_facet<num_put<wchar_t> >(const locale&);
2522 has_facet<num_get<wchar_t> >(const locale&);
2526 has_facet<moneypunct<wchar_t> >(const locale&);
2530 has_facet<money_put<wchar_t> >(const locale&);
2534 has_facet<money_get<wchar_t> >(const locale&);
2538 has_facet<__timepunct<wchar_t> >(const locale&);
2542 has_facet<time_put<wchar_t> >(const locale&);
2546 has_facet<time_get<wchar_t> >(const locale&);
2550 has_facet<messages<wchar_t> >(const locale&);