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 _CPP_BITS_LOCFACETS_TCC
34 #define _CPP_BITS_LOCFACETS_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 use_facet(const locale& __loc)
73 size_t __i = _Facet::id._M_id();
74 const locale::facet** __facets = __loc._M_impl->_M_facets;
75 if (!(__i < __loc._M_impl->_M_facets_size && __facets[__i]))
77 return static_cast<const _Facet&>(*__facets[__i]);
80 template<typename _Facet>
82 has_facet(const locale& __loc) throw()
84 size_t __i = _Facet::id._M_id();
85 const locale::facet** __facets = __loc._M_impl->_M_facets;
86 return (__i < __loc._M_impl->_M_facets_size && __facets[__i]);
90 // Stage 1: Determine a conversion specifier.
91 template<typename _CharT, typename _InIter>
93 num_get<_CharT, _InIter>::
94 _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
95 ios_base::iostate& __err, string& __xtrc) const
97 typedef char_traits<_CharT> __traits_type;
98 const locale __loc = __io.getloc();
99 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
100 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
102 // First check for sign.
103 const char_type __plus = __ctype.widen('+');
104 const char_type __minus = __ctype.widen('-');
106 char_type __c = *__beg;
107 if ((__traits_type::eq(__c, __plus) || __traits_type::eq(__c, __minus))
110 __xtrc += __ctype.narrow(__c, char());
115 // Next, strip leading zeros.
116 const char_type __zero = __ctype.widen(_S_atoms_in[_M_zero]);
117 bool __found_zero = false;
118 while (__traits_type::eq(__c, __zero) && __beg != __end)
125 __xtrc += _S_atoms_in[_M_zero];
129 // Only need acceptable digits for floating point numbers.
130 const size_t __len = _M_E - _M_zero + 1;
131 char_type __watoms[__len];
132 __ctype.widen(_S_atoms_in, _S_atoms_in + __len, __watoms);
133 bool __found_dec = false;
134 bool __found_sci = false;
135 const char_type __dec = __np.decimal_point();
137 string __found_grouping;
138 const string __grouping = __np.grouping();
139 bool __check_grouping = __grouping.size();
141 const char_type __sep = __np.thousands_sep();
143 while (__beg != __end)
145 // Only look in digits.
146 const char_type* __p = __traits_type::find(__watoms, 10, __c);
148 // NB: strchr returns true for __c == 0x0
149 if (__p && !__traits_type::eq(__c, char_type()))
151 // Try first for acceptable digit; record it if found.
153 __xtrc += _S_atoms_in[__p - __watoms];
157 else if (__traits_type::eq(__c, __sep)
158 && __check_grouping && !__found_dec)
160 // NB: Thousands separator at the beginning of a string
161 // is a no-no, as is two consecutive thousands separators.
164 __found_grouping += static_cast<char>(__sep_pos);
170 __err |= ios_base::failbit;
174 else if (__traits_type::eq(__c, __dec) && !__found_dec)
176 // According to the standard, if no grouping chars are seen,
177 // no grouping check is applied. Therefore __found_grouping
178 // must be adjusted only if __dec comes after some __sep.
179 if (__found_grouping.size())
180 __found_grouping += static_cast<char>(__sep_pos);
186 else if ((__traits_type::eq(__c, __watoms[_M_e])
187 || __traits_type::eq(__c, __watoms[_M_E]))
188 && !__found_sci && __pos)
190 // Scientific notation.
192 __xtrc += __ctype.narrow(__c, char());
195 // Remove optional plus or minus sign, if they exist.
196 if (__traits_type::eq(__c, __plus)
197 || __traits_type::eq(__c, __minus))
200 __xtrc += __ctype.narrow(__c, char());
206 // Not a valid input item.
210 // Digit grouping is checked. If grouping and found_grouping don't
211 // match, then get very very upset, and set failbit.
212 if (__check_grouping && __found_grouping.size())
214 // Add the ending grouping if a decimal wasn't found.
216 __found_grouping += static_cast<char>(__sep_pos);
217 if (!__verify_grouping(__grouping, __found_grouping))
218 __err |= ios_base::failbit;
224 __err |= ios_base::eofbit;
228 // Stage 1: Determine a conversion specifier.
229 template<typename _CharT, typename _InIter>
231 num_get<_CharT, _InIter>::
232 _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
233 ios_base::iostate& __err, string& __xtrc, int& __base) const
235 typedef char_traits<_CharT> __traits_type;
236 const locale __loc = __io.getloc();
237 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
238 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
240 // NB: Iff __basefield == 0, this can change based on contents.
241 ios_base::fmtflags __basefield = __io.flags() & ios_base::basefield;
242 if (__basefield == ios_base::oct)
244 else if (__basefield == ios_base::hex)
249 // First check for sign.
251 char_type __c = *__beg;
252 const char_type __plus = __ctype.widen('+');
253 const char_type __minus = __ctype.widen('-');
255 if ((__traits_type::eq(__c, __plus) || __traits_type::eq(__c, __minus))
258 __xtrc += __ctype.narrow(__c, char());
263 // Next, strip leading zeros and check required digits for base formats.
264 const char_type __zero = __ctype.widen(_S_atoms_in[_M_zero]);
265 const char_type __x = __ctype.widen('x');
266 const char_type __X = __ctype.widen('X');
269 bool __found_zero = false;
270 while (__traits_type::eq(__c, __zero) && __beg != __end)
277 __xtrc += _S_atoms_in[_M_zero];
279 if (__basefield == 0)
281 if ((__traits_type::eq(__c, __x)
282 || __traits_type::eq(__c, __X))
285 __xtrc += __ctype.narrow(__c, char());
295 else if (__base == 16)
297 if (__traits_type::eq(__c, __zero) && __beg != __end)
299 __xtrc += _S_atoms_in[_M_zero];
302 if ((__traits_type::eq(__c, __x) || __traits_type::eq(__c, __X))
305 __xtrc += __ctype.narrow(__c, char());
312 // At this point, base is determined. If not hex, only allow
313 // base digits as valid input.
321 char_type __watoms[_M_size];
322 __ctype.widen(_S_atoms_in, _S_atoms_in + __len, __watoms);
323 string __found_grouping;
324 const string __grouping = __np.grouping();
325 bool __check_grouping = __grouping.size();
327 const char_type __sep = __np.thousands_sep();
328 while (__beg != __end)
330 const char_type* __p = __traits_type::find(__watoms, __len, __c);
332 // NB: strchr returns true for __c == 0x0
333 if (__p && !__traits_type::eq(__c, char_type()))
335 // Try first for acceptable digit; record it if found.
336 __xtrc += _S_atoms_in[__p - __watoms];
341 else if (__traits_type::eq(__c, __sep) && __check_grouping)
343 // NB: Thousands separator at the beginning of a string
344 // is a no-no, as is two consecutive thousands separators.
347 __found_grouping += static_cast<char>(__sep_pos);
353 __err |= ios_base::failbit;
358 // Not a valid input item.
362 // Digit grouping is checked. If grouping and found_grouping don't
363 // match, then get very very upset, and set failbit.
364 if (__check_grouping && __found_grouping.size())
366 // Add the ending grouping.
367 __found_grouping += static_cast<char>(__sep_pos);
368 if (!__verify_grouping(__grouping, __found_grouping))
369 __err |= ios_base::failbit;
375 __err |= ios_base::eofbit;
379 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
380 //17. Bad bool parsing
381 template<typename _CharT, typename _InIter>
383 num_get<_CharT, _InIter>::
384 do_get(iter_type __beg, iter_type __end, ios_base& __io,
385 ios_base::iostate& __err, bool& __v) const
387 // Parse bool values as unsigned long
388 if (!(__io.flags() & ios_base::boolalpha))
390 // NB: We can't just call do_get(long) here, as it might
391 // refer to a derived class.
394 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
397 __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
398 if (!(__err & ios_base::failbit) && __ul <= 1)
401 __err |= ios_base::failbit;
404 // Parse bool values as alphanumeric
407 typedef char_traits<_CharT> __traits_type;
408 typedef basic_string<_CharT> __string_type;
410 locale __loc = __io.getloc();
411 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
412 const __string_type __true = __np.truename();
413 const __string_type __false = __np.falsename();
414 const char_type* __trues = __true.c_str();
415 const char_type* __falses = __false.c_str();
416 const size_t __truen = __true.size() - 1;
417 const size_t __falsen = __false.size() - 1;
419 for (size_t __n = 0; __beg != __end; ++__n)
421 char_type __c = *__beg++;
422 bool __testf = __n <= __falsen
423 ? __traits_type::eq(__c, __falses[__n]) : false;
424 bool __testt = __n <= __truen
425 ? __traits_type::eq(__c, __trues[__n]) : false;
426 if (!(__testf || __testt))
428 __err |= ios_base::failbit;
431 else if (__testf && __n == __falsen)
436 else if (__testt && __n == __truen)
443 __err |= ios_base::eofbit;
449 template<typename _CharT, typename _InIter>
451 num_get<_CharT, _InIter>::
452 do_get(iter_type __beg, iter_type __end, ios_base& __io,
453 ios_base::iostate& __err, long& __v) const
457 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
458 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
462 template<typename _CharT, typename _InIter>
464 num_get<_CharT, _InIter>::
465 do_get(iter_type __beg, iter_type __end, ios_base& __io,
466 ios_base::iostate& __err, unsigned short& __v) const
470 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
472 __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
473 if (!(__err & ios_base::failbit)
474 && __ul <= numeric_limits<unsigned short>::max())
475 __v = static_cast<unsigned short>(__ul);
477 __err |= ios_base::failbit;
481 template<typename _CharT, typename _InIter>
483 num_get<_CharT, _InIter>::
484 do_get(iter_type __beg, iter_type __end, ios_base& __io,
485 ios_base::iostate& __err, unsigned int& __v) const
489 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
491 __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
492 if (!(__err & ios_base::failbit)
493 && __ul <= numeric_limits<unsigned int>::max())
494 __v = static_cast<unsigned int>(__ul);
496 __err |= ios_base::failbit;
500 template<typename _CharT, typename _InIter>
502 num_get<_CharT, _InIter>::
503 do_get(iter_type __beg, iter_type __end, ios_base& __io,
504 ios_base::iostate& __err, unsigned long& __v) const
508 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
509 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
513 #ifdef _GLIBCPP_USE_LONG_LONG
514 template<typename _CharT, typename _InIter>
516 num_get<_CharT, _InIter>::
517 do_get(iter_type __beg, iter_type __end, ios_base& __io,
518 ios_base::iostate& __err, long long& __v) const
522 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
523 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
527 template<typename _CharT, typename _InIter>
529 num_get<_CharT, _InIter>::
530 do_get(iter_type __beg, iter_type __end, ios_base& __io,
531 ios_base::iostate& __err, unsigned long long& __v) const
535 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
536 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
541 template<typename _CharT, typename _InIter>
543 num_get<_CharT, _InIter>::
544 do_get(iter_type __beg, iter_type __end, ios_base& __io,
545 ios_base::iostate& __err, float& __v) const
549 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
550 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale);
554 template<typename _CharT, typename _InIter>
556 num_get<_CharT, _InIter>::
557 do_get(iter_type __beg, iter_type __end, ios_base& __io,
558 ios_base::iostate& __err, double& __v) const
562 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
563 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale);
567 template<typename _CharT, typename _InIter>
569 num_get<_CharT, _InIter>::
570 do_get(iter_type __beg, iter_type __end, ios_base& __io,
571 ios_base::iostate& __err, long double& __v) const
575 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
576 __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale);
580 template<typename _CharT, typename _InIter>
582 num_get<_CharT, _InIter>::
583 do_get(iter_type __beg, iter_type __end, ios_base& __io,
584 ios_base::iostate& __err, void*& __v) const
586 // Prepare for hex formatted input
587 typedef ios_base::fmtflags fmtflags;
588 fmtflags __fmt = __io.flags();
589 fmtflags __fmtmask = ~(ios_base::showpos | ios_base::basefield
590 | ios_base::uppercase | ios_base::internal);
591 __io.flags(__fmt & __fmtmask | (ios_base::hex | ios_base::showbase));
595 __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
597 // Reset from hex formatted input
601 __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
602 if (!(__err & ios_base::failbit))
603 __v = reinterpret_cast<void*>(__ul);
605 __err |= ios_base::failbit;
609 // For use by integer and floating-point types after they have been
610 // converted into a char_type string.
611 template<typename _CharT, typename _OutIter>
613 num_put<_CharT, _OutIter>::
614 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
615 _CharT* __new, const _CharT* __cs, int& __len) const
617 // [22.2.2.2.2] Stage 3.
618 // If necessary, pad.
619 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new, __cs,
621 __len = static_cast<int>(__w);
624 // Forwarding functions to peel signed from unsigned integer types.
625 template<typename _CharT>
627 __int_to_char(_CharT* __out, const int __size, long __v,
628 const _CharT* __lit, ios_base::fmtflags __flags)
630 unsigned long __ul = static_cast<unsigned long>(__v);
637 return __int_to_char(__out, __size, __ul, __lit, __flags, __neg);
640 template<typename _CharT>
642 __int_to_char(_CharT* __out, const int __size, unsigned long __v,
643 const _CharT* __lit, ios_base::fmtflags __flags)
644 { return __int_to_char(__out, __size, __v, __lit, __flags, false); }
646 #ifdef _GLIBCPP_USE_LONG_LONG
647 template<typename _CharT>
649 __int_to_char(_CharT* __out, const int __size, long long __v,
650 const _CharT* __lit, ios_base::fmtflags __flags)
652 unsigned long long __ull = static_cast<unsigned long long>(__v);
659 return __int_to_char(__out, __size, __ull, __lit, __flags, __neg);
662 template<typename _CharT>
664 __int_to_char(_CharT* __out, const int __size, unsigned long long __v,
665 const _CharT* __lit, ios_base::fmtflags __flags)
666 { return __int_to_char(__out, __size, __v, __lit, __flags, false); }
669 template<typename _CharT, typename _ValueT>
671 __int_to_char(_CharT* __out, const int __size, _ValueT __v,
672 const _CharT* __lit, ios_base::fmtflags __flags, bool __neg)
674 // Don't write base if already 0.
675 const bool __showbase = (__flags & ios_base::showbase) && __v;
676 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
677 _CharT* __buf = __out + __size - 1;
678 _CharT* __bufend = __out + __size;
680 if (__builtin_expect(__basefield == ios_base::oct, false))
685 *__buf-- = __lit[(__v & 0x7) + __num_base::_S_digits];
690 *__buf-- = __lit[__num_base::_S_digits];
692 else if (__builtin_expect(__basefield == ios_base::hex, false))
695 const bool __uppercase = __flags & ios_base::uppercase;
696 int __case_offset = __uppercase
697 ? __num_base::_S_udigits : __num_base::_S_digits;
700 *__buf-- = __lit[(__v & 0xf) + __case_offset];
707 *__buf-- = __lit[__num_base::_S_x + __uppercase];
709 *__buf-- = __lit[__num_base::_S_digits];
717 *__buf-- = __lit[(__v % 10) + __num_base::_S_digits];
722 *__buf-- = __lit[__num_base::_S_minus];
723 else if (__flags & ios_base::showpos)
724 *__buf-- = __lit[__num_base::_S_plus];
726 int __ret = __bufend - __buf - 1;
730 template<typename _CharT, typename _OutIter>
732 num_put<_CharT, _OutIter>::
733 _M_group_int(const string& __grouping, _CharT __sep, ios_base& __io,
734 _CharT* __new, _CharT* __cs, int& __len) const
736 // By itself __add_grouping cannot deal correctly with __ws when
737 // ios::showbase is set and ios_base::oct || ios_base::hex.
738 // Therefore we take care "by hand" of the initial 0, 0x or 0X.
739 // However, remember that the latter do not occur if the number
740 // printed is '0' (__len == 1).
741 streamsize __off = 0;
742 const ios_base::fmtflags __basefield = __io.flags()
743 & ios_base::basefield;
744 if ((__io.flags() & ios_base::showbase) && __len > 1)
745 if (__basefield == ios_base::oct)
750 else if (__basefield == ios_base::hex)
754 *(__new + 1) = *(__cs + 1);
757 __p = __add_grouping(__new + __off, __sep,
759 __grouping.c_str() + __grouping.size(),
760 __cs + __off, __cs + __len);
764 template<typename _CharT, typename _OutIter>
765 template<typename _ValueT>
767 num_put<_CharT, _OutIter>::
768 _M_convert_int(_OutIter __s, ios_base& __io, _CharT __fill,
771 // Buildup list of digits given the current ctype.
772 _CharT __lit[_S_udigits_end];
773 const locale __loc = __io.getloc();
774 if (__builtin_expect(has_facet< ctype<_CharT> >(__loc), true))
776 const ctype<_CharT>& __ct = use_facet< ctype<_CharT> >(__loc);
777 __ct.widen(_S_atoms_out, _S_atoms_out + _S_udigits_end, __lit);
780 // Long enough to hold hex, dec, and octal representations.
781 int __ilen = 4 * sizeof(_ValueT);
782 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
785 // [22.2.2.2.2] Stage 1, numeric conversion to character.
786 // Result is returned right-justified in the buffer.
788 __len = __int_to_char(&__cs[0], __ilen, __v, __lit, __io.flags());
789 __cs = __cs + __ilen - __len;
791 // Add grouping, if necessary.
793 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
794 const string __grouping = __np.grouping();
795 if (__grouping.size())
797 // Grouping can add (almost) as many separators as the
798 // number of digits, but no more.
799 __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
801 _M_group_int(__grouping, __np.thousands_sep(), __io,
808 streamsize __w = __io.width();
809 if (__w > static_cast<streamsize>(__len))
811 __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
813 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
818 // [22.2.2.2.2] Stage 4.
819 // Write resulting, fully-formatted string to output iterator.
820 return __write(__s, __cs, __len);
823 template<typename _CharT, typename _OutIter>
825 num_put<_CharT, _OutIter>::
826 _M_group_float(const string& __grouping, _CharT __sep, const _CharT* __p,
827 _CharT* __new, _CharT* __cs, int& __len) const
829 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
830 //282. What types does numpunct grouping refer to?
831 // Add grouping, if necessary.
833 int __declen = __p ? __p - __cs : __len;
834 __p2 = __add_grouping(__new, __sep,
836 __grouping.c_str() + __grouping.size(),
837 __cs, __cs + __declen);
839 // Tack on decimal part.
840 int __newlen = __p2 - __new;
843 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
844 __newlen += __len - __declen;
850 // The following code uses snprintf (or sprintf(), when
851 // _GLIBCPP_USE_C99 is not defined) to convert floating point values
852 // for insertion into a stream. An optimization would be to replace
853 // them with code that works directly on a wide buffer and then use
854 // __pad to do the padding. It would be good to replace them anyway
855 // to gain back the efficiency that C++ provides by knowing up front
856 // the type of the values to insert. Also, sprintf is dangerous
857 // since may lead to accidental buffer overruns. This
858 // implementation follows the C++ standard fairly directly as
859 // outlined in 22.2.2.2 [lib.locale.num.put]
860 template<typename _CharT, typename _OutIter>
861 template<typename _ValueT>
863 num_put<_CharT, _OutIter>::
864 _M_convert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
867 // Note: digits10 is rounded down: add 1 to ensure the maximum
868 // available precision. Then, in general, one more 1 needs to
869 // be added since, when the %{g,G} conversion specifiers are
870 // chosen inside _S_format_float, the precision field is "the
871 // maximum number of significant digits", *not* the "number of
872 // digits to appear after the decimal point", as happens for
873 // %{e,E,f,F} (C99, 7.19.6.1,4).
874 const int __max_digits = numeric_limits<_ValueT>::digits10 + 2;
876 // Use default precision if out of range.
877 streamsize __prec = __io.precision();
878 if (__prec > static_cast<streamsize>(__max_digits))
879 __prec = static_cast<streamsize>(__max_digits);
880 else if (__prec < static_cast<streamsize>(0))
881 __prec = static_cast<streamsize>(6);
883 // [22.2.2.2.2] Stage 1, numeric conversion to character.
885 // Long enough for the max format spec.
888 #ifdef _GLIBCPP_USE_C99
889 // First try a buffer perhaps big enough (for sure sufficient
890 // for non-ios_base::fixed outputs)
891 int __cs_size = __max_digits * 3;
892 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
894 _S_format_float(__io, __fbuf, __mod);
895 __len = __convert_from_v(__cs, __cs_size, __fbuf, __v,
896 _S_c_locale, __prec);
898 // If the buffer was not large enough, try again with the correct size.
899 if (__len >= __cs_size)
901 __cs_size = __len + 1;
902 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
903 __len = __convert_from_v(__cs, __cs_size, __fbuf, __v,
904 _S_c_locale, __prec);
907 // Consider the possibility of long ios_base::fixed outputs
908 const bool __fixed = __io.flags() & ios_base::fixed;
909 const int __max_exp = numeric_limits<_ValueT>::max_exponent10;
911 // ios_base::fixed outputs may need up to __max_exp+1 chars
912 // for the integer part + up to __max_digits chars for the
913 // fractional part + 3 chars for sign, decimal point, '\0'. On
914 // the other hand, for non-fixed outputs __max_digits*3 chars
915 // are largely sufficient.
916 const int __cs_size = __fixed ? __max_exp + __max_digits + 4
918 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
920 _S_format_float(__io, __fbuf, __mod);
921 __len = __convert_from_v(__cs, 0, __fbuf, __v, _S_c_locale, __prec);
924 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
925 // numpunct.decimal_point() values for '.' and adding grouping.
926 const locale __loc = __io.getloc();
927 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
928 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
930 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
932 __ctype.widen(__cs, __cs + __len, __ws);
934 // Replace decimal point.
935 const _CharT __cdec = __ctype.widen('.');
936 const _CharT __dec = __np.decimal_point();
938 if (__p = char_traits<_CharT>::find(__ws, __len, __cdec))
939 __ws[__p - __ws] = __dec;
941 // Add grouping, if necessary.
943 const string __grouping = __np.grouping();
944 if (__grouping.size())
946 // Grouping can add (almost) as many separators as the
947 // number of digits, but no more.
948 __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
950 _M_group_float(__grouping, __np.thousands_sep(), __p,
957 streamsize __w = __io.width();
958 if (__w > static_cast<streamsize>(__len))
960 __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
961 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
966 // [22.2.2.2.2] Stage 4.
967 // Write resulting, fully-formatted string to output iterator.
968 return __write(__s, __ws, __len);
971 template<typename _CharT, typename _OutIter>
973 num_put<_CharT, _OutIter>::
974 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
976 ios_base::fmtflags __flags = __io.flags();
977 if ((__flags & ios_base::boolalpha) == 0)
979 unsigned long __uv = __v;
980 __s = _M_convert_int(__s, __io, __fill, __uv);
984 locale __loc = __io.getloc();
985 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
986 typedef basic_string<_CharT> __string_type;
987 __string_type __name;
989 __name = __np.truename();
991 __name = __np.falsename();
993 const _CharT* __cs = __name.c_str();
994 int __len = __name.size();
996 streamsize __w = __io.width();
997 if (__w > static_cast<streamsize>(__len))
999 __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1001 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
1005 __s = __write(__s, __cs, __len);
1010 template<typename _CharT, typename _OutIter>
1012 num_put<_CharT, _OutIter>::
1013 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
1014 { return _M_convert_int(__s, __io, __fill, __v); }
1016 template<typename _CharT, typename _OutIter>
1018 num_put<_CharT, _OutIter>::
1019 do_put(iter_type __s, ios_base& __io, char_type __fill,
1020 unsigned long __v) const
1021 { return _M_convert_int(__s, __io, __fill, __v); }
1023 #ifdef _GLIBCPP_USE_LONG_LONG
1024 template<typename _CharT, typename _OutIter>
1026 num_put<_CharT, _OutIter>::
1027 do_put(iter_type __s, ios_base& __b, char_type __fill, long long __v) const
1028 { return _M_convert_int(__s, __b, __fill, __v); }
1030 template<typename _CharT, typename _OutIter>
1032 num_put<_CharT, _OutIter>::
1033 do_put(iter_type __s, ios_base& __io, char_type __fill,
1034 unsigned long long __v) const
1035 { return _M_convert_int(__s, __io, __fill, __v); }
1038 template<typename _CharT, typename _OutIter>
1040 num_put<_CharT, _OutIter>::
1041 do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1042 { return _M_convert_float(__s, __io, __fill, char(), __v); }
1044 template<typename _CharT, typename _OutIter>
1046 num_put<_CharT, _OutIter>::
1047 do_put(iter_type __s, ios_base& __io, char_type __fill,
1048 long double __v) const
1049 { return _M_convert_float(__s, __io, __fill, 'L', __v); }
1051 template<typename _CharT, typename _OutIter>
1053 num_put<_CharT, _OutIter>::
1054 do_put(iter_type __s, ios_base& __io, char_type __fill,
1055 const void* __v) const
1057 ios_base::fmtflags __flags = __io.flags();
1058 ios_base::fmtflags __fmt = ~(ios_base::showpos | ios_base::basefield
1059 | ios_base::uppercase | ios_base::internal);
1060 __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase));
1063 __s = _M_convert_int(__s, __io, __fill,
1064 reinterpret_cast<unsigned long>(__v));
1065 __io.flags(__flags);
1069 __io.flags(__flags);
1070 __throw_exception_again;
1076 template<typename _CharT, typename _InIter>
1078 money_get<_CharT, _InIter>::
1079 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1080 ios_base::iostate& __err, long double& __units) const
1083 __beg = this->do_get(__beg, __end, __intl, __io, __err, __str);
1085 const int __n = numeric_limits<long double>::digits10;
1086 char* __cs = static_cast<char*>(__builtin_alloca(__n));
1087 const locale __loc = __io.getloc();
1088 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1089 const _CharT* __wcs = __str.c_str();
1090 __ctype.narrow(__wcs, __wcs + __str.size() + 1, char(), __cs);
1091 __convert_to_v(__cs, __units, __err, _S_c_locale);
1095 template<typename _CharT, typename _InIter>
1097 money_get<_CharT, _InIter>::
1098 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1099 ios_base::iostate& __err, string_type& __units) const
1101 // These contortions are quite unfortunate.
1102 typedef moneypunct<_CharT, true> __money_true;
1103 typedef moneypunct<_CharT, false> __money_false;
1104 typedef money_base::part part;
1105 typedef typename string_type::size_type size_type;
1107 const locale __loc = __io.getloc();
1108 const __money_true& __mpt = use_facet<__money_true>(__loc);
1109 const __money_false& __mpf = use_facet<__money_false>(__loc);
1110 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1112 const money_base::pattern __p = __intl ? __mpt.neg_format()
1113 : __mpf.neg_format();
1115 const string_type __pos_sign =__intl ? __mpt.positive_sign()
1116 : __mpf.positive_sign();
1117 const string_type __neg_sign =__intl ? __mpt.negative_sign()
1118 : __mpf.negative_sign();
1119 const char_type __d = __intl ? __mpt.decimal_point()
1120 : __mpf.decimal_point();
1121 const char_type __sep = __intl ? __mpt.thousands_sep()
1122 : __mpf.thousands_sep();
1124 const string __grouping = __intl ? __mpt.grouping() : __mpf.grouping();
1126 // Set to deduced positive or negative sign, depending.
1128 // String of grouping info from thousands_sep plucked from __units.
1129 string __grouping_tmp;
1130 // Marker for thousands_sep position.
1132 // If input iterator is in a valid state.
1133 bool __testvalid = true;
1134 // Flag marking when a decimal point is found.
1135 bool __testdecfound = false;
1137 // The tentative returned string is stored here.
1138 string_type __temp_units;
1140 char_type __c = *__beg;
1141 char_type __eof = static_cast<char_type>(char_traits<char_type>::eof());
1142 for (int __i = 0; __beg != __end && __i < 4 && __testvalid; ++__i)
1144 part __which = static_cast<part>(__p.field[__i]);
1147 case money_base::symbol:
1148 if (__io.flags() & ios_base::showbase
1149 || __i < 2 || __sign.size() > 1
1150 || ((static_cast<part>(__p.field[3]) != money_base::none)
1153 // According to 22.2.6.1.2.2, symbol is required
1154 // if (__io.flags() & ios_base::showbase),
1155 // otherwise is optional and consumed only if
1156 // other characters are needed to complete the
1158 const string_type __symbol = __intl ? __mpt.curr_symbol()
1159 : __mpf.curr_symbol();
1160 size_type __len = __symbol.size();
1162 while (__beg != __end
1163 && __j < __len && __symbol[__j] == __c)
1168 // When (__io.flags() & ios_base::showbase)
1169 // symbol is required.
1170 if (__j != __len && (__io.flags() & ios_base::showbase))
1171 __testvalid = false;
1174 case money_base::sign:
1175 // Sign might not exist, or be more than one character long.
1176 if (__pos_sign.size() && __neg_sign.size())
1178 // Sign is mandatory.
1179 if (__c == __pos_sign[0])
1181 __sign = __pos_sign;
1184 else if (__c == __neg_sign[0])
1186 __sign = __neg_sign;
1190 __testvalid = false;
1192 else if (__pos_sign.size() && __c == __pos_sign[0])
1194 __sign = __pos_sign;
1197 else if (__neg_sign.size() && __c == __neg_sign[0])
1199 __sign = __neg_sign;
1203 case money_base::value:
1204 // Extract digits, remove and stash away the
1205 // grouping of found thousands separators.
1206 while (__beg != __end
1207 && (__ctype.is(ctype_base::digit, __c)
1208 || (__c == __d && !__testdecfound)
1213 __grouping_tmp += static_cast<char>(__sep_pos);
1215 __testdecfound = true;
1217 else if (__c == __sep)
1219 if (__grouping.size())
1221 // Mark position for later analysis.
1222 __grouping_tmp += static_cast<char>(__sep_pos);
1227 __testvalid = false;
1233 __temp_units += __c;
1239 case money_base::space:
1240 case money_base::none:
1241 // Only if not at the end of the pattern.
1243 while (__beg != __end
1244 && __ctype.is(ctype_base::space, __c))
1250 // Need to get the rest of the sign characters, if they exist.
1251 if (__sign.size() > 1)
1253 size_type __len = __sign.size();
1255 for (; __c != __eof && __i < __len; ++__i)
1256 while (__beg != __end && __c != __sign[__i])
1260 __testvalid = false;
1263 // Strip leading zeros.
1264 while (__temp_units[0] == __ctype.widen('0'))
1265 __temp_units.erase(__temp_units.begin());
1267 if (__sign.size() && __sign == __neg_sign)
1268 __temp_units.insert(__temp_units.begin(), __ctype.widen('-'));
1270 // Test for grouping fidelity.
1271 if (__grouping.size() && __grouping_tmp.size())
1273 if (!__verify_grouping(__grouping, __grouping_tmp))
1274 __testvalid = false;
1277 // Iff no more characters are available.
1279 __err |= ios_base::eofbit;
1281 // Iff valid sequence is not recognized.
1282 if (!__testvalid || !__temp_units.size())
1283 __err |= ios_base::failbit;
1285 // Use the "swap trick" to copy __temp_units into __units.
1286 __temp_units.swap(__units);
1291 template<typename _CharT, typename _OutIter>
1293 money_put<_CharT, _OutIter>::
1294 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1295 long double __units) const
1297 const locale __loc = __io.getloc();
1298 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1299 #ifdef _GLIBCPP_USE_C99
1300 // First try a buffer perhaps big enough.
1302 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1303 int __len = __convert_from_v(__cs, __cs_size, "%.01Lf", __units,
1305 // If the buffer was not large enough, try again with the correct size.
1306 if (__len >= __cs_size)
1308 __cs_size = __len + 1;
1309 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1310 __len = __convert_from_v(__cs, __cs_size, "%.01Lf", __units,
1314 // max_exponent10 + 1 for the integer part, + 4 for sign, decimal point,
1315 // decimal digit, '\0'.
1316 const int __cs_size = numeric_limits<long double>::max_exponent10 + 5;
1317 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1318 int __len = __convert_from_v(__cs, 0, "%.01Lf", __units, _S_c_locale);
1320 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1322 __ctype.widen(__cs, __cs + __len, __ws);
1323 string_type __digits(__ws);
1324 return this->do_put(__s, __intl, __io, __fill, __digits);
1327 template<typename _CharT, typename _OutIter>
1329 money_put<_CharT, _OutIter>::
1330 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1331 const string_type& __digits) const
1333 typedef typename string_type::size_type size_type;
1334 typedef money_base::part part;
1336 const locale __loc = __io.getloc();
1337 const size_type __width = static_cast<size_type>(__io.width());
1339 // These contortions are quite unfortunate.
1340 typedef moneypunct<_CharT, true> __money_true;
1341 typedef moneypunct<_CharT, false> __money_false;
1342 const __money_true& __mpt = use_facet<__money_true>(__loc);
1343 const __money_false& __mpf = use_facet<__money_false>(__loc);
1344 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1346 // Determine if negative or positive formats are to be used, and
1347 // discard leading negative_sign if it is present.
1348 const char_type* __beg = __digits.data();
1349 const char_type* __end = __beg + __digits.size();
1350 money_base::pattern __p;
1352 if (*__beg != __ctype.widen('-'))
1354 __p = __intl ? __mpt.pos_format() : __mpf.pos_format();
1355 __sign =__intl ? __mpt.positive_sign() : __mpf.positive_sign();
1359 __p = __intl ? __mpt.neg_format() : __mpf.neg_format();
1360 __sign =__intl ? __mpt.negative_sign() : __mpf.negative_sign();
1364 // Look for valid numbers in the current ctype facet within input digits.
1365 __end = __ctype.scan_not(ctype_base::digit, __beg, __end);
1368 // Assume valid input, and attempt to format.
1369 // Break down input numbers into base components, as follows:
1370 // final_value = grouped units + (decimal point) + (digits)
1372 string_type __value;
1373 const string_type __symbol = __intl ? __mpt.curr_symbol()
1374 : __mpf.curr_symbol();
1376 // Deal with decimal point, decimal digits.
1377 const int __frac = __intl ? __mpt.frac_digits()
1378 : __mpf.frac_digits();
1381 const char_type __d = __intl ? __mpt.decimal_point()
1382 : __mpf.decimal_point();
1383 if (__end - __beg >= __frac)
1385 __value = string_type(__end - __frac, __end);
1386 __value.insert(__value.begin(), __d);
1391 // Have to pad zeros in the decimal position.
1392 __value = string_type(__beg, __end);
1393 int __paddec = __frac - (__end - __beg);
1394 char_type __zero = __ctype.widen('0');
1395 __value.insert(__value.begin(), __paddec, __zero);
1396 __value.insert(__value.begin(), __d);
1401 // Add thousands separators to non-decimal digits, per
1405 const string __grouping = __intl ? __mpt.grouping()
1407 if (__grouping.size())
1409 const char_type __sep = __intl ? __mpt.thousands_sep()
1410 : __mpf.thousands_sep();
1411 const char* __gbeg = __grouping.c_str();
1412 const char* __gend = __gbeg + __grouping.size();
1413 const int __n = (__end - __beg) * 2;
1415 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __n));
1416 _CharT* __ws_end = __add_grouping(__ws2, __sep, __gbeg,
1417 __gend, __beg, __end);
1418 __value.insert(0, __ws2, __ws_end - __ws2);
1421 __value.insert(0, string_type(__beg, __end));
1424 // Calculate length of resulting string.
1425 ios_base::fmtflags __f = __io.flags() & ios_base::adjustfield;
1426 size_type __len = __value.size() + __sign.size();
1427 __len += (__io.flags() & ios_base::showbase) ? __symbol.size() : 0;
1428 bool __testipad = __f == ios_base::internal && __len < __width;
1430 // Fit formatted digits into the required pattern.
1431 for (int __i = 0; __i < 4; ++__i)
1433 part __which = static_cast<part>(__p.field[__i]);
1436 case money_base::symbol:
1437 if (__io.flags() & ios_base::showbase)
1440 case money_base::sign:
1441 // Sign might not exist, or be more than one
1442 // charater long. In that case, add in the rest
1447 case money_base::value:
1450 case money_base::space:
1451 // At least one space is required, but if internal
1452 // formatting is required, an arbitrary number of
1453 // fill spaces will be necessary.
1455 __res += string_type(__width - __len, __fill);
1457 __res += __ctype.widen(__fill);
1459 case money_base::none:
1461 __res += string_type(__width - __len, __fill);
1466 // Special case of multi-part sign parts.
1467 if (__sign.size() > 1)
1468 __res += string_type(__sign.begin() + 1, __sign.end());
1470 // Pad, if still necessary.
1471 __len = __res.size();
1472 if (__width > __len)
1474 if (__f == ios_base::left)
1476 __res.append(__width - __len, __fill);
1479 __res.insert(0, string_type(__width - __len, __fill));
1483 // Write resulting, fully-formatted string to output iterator.
1484 __s = __write(__s, __res.c_str(), __len);
1491 // NB: Not especially useful. Without an ios_base object or some
1492 // kind of locale reference, we are left clawing at the air where
1493 // the side of the mountain used to be...
1494 template<typename _CharT, typename _InIter>
1495 time_base::dateorder
1496 time_get<_CharT, _InIter>::do_date_order() const
1497 { return time_base::no_order; }
1499 template<typename _CharT, typename _InIter>
1501 time_get<_CharT, _InIter>::
1502 _M_extract_via_format(iter_type& __beg, iter_type& __end, ios_base& __io,
1503 ios_base::iostate& __err, tm* __tm,
1504 const _CharT* __format) const
1506 locale __loc = __io.getloc();
1507 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1508 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1509 size_t __len = char_traits<_CharT>::length(__format);
1511 for (size_t __i = 0; __beg != __end && __i < __len && !__err; ++__i)
1513 char __c = __format[__i];
1516 // Verify valid formatting code, attempt to extract.
1517 __c = __format[++__i];
1520 if (__c == 'E' || __c == 'O')
1523 __c = __format[++__i];
1530 // Abbreviated weekday name [tm_wday]
1531 const char_type* __days1[7];
1532 __tp._M_days_abbreviated(__days1);
1533 _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 7,
1537 // Weekday name [tm_wday].
1538 const char_type* __days2[7];
1539 __tp._M_days(__days2);
1540 _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 7,
1545 // Abbreviated month name [tm_mon]
1546 const char_type* __months1[12];
1547 __tp._M_months_abbreviated(__months1);
1548 _M_extract_name(__beg, __end, __tm->tm_mon, __months1, 12,
1552 // Month name [tm_mon].
1553 const char_type* __months2[12];
1554 __tp._M_months(__months2);
1555 _M_extract_name(__beg, __end, __tm->tm_mon, __months2, 12,
1559 // Default time and date representation.
1560 const char_type* __dt[2];
1561 __tp._M_date_time_formats(__dt);
1562 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1566 // Day [01, 31]. [tm_mday]
1567 _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2,
1571 // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
1573 __ctype.widen(__cs, __cs + 9, __wcs);
1574 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1578 // Hour [00, 23]. [tm_hour]
1579 _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
1583 // Hour [01, 12]. [tm_hour]
1584 _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2,
1588 // Month [01, 12]. [tm_mon]
1589 _M_extract_num(__beg, __end, __mem, 1, 12, 2, __ctype,
1592 __tm->tm_mon = __mem - 1;
1595 // Minute [00, 59]. [tm_min]
1596 _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
1600 if (__ctype.narrow(*__beg, 0) == '\n')
1603 __err |= ios_base::failbit;
1606 // Equivalent to (%H:%M).
1608 __ctype.widen(__cs, __cs + 6, __wcs);
1609 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1614 _M_extract_num(__beg, __end, __tm->tm_sec, 0, 59, 2,
1618 if (__ctype.narrow(*__beg, 0) == '\t')
1621 __err |= ios_base::failbit;
1624 // Equivalent to (%H:%M:%S).
1626 __ctype.widen(__cs, __cs + 9, __wcs);
1627 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1632 const char_type* __dates[2];
1633 __tp._M_date_formats(__dates);
1634 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1639 const char_type* __times[2];
1640 __tp._M_time_formats(__times);
1641 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1645 // Two digit year. [tm_year]
1646 _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2,
1650 // Year [1900). [tm_year]
1651 _M_extract_num(__beg, __end, __mem, 0,
1652 numeric_limits<int>::max(), 4,
1655 __tm->tm_year = __mem - 1900;
1659 if (__ctype.is(ctype_base::upper, *__beg))
1662 _M_extract_name(__beg, __end, __tmp,
1663 __timepunct<_CharT>::_S_timezones,
1666 // GMT requires special effort.
1667 char_type __c = *__beg;
1668 if (!__err && __tmp == 0
1669 && (__c == __ctype.widen('-')
1670 || __c == __ctype.widen('+')))
1672 _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
1674 _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
1679 __err |= ios_base::failbit;
1683 __err |= ios_base::failbit;
1688 // Verify format and input match, extract and discard.
1689 if (__c == __ctype.narrow(*__beg, 0))
1692 __err |= ios_base::failbit;
1697 template<typename _CharT, typename _InIter>
1699 time_get<_CharT, _InIter>::
1700 _M_extract_num(iter_type& __beg, iter_type& __end, int& __member,
1701 int __min, int __max, size_t __len,
1702 const ctype<_CharT>& __ctype,
1703 ios_base::iostate& __err) const
1707 bool __testvalid = true;
1708 char_type __c = *__beg;
1709 while (__beg != __end && __i < __len
1710 && __ctype.is(ctype_base::digit, __c))
1712 __digits += __ctype.narrow(__c, 0);
1718 int __value = atoi(__digits.c_str());
1719 if (__min <= __value && __value <= __max)
1722 __testvalid = false;
1725 __testvalid = false;
1727 __err |= ios_base::failbit;
1731 // All elements in __names are unique.
1732 template<typename _CharT, typename _InIter>
1734 time_get<_CharT, _InIter>::
1735 _M_extract_name(iter_type& __beg, iter_type& __end, int& __member,
1736 const _CharT** __names, size_t __indexlen,
1737 ios_base::iostate& __err) const
1739 typedef char_traits<_CharT> __traits_type;
1740 int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int)
1742 size_t __nmatches = 0;
1744 bool __testvalid = true;
1745 const char_type* __name;
1747 char_type __c = *__beg;
1748 // Look for initial matches.
1749 for (size_t __i1 = 0; __i1 < __indexlen; ++__i1)
1750 if (__c == __names[__i1][0])
1751 __matches[__nmatches++] = __i1;
1753 while (__nmatches > 1)
1755 // Find smallest matching string.
1756 size_t __minlen = 10;
1757 for (size_t __i2 = 0; __i2 < __nmatches; ++__i2)
1758 __minlen = min(__minlen,
1759 __traits_type::length(__names[__matches[__i2]]));
1761 if (__pos < __minlen && __beg != __end)
1765 for (size_t __i3 = 0; __i3 < __nmatches; ++__i3)
1767 __name = __names[__matches[__i3]];
1768 if (__name[__pos] != __c)
1769 __matches[__i3] = __matches[--__nmatches];
1776 if (__nmatches == 1)
1778 // Make sure found name is completely extracted.
1779 __name = __names[__matches[0]];
1780 const size_t __len = __traits_type::length(__name);
1781 while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
1785 __member = __matches[0];
1787 __testvalid = false;
1790 __testvalid = false;
1792 __err |= ios_base::failbit;
1795 template<typename _CharT, typename _InIter>
1797 time_get<_CharT, _InIter>::
1798 do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
1799 ios_base::iostate& __err, tm* __tm) const
1802 const char* __cs = "%X";
1803 locale __loc = __io.getloc();
1804 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1805 __ctype.widen(__cs, __cs + 3, __wcs);
1806 _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1808 __err |= ios_base::eofbit;
1812 template<typename _CharT, typename _InIter>
1814 time_get<_CharT, _InIter>::
1815 do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
1816 ios_base::iostate& __err, tm* __tm) const
1819 const char* __cs = "%x";
1820 locale __loc = __io.getloc();
1821 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1822 __ctype.widen(__cs, __cs + 3, __wcs);
1823 _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1825 __err |= ios_base::eofbit;
1829 template<typename _CharT, typename _InIter>
1831 time_get<_CharT, _InIter>::
1832 do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
1833 ios_base::iostate& __err, tm* __tm) const
1835 typedef char_traits<_CharT> __traits_type;
1836 locale __loc = __io.getloc();
1837 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1838 const char_type* __days[7];
1839 __tp._M_days_abbreviated(__days);
1841 _M_extract_name(__beg, __end, __tmpwday, __days, 7, __err);
1843 // Check to see if non-abbreviated name exists, and extract.
1844 // NB: Assumes both _M_days and _M_days_abbreviated organized in
1845 // exact same order, first to last, such that the resulting
1846 // __days array with the same index points to a day, and that
1847 // day's abbreviated form.
1848 // NB: Also assumes that an abbreviated name is a subset of the name.
1851 size_t __pos = __traits_type::length(__days[__tmpwday]);
1852 __tp._M_days(__days);
1853 const char_type* __name = __days[__tmpwday];
1854 if (__name[__pos] == *__beg)
1856 // Extract the rest of it.
1857 const size_t __len = __traits_type::length(__name);
1858 while (__pos < __len && __beg != __end
1859 && __name[__pos] == *__beg)
1862 __err |= ios_base::failbit;
1865 __tm->tm_wday = __tmpwday;
1868 __err |= ios_base::eofbit;
1872 template<typename _CharT, typename _InIter>
1874 time_get<_CharT, _InIter>::
1875 do_get_monthname(iter_type __beg, iter_type __end,
1876 ios_base& __io, ios_base::iostate& __err, tm* __tm) const
1878 typedef char_traits<_CharT> __traits_type;
1879 locale __loc = __io.getloc();
1880 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1881 const char_type* __months[12];
1882 __tp._M_months_abbreviated(__months);
1884 _M_extract_name(__beg, __end, __tmpmon, __months, 12, __err);
1886 // Check to see if non-abbreviated name exists, and extract.
1887 // NB: Assumes both _M_months and _M_months_abbreviated organized in
1888 // exact same order, first to last, such that the resulting
1889 // __months array with the same index points to a month, and that
1890 // month's abbreviated form.
1891 // NB: Also assumes that an abbreviated name is a subset of the name.
1894 size_t __pos = __traits_type::length(__months[__tmpmon]);
1895 __tp._M_months(__months);
1896 const char_type* __name = __months[__tmpmon];
1897 if (__name[__pos] == *__beg)
1899 // Extract the rest of it.
1900 const size_t __len = __traits_type::length(__name);
1901 while (__pos < __len && __beg != __end
1902 && __name[__pos] == *__beg)
1905 __err |= ios_base::failbit;
1908 __tm->tm_mon = __tmpmon;
1912 __err |= ios_base::eofbit;
1916 template<typename _CharT, typename _InIter>
1918 time_get<_CharT, _InIter>::
1919 do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
1920 ios_base::iostate& __err, tm* __tm) const
1922 locale __loc = __io.getloc();
1923 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1925 char_type __c = *__beg;
1928 while (__i < 4 && __beg != __end && __ctype.is(ctype_base::digit, __c))
1930 __digits += __ctype.narrow(__c, 0);
1934 if (__i == 2 || __i == 4)
1937 __convert_to_v(__digits.c_str(), __l, __err, _S_c_locale);
1938 if (!(__err & ios_base::failbit) && __l <= INT_MAX)
1940 __l = __i == 2 ? __l : __l - 1900;
1941 __tm->tm_year = static_cast<int>(__l);
1945 __err |= ios_base::failbit;
1947 __err |= ios_base::eofbit;
1951 template<typename _CharT, typename _OutIter>
1953 time_put<_CharT, _OutIter>::
1954 put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
1955 const _CharT* __beg, const _CharT* __end) const
1957 locale __loc = __io.getloc();
1958 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1959 while (__beg != __end)
1961 char __c = __ctype.narrow(*__beg, 0);
1968 __c = __ctype.narrow(*__beg, 0);
1970 if (__c == 'E' || __c == 'O')
1973 __format = __ctype.narrow(*__beg, 0);
1978 __s = this->do_put(__s, __io, _CharT(), __tm, __format, __mod);
1989 template<typename _CharT, typename _OutIter>
1991 time_put<_CharT, _OutIter>::
1992 do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
1993 char __format, char __mod) const
1995 locale __loc = __io.getloc();
1996 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1997 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1999 // NB: This size is arbitrary. Should this be a data member,
2000 // initialized at construction?
2001 const size_t __maxlen = 64;
2002 char_type* __res = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen));
2004 // NB: In IEE 1003.1-200x, and perhaps other locale models, it
2005 // is possible that the format character will be longer than one
2006 // character. Possibilities include 'E' or 'O' followed by a
2007 // format character: if __mod is not the default argument, assume
2008 // it's a valid modifier.
2010 __fmt[0] = __ctype.widen('%');
2013 __fmt[1] = __format;
2014 __fmt[2] = char_type();
2019 __fmt[2] = __format;
2020 __fmt[3] = char_type();
2023 __tp._M_put(__res, __maxlen, __fmt, __tm);
2025 // Write resulting, fully-formatted string to output iterator.
2026 return __write(__s, __res, char_traits<char_type>::length(__res));
2030 // Generic version does nothing.
2031 template<typename _CharT>
2033 collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
2036 // Generic version does nothing.
2037 template<typename _CharT>
2039 collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
2042 template<typename _CharT>
2045 do_compare(const _CharT* __lo1, const _CharT* __hi1,
2046 const _CharT* __lo2, const _CharT* __hi2) const
2048 const string_type __one(__lo1, __hi1);
2049 const string_type __two(__lo2, __hi2);
2050 return _M_compare(__one.c_str(), __two.c_str());
2053 template<typename _CharT>
2054 typename collate<_CharT>::string_type
2056 do_transform(const _CharT* __lo, const _CharT* __hi) const
2058 size_t __len = (__hi - __lo) * 2;
2059 // First try a buffer perhaps big enough.
2061 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len));
2062 size_t __res = _M_transform(__c, __lo, __len);
2063 // If the buffer was not large enough, try again with the correct size.
2066 __c = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
2068 _M_transform(__c, __lo, __res + 1);
2070 return string_type(__c);
2073 template<typename _CharT>
2076 do_hash(const _CharT* __lo, const _CharT* __hi) const
2078 unsigned long __val = 0;
2079 for (; __lo < __hi; ++__lo)
2080 __val = *__lo + ((__val << 7) |
2081 (__val >> (numeric_limits<unsigned long>::digits - 7)));
2082 return static_cast<long>(__val);
2085 // Construct correctly padded string, as per 22.2.2.2.2
2087 // __newlen > __oldlen
2088 // __news is allocated for __newlen size
2089 // Used by both num_put and ostream inserters: if __num,
2090 // internal-adjusted objects are padded according to the rules below
2091 // concerning 0[xX] and +-, otherwise, exactly as right-adjusted
2094 // NB: Of the two parameters, _CharT can be deduced from the
2095 // function arguments. The other (_Traits) has to be explicitly specified.
2096 template<typename _CharT, typename _Traits>
2098 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
2099 _CharT* __news, const _CharT* __olds,
2100 const streamsize __newlen,
2101 const streamsize __oldlen, const bool __num)
2103 size_t __plen = static_cast<size_t>(__newlen - __oldlen);
2104 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
2106 _Traits::assign(__pads, __plen, __fill);
2111 size_t __beglen; //either __plen or __oldlen
2112 ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
2114 if (__adjust == ios_base::left)
2117 __beg = const_cast<_CharT*>(__olds);
2118 __beglen = __oldlen;
2121 else if (__adjust == ios_base::internal && __num)
2123 // Pad after the sign, if there is one.
2124 // Pad after 0[xX], if there is one.
2125 // Who came up with these rules, anyway? Jeeze.
2126 locale __loc = __io.getloc();
2127 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2128 const _CharT __minus = __ctype.widen('-');
2129 const _CharT __plus = __ctype.widen('+');
2130 bool __testsign = _Traits::eq(__olds[0], __minus)
2131 || _Traits::eq(__olds[0], __plus);
2133 bool __testhex = _Traits::eq(__ctype.widen('0'), __olds[0])
2134 && (_Traits::eq(__ctype.widen('x'), __olds[1])
2135 || _Traits::eq(__ctype.widen('X'), __olds[1]));
2138 __news[0] = __olds[0];
2139 __news[1] = __olds[1];
2144 __end = const_cast<_CharT*>(__olds + __mod);
2146 else if (__testsign)
2148 _Traits::eq((__news[0] = __olds[0]), __plus) ? __plus : __minus;
2153 __end = const_cast<_CharT*>(__olds + __mod);
2160 __end = const_cast<_CharT*>(__olds);
2168 __end = const_cast<_CharT*>(__olds);
2170 _Traits::copy(__news, __beg, __beglen);
2171 _Traits::copy(__news + __beglen, __end,
2172 __newlen - __beglen - __mod);
2175 template<typename _CharT>
2177 __verify_grouping(const basic_string<_CharT>& __grouping,
2178 basic_string<_CharT>& __grouping_tmp)
2182 const int __len = __grouping.size();
2183 const int __n = __grouping_tmp.size();
2186 // Parsed number groupings have to match the
2187 // numpunct::grouping string exactly, starting at the
2188 // right-most point of the parsed sequence of elements ...
2189 while (__test && __i < __n - 1)
2190 for (__j = 0; __test && __j < __len && __i < __n - 1; ++__j,++__i)
2191 __test &= __grouping[__j] == __grouping_tmp[__n - __i - 1];
2192 // ... but the last parsed grouping can be <= numpunct
2194 __j == __len ? __j = 0 : __j;
2195 __test &= __grouping[__j] >= __grouping_tmp[__n - __i - 1];
2199 template<typename _CharT>
2201 __add_grouping(_CharT* __s, _CharT __sep,
2202 const char* __gbeg, const char* __gend,
2203 const _CharT* __first, const _CharT* __last)
2205 if (__last - __first > *__gbeg)
2207 __s = __add_grouping(__s, __sep,
2208 (__gbeg + 1 == __gend ? __gbeg : __gbeg + 1),
2209 __gend, __first, __last - *__gbeg);
2210 __first = __last - *__gbeg;
2214 *__s++ = *__first++;
2215 while (__first != __last);
2219 // Inhibit implicit instantiations for required instantiations,
2220 // which are defined via explicit instantiations elsewhere.
2221 // NB: This syntax is a GNU extension.
2222 extern template class moneypunct<char, false>;
2223 extern template class moneypunct<char, true>;
2224 extern template class moneypunct_byname<char, false>;
2225 extern template class moneypunct_byname<char, true>;
2226 extern template class money_get<char>;
2227 extern template class money_put<char>;
2228 extern template class numpunct<char>;
2229 extern template class numpunct_byname<char>;
2230 extern template class num_get<char>;
2231 extern template class num_put<char>;
2232 extern template class __timepunct<char>;
2233 extern template class time_put<char>;
2234 extern template class time_put_byname<char>;
2235 extern template class time_get<char>;
2236 extern template class time_get_byname<char>;
2237 extern template class messages<char>;
2238 extern template class messages_byname<char>;
2239 extern template class ctype_byname<char>;
2240 extern template class codecvt_byname<char, char, mbstate_t>;
2241 extern template class collate<char>;
2242 extern template class collate_byname<char>;
2245 const codecvt<char, char, mbstate_t>&
2246 use_facet<codecvt<char, char, mbstate_t> >(const locale&);
2249 const collate<char>&
2250 use_facet<collate<char> >(const locale&);
2253 const numpunct<char>&
2254 use_facet<numpunct<char> >(const locale&);
2257 const num_put<char>&
2258 use_facet<num_put<char> >(const locale&);
2261 const num_get<char>&
2262 use_facet<num_get<char> >(const locale&);
2265 const moneypunct<char, true>&
2266 use_facet<moneypunct<char, true> >(const locale&);
2269 const moneypunct<char, false>&
2270 use_facet<moneypunct<char, false> >(const locale&);
2273 const money_put<char>&
2274 use_facet<money_put<char> >(const locale&);
2277 const money_get<char>&
2278 use_facet<money_get<char> >(const locale&);
2281 const __timepunct<char>&
2282 use_facet<__timepunct<char> >(const locale&);
2285 const time_put<char>&
2286 use_facet<time_put<char> >(const locale&);
2289 const time_get<char>&
2290 use_facet<time_get<char> >(const locale&);
2293 const messages<char>&
2294 use_facet<messages<char> >(const locale&);
2298 has_facet<ctype<char> >(const locale&);
2302 has_facet<codecvt<char, char, mbstate_t> >(const locale&);
2306 has_facet<collate<char> >(const locale&);
2310 has_facet<numpunct<char> >(const locale&);
2314 has_facet<num_put<char> >(const locale&);
2318 has_facet<num_get<char> >(const locale&);
2322 has_facet<moneypunct<char> >(const locale&);
2326 has_facet<money_put<char> >(const locale&);
2330 has_facet<money_get<char> >(const locale&);
2334 has_facet<__timepunct<char> >(const locale&);
2338 has_facet<time_put<char> >(const locale&);
2342 has_facet<time_get<char> >(const locale&);
2346 has_facet<messages<char> >(const locale&);
2348 #ifdef _GLIBCPP_USE_WCHAR_T
2349 extern template class moneypunct<wchar_t, false>;
2350 extern template class moneypunct<wchar_t, true>;
2351 extern template class moneypunct_byname<wchar_t, false>;
2352 extern template class moneypunct_byname<wchar_t, true>;
2353 extern template class money_get<wchar_t>;
2354 extern template class money_put<wchar_t>;
2355 extern template class numpunct<wchar_t>;
2356 extern template class numpunct_byname<wchar_t>;
2357 extern template class num_get<wchar_t>;
2358 extern template class num_put<wchar_t>;
2359 extern template class __timepunct<wchar_t>;
2360 extern template class time_put<wchar_t>;
2361 extern template class time_put_byname<wchar_t>;
2362 extern template class time_get<wchar_t>;
2363 extern template class time_get_byname<wchar_t>;
2364 extern template class messages<wchar_t>;
2365 extern template class messages_byname<wchar_t>;
2366 extern template class ctype_byname<wchar_t>;
2367 extern template class codecvt_byname<wchar_t, char, mbstate_t>;
2368 extern template class collate<wchar_t>;
2369 extern template class collate_byname<wchar_t>;
2372 const codecvt<wchar_t, char, mbstate_t>&
2373 use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&);
2376 const collate<wchar_t>&
2377 use_facet<collate<wchar_t> >(const locale&);
2380 const numpunct<wchar_t>&
2381 use_facet<numpunct<wchar_t> >(const locale&);
2384 const num_put<wchar_t>&
2385 use_facet<num_put<wchar_t> >(const locale&);
2388 const num_get<wchar_t>&
2389 use_facet<num_get<wchar_t> >(const locale&);
2392 const moneypunct<wchar_t, true>&
2393 use_facet<moneypunct<wchar_t, true> >(const locale&);
2396 const moneypunct<wchar_t, false>&
2397 use_facet<moneypunct<wchar_t, false> >(const locale&);
2400 const money_put<wchar_t>&
2401 use_facet<money_put<wchar_t> >(const locale&);
2404 const money_get<wchar_t>&
2405 use_facet<money_get<wchar_t> >(const locale&);
2408 const __timepunct<wchar_t>&
2409 use_facet<__timepunct<wchar_t> >(const locale&);
2412 const time_put<wchar_t>&
2413 use_facet<time_put<wchar_t> >(const locale&);
2416 const time_get<wchar_t>&
2417 use_facet<time_get<wchar_t> >(const locale&);
2420 const messages<wchar_t>&
2421 use_facet<messages<wchar_t> >(const locale&);
2425 has_facet<ctype<wchar_t> >(const locale&);
2429 has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
2433 has_facet<collate<wchar_t> >(const locale&);
2437 has_facet<numpunct<wchar_t> >(const locale&);
2441 has_facet<num_put<wchar_t> >(const locale&);
2445 has_facet<num_get<wchar_t> >(const locale&);
2449 has_facet<moneypunct<wchar_t> >(const locale&);
2453 has_facet<money_put<wchar_t> >(const locale&);
2457 has_facet<money_get<wchar_t> >(const locale&);
2461 has_facet<__timepunct<wchar_t> >(const locale&);
2465 has_facet<time_put<wchar_t> >(const locale&);
2469 has_facet<time_get<wchar_t> >(const locale&);
2473 has_facet<messages<wchar_t> >(const locale&);