2003-07-04 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / bits / locale_facets.tcc
blob518f41ad0566983898b03fe568abcee4ee8fdc24
1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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 <cerrno>
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>
47 namespace std
49   template<typename _Facet>
50     locale
51     locale::combine(const locale& __other) const
52     {
53       _Impl* __tmp = new _Impl(*_M_impl, 1);
54       __tmp->_M_replace_facet(__other._M_impl, &_Facet::id);
55       return locale(__tmp);
56     }
58   template<typename _CharT, typename _Traits, typename _Alloc>
59     bool
60     locale::operator()(const basic_string<_CharT, _Traits, _Alloc>& __s1,
61                        const basic_string<_CharT, _Traits, _Alloc>& __s2) const
62     {
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);
67     }
69   template<typename _Facet>
70     inline bool
71     has_facet(const locale& __loc) throw()
72     {
73       size_t __i = _Facet::id._M_id();
74       const locale::facet** __facets = __loc._M_impl->_M_facets;
75       return (__i < __loc._M_impl->_M_facets_size && __facets[__i]);
76     }
78   template<typename _Facet>
79     inline const _Facet&
80     use_facet(const locale& __loc)
81     {
82       size_t __i = _Facet::id._M_id();
83       const locale::facet** __facets = __loc._M_impl->_M_facets;
84       if (!(__i < __loc._M_impl->_M_facets_size && __facets[__i]))
85         __throw_bad_cast();
86       return static_cast<const _Facet&>(*__facets[__i]);
87     }
89   // Routine to access a cache for the facet.  If the cache didn't
90   // exist before, it gets constructed on the fly.
91   template<typename _Facet>
92     const _Facet&
93     __use_cache(const locale& __loc);
95   template<>
96     const __numpunct_cache<char>&
97     __use_cache(const locale& __loc);
99 #ifdef _GLIBCXX_USE_WCHAR_T
100   template<>
101     const __numpunct_cache<wchar_t>&
102     __use_cache(const locale& __loc);
103 #endif
105   // Stage 1: Determine a conversion specifier.
106   template<typename _CharT, typename _InIter>
107     _InIter
108     num_get<_CharT, _InIter>::
109     _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
110                      ios_base::iostate& __err, string& __xtrc) const
111     {
112       typedef char_traits<_CharT>               __traits_type;
113       const locale __loc = __io.getloc();
114       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
115       const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
117       // First check for sign.
118       const char_type __plus = __ctype.widen('+');
119       const char_type __minus = __ctype.widen('-');
120       int __pos = 0;
121       char_type  __c = *__beg;
122       if ((__traits_type::eq(__c, __plus) || __traits_type::eq(__c, __minus))
123           && __beg != __end)
124         {
125           __xtrc += __ctype.narrow(__c, char());
126           ++__pos;
127           __c = *(++__beg);
128         }
130       // Next, strip leading zeros.
131       const char_type __zero = __ctype.widen(_S_atoms_in[_S_izero]);
132       bool __found_zero = false;
133       while (__traits_type::eq(__c, __zero) && __beg != __end)
134         {
135           __c = *(++__beg);
136           __found_zero = true;
137         }
138       if (__found_zero)
139         {
140           __xtrc += _S_atoms_in[_S_izero];
141           ++__pos;
142         }
144       // Only need acceptable digits for floating point numbers.
145       const size_t __len = _S_iE - _S_izero + 1;
146       char_type  __watoms[__len];
147       __ctype.widen(_S_atoms_in, _S_atoms_in + __len, __watoms);
148       bool __found_dec = false;
149       bool __found_sci = false;
150       const char_type __dec = __np.decimal_point();
152       string __found_grouping;
153       const string __grouping = __np.grouping();
154       bool __check_grouping = __grouping.size();
155       int __sep_pos = 0;
156       const char_type __sep = __np.thousands_sep();
158       while (__beg != __end)
159         {
160           // Only look in digits.
161           const char_type* __p = __traits_type::find(__watoms, 10,  __c);
163           // NB: strchr returns true for __c == 0x0
164           if (__p && !__traits_type::eq(__c, char_type()))
165             {
166               // Try first for acceptable digit; record it if found.
167               ++__pos;
168               __xtrc += _S_atoms_in[__p - __watoms];
169               ++__sep_pos;
170               __c = *(++__beg);
171             }
172           else if (__traits_type::eq(__c, __sep) 
173                    && __check_grouping && !__found_dec)
174             {
175               // NB: Thousands separator at the beginning of a string
176               // is a no-no, as is two consecutive thousands separators.
177               if (__sep_pos)
178                 {
179                   __found_grouping += static_cast<char>(__sep_pos);
180                   __sep_pos = 0;
181                   __c = *(++__beg);
182                 }
183               else
184                 {
185                   __err |= ios_base::failbit;
186                   break;
187                 }
188             }
189           else if (__traits_type::eq(__c, __dec) && !__found_dec)
190             {
191               // According to the standard, if no grouping chars are seen,
192               // no grouping check is applied. Therefore __found_grouping
193               // must be adjusted only if __dec comes after some __sep.
194               if (__found_grouping.size())
195                 __found_grouping += static_cast<char>(__sep_pos);
196               ++__pos;
197               __xtrc += '.';
198               __c = *(++__beg);
199               __found_dec = true;
200             }
201           else if ((__traits_type::eq(__c, __watoms[_S_ie]) 
202                     || __traits_type::eq(__c, __watoms[_S_iE])) 
203                    && !__found_sci && __pos)
204             {
205               // Scientific notation.
206               ++__pos;
207               __xtrc += __ctype.narrow(__c, char());
208               __c = *(++__beg);
210               // Remove optional plus or minus sign, if they exist.
211               if (__traits_type::eq(__c, __plus) 
212                   || __traits_type::eq(__c, __minus))
213                 {
214                   ++__pos;
215                   __xtrc += __ctype.narrow(__c, char());
216                   __c = *(++__beg);
217                 }
218               __found_sci = true;
219             }
220           else
221             // Not a valid input item.
222             break;
223         }
225       // Digit grouping is checked. If grouping and found_grouping don't
226       // match, then get very very upset, and set failbit.
227       if (__check_grouping && __found_grouping.size())
228         {
229           // Add the ending grouping if a decimal wasn't found.
230           if (!__found_dec)
231             __found_grouping += static_cast<char>(__sep_pos);
232           if (!__verify_grouping(__grouping, __found_grouping))
233             __err |= ios_base::failbit;
234         }
236       // Finish up
237       __xtrc += char();
238       if (__beg == __end)
239         __err |= ios_base::eofbit;
240       return __beg;
241     }
243   // Stage 1: Determine a conversion specifier.
244   template<typename _CharT, typename _InIter>
245     _InIter
246     num_get<_CharT, _InIter>::
247     _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
248                    ios_base::iostate& __err, string& __xtrc, int& __base) const
249     {
250       typedef char_traits<_CharT>               __traits_type;
251       const locale __loc = __io.getloc();
252       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
253       const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
255       // NB: Iff __basefield == 0, this can change based on contents.
256       ios_base::fmtflags __basefield = __io.flags() & ios_base::basefield;
257       if (__basefield == ios_base::oct)
258         __base = 8;
259       else if (__basefield == ios_base::hex)
260         __base = 16;
261       else
262         __base = 10;
264       // First check for sign.
265       int __pos = 0;
266       char_type  __c = *__beg;
267       const char_type __plus = __ctype.widen('+');
268       const char_type __minus = __ctype.widen('-');
270       if ((__traits_type::eq(__c, __plus) || __traits_type::eq(__c, __minus))
271           && __beg != __end)
272         {
273           __xtrc += __ctype.narrow(__c, char());
274           ++__pos;
275           __c = *(++__beg);
276         }
278       // Next, strip leading zeros and check required digits for base formats.
279       const char_type __zero = __ctype.widen(_S_atoms_in[_S_izero]);
280       const char_type __x = __ctype.widen('x');
281       const char_type __X = __ctype.widen('X');
282       if (__base == 10)
283         {
284           bool __found_zero = false;
285           while (__traits_type::eq(__c, __zero) && __beg != __end)
286             {
287               __c = *(++__beg);
288               __found_zero = true;
289             }
290           if (__found_zero)
291             {
292               __xtrc += _S_atoms_in[_S_izero];
293               ++__pos;
294               if (__basefield == 0)
295                 {             
296                   if ((__traits_type::eq(__c, __x) 
297                        || __traits_type::eq(__c, __X))
298                       && __beg != __end)
299                     {
300                       __xtrc += __ctype.narrow(__c, char());
301                       ++__pos;
302                       __c = *(++__beg);
303                       __base = 16;
304                     }
305                   else 
306                     __base = 8;
307                 }
308             }
309         }
310       else if (__base == 16)
311         {
312           if (__traits_type::eq(__c, __zero) && __beg != __end)
313             {
314               __xtrc += _S_atoms_in[_S_izero];
315               ++__pos;
316               __c = *(++__beg); 
317               if ((__traits_type::eq(__c, __x) || __traits_type::eq(__c, __X))
318                   && __beg != __end)
319                 {
320                   __xtrc += __ctype.narrow(__c, char());
321                   ++__pos;
322                   __c = *(++__beg);
323                 }
324             }
325         }
327       // At this point, base is determined. If not hex, only allow
328       // base digits as valid input.
329       size_t __len;
330       if (__base == 16)
331         __len = _S_iend;
332       else
333         __len = __base;
335       // Extract.
336       char_type __watoms[_S_iend];
337       __ctype.widen(_S_atoms_in, _S_atoms_in + __len, __watoms);
338       string __found_grouping;
339       const string __grouping = __np.grouping();
340       bool __check_grouping = __grouping.size();
341       int __sep_pos = 0;
342       const char_type __sep = __np.thousands_sep();
343       while (__beg != __end)
344         {
345           const char_type* __p = __traits_type::find(__watoms, __len,  __c);
347           // NB: strchr returns true for __c == 0x0
348           if (__p && !__traits_type::eq(__c, char_type()))
349             {
350               // Try first for acceptable digit; record it if found.
351               __xtrc += _S_atoms_in[__p - __watoms];
352               ++__pos;
353               ++__sep_pos;
354               __c = *(++__beg);
355             }
356           else if (__traits_type::eq(__c, __sep) && __check_grouping)
357             {
358               // NB: Thousands separator at the beginning of a string
359               // is a no-no, as is two consecutive thousands separators.
360               if (__sep_pos)
361                 {
362                   __found_grouping += static_cast<char>(__sep_pos);
363                   __sep_pos = 0;
364                   __c = *(++__beg);
365                 }
366               else
367                 {
368                   __err |= ios_base::failbit;
369                   break;
370                 }
371             }
372           else
373             // Not a valid input item.
374             break;
375         }
377       // Digit grouping is checked. If grouping and found_grouping don't
378       // match, then get very very upset, and set failbit.
379       if (__check_grouping && __found_grouping.size())
380         {
381           // Add the ending grouping.
382           __found_grouping += static_cast<char>(__sep_pos);
383           if (!__verify_grouping(__grouping, __found_grouping))
384             __err |= ios_base::failbit;
385         }
387       // Finish up.
388       __xtrc += char();
389       if (__beg == __end)
390         __err |= ios_base::eofbit;
391       return __beg;
392     }
394 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
395   //17.  Bad bool parsing
396   template<typename _CharT, typename _InIter>
397     _InIter
398     num_get<_CharT, _InIter>::
399     do_get(iter_type __beg, iter_type __end, ios_base& __io,
400            ios_base::iostate& __err, bool& __v) const
401     {
402       // Parse bool values as unsigned long
403       if (!(__io.flags() & ios_base::boolalpha))
404         {
405           // NB: We can't just call do_get(long) here, as it might
406           // refer to a derived class.
407           string __xtrc;
408           int __base;
409           __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
411           unsigned long __ul; 
412           __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
413           if (!(__err & ios_base::failbit) && __ul <= 1)
414             __v = __ul;
415           else 
416             __err |= ios_base::failbit;
417         }
419       // Parse bool values as alphanumeric
420       else
421         {
422           typedef char_traits<_CharT>           __traits_type;
423           typedef basic_string<_CharT>          __string_type;
425           locale __loc = __io.getloc();
426           const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc); 
427           const __string_type __true = __np.truename();
428           const __string_type __false = __np.falsename();
429           const char_type* __trues = __true.c_str();
430           const char_type* __falses = __false.c_str();
431           const size_t __truen =  __true.size() - 1;
432           const size_t __falsen =  __false.size() - 1;
434           for (size_t __n = 0; __beg != __end; ++__n)
435             {
436               char_type __c = *__beg++;
437               bool __testf = __n <= __falsen 
438                              ? __traits_type::eq(__c, __falses[__n]) : false;
439               bool __testt = __n <= __truen 
440                              ? __traits_type::eq(__c, __trues[__n]) : false;
441               if (!(__testf || __testt))
442                 {
443                   __err |= ios_base::failbit;
444                   break;
445                 }
446               else if (__testf && __n == __falsen)
447                 {
448                   __v = 0;
449                   break;
450                 }
451               else if (__testt && __n == __truen)
452                 {
453                   __v = 1;
454                   break;
455                 }
456             }
457           if (__beg == __end)
458             __err |= ios_base::eofbit;
459         }
460       return __beg;
461     }
462 #endif
464   template<typename _CharT, typename _InIter>
465     _InIter
466     num_get<_CharT, _InIter>::
467     do_get(iter_type __beg, iter_type __end, ios_base& __io,
468            ios_base::iostate& __err, long& __v) const
469     {
470       string __xtrc;
471       int __base;
472       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
473       __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
474       return __beg;
475     }
477   template<typename _CharT, typename _InIter>
478     _InIter
479     num_get<_CharT, _InIter>::
480     do_get(iter_type __beg, iter_type __end, ios_base& __io,
481            ios_base::iostate& __err, unsigned short& __v) const
482     {
483       string __xtrc;
484       int __base;
485       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
486       unsigned long __ul;
487       __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
488       if (!(__err & ios_base::failbit) 
489           && __ul <= numeric_limits<unsigned short>::max())
490         __v = static_cast<unsigned short>(__ul);
491       else 
492         __err |= ios_base::failbit;
493       return __beg;
494     }
496   template<typename _CharT, typename _InIter>
497     _InIter
498     num_get<_CharT, _InIter>::
499     do_get(iter_type __beg, iter_type __end, ios_base& __io,
500            ios_base::iostate& __err, unsigned int& __v) const
501     {
502       string __xtrc;
503       int __base;
504       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
505       unsigned long __ul;
506       __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
507       if (!(__err & ios_base::failbit) 
508           && __ul <= numeric_limits<unsigned int>::max())
509         __v = static_cast<unsigned int>(__ul);
510       else 
511         __err |= ios_base::failbit;
512       return __beg;
513     }
515   template<typename _CharT, typename _InIter>
516     _InIter
517     num_get<_CharT, _InIter>::
518     do_get(iter_type __beg, iter_type __end, ios_base& __io,
519            ios_base::iostate& __err, unsigned long& __v) const
520     {
521       string __xtrc;
522       int __base;
523       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
524       __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
525       return __beg;
526     }
528 #ifdef _GLIBCXX_USE_LONG_LONG
529   template<typename _CharT, typename _InIter>
530     _InIter
531     num_get<_CharT, _InIter>::
532     do_get(iter_type __beg, iter_type __end, ios_base& __io,
533            ios_base::iostate& __err, long long& __v) const
534     {
535       string __xtrc;
536       int __base;
537       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
538       __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
539       return __beg;
540     }
542   template<typename _CharT, typename _InIter>
543     _InIter
544     num_get<_CharT, _InIter>::
545     do_get(iter_type __beg, iter_type __end, ios_base& __io,
546            ios_base::iostate& __err, unsigned long long& __v) const
547     {
548       string __xtrc;
549       int __base;
550       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
551       __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale, __base);
552       return __beg;
553     }
554 #endif
556   template<typename _CharT, typename _InIter>
557     _InIter
558     num_get<_CharT, _InIter>::
559     do_get(iter_type __beg, iter_type __end, ios_base& __io, 
560            ios_base::iostate& __err, float& __v) const
561     {
562       string __xtrc;
563       __xtrc.reserve(32);
564       __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
565       __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale);
566       return __beg;
567     }
569   template<typename _CharT, typename _InIter>
570     _InIter
571     num_get<_CharT, _InIter>::
572     do_get(iter_type __beg, iter_type __end, ios_base& __io,
573            ios_base::iostate& __err, double& __v) const
574     {
575       string __xtrc;
576       __xtrc.reserve(32);
577       __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
578       __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale);
579       return __beg;
580     }
582   template<typename _CharT, typename _InIter>
583     _InIter
584     num_get<_CharT, _InIter>::
585     do_get(iter_type __beg, iter_type __end, ios_base& __io,
586            ios_base::iostate& __err, long double& __v) const
587     {
588       string __xtrc;
589       __xtrc.reserve(32);
590       __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
591       __convert_to_v(__xtrc.c_str(), __v, __err, _S_c_locale);
592       return __beg;
593     }
595   template<typename _CharT, typename _InIter>
596     _InIter
597     num_get<_CharT, _InIter>::
598     do_get(iter_type __beg, iter_type __end, ios_base& __io,
599            ios_base::iostate& __err, void*& __v) const
600     {
601       // Prepare for hex formatted input
602       typedef ios_base::fmtflags        fmtflags;
603       fmtflags __fmt = __io.flags();
604       fmtflags __fmtmask = ~(ios_base::showpos | ios_base::basefield
605                              | ios_base::uppercase | ios_base::internal);
606       __io.flags(__fmt & __fmtmask | (ios_base::hex | ios_base::showbase));
608       string __xtrc;
609       int __base;
610       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
612       // Reset from hex formatted input
613       __io.flags(__fmt);
615       unsigned long __ul;
616       __convert_to_v(__xtrc.c_str(), __ul, __err, _S_c_locale, __base);
617       if (!(__err & ios_base::failbit))
618         __v = reinterpret_cast<void*>(__ul);
619       else 
620         __err |= ios_base::failbit;
621       return __beg;
622     }
624   // For use by integer and floating-point types after they have been
625   // converted into a char_type string.
626   template<typename _CharT, typename _OutIter>
627     void
628     num_put<_CharT, _OutIter>::
629     _M_pad(_CharT __fill, streamsize __w, ios_base& __io, 
630            _CharT* __new, const _CharT* __cs, int& __len) const
631     {
632       // [22.2.2.2.2] Stage 3.
633       // If necessary, pad.
634       __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new, __cs, 
635                                                   __w, __len, true);
636       __len = static_cast<int>(__w);
637     }
639   // Forwarding functions to peel signed from unsigned integer types.
640   template<typename _CharT>
641     inline int
642     __int_to_char(_CharT* __out, const int __size, long __v,
643                        const _CharT* __lit, ios_base::fmtflags __flags)
644     {
645       unsigned long __ul = static_cast<unsigned long>(__v);
646       bool __neg = false;
647       if (__v < 0) 
648         {
649           __ul = -__ul;
650           __neg = true;
651         }
652       return __int_to_char(__out, __size, __ul, __lit, __flags, __neg); 
653     }
655   template<typename _CharT>
656     inline int
657     __int_to_char(_CharT* __out, const int __size, unsigned long __v,
658                        const _CharT* __lit, ios_base::fmtflags __flags)
659     { return __int_to_char(__out, __size, __v, __lit, __flags, false); }
661 #ifdef _GLIBCXX_USE_LONG_LONG
662   template<typename _CharT>
663     inline int
664     __int_to_char(_CharT* __out, const int __size, long long __v,
665                        const _CharT* __lit, ios_base::fmtflags __flags)
666     { 
667       unsigned long long __ull = static_cast<unsigned long long>(__v);
668       bool __neg = false;
669       if (__v < 0) 
670         {
671           __ull = -__ull;
672           __neg = true;
673         }
674       return __int_to_char(__out, __size, __ull, __lit, __flags, __neg); 
675     }
677   template<typename _CharT>
678     inline int
679     __int_to_char(_CharT* __out, const int __size, unsigned long long __v,
680                        const _CharT* __lit, ios_base::fmtflags __flags)
681     { return __int_to_char(__out, __size, __v, __lit, __flags, false); }
682 #endif
683       
684   template<typename _CharT, typename _ValueT>
685     int
686     __int_to_char(_CharT* __out, const int __size, _ValueT __v,
687                   const _CharT* __lit, ios_base::fmtflags __flags, bool __neg)
688     {
689       // Don't write base if already 0.
690       const bool __showbase = (__flags & ios_base::showbase) && __v;
691       const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
692       _CharT* __buf = __out + __size - 1;
693       _CharT* __bufend = __out + __size;
695       if (__builtin_expect(__basefield != ios_base::oct &&
696                            __basefield != ios_base::hex, true))
697         {
698           // Decimal.
699           do 
700             {
701               *__buf-- = __lit[(__v % 10) + __num_base::_S_odigits];
702               __v /= 10;
703             } 
704           while (__v != 0);
705           if (__neg)
706             *__buf-- = __lit[__num_base::_S_ominus];
707           else if (__flags & ios_base::showpos)
708             *__buf-- = __lit[__num_base::_S_oplus];
709         }
710         else if (__basefield == ios_base::oct)
711         {
712           // Octal.
713           do 
714             {
715               *__buf-- = __lit[(__v & 0x7) + __num_base::_S_odigits];
716               __v >>= 3;
717             } 
718           while (__v != 0);
719           if (__showbase)
720             *__buf-- = __lit[__num_base::_S_odigits];
721         }
722       else
723         {
724           // Hex.
725           const bool __uppercase = __flags & ios_base::uppercase;
726           int __case_offset = __uppercase ? __num_base::_S_oudigits 
727                                           : __num_base::_S_odigits;
728           do 
729             {
730               *__buf-- = __lit[(__v & 0xf) + __case_offset];
731               __v >>= 4;
732             } 
733           while (__v != 0);
734           if (__showbase)
735             {
736               // 'x' or 'X'
737               *__buf-- = __lit[__num_base::_S_ox + __uppercase];
738               // '0'
739               *__buf-- = __lit[__num_base::_S_odigits];
740             }
741         }
742       int __ret = __bufend - __buf - 1;
743       return __ret;
744     }
746   template<typename _CharT, typename _OutIter>
747     void
748     num_put<_CharT, _OutIter>::
749     _M_group_int(const string& __grouping, _CharT __sep, ios_base& __io, 
750                  _CharT* __new, _CharT* __cs, int& __len) const
751     {
752       // By itself __add_grouping cannot deal correctly with __ws when
753       // ios::showbase is set and ios_base::oct || ios_base::hex.
754       // Therefore we take care "by hand" of the initial 0, 0x or 0X.
755       // However, remember that the latter do not occur if the number
756       // printed is '0' (__len == 1).
757       streamsize __off = 0;
758       const ios_base::fmtflags __basefield = __io.flags() 
759                                              & ios_base::basefield;
760       if ((__io.flags() & ios_base::showbase) && __len > 1)
761         if (__basefield == ios_base::oct)
762           {
763             __off = 1;
764             *__new = *__cs;
765           }
766         else if (__basefield == ios_base::hex)
767           {
768             __off = 2;
769             *__new = *__cs;
770             *(__new + 1) = *(__cs + 1);
771           }
772       _CharT* __p;
773       __p = __add_grouping(__new + __off, __sep, 
774                            __grouping.c_str(),
775                            __grouping.c_str() + __grouping.size(),
776                            __cs + __off, __cs + __len);
777       __len = __p - __new;
778     }
780   template<typename _CharT, typename _OutIter>
781     template<typename _ValueT>
782       _OutIter
783       num_put<_CharT, _OutIter>::
784       _M_convert_int(_OutIter __s, ios_base& __io, _CharT __fill, 
785                      _ValueT __v) const
786       {
787         typedef typename numpunct<_CharT>::__cache_type  __cache_type;
788         const locale& __loc = __io._M_getloc();
789         const __cache_type& __lc = __use_cache<__cache_type>(__loc);
790         const _CharT* __lit = __lc._M_atoms_out;
792         // Long enough to hold hex, dec, and octal representations.
793         int __ilen = 4 * sizeof(_ValueT);
794         _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
795                                                              * __ilen));
796         // [22.2.2.2.2] Stage 1, numeric conversion to character.
797         // Result is returned right-justified in the buffer.
798         int __len;
799         __len = __int_to_char(&__cs[0], __ilen, __v, __lit, __io.flags());
800         __cs = __cs + __ilen - __len;
801         
802         // Add grouping, if necessary. 
803         _CharT* __cs2;
804         if (__lc._M_use_grouping)
805           {
806             // Grouping can add (almost) as many separators as the
807             // number of digits, but no more.
808             __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
809                                                           * __len * 2));
810             _M_group_int(__lc._M_grouping, __lc._M_thousands_sep, __io, 
811                          __cs2, __cs, __len);
812             __cs = __cs2;
813           }
814         
815         // Pad.
816         _CharT* __cs3;
817         streamsize __w = __io.width();
818         if (__w > static_cast<streamsize>(__len))
819           {
820             __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
821                                                           * __w));
822             _M_pad(__fill, __w, __io, __cs3, __cs, __len);
823             __cs = __cs3;
824           }
825         __io.width(0);
827         // [22.2.2.2.2] Stage 4.
828         // Write resulting, fully-formatted string to output iterator.
829         return __write(__s, __cs, __len);
830       } 
832   template<typename _CharT, typename _OutIter>
833     void
834     num_put<_CharT, _OutIter>::
835     _M_group_float(const string& __grouping, _CharT __sep, const _CharT* __p, 
836                    _CharT* __new, _CharT* __cs, int& __len) const
837     {
838 #ifdef _GLIBCXX_RESOLVE_LIB_DEFECTS
839       //282. What types does numpunct grouping refer to?
840       // Add grouping, if necessary. 
841       _CharT* __p2;
842       int __declen = __p ? __p - __cs : __len;
843       __p2 = __add_grouping(__new, __sep, 
844                             __grouping.c_str(),
845                             __grouping.c_str() + __grouping.size(),
846                             __cs, __cs + __declen);
847       
848       // Tack on decimal part.
849       int __newlen = __p2 - __new;
850       if (__p)
851         {
852           char_traits<_CharT>::copy(__p2, __p, __len - __declen);
853           __newlen += __len - __declen;
854         }    
855       __len = __newlen;
856 #endif
857     }
859   // The following code uses snprintf (or sprintf(), when
860   // _GLIBCXX_USE_C99 is not defined) to convert floating point values
861   // for insertion into a stream.  An optimization would be to replace
862   // them with code that works directly on a wide buffer and then use
863   // __pad to do the padding.  It would be good to replace them anyway
864   // to gain back the efficiency that C++ provides by knowing up front
865   // the type of the values to insert.  Also, sprintf is dangerous
866   // since may lead to accidental buffer overruns.  This
867   // implementation follows the C++ standard fairly directly as
868   // outlined in 22.2.2.2 [lib.locale.num.put]
869   template<typename _CharT, typename _OutIter>
870     template<typename _ValueT>
871       _OutIter
872       num_put<_CharT, _OutIter>::
873       _M_convert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
874                        _ValueT __v) const
875       {
876         // Note: digits10 is rounded down: add 1 to ensure the maximum
877         // available precision.  Then, in general, one more 1 needs to
878         // be added since, when the %{g,G} conversion specifiers are
879         // chosen inside _S_format_float, the precision field is "the
880         // maximum number of significant digits", *not* the "number of
881         // digits to appear after the decimal point", as happens for
882         // %{e,E,f,F} (C99, 7.19.6.1,4).
883         const int __max_digits = numeric_limits<_ValueT>::digits10 + 2;
885         // Use default precision if out of range.
886         streamsize __prec = __io.precision();
887         if (__prec > static_cast<streamsize>(__max_digits))
888           __prec = static_cast<streamsize>(__max_digits);
889         else if (__prec < static_cast<streamsize>(0))
890           __prec = static_cast<streamsize>(6);
892         typedef typename numpunct<_CharT>::__cache_type  __cache_type;
893         const locale& __loc = __io._M_getloc();
894         const __cache_type& __lc = __use_cache<__cache_type>(__loc);
896         // [22.2.2.2.2] Stage 1, numeric conversion to character.
897         int __len;
898         // Long enough for the max format spec.
899         char __fbuf[16];
901 #ifdef _GLIBCXX_USE_C99
902         // First try a buffer perhaps big enough (for sure sufficient
903         // for non-ios_base::fixed outputs)
904         int __cs_size = __max_digits * 3;
905         char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
907         _S_format_float(__io, __fbuf, __mod);
908         __len = __convert_from_v(__cs, __cs_size, __fbuf, __v,
909                                  _S_c_locale, __prec);
911         // If the buffer was not large enough, try again with the correct size.
912         if (__len >= __cs_size)
913           {
914             __cs_size = __len + 1; 
915             __cs = static_cast<char*>(__builtin_alloca(__cs_size));
916             __len = __convert_from_v(__cs, __cs_size, __fbuf, __v,
917                                      _S_c_locale, __prec);
918           }
919 #else
920         // Consider the possibility of long ios_base::fixed outputs
921         const bool __fixed = __io.flags() & ios_base::fixed;
922         const int __max_exp = numeric_limits<_ValueT>::max_exponent10;
924         // ios_base::fixed outputs may need up to __max_exp+1 chars
925         // for the integer part + up to __max_digits chars for the
926         // fractional part + 3 chars for sign, decimal point, '\0'. On
927         // the other hand, for non-fixed outputs __max_digits*3 chars
928         // are largely sufficient.
929         const int __cs_size = __fixed ? __max_exp + __max_digits + 4 
930                                       : __max_digits * 3;
931         char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
933         _S_format_float(__io, __fbuf, __mod);
934         __len = __convert_from_v(__cs, 0, __fbuf, __v, _S_c_locale, __prec);
935 #endif
937       // [22.2.2.2.2] Stage 2, convert to char_type, using correct
938       // numpunct.decimal_point() values for '.' and adding grouping.
939       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
941       _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
942                                                            * __len));
943       __ctype.widen(__cs, __cs + __len, __ws);
944       
945       // Replace decimal point.
946       const _CharT __cdec = __ctype.widen('.');
947       const _CharT __dec = __lc._M_decimal_point;
948       const _CharT* __p;
949       if (__p = char_traits<_CharT>::find(__ws, __len, __cdec))
950         __ws[__p - __ws] = __dec;
952       // Add grouping, if necessary. 
953       _CharT* __ws2;
954       if (__lc._M_use_grouping)
955         {
956             // Grouping can add (almost) as many separators as the
957             // number of digits, but no more.
958             __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
959                                                           * __len * 2));
960             _M_group_float(__lc._M_grouping, __lc._M_thousands_sep, __p,
961                            __ws2, __ws, __len);
962             __ws = __ws2;
963         }
965       // Pad.
966       _CharT* __ws3;
967       streamsize __w = __io.width();
968       if (__w > static_cast<streamsize>(__len))
969         {
970           __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
971           _M_pad(__fill, __w, __io, __ws3, __ws, __len);
972           __ws = __ws3;
973         }
974       __io.width(0);
975       
976       // [22.2.2.2.2] Stage 4.
977       // Write resulting, fully-formatted string to output iterator.
978       return __write(__s, __ws, __len);
979       }
981   template<typename _CharT, typename _OutIter>
982     _OutIter
983     num_put<_CharT, _OutIter>::
984     do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
985     {
986       ios_base::fmtflags __flags = __io.flags();
987       if ((__flags & ios_base::boolalpha) == 0)
988         {
989           unsigned long __uv = __v;
990           __s = _M_convert_int(__s, __io, __fill, __uv);
991         }
992       else
993         {
994           typedef typename numpunct<_CharT>::__cache_type  __cache_type;
995           const locale& __loc = __io._M_getloc();
996           const __cache_type& __lc = __use_cache<__cache_type>(__loc);
998           typedef basic_string<_CharT>  __string_type;
999           __string_type __name;
1000           if (__v)
1001             __name = __lc._M_truename;
1002           else
1003             __name = __lc._M_falsename;
1005           const _CharT* __cs = __name.c_str();
1006           int __len = __name.size();
1007           _CharT* __cs3;
1008           streamsize __w = __io.width();
1009           if (__w > static_cast<streamsize>(__len))
1010             {
1011               __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
1012                                                             * __w));
1013               _M_pad(__fill, __w, __io, __cs3, __cs, __len);
1014               __cs = __cs3;
1015             }
1016           __io.width(0);
1017           __s = __write(__s, __cs, __len);
1018         }
1019       return __s;
1020     }
1022   template<typename _CharT, typename _OutIter>
1023     _OutIter
1024     num_put<_CharT, _OutIter>::
1025     do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
1026     { return _M_convert_int(__s, __io, __fill, __v); }
1028   template<typename _CharT, typename _OutIter>
1029     _OutIter
1030     num_put<_CharT, _OutIter>::
1031     do_put(iter_type __s, ios_base& __io, char_type __fill,
1032            unsigned long __v) const
1033     { return _M_convert_int(__s, __io, __fill, __v); }
1035 #ifdef _GLIBCXX_USE_LONG_LONG
1036   template<typename _CharT, typename _OutIter>
1037     _OutIter
1038     num_put<_CharT, _OutIter>::
1039     do_put(iter_type __s, ios_base& __b, char_type __fill, long long __v) const
1040     { return _M_convert_int(__s, __b, __fill, __v); }
1042   template<typename _CharT, typename _OutIter>
1043     _OutIter
1044     num_put<_CharT, _OutIter>::
1045     do_put(iter_type __s, ios_base& __io, char_type __fill,
1046            unsigned long long __v) const
1047     { return _M_convert_int(__s, __io, __fill, __v); }
1048 #endif
1050   template<typename _CharT, typename _OutIter>
1051     _OutIter
1052     num_put<_CharT, _OutIter>::
1053     do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1054     { return _M_convert_float(__s, __io, __fill, char(), __v); }
1056   template<typename _CharT, typename _OutIter>
1057     _OutIter
1058     num_put<_CharT, _OutIter>::
1059     do_put(iter_type __s, ios_base& __io, char_type __fill, 
1060            long double __v) const
1061     { return _M_convert_float(__s, __io, __fill, 'L', __v); }
1063   template<typename _CharT, typename _OutIter>
1064     _OutIter
1065     num_put<_CharT, _OutIter>::
1066     do_put(iter_type __s, ios_base& __io, char_type __fill,
1067            const void* __v) const
1068     {
1069       ios_base::fmtflags __flags = __io.flags();
1070       ios_base::fmtflags __fmt = ~(ios_base::showpos | ios_base::basefield
1071                                    | ios_base::uppercase | ios_base::internal);
1072       __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase));
1073       try 
1074         {
1075           __s = _M_convert_int(__s, __io, __fill, 
1076                                reinterpret_cast<unsigned long>(__v));
1077           __io.flags(__flags);
1078         }
1079       catch (...) 
1080         {
1081           __io.flags(__flags);
1082           __throw_exception_again;
1083         }
1084       return __s;
1085     }
1088   template<typename _CharT, typename _InIter>
1089     _InIter
1090     money_get<_CharT, _InIter>::
1091     do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 
1092            ios_base::iostate& __err, long double& __units) const
1093     { 
1094       string_type __str;
1095       __beg = this->do_get(__beg, __end, __intl, __io, __err, __str); 
1097       const int __n = numeric_limits<long double>::digits10;
1098       char* __cs = static_cast<char*>(__builtin_alloca(__n));
1099       const locale __loc = __io.getloc();
1100       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1101       const _CharT* __wcs = __str.c_str();
1102       __ctype.narrow(__wcs, __wcs + __str.size() + 1, char(), __cs);      
1103       __convert_to_v(__cs, __units, __err, _S_c_locale);
1104       return __beg;
1105     }
1107   template<typename _CharT, typename _InIter>
1108     _InIter
1109     money_get<_CharT, _InIter>::
1110     do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 
1111            ios_base::iostate& __err, string_type& __units) const
1112     { 
1113       // These contortions are quite unfortunate.
1114       typedef moneypunct<_CharT, true>          __money_true;
1115       typedef moneypunct<_CharT, false>         __money_false;
1116       typedef money_base::part                  part;
1117       typedef typename string_type::size_type   size_type;
1119       const locale __loc = __io.getloc();
1120       const __money_true& __mpt = use_facet<__money_true>(__loc); 
1121       const __money_false& __mpf = use_facet<__money_false>(__loc); 
1122       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1124       const money_base::pattern __p = __intl ? __mpt.neg_format() 
1125                                              : __mpf.neg_format();
1127       const string_type __pos_sign =__intl ? __mpt.positive_sign() 
1128                                            : __mpf.positive_sign();
1129       const string_type __neg_sign =__intl ? __mpt.negative_sign() 
1130                                            : __mpf.negative_sign();
1131       const char_type __d = __intl ? __mpt.decimal_point() 
1132                                    : __mpf.decimal_point();
1133       const char_type __sep = __intl ? __mpt.thousands_sep() 
1134                                      : __mpf.thousands_sep();
1136       const string __grouping = __intl ? __mpt.grouping() : __mpf.grouping();
1138       // Set to deduced positive or negative sign, depending.
1139       string_type __sign;
1140       // String of grouping info from thousands_sep plucked from __units.
1141       string __grouping_tmp; 
1142       // Marker for thousands_sep position.
1143       int __sep_pos = 0;
1144       // If input iterator is in a valid state.
1145       bool __testvalid = true;
1146       // Flag marking when a decimal point is found.
1147       bool __testdecfound = false; 
1149       // The tentative returned string is stored here.
1150       string_type __tmp_units;
1152       char_type __c = *__beg;
1153       char_type __eof = static_cast<char_type>(char_traits<char_type>::eof());
1154       for (int __i = 0; __beg != __end && __i < 4 && __testvalid; ++__i)
1155         {
1156           part __which = static_cast<part>(__p.field[__i]);
1157           switch (__which)
1158                 {
1159                 case money_base::symbol:
1160                   if (__io.flags() & ios_base::showbase 
1161                       || __i < 2 || __sign.size() > 1
1162                       || ((static_cast<part>(__p.field[3]) != money_base::none)
1163                           && __i == 2)) 
1164                     {
1165                       // According to 22.2.6.1.2.2, symbol is required
1166                       // if (__io.flags() & ios_base::showbase),
1167                       // otherwise is optional and consumed only if
1168                       // other characters are needed to complete the
1169                       // format.
1170                       const string_type __symbol = __intl ? __mpt.curr_symbol()
1171                                                          : __mpf.curr_symbol();
1172                       size_type __len = __symbol.size();
1173                       size_type __j = 0;
1174                       while (__beg != __end 
1175                              && __j < __len && __symbol[__j] == __c)
1176                         {
1177                           __c = *(++__beg);
1178                           ++__j;
1179                         }
1180                       // When (__io.flags() & ios_base::showbase)
1181                       // symbol is required.
1182                       if (__j != __len && (__io.flags() & ios_base::showbase))
1183                         __testvalid = false;
1184                     }
1185                   break;
1186                 case money_base::sign:              
1187                   // Sign might not exist, or be more than one character long. 
1188                   if (__pos_sign.size() && __neg_sign.size())
1189                   {
1190                     // Sign is mandatory.
1191                     if (__c == __pos_sign[0])
1192                       {
1193                         __sign = __pos_sign;
1194                         __c = *(++__beg);
1195                       }
1196                     else if (__c == __neg_sign[0])
1197                       {
1198                         __sign = __neg_sign;
1199                         __c = *(++__beg);
1200                       }
1201                     else
1202                       __testvalid = false;
1203                   }
1204                   else if (__pos_sign.size() && __c == __pos_sign[0])
1205                     {
1206                       __sign = __pos_sign;
1207                       __c = *(++__beg);
1208                     }
1209                   else if (__neg_sign.size() && __c == __neg_sign[0])
1210                     {
1211                       __sign = __neg_sign;
1212                       __c = *(++__beg);
1213                     }
1214                   break;
1215                 case money_base::value:
1216                   // Extract digits, remove and stash away the
1217                   // grouping of found thousands separators.
1218                   while (__beg != __end 
1219                          && (__ctype.is(ctype_base::digit, __c) 
1220                              || (__c == __d && !__testdecfound)
1221                              || __c == __sep))
1222                     {
1223                       if (__c == __d)
1224                         {
1225                           __grouping_tmp += static_cast<char>(__sep_pos);
1226                           __sep_pos = 0;
1227                           __testdecfound = true;
1228                         }
1229                       else if (__c == __sep)
1230                         {
1231                           if (__grouping.size())
1232                             {
1233                               // Mark position for later analysis.
1234                               __grouping_tmp += static_cast<char>(__sep_pos);
1235                               __sep_pos = 0;
1236                             }
1237                           else
1238                             {
1239                               __testvalid = false;
1240                               break;
1241                             }
1242                         }
1243                       else
1244                         {
1245                           __tmp_units += __c;
1246                           ++__sep_pos;
1247                         }
1248                       __c = *(++__beg);
1249                     }
1250                   break;
1251                 case money_base::space:
1252                 case money_base::none:
1253                   // Only if not at the end of the pattern.
1254                   if (__i != 3)
1255                     while (__beg != __end 
1256                            && __ctype.is(ctype_base::space, __c))
1257                       __c = *(++__beg);
1258                   break;
1259                 }
1260         }
1262       // Need to get the rest of the sign characters, if they exist.
1263       if (__sign.size() > 1)
1264         {
1265           size_type __len = __sign.size();
1266           size_type __i = 1;
1267           for (; __c != __eof && __i < __len; ++__i)
1268             while (__beg != __end && __c != __sign[__i])
1269               __c = *(++__beg);
1270           
1271           if (__i != __len)
1272             __testvalid = false;
1273         }
1275       // Strip leading zeros.
1276       while (!__tmp_units.empty() && __tmp_units[0] == __ctype.widen('0'))
1277         __tmp_units.erase(__tmp_units.begin());
1279       if (__sign.size() && __sign == __neg_sign)
1280         __tmp_units.insert(__tmp_units.begin(), __ctype.widen('-'));
1282       // Test for grouping fidelity.
1283       if (__grouping.size() && __grouping_tmp.size())
1284         {
1285           if (!__verify_grouping(__grouping, __grouping_tmp))
1286             __testvalid = false;
1287         }
1289       // Iff no more characters are available.      
1290       if (__c == __eof)
1291         __err |= ios_base::eofbit;
1293       // Iff not enough digits were supplied after the decimal-point.
1294       if (__testdecfound)
1295         {
1296           const int __frac = __intl ? __mpt.frac_digits() 
1297                                     : __mpf.frac_digits();
1298           if (__frac > 0)
1299             {
1300               if (__sep_pos != __frac)
1301                 __testvalid = false;
1302             }
1303         }
1305       // Iff valid sequence is not recognized.
1306       if (!__testvalid || !__tmp_units.size())
1307         __err |= ios_base::failbit;
1308       else
1309         // Use the "swap trick" to copy __tmp_units into __units.
1310         __tmp_units.swap(__units);
1312       return __beg; 
1313     }
1315   template<typename _CharT, typename _OutIter>
1316     _OutIter
1317     money_put<_CharT, _OutIter>::
1318     do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1319            long double __units) const
1320     { 
1321       const locale __loc = __io.getloc();
1322       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1323 #ifdef _GLIBCXX_USE_C99
1324       // First try a buffer perhaps big enough.
1325       int __cs_size = 64;
1326       char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1327       int __len = __convert_from_v(__cs, __cs_size, "%.01Lf", __units, 
1328                                    _S_c_locale);
1329       // If the buffer was not large enough, try again with the correct size.
1330       if (__len >= __cs_size)
1331         {
1332           __cs_size = __len + 1;
1333           __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1334           __len = __convert_from_v(__cs, __cs_size, "%.01Lf", __units, 
1335                                    _S_c_locale);
1336         }
1337 #else
1338       // max_exponent10 + 1 for the integer part, + 4 for sign, decimal point,
1339       // decimal digit, '\0'. 
1340       const int __cs_size = numeric_limits<long double>::max_exponent10 + 5;
1341       char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1342       int __len = __convert_from_v(__cs, 0, "%.01Lf", __units, _S_c_locale);
1343 #endif
1344       _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
1345                                                            * __cs_size));
1346       __ctype.widen(__cs, __cs + __len, __ws);
1347       string_type __digits(__ws);
1348       return this->do_put(__s, __intl, __io, __fill, __digits); 
1349     }
1351   template<typename _CharT, typename _OutIter>
1352     _OutIter
1353     money_put<_CharT, _OutIter>::
1354     do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1355            const string_type& __digits) const
1356     { 
1357       typedef typename string_type::size_type   size_type;
1358       typedef money_base::part                  part;
1360       const locale __loc = __io.getloc();
1361       const size_type __width = static_cast<size_type>(__io.width());
1363       // These contortions are quite unfortunate.
1364       typedef moneypunct<_CharT, true> __money_true;
1365       typedef moneypunct<_CharT, false> __money_false;
1366       const __money_true& __mpt = use_facet<__money_true>(__loc); 
1367       const __money_false& __mpf = use_facet<__money_false>(__loc); 
1368       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1370       // Determine if negative or positive formats are to be used, and
1371       // discard leading negative_sign if it is present.
1372       const char_type* __beg = __digits.data();
1373       const char_type* __end = __beg + __digits.size();
1374       money_base::pattern __p;
1375       string_type __sign;
1376       if (*__beg != __ctype.widen('-'))
1377         {
1378           __p = __intl ? __mpt.pos_format() : __mpf.pos_format();
1379           __sign =__intl ? __mpt.positive_sign() : __mpf.positive_sign();
1380         }
1381       else
1382         {
1383           __p = __intl ? __mpt.neg_format() : __mpf.neg_format();
1384           __sign =__intl ? __mpt.negative_sign() : __mpf.negative_sign();
1385           ++__beg;
1386         }
1387       
1388       // Look for valid numbers in the current ctype facet within input digits.
1389       __end = __ctype.scan_not(ctype_base::digit, __beg, __end);
1390       if (__beg != __end)
1391         {
1392           // Assume valid input, and attempt to format.
1393           // Break down input numbers into base components, as follows:
1394           //   final_value = grouped units + (decimal point) + (digits)
1395           string_type __res;
1396           string_type __value;
1397           const string_type __symbol = __intl ? __mpt.curr_symbol() 
1398                                               : __mpf.curr_symbol();
1400           // Deal with decimal point, decimal digits.
1401           const int __frac = __intl ? __mpt.frac_digits() 
1402                                     : __mpf.frac_digits();
1403           if (__frac > 0)
1404             {
1405               const char_type __d = __intl ? __mpt.decimal_point() 
1406                                            : __mpf.decimal_point();
1407               if (__end - __beg >= __frac)
1408                 {
1409                   __value = string_type(__end - __frac, __end);
1410                   __value.insert(__value.begin(), __d);
1411                   __end -= __frac;
1412                 }
1413               else
1414                 {
1415                   // Have to pad zeros in the decimal position.
1416                   __value = string_type(__beg, __end);
1417                   int __paddec = __frac - (__end - __beg);
1418                   char_type __zero = __ctype.widen('0');
1419                   __value.insert(__value.begin(), __paddec, __zero);
1420                   __value.insert(__value.begin(), __d);
1421                   __beg = __end;
1422                 }
1423             }
1425           // Add thousands separators to non-decimal digits, per
1426           // grouping rules.
1427           if (__beg != __end)
1428             {
1429               const string __grouping = __intl ? __mpt.grouping() 
1430                                                : __mpf.grouping();
1431               if (__grouping.size())
1432                 {
1433                   const char_type __sep = __intl ? __mpt.thousands_sep() 
1434                                                  : __mpf.thousands_sep();
1435                   const char* __gbeg = __grouping.c_str();
1436                   const char* __gend = __gbeg + __grouping.size();
1437                   const int __n = (__end - __beg) * 2;
1438                   _CharT* __ws2 =
1439                   static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __n));
1440                   _CharT* __ws_end = __add_grouping(__ws2, __sep, __gbeg, 
1441                                                     __gend, __beg, __end);
1442                   __value.insert(0, __ws2, __ws_end - __ws2);
1443                 }
1444               else
1445                 __value.insert(0, string_type(__beg, __end));
1446             }
1448           // Calculate length of resulting string.
1449           ios_base::fmtflags __f = __io.flags() & ios_base::adjustfield;
1450           size_type __len = __value.size() + __sign.size();
1451           __len += (__io.flags() & ios_base::showbase) ? __symbol.size() : 0;
1452           bool __testipad = __f == ios_base::internal && __len < __width;
1454           // Fit formatted digits into the required pattern.
1455           for (int __i = 0; __i < 4; ++__i)
1456             {
1457               part __which = static_cast<part>(__p.field[__i]);
1458               switch (__which)
1459                 {
1460                 case money_base::symbol:
1461                   if (__io.flags() & ios_base::showbase)
1462                     __res += __symbol;
1463                   break;
1464                 case money_base::sign:              
1465                   // Sign might not exist, or be more than one
1466                   // charater long. In that case, add in the rest
1467                   // below.
1468                   if (__sign.size())
1469                     __res += __sign[0];
1470                   break;
1471                 case money_base::value:
1472                   __res += __value;
1473                   break;
1474                 case money_base::space:
1475                   // At least one space is required, but if internal
1476                   // formatting is required, an arbitrary number of
1477                   // fill spaces will be necessary.
1478                   if (__testipad)
1479                     __res += string_type(__width - __len, __fill);
1480                   else
1481                     __res += __ctype.widen(__fill);
1482                   break;
1483                 case money_base::none:
1484                   if (__testipad)
1485                     __res += string_type(__width - __len, __fill);
1486                   break;
1487                 }
1488             }
1490           // Special case of multi-part sign parts.
1491           if (__sign.size() > 1)
1492             __res += string_type(__sign.begin() + 1, __sign.end());
1494           // Pad, if still necessary.
1495           __len = __res.size();
1496           if (__width > __len)
1497             {
1498               if (__f == ios_base::left)
1499                 // After.
1500                 __res.append(__width - __len, __fill);
1501               else
1502                 // Before.
1503                 __res.insert(0, string_type(__width - __len, __fill));
1504               __len = __width;
1505             }
1507           // Write resulting, fully-formatted string to output iterator.
1508           __s = __write(__s, __res.c_str(), __len);
1509         }
1510       __io.width(0);
1511       return __s; 
1512     }
1515   // NB: Not especially useful. Without an ios_base object or some
1516   // kind of locale reference, we are left clawing at the air where
1517   // the side of the mountain used to be...
1518   template<typename _CharT, typename _InIter>
1519     time_base::dateorder
1520     time_get<_CharT, _InIter>::do_date_order() const
1521     { return time_base::no_order; }
1523   template<typename _CharT, typename _InIter>
1524     void
1525     time_get<_CharT, _InIter>::
1526     _M_extract_via_format(iter_type& __beg, iter_type& __end, ios_base& __io,
1527                           ios_base::iostate& __err, tm* __tm, 
1528                           const _CharT* __format) const
1529     {  
1530       locale __loc = __io.getloc();
1531       __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1532       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1533       size_t __len = char_traits<_CharT>::length(__format);
1535       for (size_t __i = 0; __beg != __end && __i < __len && !__err; ++__i)
1536         {
1537           char __c = __format[__i];
1538           if (__c == '%')
1539             {
1540               // Verify valid formatting code, attempt to extract.
1541               __c = __format[++__i];
1542               char __mod = 0;
1543               int __mem = 0; 
1544               if (__c == 'E' || __c == 'O')
1545                 {
1546                   __mod = __c;
1547                   __c = __format[++__i];
1548                 }
1549               switch (__c)
1550                 {
1551                   const char* __cs;
1552                   _CharT __wcs[10];
1553                 case 'a':
1554                   // Abbreviated weekday name [tm_wday]
1555                   const char_type*  __days1[7];
1556                   __tp._M_days_abbreviated(__days1);
1557                   _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 7, 
1558                                   __err);
1559                   break;
1560                 case 'A':
1561                   // Weekday name [tm_wday].
1562                   const char_type*  __days2[7];
1563                   __tp._M_days(__days2);
1564                   _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 7, 
1565                                   __err);
1566                   break;
1567                 case 'h':
1568                 case 'b':
1569                   // Abbreviated month name [tm_mon]
1570                   const char_type*  __months1[12];
1571                   __tp._M_months_abbreviated(__months1);
1572                   _M_extract_name(__beg, __end, __tm->tm_mon, __months1, 12, 
1573                                   __err);
1574                   break;
1575                 case 'B':
1576                   // Month name [tm_mon].
1577                   const char_type*  __months2[12];
1578                   __tp._M_months(__months2);
1579                   _M_extract_name(__beg, __end, __tm->tm_mon, __months2, 12, 
1580                                   __err);
1581                   break;
1582                 case 'c':
1583                   // Default time and date representation.
1584                   const char_type*  __dt[2];
1585                   __tp._M_date_time_formats(__dt);
1586                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1587                                         __dt[0]);
1588                   break;
1589                 case 'd':
1590                   // Day [01, 31]. [tm_mday]
1591                   _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2, 
1592                                  __ctype, __err);
1593                   break;
1594                 case 'D':
1595                   // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
1596                   __cs = "%m/%d/%y";
1597                   __ctype.widen(__cs, __cs + 9, __wcs);
1598                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1599                                         __wcs);
1600                   break;
1601                 case 'H':
1602                   // Hour [00, 23]. [tm_hour]
1603                   _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
1604                                  __ctype, __err);
1605                   break;
1606                 case 'I':
1607                   // Hour [01, 12]. [tm_hour]
1608                   _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2, 
1609                                  __ctype, __err);
1610                   break;
1611                 case 'm':
1612                   // Month [01, 12]. [tm_mon]
1613                   _M_extract_num(__beg, __end, __mem, 1, 12, 2, __ctype, 
1614                                  __err);
1615                   if (!__err)
1616                     __tm->tm_mon = __mem - 1;
1617                   break;
1618                 case 'M':
1619                   // Minute [00, 59]. [tm_min]
1620                   _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
1621                                  __ctype, __err);
1622                   break;
1623                 case 'n':
1624                   if (__ctype.narrow(*__beg, 0) == '\n')
1625                     ++__beg;
1626                   else
1627                     __err |= ios_base::failbit;
1628                   break;
1629                 case 'R':
1630                   // Equivalent to (%H:%M).
1631                   __cs = "%H:%M";
1632                   __ctype.widen(__cs, __cs + 6, __wcs);
1633                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1634                                         __wcs);
1635                   break;
1636                 case 'S':
1637                   // Seconds.
1638                   _M_extract_num(__beg, __end, __tm->tm_sec, 0, 59, 2,
1639                                  __ctype, __err);
1640                   break;
1641                 case 't':
1642                   if (__ctype.narrow(*__beg, 0) == '\t')
1643                     ++__beg;
1644                   else
1645                 __err |= ios_base::failbit;
1646                   break;
1647                 case 'T':
1648                   // Equivalent to (%H:%M:%S).
1649                   __cs = "%H:%M:%S";
1650                   __ctype.widen(__cs, __cs + 9, __wcs);
1651                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1652                                         __wcs);
1653                   break;
1654                 case 'x':
1655                   // Locale's date.
1656                   const char_type*  __dates[2];
1657                   __tp._M_date_formats(__dates);
1658                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1659                                         __dates[0]);
1660                   break;
1661                 case 'X':
1662                   // Locale's time.
1663                   const char_type*  __times[2];
1664                   __tp._M_time_formats(__times);
1665                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1666                                         __times[0]);
1667                   break;
1668                 case 'y':
1669                   // Two digit year. [tm_year]
1670                   _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2, 
1671                                  __ctype, __err);
1672                   break;
1673                 case 'Y':
1674                   // Year [1900). [tm_year]
1675                   _M_extract_num(__beg, __end, __mem, 0, 
1676                                  numeric_limits<int>::max(), 4, 
1677                                  __ctype, __err);
1678                   if (!__err)
1679                     __tm->tm_year = __mem - 1900;
1680                   break;
1681                 case 'Z':
1682                   // Timezone info.
1683                   if (__ctype.is(ctype_base::upper, *__beg))
1684                     {
1685                       int __tmp;
1686                       _M_extract_name(__beg, __end, __tmp, 
1687                                       __timepunct<_CharT>::_S_timezones, 
1688                                       14, __err);
1689                       
1690                       // GMT requires special effort.
1691                       char_type __c = *__beg;
1692                       if (!__err && __tmp == 0 
1693                           && (__c == __ctype.widen('-') 
1694                               || __c == __ctype.widen('+')))
1695                         {
1696                           _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
1697                                           __ctype, __err);
1698                           _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
1699                                           __ctype, __err);
1700                         }           
1701                           }
1702                       else
1703                         __err |= ios_base::failbit;
1704                       break;
1705                     default:
1706                       // Not recognized.
1707                       __err |= ios_base::failbit;
1708                     }
1709                 }
1710               else
1711                 {
1712                   // Verify format and input match, extract and discard.
1713                   if (__c == __ctype.narrow(*__beg, 0))
1714                     ++__beg;
1715                   else
1716                     __err |= ios_base::failbit;
1717                 }
1718         }
1719     }
1721   template<typename _CharT, typename _InIter>
1722     void
1723     time_get<_CharT, _InIter>::
1724     _M_extract_num(iter_type& __beg, iter_type& __end, int& __member,
1725                    int __min, int __max, size_t __len, 
1726                    const ctype<_CharT>& __ctype, 
1727                    ios_base::iostate& __err) const
1728     {
1729       size_t __i = 0;
1730       string __digits;
1731       bool __testvalid = true;
1732       char_type __c = *__beg;
1733       while (__beg != __end && __i < __len 
1734              && __ctype.is(ctype_base::digit, __c)) 
1735         {
1736           __digits += __ctype.narrow(__c, 0);
1737           __c = *(++__beg);
1738           ++__i;
1739         }
1740       if (__i == __len)
1741         {
1742           int __value = atoi(__digits.c_str());
1743           if (__min <= __value && __value <= __max)
1744             __member = __value;
1745           else
1746             __testvalid = false;
1747         }
1748       else
1749         __testvalid = false;
1750       if (!__testvalid)
1751         __err |= ios_base::failbit;
1752     }
1754   // Assumptions:
1755   // All elements in __names are unique.
1756   template<typename _CharT, typename _InIter>
1757     void
1758     time_get<_CharT, _InIter>::
1759     _M_extract_name(iter_type& __beg, iter_type& __end, int& __member,
1760                     const _CharT** __names, size_t __indexlen, 
1761                     ios_base::iostate& __err) const
1762     {
1763       typedef char_traits<_CharT>               __traits_type;
1764       int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int) 
1765                                                           * __indexlen));
1766       size_t __nmatches = 0;
1767       size_t __pos = 0;
1768       bool __testvalid = true;
1769       const char_type* __name;
1771       char_type __c = *__beg;
1772       // Look for initial matches.
1773       for (size_t __i1 = 0; __i1 < __indexlen; ++__i1)
1774         if (__c == __names[__i1][0])
1775           __matches[__nmatches++] = __i1;
1776       
1777       while (__nmatches > 1)
1778         {
1779           // Find smallest matching string.
1780           size_t __minlen = 10;
1781           for (size_t __i2 = 0; __i2 < __nmatches; ++__i2)
1782             __minlen = min(__minlen, 
1783                            __traits_type::length(__names[__matches[__i2]]));
1784           
1785           if (__pos < __minlen && __beg != __end)
1786             {
1787               ++__pos;
1788               __c = *(++__beg);
1789               for (size_t __i3 = 0; __i3 < __nmatches; ++__i3)
1790                 {
1791                   __name = __names[__matches[__i3]];
1792                   if (__name[__pos] != __c)
1793                     __matches[__i3] = __matches[--__nmatches];
1794                 }
1795             }
1796           else
1797             break;
1798         }
1800       if (__nmatches == 1)
1801         {
1802           // Make sure found name is completely extracted.
1803           __name = __names[__matches[0]];
1804           const size_t __len = __traits_type::length(__name);
1805           while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
1806             ++__beg, ++__pos;
1808           if (__len == __pos)
1809             __member = __matches[0];
1810           else
1811             __testvalid = false;
1812         }
1813       else
1814         __testvalid = false;
1815       if (!__testvalid)
1816         __err |= ios_base::failbit;
1817     }
1819   template<typename _CharT, typename _InIter>
1820     _InIter
1821     time_get<_CharT, _InIter>::
1822     do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
1823                 ios_base::iostate& __err, tm* __tm) const
1824     {
1825       _CharT __wcs[3];
1826       const char* __cs = "%X";
1827       locale __loc = __io.getloc();
1828       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1829       __ctype.widen(__cs, __cs + 3, __wcs);
1830       _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1831       if (__beg == __end)
1832         __err |= ios_base::eofbit;
1833       return __beg;
1834     }
1836   template<typename _CharT, typename _InIter>
1837     _InIter
1838     time_get<_CharT, _InIter>::
1839     do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
1840                 ios_base::iostate& __err, tm* __tm) const
1841     {
1842       _CharT __wcs[3];
1843       const char* __cs = "%x";
1844       locale __loc = __io.getloc();
1845       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1846       __ctype.widen(__cs, __cs + 3, __wcs);
1847       _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1848       if (__beg == __end)
1849         __err |= ios_base::eofbit;
1850       return __beg;
1851     }
1853   template<typename _CharT, typename _InIter>
1854     _InIter
1855     time_get<_CharT, _InIter>::
1856     do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io, 
1857                    ios_base::iostate& __err, tm* __tm) const
1858     {
1859       typedef char_traits<_CharT>               __traits_type;
1860       locale __loc = __io.getloc();
1861       __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1862       const char_type*  __days[7];
1863       __tp._M_days_abbreviated(__days);
1864       int __tmpwday;
1865       _M_extract_name(__beg, __end, __tmpwday, __days, 7, __err);
1867       // Check to see if non-abbreviated name exists, and extract.
1868       // NB: Assumes both _M_days and _M_days_abbreviated organized in
1869       // exact same order, first to last, such that the resulting
1870       // __days array with the same index points to a day, and that
1871       // day's abbreviated form.
1872       // NB: Also assumes that an abbreviated name is a subset of the name. 
1873       if (!__err)
1874         {
1875           size_t __pos = __traits_type::length(__days[__tmpwday]);
1876           __tp._M_days(__days);
1877           const char_type* __name = __days[__tmpwday];
1878           if (__name[__pos] == *__beg)
1879             {
1880               // Extract the rest of it.
1881               const size_t __len = __traits_type::length(__name);
1882               while (__pos < __len && __beg != __end 
1883                      && __name[__pos] == *__beg)
1884                 ++__beg, ++__pos;
1885               if (__len != __pos)
1886                 __err |= ios_base::failbit;
1887             }
1888           if (!__err)
1889             __tm->tm_wday = __tmpwday;
1890         }
1891       if (__beg == __end)
1892         __err |= ios_base::eofbit;
1893       return __beg;
1894      }
1896   template<typename _CharT, typename _InIter>
1897     _InIter
1898     time_get<_CharT, _InIter>::
1899     do_get_monthname(iter_type __beg, iter_type __end,
1900                      ios_base& __io, ios_base::iostate& __err, tm* __tm) const
1901     {
1902       typedef char_traits<_CharT>               __traits_type;
1903       locale __loc = __io.getloc();
1904       __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1905       const char_type*  __months[12];
1906       __tp._M_months_abbreviated(__months);
1907       int __tmpmon;
1908       _M_extract_name(__beg, __end, __tmpmon, __months, 12, __err);
1910       // Check to see if non-abbreviated name exists, and extract.
1911       // NB: Assumes both _M_months and _M_months_abbreviated organized in
1912       // exact same order, first to last, such that the resulting
1913       // __months array with the same index points to a month, and that
1914       // month's abbreviated form.
1915       // NB: Also assumes that an abbreviated name is a subset of the name. 
1916       if (!__err)
1917         {
1918           size_t __pos = __traits_type::length(__months[__tmpmon]);
1919           __tp._M_months(__months);
1920           const char_type* __name = __months[__tmpmon];
1921           if (__name[__pos] == *__beg)
1922             {
1923               // Extract the rest of it.
1924               const size_t __len = __traits_type::length(__name);
1925               while (__pos < __len && __beg != __end 
1926                      && __name[__pos] == *__beg)
1927                 ++__beg, ++__pos;
1928               if (__len != __pos)
1929                 __err |= ios_base::failbit;
1930             }
1931           if (!__err)
1932             __tm->tm_mon = __tmpmon;
1933         }
1935       if (__beg == __end)
1936         __err |= ios_base::eofbit;
1937       return __beg;
1938     }
1940   template<typename _CharT, typename _InIter>
1941     _InIter
1942     time_get<_CharT, _InIter>::
1943     do_get_year(iter_type __beg, iter_type __end, ios_base& __io, 
1944                 ios_base::iostate& __err, tm* __tm) const
1945     {
1946       locale __loc = __io.getloc();
1947       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1949       char_type __c = *__beg;
1950       size_t __i = 0;
1951       string __digits;
1952       while (__i < 4 && __beg != __end && __ctype.is(ctype_base::digit, __c))
1953         {
1954           __digits += __ctype.narrow(__c, 0);
1955           __c = *(++__beg);
1956           ++__i;
1957         }
1958       if (__i == 2 || __i == 4)
1959         {
1960           long __l;
1961           __convert_to_v(__digits.c_str(), __l, __err, _S_c_locale);
1962           if (!(__err & ios_base::failbit) && __l <= INT_MAX)
1963             {
1964               __l = __i == 2 ? __l : __l - 1900; 
1965               __tm->tm_year = static_cast<int>(__l);
1966             }
1967         }
1968       else
1969         __err |= ios_base::failbit;
1970       if (__beg == __end)
1971         __err |= ios_base::eofbit;
1972       return __beg;
1973     }
1975   template<typename _CharT, typename _OutIter>
1976     _OutIter
1977     time_put<_CharT, _OutIter>::
1978     put(iter_type __s, ios_base& __io, char_type, const tm* __tm, 
1979         const _CharT* __beg, const _CharT* __end) const
1980     {
1981       locale __loc = __io.getloc();
1982       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1983       while (__beg != __end)
1984         {
1985           char __c = __ctype.narrow(*__beg, 0);
1986           ++__beg;
1987           if (__c == '%')
1988             {
1989               char __format;
1990               char __mod = 0;
1991               size_t __len = 1; 
1992               __c = __ctype.narrow(*__beg, 0);
1993               ++__beg;
1994               if (__c == 'E' || __c == 'O')
1995                 {
1996                   __mod = __c;
1997                   __format = __ctype.narrow(*__beg, 0);
1998                   ++__beg;
1999                 }
2000               else
2001                 __format = __c;
2002               __s = this->do_put(__s, __io, _CharT(), __tm, __format, __mod);
2003             }
2004           else
2005             {
2006               *__s = __c;
2007               ++__s;
2008             }
2009         }
2010       return __s;
2011     }
2013   template<typename _CharT, typename _OutIter>
2014     _OutIter
2015     time_put<_CharT, _OutIter>::
2016     do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm, 
2017            char __format, char __mod) const
2018     { 
2019       locale __loc = __io.getloc();
2020       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2021       __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
2023       // NB: This size is arbitrary. Should this be a data member,
2024       // initialized at construction?
2025       const size_t __maxlen = 64;
2026       char_type* __res = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen));
2028       // NB: In IEE 1003.1-200x, and perhaps other locale models, it
2029       // is possible that the format character will be longer than one
2030       // character. Possibilities include 'E' or 'O' followed by a
2031       // format character: if __mod is not the default argument, assume
2032       // it's a valid modifier.
2033       char_type __fmt[4];
2034       __fmt[0] = __ctype.widen('%'); 
2035       if (!__mod)
2036         {
2037           __fmt[1] = __format;
2038           __fmt[2] = char_type();
2039         }
2040       else
2041         {
2042           __fmt[1] = __mod;
2043           __fmt[2] = __format;
2044           __fmt[3] = char_type();
2045         }
2047       __tp._M_put(__res, __maxlen, __fmt, __tm);
2049       // Write resulting, fully-formatted string to output iterator.
2050       return __write(__s, __res, char_traits<char_type>::length(__res));
2051     }
2054   // Generic version does nothing.
2055   template<typename _CharT>
2056     int
2057     collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
2058     { return 0; }
2060   // Generic version does nothing.
2061   template<typename _CharT>
2062     size_t
2063     collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
2064     { return 0; }
2066   template<typename _CharT>
2067     int
2068     collate<_CharT>::
2069     do_compare(const _CharT* __lo1, const _CharT* __hi1, 
2070                const _CharT* __lo2, const _CharT* __hi2) const
2071     { 
2072       // strcoll assumes zero-terminated strings so we make a copy
2073       // and then put a zero at the end.
2074       const string_type __one(__lo1, __hi1);
2075       const string_type __two(__lo2, __hi2);
2077       const _CharT* __p = __one.c_str();
2078       const _CharT* __pend = __one.c_str() + __one.length();
2079       const _CharT* __q = __two.c_str();
2080       const _CharT* __qend = __two.c_str() + __two.length();
2082       // strcoll stops when it sees a nul character so we break
2083       // the strings into zero-terminated substrings and pass those
2084       // to strcoll.
2085       for (;;)
2086         {
2087           int __res = _M_compare(__p, __q);
2088           if (__res)
2089             return __res;
2091           __p += char_traits<_CharT>::length(__p);
2092           __q += char_traits<_CharT>::length(__q);
2093           if (__p == __pend && __q == __qend)
2094             return 0;
2095           else if (__p == __pend)
2096             return -1;
2097           else if (__q == __qend)
2098             return 1;
2100           __p++;
2101           __q++;
2102         }
2103     }
2105  template<typename _CharT>
2106     typename collate<_CharT>::string_type
2107     collate<_CharT>::
2108     do_transform(const _CharT* __lo, const _CharT* __hi) const
2109     {
2110       // strxfrm assumes zero-terminated strings so we make a copy
2111       string_type __str(__lo, __hi);
2113       const _CharT* __p = __str.c_str();
2114       const _CharT* __pend = __str.c_str() + __str.length();
2116       size_t __len = (__hi - __lo) * 2;
2118       string_type __ret;
2120       // strxfrm stops when it sees a nul character so we break
2121       // the string into zero-terminated substrings and pass those
2122       // to strxfrm.
2123       for (;;)
2124         {
2125           // First try a buffer perhaps big enough.
2126           _CharT* __c =
2127             static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len));
2128           size_t __res = _M_transform(__c, __p, __len);
2129           // If the buffer was not large enough, try again with the
2130           // correct size.
2131           if (__res >= __len)
2132             {
2133               __len = __res + 1;
2134               __c = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
2135                                                           * __len));
2136               __res = _M_transform(__c, __p, __res + 1);
2137             }
2139           __ret.append(__c, __res);
2140           __p += char_traits<_CharT>::length(__p);
2141           if (__p == __pend)
2142             return __ret;
2144           __p++;
2145           __ret.push_back(_CharT());
2146         }
2147     }
2149  template<typename _CharT>
2150     long
2151     collate<_CharT>::
2152     do_hash(const _CharT* __lo, const _CharT* __hi) const
2153     { 
2154       unsigned long __val = 0;
2155       for (; __lo < __hi; ++__lo)
2156         __val = *__lo + ((__val << 7) | 
2157                        (__val >> (numeric_limits<unsigned long>::digits - 7)));
2158       return static_cast<long>(__val);
2159     }
2161   // Construct correctly padded string, as per 22.2.2.2.2
2162   // Assumes 
2163   // __newlen > __oldlen
2164   // __news is allocated for __newlen size
2165   // Used by both num_put and ostream inserters: if __num,
2166   // internal-adjusted objects are padded according to the rules below
2167   // concerning 0[xX] and +-, otherwise, exactly as right-adjusted
2168   // ones are.
2170   // NB: Of the two parameters, _CharT can be deduced from the
2171   // function arguments. The other (_Traits) has to be explicitly specified.
2172   template<typename _CharT, typename _Traits>
2173     void 
2174     __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill, 
2175                                    _CharT* __news, const _CharT* __olds, 
2176                                    const streamsize __newlen, 
2177                                    const streamsize __oldlen, const bool __num)
2178     {
2179       size_t __plen = static_cast<size_t>(__newlen - __oldlen);
2180       _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
2181                                                              * __plen));
2182       _Traits::assign(__pads, __plen, __fill); 
2184       _CharT* __beg;
2185       _CharT* __end;
2186       size_t __mod = 0;
2187       size_t __beglen; //either __plen or __oldlen
2188       ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
2190       if (__adjust == ios_base::left)
2191         {
2192           // Padding last.
2193           __beg = const_cast<_CharT*>(__olds);
2194           __beglen = __oldlen;
2195           __end = __pads;
2196         }
2197       else if (__adjust == ios_base::internal && __num)
2198         {
2199           // Pad after the sign, if there is one.
2200           // Pad after 0[xX], if there is one.
2201           // Who came up with these rules, anyway? Jeeze.
2202           locale __loc = __io.getloc();
2203           const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
2204           const _CharT __minus = __ctype.widen('-');
2205           const _CharT __plus = __ctype.widen('+');
2206           bool __testsign = _Traits::eq(__olds[0], __minus)
2207                             || _Traits::eq(__olds[0], __plus);
2209           bool __testhex = _Traits::eq(__ctype.widen('0'), __olds[0]) 
2210                            && (_Traits::eq(__ctype.widen('x'), __olds[1]) 
2211                                || _Traits::eq(__ctype.widen('X'), __olds[1]));
2212           if (__testhex)
2213             {
2214               __news[0] = __olds[0]; 
2215               __news[1] = __olds[1];
2216               __mod += 2;
2217               __news += 2;
2218               __beg = __pads;
2219               __beglen = __plen;
2220               __end = const_cast<_CharT*>(__olds + __mod);
2221             }
2222           else if (__testsign)
2223             {
2224               _Traits::eq((__news[0] = __olds[0]), __plus) ? __plus : __minus;
2225               ++__mod;
2226               ++__news;
2227               __beg = __pads;
2228               __beglen = __plen;
2229               __end = const_cast<_CharT*>(__olds + __mod);
2230             }
2231           else
2232             {
2233               // Padding first.
2234               __beg = __pads;
2235               __beglen = __plen;
2236               __end = const_cast<_CharT*>(__olds);
2237             }
2238         }
2239       else
2240         {
2241           // Padding first.
2242           __beg = __pads;
2243           __beglen = __plen;
2244           __end = const_cast<_CharT*>(__olds);
2245         }
2246       _Traits::copy(__news, __beg, __beglen);
2247       _Traits::copy(__news + __beglen, __end, 
2248                           __newlen - __beglen - __mod);
2249     }
2251   template<typename _CharT>
2252     bool
2253     __verify_grouping(const basic_string<_CharT>& __grouping, 
2254                       basic_string<_CharT>& __grouping_tmp)
2255     {         
2256       int __i = 0;
2257       int __j = 0;
2258       const int __len = __grouping.size();
2259       const int __n = __grouping_tmp.size();
2260       bool __test = true;
2261       
2262       // Parsed number groupings have to match the
2263       // numpunct::grouping string exactly, starting at the
2264       // right-most point of the parsed sequence of elements ...
2265       while (__test && __i < __n - 1)
2266         for (__j = 0; __test && __j < __len && __i < __n - 1; ++__j,++__i)
2267           __test &= __grouping[__j] == __grouping_tmp[__n - __i - 1];
2268       // ... but the last parsed grouping can be <= numpunct
2269       // grouping.
2270       __j == __len ? __j = 0 : __j;
2271       __test &= __grouping[__j] >= __grouping_tmp[__n - __i - 1];
2272       return __test;
2273     }
2275   template<typename _CharT>
2276     _CharT*
2277     __add_grouping(_CharT* __s, _CharT __sep,  
2278                    const char* __gbeg, const char* __gend, 
2279                    const _CharT* __first, const _CharT* __last)
2280     {
2281       if (__last - __first > *__gbeg)
2282         {
2283           __s = __add_grouping(__s,  __sep, 
2284                                (__gbeg + 1 == __gend ? __gbeg : __gbeg + 1),
2285                                __gend, __first, __last - *__gbeg);
2286           __first = __last - *__gbeg;
2287           *__s++ = __sep;
2288         }
2289       do
2290         *__s++ = *__first++;
2291       while (__first != __last);
2292       return __s;
2293     }
2295   // Inhibit implicit instantiations for required instantiations,
2296   // which are defined via explicit instantiations elsewhere.  
2297   // NB: This syntax is a GNU extension.
2298 #if _GLIBCXX_EXTERN_TEMPLATE
2299   extern template class moneypunct<char, false>;
2300   extern template class moneypunct<char, true>;
2301   extern template class moneypunct_byname<char, false>;
2302   extern template class moneypunct_byname<char, true>;
2303   extern template class money_get<char>;
2304   extern template class money_put<char>;
2305   extern template class numpunct<char>;
2306   extern template class numpunct_byname<char>;
2307   extern template class num_get<char>;
2308   extern template class num_put<char>; 
2309   extern template class __timepunct<char>;
2310   extern template class time_put<char>;
2311   extern template class time_put_byname<char>;
2312   extern template class time_get<char>;
2313   extern template class time_get_byname<char>;
2314   extern template class messages<char>;
2315   extern template class messages_byname<char>;
2316   extern template class ctype_byname<char>;
2317   extern template class codecvt_byname<char, char, mbstate_t>;
2318   extern template class collate<char>;
2319   extern template class collate_byname<char>;
2321   extern template
2322     const codecvt<char, char, mbstate_t>& 
2323     use_facet<codecvt<char, char, mbstate_t> >(const locale&);
2325   extern template
2326     const collate<char>& 
2327     use_facet<collate<char> >(const locale&);
2329   extern template
2330     const numpunct<char>& 
2331     use_facet<numpunct<char> >(const locale&);
2333   extern template 
2334     const num_put<char>& 
2335     use_facet<num_put<char> >(const locale&);
2337   extern template 
2338     const num_get<char>& 
2339     use_facet<num_get<char> >(const locale&);
2341   extern template
2342     const moneypunct<char, true>& 
2343     use_facet<moneypunct<char, true> >(const locale&);
2345   extern template
2346     const moneypunct<char, false>& 
2347     use_facet<moneypunct<char, false> >(const locale&);
2349   extern template 
2350     const money_put<char>& 
2351     use_facet<money_put<char> >(const locale&);
2353   extern template 
2354     const money_get<char>& 
2355     use_facet<money_get<char> >(const locale&);
2357   extern template
2358     const __timepunct<char>& 
2359     use_facet<__timepunct<char> >(const locale&);
2361   extern template 
2362     const time_put<char>& 
2363     use_facet<time_put<char> >(const locale&);
2365   extern template 
2366     const time_get<char>& 
2367     use_facet<time_get<char> >(const locale&);
2369   extern template 
2370     const messages<char>& 
2371     use_facet<messages<char> >(const locale&);
2373   extern template 
2374     bool
2375     has_facet<ctype<char> >(const locale&);
2377   extern template 
2378     bool
2379     has_facet<codecvt<char, char, mbstate_t> >(const locale&);
2381   extern template 
2382     bool
2383     has_facet<collate<char> >(const locale&);
2385   extern template 
2386     bool
2387     has_facet<numpunct<char> >(const locale&);
2389   extern template 
2390     bool
2391     has_facet<num_put<char> >(const locale&);
2393   extern template 
2394     bool
2395     has_facet<num_get<char> >(const locale&);
2397   extern template 
2398     bool
2399     has_facet<moneypunct<char> >(const locale&);
2401   extern template 
2402     bool
2403     has_facet<money_put<char> >(const locale&);
2405   extern template 
2406     bool
2407     has_facet<money_get<char> >(const locale&);
2409   extern template 
2410     bool
2411     has_facet<__timepunct<char> >(const locale&);
2413   extern template 
2414     bool
2415     has_facet<time_put<char> >(const locale&);
2417   extern template 
2418     bool
2419     has_facet<time_get<char> >(const locale&);
2421   extern template 
2422     bool
2423     has_facet<messages<char> >(const locale&);
2425 #ifdef _GLIBCXX_USE_WCHAR_T
2426   extern template class moneypunct<wchar_t, false>;
2427   extern template class moneypunct<wchar_t, true>;
2428   extern template class moneypunct_byname<wchar_t, false>;
2429   extern template class moneypunct_byname<wchar_t, true>;
2430   extern template class money_get<wchar_t>;
2431   extern template class money_put<wchar_t>;
2432   extern template class numpunct<wchar_t>;
2433   extern template class numpunct_byname<wchar_t>;
2434   extern template class num_get<wchar_t>;
2435   extern template class num_put<wchar_t>;
2436   extern template class __timepunct<wchar_t>;
2437   extern template class time_put<wchar_t>;
2438   extern template class time_put_byname<wchar_t>;
2439   extern template class time_get<wchar_t>;
2440   extern template class time_get_byname<wchar_t>;
2441   extern template class messages<wchar_t>;
2442   extern template class messages_byname<wchar_t>;
2443   extern template class ctype_byname<wchar_t>;
2444   extern template class codecvt_byname<wchar_t, char, mbstate_t>;
2445   extern template class collate<wchar_t>;
2446   extern template class collate_byname<wchar_t>;
2448   extern template
2449     const codecvt<wchar_t, char, mbstate_t>& 
2450     use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&);
2452   extern template
2453     const collate<wchar_t>& 
2454     use_facet<collate<wchar_t> >(const locale&);
2456   extern template
2457     const numpunct<wchar_t>& 
2458     use_facet<numpunct<wchar_t> >(const locale&);
2460   extern template 
2461     const num_put<wchar_t>& 
2462     use_facet<num_put<wchar_t> >(const locale&);
2464   extern template 
2465     const num_get<wchar_t>& 
2466     use_facet<num_get<wchar_t> >(const locale&);
2468   extern template
2469     const moneypunct<wchar_t, true>& 
2470     use_facet<moneypunct<wchar_t, true> >(const locale&);
2472   extern template
2473     const moneypunct<wchar_t, false>& 
2474     use_facet<moneypunct<wchar_t, false> >(const locale&);
2476   extern template 
2477     const money_put<wchar_t>& 
2478     use_facet<money_put<wchar_t> >(const locale&);
2480   extern template 
2481     const money_get<wchar_t>& 
2482     use_facet<money_get<wchar_t> >(const locale&);
2484   extern template
2485     const __timepunct<wchar_t>& 
2486     use_facet<__timepunct<wchar_t> >(const locale&);
2488   extern template 
2489     const time_put<wchar_t>& 
2490     use_facet<time_put<wchar_t> >(const locale&);
2492   extern template 
2493     const time_get<wchar_t>& 
2494     use_facet<time_get<wchar_t> >(const locale&);
2496   extern template 
2497     const messages<wchar_t>& 
2498     use_facet<messages<wchar_t> >(const locale&);
2500  extern template 
2501     bool
2502     has_facet<ctype<wchar_t> >(const locale&);
2504   extern template 
2505     bool
2506     has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
2508   extern template 
2509     bool
2510     has_facet<collate<wchar_t> >(const locale&);
2512   extern template 
2513     bool
2514     has_facet<numpunct<wchar_t> >(const locale&);
2516   extern template 
2517     bool
2518     has_facet<num_put<wchar_t> >(const locale&);
2520   extern template 
2521     bool
2522     has_facet<num_get<wchar_t> >(const locale&);
2524   extern template 
2525     bool
2526     has_facet<moneypunct<wchar_t> >(const locale&);
2528   extern template 
2529     bool
2530     has_facet<money_put<wchar_t> >(const locale&);
2532   extern template 
2533     bool
2534     has_facet<money_get<wchar_t> >(const locale&);
2536   extern template 
2537     bool
2538     has_facet<__timepunct<wchar_t> >(const locale&);
2540   extern template 
2541     bool
2542     has_facet<time_put<wchar_t> >(const locale&);
2544   extern template 
2545     bool
2546     has_facet<time_get<wchar_t> >(const locale&);
2548   extern template 
2549     bool
2550     has_facet<messages<wchar_t> >(const locale&);
2551 #endif
2552 #endif
2553 } // namespace std
2555 #endif