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
38 #include <limits> // For numeric_limits
39 #include <typeinfo> // For bad_cast.
40 #include <bits/streambuf_iterator.h>
44 template<typename _Facet>
46 locale::combine(const locale& __other) const
48 _Impl* __tmp = new _Impl(*_M_impl, 1);
51 __tmp->_M_replace_facet(__other._M_impl, &_Facet::id);
55 __tmp->_M_remove_reference();
56 __throw_exception_again;
61 template<typename _CharT, typename _Traits, typename _Alloc>
63 locale::operator()(const basic_string<_CharT, _Traits, _Alloc>& __s1,
64 const basic_string<_CharT, _Traits, _Alloc>& __s2) const
66 typedef std::collate<_CharT> __collate_type;
67 const __collate_type& __collate = use_facet<__collate_type>(*this);
68 return (__collate.compare(__s1.data(), __s1.data() + __s1.length(),
69 __s2.data(), __s2.data() + __s2.length()) < 0);
72 template<typename _Facet>
74 has_facet(const locale& __loc) throw()
76 const size_t __i = _Facet::id._M_id();
77 const locale::facet** __facets = __loc._M_impl->_M_facets;
78 return (__i < __loc._M_impl->_M_facets_size && __facets[__i]);
81 template<typename _Facet>
83 use_facet(const locale& __loc)
85 const size_t __i = _Facet::id._M_id();
86 const locale::facet** __facets = __loc._M_impl->_M_facets;
87 if (!(__i < __loc._M_impl->_M_facets_size && __facets[__i]))
89 return static_cast<const _Facet&>(*__facets[__i]);
92 // Routine to access a cache for the facet. If the cache didn't
93 // exist before, it gets constructed on the fly.
94 template<typename _Facet>
98 operator() (const locale& __loc) const;
101 template<typename _CharT>
102 struct __use_cache<__numpunct_cache<_CharT> >
104 const __numpunct_cache<_CharT>*
105 operator() (const locale& __loc) const
107 const size_t __i = numpunct<_CharT>::id._M_id();
108 const locale::facet** __caches = __loc._M_impl->_M_caches;
111 __numpunct_cache<_CharT>* __tmp = NULL;
114 __tmp = new __numpunct_cache<_CharT>;
115 __tmp->_M_cache(__loc);
120 __throw_exception_again;
122 __loc._M_impl->_M_install_cache(__tmp, __i);
124 return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]);
128 template<typename _CharT, typename _InIter>
130 num_get<_CharT, _InIter>::
131 _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
132 ios_base::iostate& __err, string& __xtrc) const
134 typedef char_traits<_CharT> __traits_type;
135 typedef typename numpunct<_CharT>::__cache_type __cache_type;
136 __use_cache<__cache_type> __uc;
137 const locale& __loc = __io._M_getloc();
138 const __cache_type* __lc = __uc(__loc);
139 const _CharT* __lit = __lc->_M_atoms_in;
141 // First check for sign.
144 const char_type __c = *__beg;
145 const bool __plus = __traits_type::eq(__c, __lit[_S_iplus]);
146 if (__plus || __traits_type::eq(__c, __lit[_S_iminus]))
148 __xtrc += __plus ? _S_atoms_in[_S_iplus]
149 : _S_atoms_in[_S_iminus];
154 // Next, look for a zero...
155 bool __found_mantissa = false;
156 if (__beg != __end && __traits_type::eq(*__beg, __lit[_S_izero]))
158 __xtrc += _S_atoms_in[_S_izero];
159 __found_mantissa = true;
161 // ... and skip the additional ones.
162 for (; __beg != __end
163 && __traits_type::eq(*__beg, __lit[_S_izero]); ++__beg);
166 // Only need acceptable digits for floating point numbers.
167 bool __found_dec = false;
168 bool __found_sci = false;
169 string __found_grouping;
172 while (__beg != __end)
174 // Only look in digits.
175 const char_type __c = *__beg;
176 const char_type* __p = __traits_type::find(__lit + _S_izero, 10,
180 // Try first for acceptable digit; record it if found.
181 __xtrc += _S_atoms_in[__p - __lit];
182 __found_mantissa = true;
186 else if (__traits_type::eq(__c, __lc->_M_thousands_sep)
187 && __lc->_M_use_grouping && !__found_dec)
189 // NB: Thousands separator at the beginning of a string
190 // is a no-no, as is two consecutive thousands separators.
193 __found_grouping += static_cast<char>(__sep_pos);
199 __err |= ios_base::failbit;
203 else if (__traits_type::eq(__c, __lc->_M_decimal_point)
206 // According to the standard, if no grouping chars are seen,
207 // no grouping check is applied. Therefore __found_grouping
208 // must be adjusted only if __dec comes after some __sep.
209 if (__found_grouping.size())
210 __found_grouping += static_cast<char>(__sep_pos);
215 else if ((__e = __traits_type::eq(__c, __lit[_S_ie])
216 || __traits_type::eq(__c, __lit[_S_iE]))
217 && __found_mantissa && !__found_sci)
219 // Scientific notation.
220 __xtrc += __e ? _S_atoms_in[_S_ie] : _S_atoms_in[_S_iE];
223 // Remove optional plus or minus sign, if they exist.
224 if (++__beg != __end)
226 const bool __plus = __traits_type::eq(*__beg, __lit[_S_iplus]);
227 if (__plus || __traits_type::eq(*__beg, __lit[_S_iminus]))
229 __xtrc += __plus ? _S_atoms_in[_S_iplus]
230 : _S_atoms_in[_S_iminus];
236 // Not a valid input item.
240 // Digit grouping is checked. If grouping and found_grouping don't
241 // match, then get very very upset, and set failbit.
242 if (__lc->_M_use_grouping && __found_grouping.size())
244 // Add the ending grouping if a decimal wasn't found.
246 __found_grouping += static_cast<char>(__sep_pos);
248 const string __grouping = __lc->_M_grouping;
249 if (!std::__verify_grouping(__grouping, __found_grouping))
250 __err |= ios_base::failbit;
256 __err |= ios_base::eofbit;
260 template<typename _CharT, typename _InIter>
261 template<typename _ValueT>
263 num_get<_CharT, _InIter>::
264 _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
265 ios_base::iostate& __err, _ValueT& __v) const
267 typedef char_traits<_CharT> __traits_type;
268 typedef typename numpunct<_CharT>::__cache_type __cache_type;
269 __use_cache<__cache_type> __uc;
270 const locale& __loc = __io._M_getloc();
271 const __cache_type* __lc = __uc(__loc);
272 const _CharT* __lit = __lc->_M_atoms_in;
274 // NB: Iff __basefield == 0, __base can change based on contents.
275 const ios_base::fmtflags __basefield = __io.flags() & ios_base::basefield;
276 const bool __oct = __basefield == ios_base::oct;
277 int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10);
279 // True if numeric digits are found.
280 bool __found_num = false;
282 // First check for sign.
283 bool __negative = false;
286 if (numeric_limits<_ValueT>::is_signed)
287 __negative = __traits_type::eq(*__beg, __lit[_S_iminus]);
288 if (__negative || __traits_type::eq(*__beg, __lit[_S_iplus]))
292 // Next, look for leading zeros and check required digits
294 if (__beg != __end && __traits_type::eq(*__beg, __lit[_S_izero]))
298 if (__builtin_expect(__base == 10, true))
300 // Skip the additional zeros.
301 for (; __beg != __end
302 && __traits_type::eq(*__beg, __lit[_S_izero]); ++__beg);
304 // Check required digits.
305 if (__beg != __end && __basefield == 0)
307 const bool __x = __traits_type::eq(*__beg, __lit[_S_ix]);
308 if (__x || __traits_type::eq(*__beg, __lit[_S_iX]))
318 else if (__base == 16 && __beg != __end)
320 const bool __x = __traits_type::eq(*__beg, __lit[_S_ix]);
321 if (__x || __traits_type::eq(*__beg, __lit[_S_iX]))
329 // At this point, base is determined. If not hex, only allow
330 // base digits as valid input.
331 const size_t __len = __base == 16 ? _S_iend : __base;
334 string __found_grouping;
336 bool __overflow = false;
337 _ValueT __result = 0;
338 const char_type* __lit_zero = __lit + _S_izero;
341 const _ValueT __min = numeric_limits<_ValueT>::min() / __base;
342 for (; __beg != __end; ++__beg)
344 const char_type* __p = __traits_type::find(__lit_zero,
348 int __digit = __p - __lit_zero;
351 if (__result < __min)
355 const _ValueT __new_result = __result * __base - __digit;
356 __overflow |= __new_result > __result;
357 __result = __new_result;
362 else if (__lc->_M_use_grouping
363 && __traits_type::eq(*__beg, __lc->_M_thousands_sep))
365 // NB: Thousands separator at the beginning of a string
366 // is a no-no, as is two consecutive thousands separators.
369 __found_grouping += static_cast<char>(__sep_pos);
374 __err |= ios_base::failbit;
379 // Not a valid input item.
385 const _ValueT __max = numeric_limits<_ValueT>::max() / __base;
386 for (; __beg != __end; ++__beg)
388 const char_type* __p = __traits_type::find(__lit_zero,
392 int __digit = __p - __lit_zero;
395 if (__result > __max)
399 const _ValueT __new_result = __result * __base + __digit;
400 __overflow |= __new_result < __result;
401 __result = __new_result;
406 else if (__lc->_M_use_grouping
407 && __traits_type::eq(*__beg, __lc->_M_thousands_sep))
411 __found_grouping += static_cast<char>(__sep_pos);
416 __err |= ios_base::failbit;
425 // Digit grouping is checked. If grouping and found_grouping don't
426 // match, then get very very upset, and set failbit.
427 if (__lc->_M_use_grouping && __found_grouping.size())
429 // Add the ending grouping.
430 __found_grouping += static_cast<char>(__sep_pos);
432 const string __grouping = __lc->_M_grouping;
433 if (!std::__verify_grouping(__grouping, __found_grouping))
434 __err |= ios_base::failbit;
437 if (!(__err & ios_base::failbit) && !__overflow
441 __err |= ios_base::failbit;
444 __err |= ios_base::eofbit;
448 // _GLIBCXX_RESOLVE_LIB_DEFECTS
449 // 17. Bad bool parsing
450 template<typename _CharT, typename _InIter>
452 num_get<_CharT, _InIter>::
453 do_get(iter_type __beg, iter_type __end, ios_base& __io,
454 ios_base::iostate& __err, bool& __v) const
456 if (!(__io.flags() & ios_base::boolalpha))
458 // Parse bool values as long.
459 // NB: We can't just call do_get(long) here, as it might
460 // refer to a derived class.
462 __beg = _M_extract_int(__beg, __end, __io, __err, __l);
463 if (__l == 0 || __l == 1)
466 __err |= ios_base::failbit;
470 // Parse bool values as alphanumeric.
471 typedef char_traits<_CharT> __traits_type;
472 typedef typename numpunct<_CharT>::__cache_type __cache_type;
473 __use_cache<__cache_type> __uc;
474 const locale& __loc = __io._M_getloc();
475 const __cache_type* __lc = __uc(__loc);
476 const size_t __tn = __traits_type::length(__lc->_M_truename) - 1;
477 const size_t __fn = __traits_type::length(__lc->_M_falsename) - 1;
479 bool __testf = false;
480 bool __testt = false;
481 for (size_t __n = 0; __beg != __end; ++__n)
483 const char_type __c = *__beg;
487 __testf = __traits_type::eq(__c, __lc->_M_falsename[__n]);
490 __testt = __traits_type::eq(__c, __lc->_M_truename[__n]);
492 if (!(__testf || __testt))
494 __err |= ios_base::failbit;
497 else if (__testf && __n == __fn)
502 else if (__testt && __n == __tn)
509 __err |= ios_base::eofbit;
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& __v) const
519 { return _M_extract_int(__beg, __end, __io, __err, __v); }
521 template<typename _CharT, typename _InIter>
523 num_get<_CharT, _InIter>::
524 do_get(iter_type __beg, iter_type __end, ios_base& __io,
525 ios_base::iostate& __err, unsigned short& __v) const
526 { return _M_extract_int(__beg, __end, __io, __err, __v); }
528 template<typename _CharT, typename _InIter>
530 num_get<_CharT, _InIter>::
531 do_get(iter_type __beg, iter_type __end, ios_base& __io,
532 ios_base::iostate& __err, unsigned int& __v) const
533 { return _M_extract_int(__beg, __end, __io, __err, __v); }
535 template<typename _CharT, typename _InIter>
537 num_get<_CharT, _InIter>::
538 do_get(iter_type __beg, iter_type __end, ios_base& __io,
539 ios_base::iostate& __err, unsigned long& __v) const
540 { return _M_extract_int(__beg, __end, __io, __err, __v); }
542 #ifdef _GLIBCXX_USE_LONG_LONG
543 template<typename _CharT, typename _InIter>
545 num_get<_CharT, _InIter>::
546 do_get(iter_type __beg, iter_type __end, ios_base& __io,
547 ios_base::iostate& __err, long long& __v) const
548 { return _M_extract_int(__beg, __end, __io, __err, __v); }
550 template<typename _CharT, typename _InIter>
552 num_get<_CharT, _InIter>::
553 do_get(iter_type __beg, iter_type __end, ios_base& __io,
554 ios_base::iostate& __err, unsigned long long& __v) const
555 { return _M_extract_int(__beg, __end, __io, __err, __v); }
558 template<typename _CharT, typename _InIter>
560 num_get<_CharT, _InIter>::
561 do_get(iter_type __beg, iter_type __end, ios_base& __io,
562 ios_base::iostate& __err, float& __v) const
566 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
567 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
571 template<typename _CharT, typename _InIter>
573 num_get<_CharT, _InIter>::
574 do_get(iter_type __beg, iter_type __end, ios_base& __io,
575 ios_base::iostate& __err, double& __v) const
579 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
580 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
584 template<typename _CharT, typename _InIter>
586 num_get<_CharT, _InIter>::
587 do_get(iter_type __beg, iter_type __end, ios_base& __io,
588 ios_base::iostate& __err, long double& __v) const
592 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
593 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
597 template<typename _CharT, typename _InIter>
599 num_get<_CharT, _InIter>::
600 do_get(iter_type __beg, iter_type __end, ios_base& __io,
601 ios_base::iostate& __err, void*& __v) const
603 // Prepare for hex formatted input.
604 typedef ios_base::fmtflags fmtflags;
605 const fmtflags __fmt = __io.flags();
606 const fmtflags __fmtmask = ~(ios_base::showpos | ios_base::basefield
607 | ios_base::uppercase | ios_base::internal);
608 __io.flags(__fmt & __fmtmask | (ios_base::hex | ios_base::showbase));
611 __beg = _M_extract_int(__beg, __end, __io, __err, __ul);
613 // Reset from hex formatted input.
616 if (!(__err & ios_base::failbit))
617 __v = reinterpret_cast<void*>(__ul);
619 __err |= ios_base::failbit;
623 // For use by integer and floating-point types after they have been
624 // converted into a char_type string.
625 template<typename _CharT, typename _OutIter>
627 num_put<_CharT, _OutIter>::
628 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
629 _CharT* __new, const _CharT* __cs, int& __len) const
631 // [22.2.2.2.2] Stage 3.
632 // If necessary, pad.
633 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new, __cs,
635 __len = static_cast<int>(__w);
638 // Forwarding functions to peel signed from unsigned integer types.
639 template<typename _CharT>
641 __int_to_char(_CharT* __bufend, long __v, const _CharT* __lit,
642 ios_base::fmtflags __flags)
644 unsigned long __ul = static_cast<unsigned long>(__v);
651 return __int_to_char(__bufend, __ul, __lit, __flags, __neg);
654 template<typename _CharT>
656 __int_to_char(_CharT* __bufend, unsigned long __v, const _CharT* __lit,
657 ios_base::fmtflags __flags)
658 { return __int_to_char(__bufend, __v, __lit, __flags, false); }
660 #ifdef _GLIBCXX_USE_LONG_LONG
661 template<typename _CharT>
663 __int_to_char(_CharT* __bufend, long long __v, const _CharT* __lit,
664 ios_base::fmtflags __flags)
666 unsigned long long __ull = static_cast<unsigned long long>(__v);
673 return __int_to_char(__bufend, __ull, __lit, __flags, __neg);
676 template<typename _CharT>
678 __int_to_char(_CharT* __bufend, unsigned long long __v, const _CharT* __lit,
679 ios_base::fmtflags __flags)
680 { return __int_to_char(__bufend, __v, __lit, __flags, false); }
683 template<typename _CharT, typename _ValueT>
685 __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
686 ios_base::fmtflags __flags, bool __neg)
688 // Don't write base if already 0.
689 const bool __showbase = (__flags & ios_base::showbase) && __v;
690 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
691 _CharT* __buf = __bufend - 1;
693 if (__builtin_expect(__basefield != ios_base::oct &&
694 __basefield != ios_base::hex, true))
699 *__buf-- = __lit[(__v % 10) + __num_base::_S_odigits];
704 *__buf-- = __lit[__num_base::_S_ominus];
705 else if (__flags & ios_base::showpos)
706 *__buf-- = __lit[__num_base::_S_oplus];
708 else if (__basefield == ios_base::oct)
713 *__buf-- = __lit[(__v & 0x7) + __num_base::_S_odigits];
718 *__buf-- = __lit[__num_base::_S_odigits];
723 const bool __uppercase = __flags & ios_base::uppercase;
724 const int __case_offset = __uppercase ? __num_base::_S_oudigits
725 : __num_base::_S_odigits;
728 *__buf-- = __lit[(__v & 0xf) + __case_offset];
735 *__buf-- = __lit[__num_base::_S_ox + __uppercase];
737 *__buf-- = __lit[__num_base::_S_odigits];
740 return __bufend - __buf - 1;
743 template<typename _CharT, typename _OutIter>
745 num_put<_CharT, _OutIter>::
746 _M_group_int(const string& __grouping, _CharT __sep, ios_base& __io,
747 _CharT* __new, _CharT* __cs, int& __len) const
749 // By itself __add_grouping cannot deal correctly with __cs when
750 // ios::showbase is set and ios_base::oct || ios_base::hex.
751 // Therefore we take care "by hand" of the initial 0, 0x or 0X.
752 // However, remember that the latter do not occur if the number
753 // printed is '0' (__len == 1).
754 streamsize __off = 0;
755 const ios_base::fmtflags __basefield = __io.flags()
756 & ios_base::basefield;
757 if ((__io.flags() & ios_base::showbase) && __len > 1)
758 if (__basefield == ios_base::oct)
763 else if (__basefield == ios_base::hex)
770 __p = std::__add_grouping(__new + __off, __sep, __grouping.data(),
771 __grouping.data() + __grouping.size(),
772 __cs + __off, __cs + __len);
776 template<typename _CharT, typename _OutIter>
777 template<typename _ValueT>
779 num_put<_CharT, _OutIter>::
780 _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
783 typedef typename numpunct<_CharT>::__cache_type __cache_type;
784 __use_cache<__cache_type> __uc;
785 const locale& __loc = __io._M_getloc();
786 const __cache_type* __lc = __uc(__loc);
787 const _CharT* __lit = __lc->_M_atoms_out;
789 // Long enough to hold hex, dec, and octal representations.
790 const int __ilen = 4 * sizeof(_ValueT);
791 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
794 // [22.2.2.2.2] Stage 1, numeric conversion to character.
795 // Result is returned right-justified in the buffer.
797 __len = __int_to_char(__cs + __ilen, __v, __lit, __io.flags());
798 __cs += __ilen - __len;
800 // Add grouping, if necessary.
801 if (__lc->_M_use_grouping)
803 // Grouping can add (almost) as many separators as the
804 // number of digits, but no more.
805 _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
807 _M_group_int(__lc->_M_grouping, __lc->_M_thousands_sep, __io,
813 const streamsize __w = __io.width();
814 if (__w > static_cast<streamsize>(__len))
816 _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
818 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
823 // [22.2.2.2.2] Stage 4.
824 // Write resulting, fully-formatted string to output iterator.
825 return std::__write(__s, __cs, __len);
828 template<typename _CharT, typename _OutIter>
830 num_put<_CharT, _OutIter>::
831 _M_group_float(const string& __grouping, _CharT __sep, const _CharT* __p,
832 _CharT* __new, _CharT* __cs, int& __len) const
834 // _GLIBCXX_RESOLVE_LIB_DEFECTS
835 // 282. What types does numpunct grouping refer to?
836 // Add grouping, if necessary.
838 const int __declen = __p ? __p - __cs : __len;
839 __p2 = std::__add_grouping(__new, __sep, __grouping.data(),
840 __grouping.data() + __grouping.size(),
841 __cs, __cs + __declen);
843 // Tack on decimal part.
844 int __newlen = __p2 - __new;
847 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
848 __newlen += __len - __declen;
853 // The following code uses snprintf (or sprintf(), when
854 // _GLIBCXX_USE_C99 is not defined) to convert floating point values
855 // for insertion into a stream. An optimization would be to replace
856 // them with code that works directly on a wide buffer and then use
857 // __pad to do the padding. It would be good to replace them anyway
858 // to gain back the efficiency that C++ provides by knowing up front
859 // the type of the values to insert. Also, sprintf is dangerous
860 // since may lead to accidental buffer overruns. This
861 // implementation follows the C++ standard fairly directly as
862 // outlined in 22.2.2.2 [lib.locale.num.put]
863 template<typename _CharT, typename _OutIter>
864 template<typename _ValueT>
866 num_put<_CharT, _OutIter>::
867 _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
870 typedef typename numpunct<_CharT>::__cache_type __cache_type;
871 __use_cache<__cache_type> __uc;
872 const locale& __loc = __io._M_getloc();
873 const __cache_type* __lc = __uc(__loc);
875 // Note: digits10 is rounded down: add 1 to ensure the maximum
876 // available precision. Then, in general, one more 1 needs to
877 // be added since, when the %{g,G} conversion specifiers are
878 // chosen inside _S_format_float, the precision field is "the
879 // maximum number of significant digits", *not* the "number of
880 // digits to appear after the decimal point", as happens for
881 // %{e,E,f,F} (C99, 7.19.6.1,4).
882 const int __max_digits = numeric_limits<_ValueT>::digits10 + 2;
884 // Use default precision if out of range.
885 streamsize __prec = __io.precision();
886 if (__prec > static_cast<streamsize>(__max_digits))
887 __prec = static_cast<streamsize>(__max_digits);
888 else if (__prec < static_cast<streamsize>(0))
889 __prec = static_cast<streamsize>(6);
891 // [22.2.2.2.2] Stage 1, numeric conversion to character.
893 // Long enough for the max format spec.
896 #ifdef _GLIBCXX_USE_C99
897 // First try a buffer perhaps big enough (for sure sufficient
898 // for non-ios_base::fixed outputs)
899 int __cs_size = __max_digits * 3;
900 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
902 _S_format_float(__io, __fbuf, __mod);
903 __len = std::__convert_from_v(__cs, __cs_size, __fbuf, __v,
904 _S_get_c_locale(), __prec);
906 // If the buffer was not large enough, try again with the correct size.
907 if (__len >= __cs_size)
909 __cs_size = __len + 1;
910 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
911 __len = std::__convert_from_v(__cs, __cs_size, __fbuf, __v,
912 _S_get_c_locale(), __prec);
915 // Consider the possibility of long ios_base::fixed outputs
916 const bool __fixed = __io.flags() & ios_base::fixed;
917 const int __max_exp = numeric_limits<_ValueT>::max_exponent10;
919 // The size of the output string is computed as follows.
920 // ios_base::fixed outputs may need up to __max_exp+1 chars
921 // for the integer part + up to __max_digits chars for the
922 // fractional part + 3 chars for sign, decimal point, '\0'. On
923 // the other hand, for non-fixed outputs __max_digits*3 chars
924 // are largely sufficient.
925 const int __cs_size = __fixed ? __max_exp + __max_digits + 4
927 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
929 _S_format_float(__io, __fbuf, __mod);
930 __len = std::__convert_from_v(__cs, 0, __fbuf, __v,
931 _S_get_c_locale(), __prec);
934 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
935 // numpunct.decimal_point() values for '.' and adding grouping.
936 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
938 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
940 __ctype.widen(__cs, __cs + __len, __ws);
942 // Replace decimal point.
943 const _CharT __cdec = __ctype.widen('.');
944 const _CharT __dec = __lc->_M_decimal_point;
946 if (__p = char_traits<_CharT>::find(__ws, __len, __cdec))
947 __ws[__p - __ws] = __dec;
949 // Add grouping, if necessary.
950 if (__lc->_M_use_grouping)
952 // Grouping can add (almost) as many separators as the
953 // number of digits, but no more.
954 _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
956 _M_group_float(__lc->_M_grouping, __lc->_M_thousands_sep, __p,
962 const streamsize __w = __io.width();
963 if (__w > static_cast<streamsize>(__len))
965 _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
967 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
972 // [22.2.2.2.2] Stage 4.
973 // Write resulting, fully-formatted string to output iterator.
974 return std::__write(__s, __ws, __len);
977 template<typename _CharT, typename _OutIter>
979 num_put<_CharT, _OutIter>::
980 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
982 const ios_base::fmtflags __flags = __io.flags();
983 if ((__flags & ios_base::boolalpha) == 0)
985 unsigned long __uv = __v;
986 __s = _M_insert_int(__s, __io, __fill, __uv);
990 typedef typename numpunct<_CharT>::__cache_type __cache_type;
991 __use_cache<__cache_type> __uc;
992 const locale& __loc = __io._M_getloc();
993 const __cache_type* __lc = __uc(__loc);
995 const _CharT* __name = __v ? __lc->_M_truename
996 : __lc->_M_falsename;
997 int __len = char_traits<_CharT>::length(__name);
999 const streamsize __w = __io.width();
1000 if (__w > static_cast<streamsize>(__len))
1002 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1004 _M_pad(__fill, __w, __io, __cs, __name, __len);
1008 __s = std::__write(__s, __name, __len);
1013 template<typename _CharT, typename _OutIter>
1015 num_put<_CharT, _OutIter>::
1016 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
1017 { return _M_insert_int(__s, __io, __fill, __v); }
1019 template<typename _CharT, typename _OutIter>
1021 num_put<_CharT, _OutIter>::
1022 do_put(iter_type __s, ios_base& __io, char_type __fill,
1023 unsigned long __v) const
1024 { return _M_insert_int(__s, __io, __fill, __v); }
1026 #ifdef _GLIBCXX_USE_LONG_LONG
1027 template<typename _CharT, typename _OutIter>
1029 num_put<_CharT, _OutIter>::
1030 do_put(iter_type __s, ios_base& __b, char_type __fill, long long __v) const
1031 { return _M_insert_int(__s, __b, __fill, __v); }
1033 template<typename _CharT, typename _OutIter>
1035 num_put<_CharT, _OutIter>::
1036 do_put(iter_type __s, ios_base& __io, char_type __fill,
1037 unsigned long long __v) const
1038 { return _M_insert_int(__s, __io, __fill, __v); }
1041 template<typename _CharT, typename _OutIter>
1043 num_put<_CharT, _OutIter>::
1044 do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1045 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1047 template<typename _CharT, typename _OutIter>
1049 num_put<_CharT, _OutIter>::
1050 do_put(iter_type __s, ios_base& __io, char_type __fill,
1051 long double __v) const
1052 { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1054 template<typename _CharT, typename _OutIter>
1056 num_put<_CharT, _OutIter>::
1057 do_put(iter_type __s, ios_base& __io, char_type __fill,
1058 const void* __v) const
1060 const ios_base::fmtflags __flags = __io.flags();
1061 const ios_base::fmtflags __fmt = ~(ios_base::showpos | ios_base::basefield
1062 | ios_base::uppercase | ios_base::internal);
1063 __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase));
1065 __s = _M_insert_int(__s, __io, __fill,
1066 reinterpret_cast<unsigned long>(__v));
1067 __io.flags(__flags);
1072 template<typename _CharT, typename _InIter>
1074 money_get<_CharT, _InIter>::
1075 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1076 ios_base::iostate& __err, long double& __units) const
1079 __beg = this->do_get(__beg, __end, __intl, __io, __err, __str);
1081 const int __cs_size = __str.size() + 1;
1082 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1083 const locale __loc = __io.getloc();
1084 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1085 const _CharT* __wcs = __str.c_str();
1086 __ctype.narrow(__wcs, __wcs + __cs_size, char(), __cs);
1087 std::__convert_to_v(__cs, __units, __err, _S_get_c_locale());
1091 template<typename _CharT, typename _InIter>
1093 money_get<_CharT, _InIter>::
1094 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1095 ios_base::iostate& __err, string_type& __units) const
1097 // These contortions are quite unfortunate.
1098 typedef moneypunct<_CharT, true> __money_true;
1099 typedef moneypunct<_CharT, false> __money_false;
1100 typedef money_base::part part;
1101 typedef typename string_type::size_type size_type;
1103 const locale __loc = __io.getloc();
1104 const __money_true& __mpt = use_facet<__money_true>(__loc);
1105 const __money_false& __mpf = use_facet<__money_false>(__loc);
1106 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1108 const money_base::pattern __p = __intl ? __mpt.neg_format()
1109 : __mpf.neg_format();
1111 const string_type __pos_sign = __intl ? __mpt.positive_sign()
1112 : __mpf.positive_sign();
1113 const string_type __neg_sign = __intl ? __mpt.negative_sign()
1114 : __mpf.negative_sign();
1115 const char_type __d = __intl ? __mpt.decimal_point()
1116 : __mpf.decimal_point();
1117 const char_type __sep = __intl ? __mpt.thousands_sep()
1118 : __mpf.thousands_sep();
1120 const string __grouping = __intl ? __mpt.grouping() : __mpf.grouping();
1122 // Set to deduced positive or negative sign, depending.
1124 // String of grouping info from thousands_sep plucked from __units.
1125 string __grouping_tmp;
1126 // Marker for thousands_sep position.
1128 // If input iterator is in a valid state.
1129 bool __testvalid = true;
1130 // Flag marking when a decimal point is found.
1131 bool __testdecfound = false;
1133 // The tentative returned string is stored here.
1134 string_type __tmp_units;
1136 for (int __i = 0; __beg != __end && __i < 4 && __testvalid; ++__i)
1139 const part __which = static_cast<part>(__p.field[__i]);
1142 case money_base::symbol:
1143 if (__io.flags() & ios_base::showbase
1144 || __i < 2 || __sign.size() > 1
1145 || ((static_cast<part>(__p.field[3]) != money_base::none)
1148 // According to 22.2.6.1.2.2, symbol is required
1149 // if (__io.flags() & ios_base::showbase),
1150 // otherwise is optional and consumed only if
1151 // other characters are needed to complete the
1153 const string_type __symbol = __intl ? __mpt.curr_symbol()
1154 : __mpf.curr_symbol();
1155 const size_type __len = __symbol.size();
1157 for (; __beg != __end && __j < __len
1158 && *__beg == __symbol[__j]; ++__beg, ++__j);
1159 // When (__io.flags() & ios_base::showbase)
1160 // symbol is required.
1161 if (__j != __len && (__io.flags() & ios_base::showbase))
1162 __testvalid = false;
1165 case money_base::sign:
1166 // Sign might not exist, or be more than one character long.
1167 if (__pos_sign.size() && *__beg == __pos_sign[0])
1169 __sign = __pos_sign;
1172 else if (__neg_sign.size() && *__beg == __neg_sign[0])
1174 __sign = __neg_sign;
1177 else if (__pos_sign.size() && __neg_sign.size())
1179 // Sign is mandatory.
1180 __testvalid = false;
1183 case money_base::value:
1184 // Extract digits, remove and stash away the
1185 // grouping of found thousands separators.
1186 for (; __beg != __end; ++__beg)
1187 if (__ctype.is(ctype_base::digit, __c = *__beg))
1192 else if (__c == __d && !__testdecfound)
1194 __grouping_tmp += static_cast<char>(__sep_pos);
1196 __testdecfound = true;
1198 else if (__c == __sep)
1200 if (__grouping.size())
1202 // Mark position for later analysis.
1203 __grouping_tmp += static_cast<char>(__sep_pos);
1208 __testvalid = false;
1215 case money_base::space:
1216 case money_base::none:
1217 // Only if not at the end of the pattern.
1219 for (; __beg != __end
1220 && __ctype.is(ctype_base::space, *__beg); ++__beg);
1225 // Need to get the rest of the sign characters, if they exist.
1226 if (__sign.size() > 1)
1228 const size_type __len = __sign.size();
1230 for (; __beg != __end && __i < __len; ++__i)
1231 for (; __beg != __end
1232 && *__beg != __sign[__i]; ++__beg);
1235 __testvalid = false;
1238 if (__testvalid && __tmp_units.size())
1240 const char_type __zero = __ctype.widen('0');
1242 // Strip leading zeros.
1243 if (__tmp_units.size() > 1)
1245 const size_type __first = __tmp_units.find_first_not_of(__zero);
1246 const bool __only_zeros = __first == string_type::npos;
1248 __tmp_units.erase(0, __only_zeros ? __tmp_units.size() - 1
1253 if (__sign.size() && __sign == __neg_sign
1254 && __tmp_units[0] != __zero)
1255 __tmp_units.insert(__tmp_units.begin(), __ctype.widen('-'));
1257 // Test for grouping fidelity.
1258 if (__grouping.size() && __grouping_tmp.size())
1260 if (!std::__verify_grouping(__grouping, __grouping_tmp))
1261 __testvalid = false;
1264 // Iff not enough digits were supplied after the decimal-point.
1267 const int __frac = __intl ? __mpt.frac_digits()
1268 : __mpf.frac_digits();
1269 if (__frac > 0 && __sep_pos != __frac)
1270 __testvalid = false;
1274 __testvalid = false;
1276 // Iff no more characters are available.
1278 __err |= ios_base::eofbit;
1280 // Iff valid sequence is not recognized.
1282 __err |= ios_base::failbit;
1284 // Use the "swap trick" to copy __tmp_units into __units.
1285 __tmp_units.swap(__units);
1290 template<typename _CharT, typename _OutIter>
1292 money_put<_CharT, _OutIter>::
1293 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1294 long double __units) const
1296 const locale __loc = __io.getloc();
1297 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1298 #ifdef _GLIBCXX_USE_C99
1299 // First try a buffer perhaps big enough.
1301 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1302 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1303 // 328. Bad sprintf format modifier in money_put<>::do_put()
1304 int __len = std::__convert_from_v(__cs, __cs_size, "%.0Lf", __units,
1306 // If the buffer was not large enough, try again with the correct size.
1307 if (__len >= __cs_size)
1309 __cs_size = __len + 1;
1310 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1311 __len = std::__convert_from_v(__cs, __cs_size, "%.0Lf", __units,
1315 // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'.
1316 const int __cs_size = numeric_limits<long double>::max_exponent10 + 3;
1317 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1318 int __len = std::__convert_from_v(__cs, 0, "%.0Lf", __units,
1321 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1323 __ctype.widen(__cs, __cs + __len, __ws);
1324 string_type __digits(__ws);
1325 return this->do_put(__s, __intl, __io, __fill, __digits);
1328 template<typename _CharT, typename _OutIter>
1330 money_put<_CharT, _OutIter>::
1331 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1332 const string_type& __digits) const
1334 typedef typename string_type::size_type size_type;
1335 typedef money_base::part part;
1337 const locale __loc = __io.getloc();
1338 const size_type __width = static_cast<size_type>(__io.width());
1340 // These contortions are quite unfortunate.
1341 typedef moneypunct<_CharT, true> __money_true;
1342 typedef moneypunct<_CharT, false> __money_false;
1343 const __money_true& __mpt = use_facet<__money_true>(__loc);
1344 const __money_false& __mpf = use_facet<__money_false>(__loc);
1345 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1347 // Determine if negative or positive formats are to be used, and
1348 // discard leading negative_sign if it is present.
1349 const char_type* __beg = __digits.data();
1350 const char_type* __end = __beg + __digits.size();
1351 money_base::pattern __p;
1353 if (*__beg != __ctype.widen('-'))
1355 __p = __intl ? __mpt.pos_format() : __mpf.pos_format();
1356 __sign = __intl ? __mpt.positive_sign() : __mpf.positive_sign();
1360 __p = __intl ? __mpt.neg_format() : __mpf.neg_format();
1361 __sign = __intl ? __mpt.negative_sign() : __mpf.negative_sign();
1365 // Look for valid numbers in the current ctype facet within input digits.
1366 __end = __ctype.scan_not(ctype_base::digit, __beg, __end);
1369 // Assume valid input, and attempt to format.
1370 // Break down input numbers into base components, as follows:
1371 // final_value = grouped units + (decimal point) + (digits)
1373 string_type __value;
1374 const string_type __symbol = __intl ? __mpt.curr_symbol()
1375 : __mpf.curr_symbol();
1377 // Deal with decimal point, decimal digits.
1378 const int __frac = __intl ? __mpt.frac_digits()
1379 : __mpf.frac_digits();
1382 const char_type __d = __intl ? __mpt.decimal_point()
1383 : __mpf.decimal_point();
1384 if (__end - __beg >= __frac)
1386 __value = string_type(__end - __frac, __end);
1387 __value.insert(__value.begin(), __d);
1392 // Have to pad zeros in the decimal position.
1393 __value = string_type(__beg, __end);
1394 const int __paddec = __frac - (__end - __beg);
1395 const char_type __zero = __ctype.widen('0');
1396 __value.insert(__value.begin(), __paddec, __zero);
1397 __value.insert(__value.begin(), __d);
1402 // Add thousands separators to non-decimal digits, per
1406 const string __grouping = __intl ? __mpt.grouping()
1408 if (__grouping.size())
1410 const char_type __sep = __intl ? __mpt.thousands_sep()
1411 : __mpf.thousands_sep();
1412 const char* __gbeg = __grouping.data();
1413 const char* __gend = __gbeg + __grouping.size();
1414 const int __n = (__end - __beg) * 2;
1416 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __n));
1417 _CharT* __ws_end = std::__add_grouping(__ws2, __sep, __gbeg,
1418 __gend, __beg, __end);
1419 __value.insert(0, __ws2, __ws_end - __ws2);
1422 __value.insert(0, string_type(__beg, __end));
1425 // Calculate length of resulting string.
1426 const ios_base::fmtflags __f = __io.flags() & ios_base::adjustfield;
1427 size_type __len = __value.size() + __sign.size();
1428 __len += (__io.flags() & ios_base::showbase) ? __symbol.size() : 0;
1429 const bool __testipad = __f == ios_base::internal && __len < __width;
1431 // Fit formatted digits into the required pattern.
1432 for (int __i = 0; __i < 4; ++__i)
1434 const part __which = static_cast<part>(__p.field[__i]);
1437 case money_base::symbol:
1438 if (__io.flags() & ios_base::showbase)
1441 case money_base::sign:
1442 // Sign might not exist, or be more than one
1443 // charater long. In that case, add in the rest
1448 case money_base::value:
1451 case money_base::space:
1452 // At least one space is required, but if internal
1453 // formatting is required, an arbitrary number of
1454 // fill spaces will be necessary.
1456 __res += string_type(__width - __len, __fill);
1458 __res += __ctype.widen(__fill);
1460 case money_base::none:
1462 __res += string_type(__width - __len, __fill);
1467 // Special case of multi-part sign parts.
1468 if (__sign.size() > 1)
1469 __res += string_type(__sign.begin() + 1, __sign.end());
1471 // Pad, if still necessary.
1472 __len = __res.size();
1473 if (__width > __len)
1475 if (__f == ios_base::left)
1477 __res.append(__width - __len, __fill);
1480 __res.insert(0, string_type(__width - __len, __fill));
1484 // Write resulting, fully-formatted string to output iterator.
1485 __s = std::__write(__s, __res.data(), __len);
1492 // NB: Not especially useful. Without an ios_base object or some
1493 // kind of locale reference, we are left clawing at the air where
1494 // the side of the mountain used to be...
1495 template<typename _CharT, typename _InIter>
1496 time_base::dateorder
1497 time_get<_CharT, _InIter>::do_date_order() const
1498 { return time_base::no_order; }
1500 template<typename _CharT, typename _InIter>
1502 time_get<_CharT, _InIter>::
1503 _M_extract_via_format(iter_type& __beg, iter_type& __end, ios_base& __io,
1504 ios_base::iostate& __err, tm* __tm,
1505 const _CharT* __format) const
1507 const locale __loc = __io.getloc();
1508 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
1509 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1510 const size_t __len = char_traits<_CharT>::length(__format);
1512 for (size_t __i = 0; __beg != __end && __i < __len && !__err; ++__i)
1514 char __c = __format[__i];
1517 // Verify valid formatting code, attempt to extract.
1518 __c = __format[++__i];
1520 if (__c == 'E' || __c == 'O')
1521 __c = __format[++__i];
1527 // Abbreviated weekday name [tm_wday]
1528 const char_type* __days1[7];
1529 __tp._M_days_abbreviated(__days1);
1530 _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 7,
1534 // Weekday name [tm_wday].
1535 const char_type* __days2[7];
1536 __tp._M_days(__days2);
1537 _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 7,
1542 // Abbreviated month name [tm_mon]
1543 const char_type* __months1[12];
1544 __tp._M_months_abbreviated(__months1);
1545 _M_extract_name(__beg, __end, __tm->tm_mon, __months1, 12,
1549 // Month name [tm_mon].
1550 const char_type* __months2[12];
1551 __tp._M_months(__months2);
1552 _M_extract_name(__beg, __end, __tm->tm_mon, __months2, 12,
1556 // Default time and date representation.
1557 const char_type* __dt[2];
1558 __tp._M_date_time_formats(__dt);
1559 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1563 // Day [01, 31]. [tm_mday]
1564 _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2,
1568 // Day [1, 31], with single digits preceded by
1570 if (__ctype.is(ctype_base::space, *__beg))
1571 _M_extract_num(++__beg, __end, __tm->tm_mday, 1, 9, 1,
1574 _M_extract_num(__beg, __end, __tm->tm_mday, 10, 31, 2,
1578 // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
1580 __ctype.widen(__cs, __cs + 9, __wcs);
1581 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1585 // Hour [00, 23]. [tm_hour]
1586 _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
1590 // Hour [01, 12]. [tm_hour]
1591 _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2,
1595 // Month [01, 12]. [tm_mon]
1596 _M_extract_num(__beg, __end, __mem, 1, 12, 2, __ctype,
1599 __tm->tm_mon = __mem - 1;
1602 // Minute [00, 59]. [tm_min]
1603 _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
1607 if (__ctype.narrow(*__beg, 0) == '\n')
1610 __err |= ios_base::failbit;
1613 // Equivalent to (%H:%M).
1615 __ctype.widen(__cs, __cs + 6, __wcs);
1616 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1621 _M_extract_num(__beg, __end, __tm->tm_sec, 0, 59, 2,
1625 if (__ctype.narrow(*__beg, 0) == '\t')
1628 __err |= ios_base::failbit;
1631 // Equivalent to (%H:%M:%S).
1633 __ctype.widen(__cs, __cs + 9, __wcs);
1634 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1639 const char_type* __dates[2];
1640 __tp._M_date_formats(__dates);
1641 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1646 const char_type* __times[2];
1647 __tp._M_time_formats(__times);
1648 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1653 // Two digit year. [tm_year]
1654 _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2,
1658 // Year [1900). [tm_year]
1659 _M_extract_num(__beg, __end, __mem, 0, 9999, 4,
1662 __tm->tm_year = __mem - 1900;
1666 if (__ctype.is(ctype_base::upper, *__beg))
1669 _M_extract_name(__beg, __end, __tmp,
1670 __timepunct_cache<_CharT>::_S_timezones,
1671 14, __ctype, __err);
1673 // GMT requires special effort.
1674 if (__beg != __end && !__err && __tmp == 0
1675 && (*__beg == __ctype.widen('-')
1676 || *__beg == __ctype.widen('+')))
1678 _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
1680 _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
1685 __err |= ios_base::failbit;
1689 __err |= ios_base::failbit;
1694 // Verify format and input match, extract and discard.
1695 if (__c == __ctype.narrow(*__beg, 0))
1698 __err |= ios_base::failbit;
1703 template<typename _CharT, typename _InIter>
1705 time_get<_CharT, _InIter>::
1706 _M_extract_num(iter_type& __beg, iter_type& __end, int& __member,
1707 int __min, int __max, size_t __len,
1708 const ctype<_CharT>& __ctype,
1709 ios_base::iostate& __err) const
1711 // As-is works for __len = 1, 2, 4, the values actually used.
1712 int __mult = __len == 2 ? 10 : (__len == 4 ? 1000 : 1);
1717 for (; __beg != __end && __i < __len; ++__beg, ++__i)
1719 const char __c = __ctype.narrow(*__beg, '*');
1720 if (__c >= '0' && __c <= '9')
1722 __value = __value * 10 + (__c - '0');
1723 const int __valuec = __value * __mult;
1724 if (__valuec > __max || __valuec + __mult < __min)
1734 __err |= ios_base::failbit;
1738 // All elements in __names are unique.
1739 template<typename _CharT, typename _InIter>
1741 time_get<_CharT, _InIter>::
1742 _M_extract_name(iter_type& __beg, iter_type& __end, int& __member,
1743 const _CharT** __names, size_t __indexlen,
1744 const ctype<_CharT>& __ctype,
1745 ios_base::iostate& __err) const
1747 typedef char_traits<_CharT> __traits_type;
1748 int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int)
1750 size_t __nmatches = 0;
1752 bool __testvalid = true;
1753 const char_type* __name;
1755 // Look for initial matches.
1756 // NB: Some of the locale data is in the form of all lowercase
1757 // names, and some is in the form of initially-capitalized
1758 // names. Look for both.
1761 const char_type __c = *__beg;
1762 for (size_t __i1 = 0; __i1 < __indexlen; ++__i1)
1763 if (__c == __names[__i1][0]
1764 || __c == __ctype.toupper(__names[__i1][0]))
1765 __matches[__nmatches++] = __i1;
1768 while (__nmatches > 1)
1770 // Find smallest matching string.
1771 size_t __minlen = 10;
1772 for (size_t __i2 = 0; __i2 < __nmatches; ++__i2)
1773 __minlen = std::min(__minlen,
1774 __traits_type::length(__names[__matches[__i2]]));
1776 if (__pos < __minlen && __beg != __end)
1779 for (size_t __i3 = 0; __i3 < __nmatches; ++__i3)
1781 __name = __names[__matches[__i3]];
1782 if (__name[__pos] != *__beg)
1783 __matches[__i3] = __matches[--__nmatches];
1790 if (__nmatches == 1)
1792 // If there was only one match, the first compare is redundant.
1799 // Make sure found name is completely extracted.
1800 __name = __names[__matches[0]];
1801 const size_t __len = __traits_type::length(__name);
1802 while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
1806 __member = __matches[0];
1808 __testvalid = false;
1811 __testvalid = false;
1813 __err |= ios_base::failbit;
1816 template<typename _CharT, typename _InIter>
1818 time_get<_CharT, _InIter>::
1819 do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
1820 ios_base::iostate& __err, tm* __tm) const
1823 const char* __cs = "%X";
1824 const locale __loc = __io.getloc();
1825 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1826 __ctype.widen(__cs, __cs + 3, __wcs);
1827 _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1829 __err |= ios_base::eofbit;
1833 template<typename _CharT, typename _InIter>
1835 time_get<_CharT, _InIter>::
1836 do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
1837 ios_base::iostate& __err, tm* __tm) const
1840 const char* __cs = "%x";
1841 const locale __loc = __io.getloc();
1842 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1843 __ctype.widen(__cs, __cs + 3, __wcs);
1844 _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1846 __err |= ios_base::eofbit;
1850 template<typename _CharT, typename _InIter>
1852 time_get<_CharT, _InIter>::
1853 do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
1854 ios_base::iostate& __err, tm* __tm) const
1856 typedef char_traits<_CharT> __traits_type;
1857 const locale __loc = __io.getloc();
1858 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
1859 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1860 const char_type* __days[7];
1861 __tp._M_days_abbreviated(__days);
1863 _M_extract_name(__beg, __end, __tmpwday, __days, 7, __ctype, __err);
1865 // Check to see if non-abbreviated name exists, and extract.
1866 // NB: Assumes both _M_days and _M_days_abbreviated organized in
1867 // exact same order, first to last, such that the resulting
1868 // __days array with the same index points to a day, and that
1869 // day's abbreviated form.
1870 // NB: Also assumes that an abbreviated name is a subset of the name.
1873 size_t __pos = __traits_type::length(__days[__tmpwday]);
1874 __tp._M_days(__days);
1875 const char_type* __name = __days[__tmpwday];
1876 if (__name[__pos] == *__beg)
1878 // Extract the rest of it.
1879 const size_t __len = __traits_type::length(__name);
1880 while (__pos < __len && __beg != __end
1881 && __name[__pos] == *__beg)
1884 __err |= ios_base::failbit;
1887 __tm->tm_wday = __tmpwday;
1890 __err |= ios_base::eofbit;
1894 template<typename _CharT, typename _InIter>
1896 time_get<_CharT, _InIter>::
1897 do_get_monthname(iter_type __beg, iter_type __end,
1898 ios_base& __io, ios_base::iostate& __err, tm* __tm) const
1900 typedef char_traits<_CharT> __traits_type;
1901 const locale __loc = __io.getloc();
1902 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
1903 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1904 const char_type* __months[12];
1905 __tp._M_months_abbreviated(__months);
1907 _M_extract_name(__beg, __end, __tmpmon, __months, 12, __ctype, __err);
1909 // Check to see if non-abbreviated name exists, and extract.
1910 // NB: Assumes both _M_months and _M_months_abbreviated organized in
1911 // exact same order, first to last, such that the resulting
1912 // __months array with the same index points to a month, and that
1913 // month's abbreviated form.
1914 // NB: Also assumes that an abbreviated name is a subset of the name.
1917 size_t __pos = __traits_type::length(__months[__tmpmon]);
1918 __tp._M_months(__months);
1919 const char_type* __name = __months[__tmpmon];
1920 if (__name[__pos] == *__beg)
1922 // Extract the rest of it.
1923 const size_t __len = __traits_type::length(__name);
1924 while (__pos < __len && __beg != __end
1925 && __name[__pos] == *__beg)
1928 __err |= ios_base::failbit;
1931 __tm->tm_mon = __tmpmon;
1935 __err |= ios_base::eofbit;
1939 template<typename _CharT, typename _InIter>
1941 time_get<_CharT, _InIter>::
1942 do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
1943 ios_base::iostate& __err, tm* __tm) const
1945 const locale __loc = __io.getloc();
1946 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1950 for (; __beg != __end && __i < 4; ++__beg, ++__i)
1952 const char __c = __ctype.narrow(*__beg, '*');
1953 if (__c >= '0' && __c <= '9')
1954 __value = __value * 10 + (__c - '0');
1958 if (__i == 2 || __i == 4)
1959 __tm->tm_year = __i == 2 ? __value : __value - 1900;
1961 __err |= ios_base::failbit;
1963 __err |= ios_base::eofbit;
1967 template<typename _CharT, typename _OutIter>
1969 time_put<_CharT, _OutIter>::
1970 put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
1971 const _CharT* __beg, const _CharT* __end) const
1973 const locale __loc = __io.getloc();
1974 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1975 for (; __beg != __end; ++__beg)
1976 if (__ctype.narrow(*__beg, 0) != '%')
1981 else if (++__beg != __end)
1985 const char __c = __ctype.narrow(*__beg, 0);
1986 if (__c != 'E' && __c != 'O')
1988 else if (++__beg != __end)
1991 __format = __ctype.narrow(*__beg, 0);
1995 __s = this->do_put(__s, __io, __fill, __tm,
2003 template<typename _CharT, typename _OutIter>
2005 time_put<_CharT, _OutIter>::
2006 do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
2007 char __format, char __mod) const
2009 const locale __loc = __io.getloc();
2010 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2011 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
2013 // NB: This size is arbitrary. Should this be a data member,
2014 // initialized at construction?
2015 const size_t __maxlen = 64;
2016 char_type* __res = static_cast<char_type*>(__builtin_alloca(sizeof(char_type)
2019 // NB: In IEE 1003.1-200x, and perhaps other locale models, it
2020 // is possible that the format character will be longer than one
2021 // character. Possibilities include 'E' or 'O' followed by a
2022 // format character: if __mod is not the default argument, assume
2023 // it's a valid modifier.
2025 __fmt[0] = __ctype.widen('%');
2028 __fmt[1] = __format;
2029 __fmt[2] = char_type();
2034 __fmt[2] = __format;
2035 __fmt[3] = char_type();
2038 __tp._M_put(__res, __maxlen, __fmt, __tm);
2040 // Write resulting, fully-formatted string to output iterator.
2041 return std::__write(__s, __res, char_traits<char_type>::length(__res));
2045 // Generic version does nothing.
2046 template<typename _CharT>
2048 collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
2051 // Generic version does nothing.
2052 template<typename _CharT>
2054 collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
2057 template<typename _CharT>
2060 do_compare(const _CharT* __lo1, const _CharT* __hi1,
2061 const _CharT* __lo2, const _CharT* __hi2) const
2063 // strcoll assumes zero-terminated strings so we make a copy
2064 // and then put a zero at the end.
2065 const string_type __one(__lo1, __hi1);
2066 const string_type __two(__lo2, __hi2);
2068 const _CharT* __p = __one.c_str();
2069 const _CharT* __pend = __one.data() + __one.length();
2070 const _CharT* __q = __two.c_str();
2071 const _CharT* __qend = __two.data() + __two.length();
2073 // strcoll stops when it sees a nul character so we break
2074 // the strings into zero-terminated substrings and pass those
2078 const int __res = _M_compare(__p, __q);
2082 __p += char_traits<_CharT>::length(__p);
2083 __q += char_traits<_CharT>::length(__q);
2084 if (__p == __pend && __q == __qend)
2086 else if (__p == __pend)
2088 else if (__q == __qend)
2096 template<typename _CharT>
2097 typename collate<_CharT>::string_type
2099 do_transform(const _CharT* __lo, const _CharT* __hi) const
2101 // strxfrm assumes zero-terminated strings so we make a copy
2102 string_type __str(__lo, __hi);
2104 const _CharT* __p = __str.c_str();
2105 const _CharT* __pend = __str.data() + __str.length();
2107 size_t __len = (__hi - __lo) * 2;
2111 // strxfrm stops when it sees a nul character so we break
2112 // the string into zero-terminated substrings and pass those
2116 // First try a buffer perhaps big enough.
2118 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len));
2119 size_t __res = _M_transform(__c, __p, __len);
2120 // If the buffer was not large enough, try again with the
2125 __c = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
2127 __res = _M_transform(__c, __p, __res + 1);
2130 __ret.append(__c, __res);
2131 __p += char_traits<_CharT>::length(__p);
2136 __ret.push_back(_CharT());
2140 template<typename _CharT>
2143 do_hash(const _CharT* __lo, const _CharT* __hi) const
2145 unsigned long __val = 0;
2146 for (; __lo < __hi; ++__lo)
2147 __val = *__lo + ((__val << 7) |
2148 (__val >> (numeric_limits<unsigned long>::digits - 7)));
2149 return static_cast<long>(__val);
2152 // Construct correctly padded string, as per 22.2.2.2.2
2154 // __newlen > __oldlen
2155 // __news is allocated for __newlen size
2156 // Used by both num_put and ostream inserters: if __num,
2157 // internal-adjusted objects are padded according to the rules below
2158 // concerning 0[xX] and +-, otherwise, exactly as right-adjusted
2161 // NB: Of the two parameters, _CharT can be deduced from the
2162 // function arguments. The other (_Traits) has to be explicitly specified.
2163 template<typename _CharT, typename _Traits>
2165 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
2166 _CharT* __news, const _CharT* __olds,
2167 const streamsize __newlen,
2168 const streamsize __oldlen, const bool __num)
2170 const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
2171 const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
2174 if (__adjust == ios_base::left)
2176 _Traits::copy(__news, const_cast<_CharT*>(__olds), __oldlen);
2177 _Traits::assign(__news + __oldlen, __plen, __fill);
2182 if (__adjust == ios_base::internal && __num)
2184 // Pad after the sign, if there is one.
2185 // Pad after 0[xX], if there is one.
2186 // Who came up with these rules, anyway? Jeeze.
2187 const locale& __loc = __io._M_getloc();
2188 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2189 const _CharT __minus = __ctype.widen('-');
2190 const _CharT __plus = __ctype.widen('+');
2191 const bool __testsign = _Traits::eq(__olds[0], __minus)
2192 || _Traits::eq(__olds[0], __plus);
2194 const bool __testhex = _Traits::eq(__ctype.widen('0'), __olds[0])
2195 && (_Traits::eq(__ctype.widen('x'), __olds[1])
2196 || _Traits::eq(__ctype.widen('X'), __olds[1]));
2199 __news[0] = __olds[0];
2200 __news[1] = __olds[1];
2204 else if (__testsign)
2206 __news[0] = __olds[0];
2210 // else Padding first.
2212 _Traits::assign(__news, __plen, __fill);
2213 _Traits::copy(__news + __plen, const_cast<_CharT*>(__olds + __mod),
2217 template<typename _CharT>
2219 __verify_grouping(const basic_string<_CharT>& __grouping,
2220 const basic_string<_CharT>& __grouping_tmp)
2224 const size_t __len = __grouping.size();
2225 const size_t __n = __grouping_tmp.size();
2228 // Parsed number groupings have to match the
2229 // numpunct::grouping string exactly, starting at the
2230 // right-most point of the parsed sequence of elements ...
2231 while (__test && __i < __n - 1)
2232 for (__j = 0; __test && __j < __len && __i < __n - 1; ++__j, ++__i)
2233 __test = __grouping[__j] == __grouping_tmp[__n - __i - 1];
2234 // ... but the last parsed grouping can be <= numpunct
2236 __j == __len ? __j = 0 : __j;
2237 __test &= __grouping[__j] >= __grouping_tmp[__n - __i - 1];
2241 template<typename _CharT>
2243 __add_grouping(_CharT* __s, _CharT __sep,
2244 const char* __gbeg, const char* __gend,
2245 const _CharT* __first, const _CharT* __last)
2247 if (__last - __first > *__gbeg)
2249 const bool __bump = __gbeg + 1 != __gend;
2250 __s = std::__add_grouping(__s, __sep, __gbeg + __bump,
2251 __gend, __first, __last - *__gbeg);
2252 __first = __last - *__gbeg;
2256 *__s++ = *__first++;
2257 while (__first != __last);
2261 // Inhibit implicit instantiations for required instantiations,
2262 // which are defined via explicit instantiations elsewhere.
2263 // NB: This syntax is a GNU extension.
2264 #if _GLIBCXX_EXTERN_TEMPLATE
2265 extern template class moneypunct<char, false>;
2266 extern template class moneypunct<char, true>;
2267 extern template class moneypunct_byname<char, false>;
2268 extern template class moneypunct_byname<char, true>;
2269 extern template class money_get<char>;
2270 extern template class money_put<char>;
2271 extern template class numpunct<char>;
2272 extern template class numpunct_byname<char>;
2273 extern template class num_get<char>;
2274 extern template class num_put<char>;
2275 extern template class __timepunct<char>;
2276 extern template class time_put<char>;
2277 extern template class time_put_byname<char>;
2278 extern template class time_get<char>;
2279 extern template class time_get_byname<char>;
2280 extern template class messages<char>;
2281 extern template class messages_byname<char>;
2282 extern template class ctype_byname<char>;
2283 extern template class codecvt_byname<char, char, mbstate_t>;
2284 extern template class collate<char>;
2285 extern template class collate_byname<char>;
2288 const codecvt<char, char, mbstate_t>&
2289 use_facet<codecvt<char, char, mbstate_t> >(const locale&);
2292 const collate<char>&
2293 use_facet<collate<char> >(const locale&);
2296 const numpunct<char>&
2297 use_facet<numpunct<char> >(const locale&);
2300 const num_put<char>&
2301 use_facet<num_put<char> >(const locale&);
2304 const num_get<char>&
2305 use_facet<num_get<char> >(const locale&);
2308 const moneypunct<char, true>&
2309 use_facet<moneypunct<char, true> >(const locale&);
2312 const moneypunct<char, false>&
2313 use_facet<moneypunct<char, false> >(const locale&);
2316 const money_put<char>&
2317 use_facet<money_put<char> >(const locale&);
2320 const money_get<char>&
2321 use_facet<money_get<char> >(const locale&);
2324 const __timepunct<char>&
2325 use_facet<__timepunct<char> >(const locale&);
2328 const time_put<char>&
2329 use_facet<time_put<char> >(const locale&);
2332 const time_get<char>&
2333 use_facet<time_get<char> >(const locale&);
2336 const messages<char>&
2337 use_facet<messages<char> >(const locale&);
2341 has_facet<ctype<char> >(const locale&);
2345 has_facet<codecvt<char, char, mbstate_t> >(const locale&);
2349 has_facet<collate<char> >(const locale&);
2353 has_facet<numpunct<char> >(const locale&);
2357 has_facet<num_put<char> >(const locale&);
2361 has_facet<num_get<char> >(const locale&);
2365 has_facet<moneypunct<char> >(const locale&);
2369 has_facet<money_put<char> >(const locale&);
2373 has_facet<money_get<char> >(const locale&);
2377 has_facet<__timepunct<char> >(const locale&);
2381 has_facet<time_put<char> >(const locale&);
2385 has_facet<time_get<char> >(const locale&);
2389 has_facet<messages<char> >(const locale&);
2391 #ifdef _GLIBCXX_USE_WCHAR_T
2392 extern template class moneypunct<wchar_t, false>;
2393 extern template class moneypunct<wchar_t, true>;
2394 extern template class moneypunct_byname<wchar_t, false>;
2395 extern template class moneypunct_byname<wchar_t, true>;
2396 extern template class money_get<wchar_t>;
2397 extern template class money_put<wchar_t>;
2398 extern template class numpunct<wchar_t>;
2399 extern template class numpunct_byname<wchar_t>;
2400 extern template class num_get<wchar_t>;
2401 extern template class num_put<wchar_t>;
2402 extern template class __timepunct<wchar_t>;
2403 extern template class time_put<wchar_t>;
2404 extern template class time_put_byname<wchar_t>;
2405 extern template class time_get<wchar_t>;
2406 extern template class time_get_byname<wchar_t>;
2407 extern template class messages<wchar_t>;
2408 extern template class messages_byname<wchar_t>;
2409 extern template class ctype_byname<wchar_t>;
2410 extern template class codecvt_byname<wchar_t, char, mbstate_t>;
2411 extern template class collate<wchar_t>;
2412 extern template class collate_byname<wchar_t>;
2415 const codecvt<wchar_t, char, mbstate_t>&
2416 use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&);
2419 const collate<wchar_t>&
2420 use_facet<collate<wchar_t> >(const locale&);
2423 const numpunct<wchar_t>&
2424 use_facet<numpunct<wchar_t> >(const locale&);
2427 const num_put<wchar_t>&
2428 use_facet<num_put<wchar_t> >(const locale&);
2431 const num_get<wchar_t>&
2432 use_facet<num_get<wchar_t> >(const locale&);
2435 const moneypunct<wchar_t, true>&
2436 use_facet<moneypunct<wchar_t, true> >(const locale&);
2439 const moneypunct<wchar_t, false>&
2440 use_facet<moneypunct<wchar_t, false> >(const locale&);
2443 const money_put<wchar_t>&
2444 use_facet<money_put<wchar_t> >(const locale&);
2447 const money_get<wchar_t>&
2448 use_facet<money_get<wchar_t> >(const locale&);
2451 const __timepunct<wchar_t>&
2452 use_facet<__timepunct<wchar_t> >(const locale&);
2455 const time_put<wchar_t>&
2456 use_facet<time_put<wchar_t> >(const locale&);
2459 const time_get<wchar_t>&
2460 use_facet<time_get<wchar_t> >(const locale&);
2463 const messages<wchar_t>&
2464 use_facet<messages<wchar_t> >(const locale&);
2468 has_facet<ctype<wchar_t> >(const locale&);
2472 has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
2476 has_facet<collate<wchar_t> >(const locale&);
2480 has_facet<numpunct<wchar_t> >(const locale&);
2484 has_facet<num_put<wchar_t> >(const locale&);
2488 has_facet<num_get<wchar_t> >(const locale&);
2492 has_facet<moneypunct<wchar_t> >(const locale&);
2496 has_facet<money_put<wchar_t> >(const locale&);
2500 has_facet<money_get<wchar_t> >(const locale&);
2504 has_facet<__timepunct<wchar_t> >(const locale&);
2508 has_facet<time_put<wchar_t> >(const locale&);
2512 has_facet<time_get<wchar_t> >(const locale&);
2516 has_facet<messages<wchar_t> >(const locale&);