OSDN Git Service

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