OSDN Git Service

3b5c484fa7b7ffe68ac5c058b80d9dfd7dc8f87a
[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       while (__beg != __end)
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           if (__p)
179             {
180               // Try first for acceptable digit; record it if found.
181               __xtrc += _S_atoms_in[__p - __lit];
182               __found_mantissa = true;
183               ++__sep_pos;
184               ++__beg;
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                   ++__beg;
196                 }
197               else
198                 {
199                   __err |= ios_base::failbit;
200                   break;
201                 }
202             }
203           else if (__traits_type::eq(__c, __lc->_M_decimal_point) 
204                    && !__found_dec)
205             {
206               // According to the standard, if no grouping chars are seen,
207               // no grouping check is applied. Therefore __found_grouping
208               // must be adjusted only if __dec comes after some __sep.
209               if (__found_grouping.size())
210                 __found_grouping += static_cast<char>(__sep_pos);
211               __xtrc += '.';
212               __found_dec = true;
213               ++__beg;
214             }
215           else if ((__e = __traits_type::eq(__c, __lit[_S_ie]) 
216                     || __traits_type::eq(__c, __lit[_S_iE])) 
217                    && __found_mantissa && !__found_sci)
218             {
219               // Scientific notation.
220               __xtrc += __e ? _S_atoms_in[_S_ie] : _S_atoms_in[_S_iE];
221               __found_sci = true;
222
223               // Remove optional plus or minus sign, if they exist.
224               if (++__beg != __end)
225                 {
226                   const bool __plus = __traits_type::eq(*__beg, __lit[_S_iplus]);
227                   if (__plus || __traits_type::eq(*__beg, __lit[_S_iminus]))
228                     {
229                       __xtrc += __plus ? _S_atoms_in[_S_iplus] 
230                                        : _S_atoms_in[_S_iminus];
231                       ++__beg;
232                     }
233                 }
234             }
235           else
236             // Not a valid input item.
237             break;
238         }
239
240       // Digit grouping is checked. If grouping and found_grouping don't
241       // match, then get very very upset, and set failbit.
242       if (__lc->_M_use_grouping && __found_grouping.size())
243         {
244           // Add the ending grouping if a decimal wasn't found.
245           if (!__found_dec)
246             __found_grouping += static_cast<char>(__sep_pos);
247
248           const string __grouping = __lc->_M_grouping;
249           if (!std::__verify_grouping(__grouping, __found_grouping))
250             __err |= ios_base::failbit;
251         }
252
253       // Finish up.
254       __xtrc += char();
255       if (__beg == __end)
256         __err |= ios_base::eofbit;
257       return __beg;
258     }
259
260   template<typename _CharT, typename _InIter>
261     _InIter
262     num_get<_CharT, _InIter>::
263     _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
264                    ios_base::iostate& __err, string& __xtrc, int& __base) const
265     {
266       typedef char_traits<_CharT>                       __traits_type;
267       typedef typename numpunct<_CharT>::__cache_type   __cache_type;
268       __use_cache<__cache_type> __uc;
269       const locale& __loc = __io._M_getloc();
270       const __cache_type* __lc = __uc(__loc);
271       const _CharT* __lit = __lc->_M_atoms_in;
272  
273       // NB: Iff __basefield == 0, this can change based on contents.
274       const ios_base::fmtflags __basefield = __io.flags() & ios_base::basefield;
275       if (__basefield == ios_base::oct)
276         __base = 8;
277       else if (__basefield == ios_base::hex)
278         __base = 16;
279       else
280         __base = 10;
281
282       // First check for sign.
283       if (__beg != __end)
284         {
285           const char_type __c = *__beg;
286           const bool __plus = __traits_type::eq(__c, __lit[_S_iplus]);
287           if (__plus || __traits_type::eq(__c, __lit[_S_iminus]))
288             {
289               __xtrc += __plus ? _S_atoms_in[_S_iplus]
290                                : _S_atoms_in[_S_iminus];
291               ++__beg;
292             }
293         }
294
295       // Next, look for leading zeros and check required digits for base formats.
296       if (__builtin_expect(__base == 10, true))
297         {
298           // Look for a zero...
299           if (__beg != __end && __traits_type::eq(*__beg, __lit[_S_izero]))
300             {
301               __xtrc += _S_atoms_in[_S_izero];
302               ++__beg;
303               // ... and skip the additional ones.
304               for (; __beg != __end
305                      && __traits_type::eq(*__beg, __lit[_S_izero]); ++__beg);
306               
307               // Check required digits.
308               if (__beg != __end && __basefield == 0)
309                 {         
310                   const bool __x = __traits_type::eq(*__beg, __lit[_S_ix]);
311                   if (__x || __traits_type::eq(*__beg, __lit[_S_iX]))
312                     {
313                       __xtrc += __x ? _S_atoms_in[_S_ix] 
314                                     : _S_atoms_in[_S_iX];
315                       __base = 16;
316                       ++__beg;                
317                     }
318                   else 
319                     __base = 8;
320                 }             
321             }
322         }
323       else if (__base == 16)
324         {
325           if (__beg != __end && __traits_type::eq(*__beg, __lit[_S_izero]))
326             {
327               __xtrc += _S_atoms_in[_S_izero];
328               
329               if (++__beg != __end)
330                 {
331                   const bool __x = __traits_type::eq(*__beg, __lit[_S_ix]);
332                   if (__x || __traits_type::eq(*__beg, __lit[_S_iX]))
333                     {
334                       __xtrc += __x ? _S_atoms_in[_S_ix] 
335                                     : _S_atoms_in[_S_iX];
336                       ++__beg;
337                     }
338                 }
339             }
340         }
341
342       // At this point, base is determined. If not hex, only allow
343       // base digits as valid input.
344       const size_t __len = __base == 16 ? _S_iend : __base;
345
346       // Extract.
347       string __found_grouping;
348       int __sep_pos = 0;
349       for (; __beg != __end; ++__beg)
350         {
351           const char_type __c = *__beg;
352           const char_type* __p = __traits_type::find(__lit + _S_izero,
353                                                      __len, __c);
354           if (__p)
355             {
356               // Try first for acceptable digit; record it if found.
357               __xtrc += _S_atoms_in[__p - __lit];
358               ++__sep_pos;
359             }
360           else if (__traits_type::eq(__c, __lc->_M_thousands_sep)
361                    && __lc->_M_use_grouping)
362             {
363               // NB: Thousands separator at the beginning of a string
364               // is a no-no, as is two consecutive thousands separators.
365               if (__sep_pos)
366                 {
367                   __found_grouping += static_cast<char>(__sep_pos);
368                   __sep_pos = 0;
369                 }
370               else
371                 {
372                   __err |= ios_base::failbit;
373                   break;
374                 }
375             }
376           else
377             // Not a valid input item.
378             break;
379         }
380
381       // Digit grouping is checked. If grouping and found_grouping don't
382       // match, then get very very upset, and set failbit.
383       if (__lc->_M_use_grouping && __found_grouping.size())
384         {
385           // Add the ending grouping.
386           __found_grouping += static_cast<char>(__sep_pos);
387
388           const string __grouping = __lc->_M_grouping;
389           if (!std::__verify_grouping(__grouping, __found_grouping))
390             __err |= ios_base::failbit;
391         }
392
393       // Finish up.
394       __xtrc += char();
395       if (__beg == __end)
396         __err |= ios_base::eofbit;
397       return __beg;
398     }
399
400   // _GLIBCXX_RESOLVE_LIB_DEFECTS
401   // 17.  Bad bool parsing
402   template<typename _CharT, typename _InIter>
403     _InIter
404     num_get<_CharT, _InIter>::
405     do_get(iter_type __beg, iter_type __end, ios_base& __io,
406            ios_base::iostate& __err, bool& __v) const
407     {
408       if (!(__io.flags() & ios_base::boolalpha))
409         {
410           // Parse bool values as unsigned long.
411           // NB: We can't just call do_get(long) here, as it might
412           // refer to a derived class.
413           string __xtrc;
414           int __base;
415           __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
416
417           unsigned long __ul; 
418           std::__convert_to_v(__xtrc.c_str(), __ul, __err, 
419                               _S_get_c_locale(), __base);
420           if (!(__err & ios_base::failbit) && __ul <= 1)
421             __v = __ul;
422           else 
423             __err |= ios_base::failbit;
424         }
425       else
426         {
427           // Parse bool values as alphanumeric.
428           typedef char_traits<_CharT>           __traits_type;
429           typedef typename numpunct<_CharT>::__cache_type       __cache_type;
430           __use_cache<__cache_type> __uc;
431           const locale& __loc = __io._M_getloc();
432           const __cache_type* __lc = __uc(__loc);
433           const size_t __tn = __traits_type::length(__lc->_M_truename) - 1;
434           const size_t __fn = __traits_type::length(__lc->_M_falsename) - 1;
435
436           bool __testf = false;
437           bool __testt = false;
438           for (size_t __n = 0; __beg != __end; ++__n)
439             {
440               const char_type __c = *__beg;
441               ++__beg;
442
443               if (__n <= __fn)
444                 __testf = __traits_type::eq(__c, __lc->_M_falsename[__n]);
445
446               if (__n <= __tn)
447                 __testt = __traits_type::eq(__c, __lc->_M_truename[__n]);
448
449               if (!(__testf || __testt))
450                 {
451                   __err |= ios_base::failbit;
452                   break;
453                 }
454               else if (__testf && __n == __fn)
455                 {
456                   __v = 0;
457                   break;
458                 }
459               else if (__testt && __n == __tn)
460                 {
461                   __v = 1;
462                   break;
463                 }
464             }
465           if (__beg == __end)
466             __err |= ios_base::eofbit;
467         }
468       return __beg;
469     }
470
471   template<typename _CharT, typename _InIter>
472     _InIter
473     num_get<_CharT, _InIter>::
474     do_get(iter_type __beg, iter_type __end, ios_base& __io,
475            ios_base::iostate& __err, long& __v) const
476     {
477       string __xtrc;
478       int __base;
479       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
480       std::__convert_to_v(__xtrc.c_str(), __v, __err,
481                           _S_get_c_locale(), __base);
482       return __beg;
483     }
484
485   template<typename _CharT, typename _InIter>
486     _InIter
487     num_get<_CharT, _InIter>::
488     do_get(iter_type __beg, iter_type __end, ios_base& __io,
489            ios_base::iostate& __err, unsigned short& __v) const
490     {
491       string __xtrc;
492       int __base;
493       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
494       unsigned long __ul;
495       std::__convert_to_v(__xtrc.c_str(), __ul, __err,
496                           _S_get_c_locale(), __base);
497       if (!(__err & ios_base::failbit) 
498           && __ul <= numeric_limits<unsigned short>::max())
499         __v = static_cast<unsigned short>(__ul);
500       else 
501         __err |= ios_base::failbit;
502       return __beg;
503     }
504
505   template<typename _CharT, typename _InIter>
506     _InIter
507     num_get<_CharT, _InIter>::
508     do_get(iter_type __beg, iter_type __end, ios_base& __io,
509            ios_base::iostate& __err, unsigned int& __v) const
510     {
511       string __xtrc;
512       int __base;
513       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
514       unsigned long __ul;
515       std::__convert_to_v(__xtrc.c_str(), __ul, __err,
516                           _S_get_c_locale(), __base);
517       if (!(__err & ios_base::failbit) 
518           && __ul <= numeric_limits<unsigned int>::max())
519         __v = static_cast<unsigned int>(__ul);
520       else 
521         __err |= ios_base::failbit;
522       return __beg;
523     }
524
525   template<typename _CharT, typename _InIter>
526     _InIter
527     num_get<_CharT, _InIter>::
528     do_get(iter_type __beg, iter_type __end, ios_base& __io,
529            ios_base::iostate& __err, unsigned long& __v) const
530     {
531       string __xtrc;
532       int __base;
533       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
534       std::__convert_to_v(__xtrc.c_str(), __v, __err,
535                           _S_get_c_locale(), __base);
536       return __beg;
537     }
538
539 #ifdef _GLIBCXX_USE_LONG_LONG
540   template<typename _CharT, typename _InIter>
541     _InIter
542     num_get<_CharT, _InIter>::
543     do_get(iter_type __beg, iter_type __end, ios_base& __io,
544            ios_base::iostate& __err, long long& __v) const
545     {
546       string __xtrc;
547       int __base;
548       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
549       std::__convert_to_v(__xtrc.c_str(), __v, __err,
550                           _S_get_c_locale(), __base);
551       return __beg;
552     }
553
554   template<typename _CharT, typename _InIter>
555     _InIter
556     num_get<_CharT, _InIter>::
557     do_get(iter_type __beg, iter_type __end, ios_base& __io,
558            ios_base::iostate& __err, unsigned long long& __v) const
559     {
560       string __xtrc;
561       int __base;
562       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
563       std::__convert_to_v(__xtrc.c_str(), __v, __err,
564                           _S_get_c_locale(), __base);
565       return __beg;
566     }
567 #endif
568
569   template<typename _CharT, typename _InIter>
570     _InIter
571     num_get<_CharT, _InIter>::
572     do_get(iter_type __beg, iter_type __end, ios_base& __io, 
573            ios_base::iostate& __err, float& __v) const
574     {
575       string __xtrc;
576       __xtrc.reserve(32);
577       __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
578       std::__convert_to_v(__xtrc.c_str(), __v, __err,
579                           _S_get_c_locale());
580       return __beg;
581     }
582
583   template<typename _CharT, typename _InIter>
584     _InIter
585     num_get<_CharT, _InIter>::
586     do_get(iter_type __beg, iter_type __end, ios_base& __io,
587            ios_base::iostate& __err, double& __v) const
588     {
589       string __xtrc;
590       __xtrc.reserve(32);
591       __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
592       std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
593       return __beg;
594     }
595
596   template<typename _CharT, typename _InIter>
597     _InIter
598     num_get<_CharT, _InIter>::
599     do_get(iter_type __beg, iter_type __end, ios_base& __io,
600            ios_base::iostate& __err, long double& __v) const
601     {
602       string __xtrc;
603       __xtrc.reserve(32);
604       __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
605       std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
606       return __beg;
607     }
608
609   template<typename _CharT, typename _InIter>
610     _InIter
611     num_get<_CharT, _InIter>::
612     do_get(iter_type __beg, iter_type __end, ios_base& __io,
613            ios_base::iostate& __err, void*& __v) const
614     {
615       // Prepare for hex formatted input.
616       typedef ios_base::fmtflags        fmtflags;
617       const fmtflags __fmt = __io.flags();
618       const fmtflags __fmtmask = ~(ios_base::showpos | ios_base::basefield
619                                    | ios_base::uppercase | ios_base::internal);
620       __io.flags(__fmt & __fmtmask | (ios_base::hex | ios_base::showbase));
621
622       string __xtrc;
623       int __base;
624       __beg = _M_extract_int(__beg, __end, __io, __err, __xtrc, __base);
625
626       // Reset from hex formatted input.
627       __io.flags(__fmt);
628
629       unsigned long __ul;
630       std::__convert_to_v(__xtrc.c_str(), __ul, __err,
631                           _S_get_c_locale(), __base);
632       if (!(__err & ios_base::failbit))
633         __v = reinterpret_cast<void*>(__ul);
634       else 
635         __err |= ios_base::failbit;
636       return __beg;
637     }
638
639   // For use by integer and floating-point types after they have been
640   // converted into a char_type string.
641   template<typename _CharT, typename _OutIter>
642     void
643     num_put<_CharT, _OutIter>::
644     _M_pad(_CharT __fill, streamsize __w, ios_base& __io, 
645            _CharT* __new, const _CharT* __cs, int& __len) const
646     {
647       // [22.2.2.2.2] Stage 3.
648       // If necessary, pad.
649       __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new, __cs, 
650                                                   __w, __len, true);
651       __len = static_cast<int>(__w);
652     }
653
654   // Forwarding functions to peel signed from unsigned integer types.
655   template<typename _CharT>
656     inline int
657     __int_to_char(_CharT* __bufend, long __v, const _CharT* __lit,
658                   ios_base::fmtflags __flags)
659     {
660       unsigned long __ul = static_cast<unsigned long>(__v);
661       bool __neg = false;
662       if (__v < 0) 
663         {
664           __ul = -__ul;
665           __neg = true;
666         }
667       return __int_to_char(__bufend, __ul, __lit, __flags, __neg); 
668     }
669
670   template<typename _CharT>
671     inline int
672     __int_to_char(_CharT* __bufend, unsigned long __v, const _CharT* __lit,
673                   ios_base::fmtflags __flags)
674     { return __int_to_char(__bufend, __v, __lit, __flags, false); }
675
676 #ifdef _GLIBCXX_USE_LONG_LONG
677   template<typename _CharT>
678     inline int
679     __int_to_char(_CharT* __bufend, long long __v, const _CharT* __lit,
680                   ios_base::fmtflags __flags)
681     { 
682       unsigned long long __ull = static_cast<unsigned long long>(__v);
683       bool __neg = false;
684       if (__v < 0) 
685         {
686           __ull = -__ull;
687           __neg = true;
688         }
689       return __int_to_char(__bufend, __ull, __lit, __flags, __neg); 
690     }
691
692   template<typename _CharT>
693     inline int
694     __int_to_char(_CharT* __bufend, unsigned long long __v, const _CharT* __lit,
695                   ios_base::fmtflags __flags)
696     { return __int_to_char(__bufend, __v, __lit, __flags, false); }
697 #endif
698       
699   template<typename _CharT, typename _ValueT>
700     int
701     __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
702                   ios_base::fmtflags __flags, bool __neg)
703     {
704       // Don't write base if already 0.
705       const bool __showbase = (__flags & ios_base::showbase) && __v;
706       const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
707       _CharT* __buf = __bufend - 1;
708
709       if (__builtin_expect(__basefield != ios_base::oct &&
710                            __basefield != ios_base::hex, true))
711         {
712           // Decimal.
713           do 
714             {
715               *__buf-- = __lit[(__v % 10) + __num_base::_S_odigits];
716               __v /= 10;
717             } 
718           while (__v != 0);
719           if (__neg)
720             *__buf-- = __lit[__num_base::_S_ominus];
721           else if (__flags & ios_base::showpos)
722             *__buf-- = __lit[__num_base::_S_oplus];
723         }
724       else if (__basefield == ios_base::oct)
725         {
726           // Octal.
727           do 
728             {
729               *__buf-- = __lit[(__v & 0x7) + __num_base::_S_odigits];
730               __v >>= 3;
731             } 
732           while (__v != 0);
733           if (__showbase)
734             *__buf-- = __lit[__num_base::_S_odigits];
735         }
736       else
737         {
738           // Hex.
739           const bool __uppercase = __flags & ios_base::uppercase;
740           const int __case_offset = __uppercase ? __num_base::_S_oudigits 
741                                                 : __num_base::_S_odigits;
742           do 
743             {
744               *__buf-- = __lit[(__v & 0xf) + __case_offset];
745               __v >>= 4;
746             } 
747           while (__v != 0);
748           if (__showbase)
749             {
750               // 'x' or 'X'
751               *__buf-- = __lit[__num_base::_S_ox + __uppercase];
752               // '0'
753               *__buf-- = __lit[__num_base::_S_odigits];
754             }
755         }
756       return __bufend - __buf - 1;
757     }
758
759   template<typename _CharT, typename _OutIter>
760     void
761     num_put<_CharT, _OutIter>::
762     _M_group_int(const string& __grouping, _CharT __sep, ios_base& __io, 
763                  _CharT* __new, _CharT* __cs, int& __len) const
764     {
765       // By itself __add_grouping cannot deal correctly with __cs when
766       // ios::showbase is set and ios_base::oct || ios_base::hex.
767       // Therefore we take care "by hand" of the initial 0, 0x or 0X.
768       // However, remember that the latter do not occur if the number
769       // printed is '0' (__len == 1).
770       streamsize __off = 0;
771       const ios_base::fmtflags __basefield = __io.flags() 
772                                              & ios_base::basefield;
773       if ((__io.flags() & ios_base::showbase) && __len > 1)
774         if (__basefield == ios_base::oct)
775           {
776             __off = 1;
777             __new[0] = __cs[0];
778           }
779         else if (__basefield == ios_base::hex)
780           {
781             __off = 2;
782             __new[0] = __cs[0];
783             __new[1] = __cs[1];
784           }
785       _CharT* __p;
786       __p = std::__add_grouping(__new + __off, __sep, __grouping.data(), 
787                                 __grouping.data() + __grouping.size(),
788                                 __cs + __off, __cs + __len);
789       __len = __p - __new;
790     }
791
792   template<typename _CharT, typename _OutIter>
793     template<typename _ValueT>
794       _OutIter
795       num_put<_CharT, _OutIter>::
796       _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill, 
797                      _ValueT __v) const
798       {
799         typedef typename numpunct<_CharT>::__cache_type __cache_type;
800         __use_cache<__cache_type> __uc;
801         const locale& __loc = __io._M_getloc();
802         const __cache_type* __lc = __uc(__loc);
803         const _CharT* __lit = __lc->_M_atoms_out;
804
805         // Long enough to hold hex, dec, and octal representations.
806         const int __ilen = 4 * sizeof(_ValueT);
807         _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
808                                                              * __ilen));
809
810         // [22.2.2.2.2] Stage 1, numeric conversion to character.
811         // Result is returned right-justified in the buffer.
812         int __len;
813         __len = __int_to_char(__cs + __ilen, __v, __lit, __io.flags());
814         __cs += __ilen - __len;
815         
816         // Add grouping, if necessary. 
817         if (__lc->_M_use_grouping)
818           {
819             // Grouping can add (almost) as many separators as the
820             // number of digits, but no more.
821             _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
822                                                                   * __len * 2));
823             _M_group_int(__lc->_M_grouping, __lc->_M_thousands_sep, __io, 
824                          __cs2, __cs, __len);
825             __cs = __cs2;
826           }
827         
828         // Pad.
829         const streamsize __w = __io.width();
830         if (__w > static_cast<streamsize>(__len))
831           {
832             _CharT* __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       if (__lc->_M_use_grouping)
967         {
968           // Grouping can add (almost) as many separators as the
969           // number of digits, but no more.
970           _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
971                                                                 * __len * 2));
972           _M_group_float(__lc->_M_grouping, __lc->_M_thousands_sep, __p,
973                          __ws2, __ws, __len);
974           __ws = __ws2;
975         }
976
977       // Pad.
978       const streamsize __w = __io.width();
979       if (__w > static_cast<streamsize>(__len))
980         {
981           _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
982                                                                 * __w));
983           _M_pad(__fill, __w, __io, __ws3, __ws, __len);
984           __ws = __ws3;
985         }
986       __io.width(0);
987       
988       // [22.2.2.2.2] Stage 4.
989       // Write resulting, fully-formatted string to output iterator.
990       return std::__write(__s, __ws, __len);
991       }
992
993   template<typename _CharT, typename _OutIter>
994     _OutIter
995     num_put<_CharT, _OutIter>::
996     do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
997     {
998       const ios_base::fmtflags __flags = __io.flags();
999       if ((__flags & ios_base::boolalpha) == 0)
1000         {
1001           unsigned long __uv = __v;
1002           __s = _M_insert_int(__s, __io, __fill, __uv);
1003         }
1004       else
1005         {
1006           typedef typename numpunct<_CharT>::__cache_type __cache_type;
1007           __use_cache<__cache_type> __uc;
1008           const locale& __loc = __io._M_getloc();
1009           const __cache_type* __lc = __uc(__loc);
1010
1011           const _CharT* __name;
1012           __name = __v ? __lc->_M_truename : __lc->_M_falsename;
1013           int __len = char_traits<_CharT>::length(__name);
1014
1015           _CharT* __cs;
1016           const streamsize __w = __io.width();
1017           if (__w > static_cast<streamsize>(__len))
1018             {
1019               __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
1020                                                             * __w));
1021               _M_pad(__fill, __w, __io, __cs, __name, __len);
1022               __name = __cs;
1023             }
1024           __io.width(0);
1025           __s = std::__write(__s, __name, __len);
1026         }
1027       return __s;
1028     }
1029
1030   template<typename _CharT, typename _OutIter>
1031     _OutIter
1032     num_put<_CharT, _OutIter>::
1033     do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
1034     { return _M_insert_int(__s, __io, __fill, __v); }
1035
1036   template<typename _CharT, typename _OutIter>
1037     _OutIter
1038     num_put<_CharT, _OutIter>::
1039     do_put(iter_type __s, ios_base& __io, char_type __fill,
1040            unsigned long __v) const
1041     { return _M_insert_int(__s, __io, __fill, __v); }
1042
1043 #ifdef _GLIBCXX_USE_LONG_LONG
1044   template<typename _CharT, typename _OutIter>
1045     _OutIter
1046     num_put<_CharT, _OutIter>::
1047     do_put(iter_type __s, ios_base& __b, char_type __fill, long long __v) const
1048     { return _M_insert_int(__s, __b, __fill, __v); }
1049
1050   template<typename _CharT, typename _OutIter>
1051     _OutIter
1052     num_put<_CharT, _OutIter>::
1053     do_put(iter_type __s, ios_base& __io, char_type __fill,
1054            unsigned long long __v) const
1055     { return _M_insert_int(__s, __io, __fill, __v); }
1056 #endif
1057
1058   template<typename _CharT, typename _OutIter>
1059     _OutIter
1060     num_put<_CharT, _OutIter>::
1061     do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1062     { return _M_insert_float(__s, __io, __fill, char(), __v); }
1063
1064   template<typename _CharT, typename _OutIter>
1065     _OutIter
1066     num_put<_CharT, _OutIter>::
1067     do_put(iter_type __s, ios_base& __io, char_type __fill, 
1068            long double __v) const
1069     { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1070
1071   template<typename _CharT, typename _OutIter>
1072     _OutIter
1073     num_put<_CharT, _OutIter>::
1074     do_put(iter_type __s, ios_base& __io, char_type __fill,
1075            const void* __v) const
1076     {
1077       const ios_base::fmtflags __flags = __io.flags();
1078       const ios_base::fmtflags __fmt = ~(ios_base::showpos | ios_base::basefield
1079                                          | ios_base::uppercase | ios_base::internal);
1080       __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase));
1081       try 
1082         {
1083           __s = _M_insert_int(__s, __io, __fill, 
1084                               reinterpret_cast<unsigned long>(__v));
1085           __io.flags(__flags);
1086         }
1087       catch (...) 
1088         {
1089           __io.flags(__flags);
1090           __throw_exception_again;
1091         }
1092       return __s;
1093     }
1094
1095
1096   template<typename _CharT, typename _InIter>
1097     _InIter
1098     money_get<_CharT, _InIter>::
1099     do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 
1100            ios_base::iostate& __err, long double& __units) const
1101     { 
1102       string_type __str;
1103       __beg = this->do_get(__beg, __end, __intl, __io, __err, __str); 
1104
1105       const int __cs_size = __str.size() + 1;
1106       char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1107       const locale __loc = __io.getloc();
1108       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1109       const _CharT* __wcs = __str.c_str();
1110       __ctype.narrow(__wcs, __wcs + __cs_size, char(), __cs);      
1111       std::__convert_to_v(__cs, __units, __err, _S_get_c_locale());
1112       return __beg;
1113     }
1114
1115   template<typename _CharT, typename _InIter>
1116     _InIter
1117     money_get<_CharT, _InIter>::
1118     do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 
1119            ios_base::iostate& __err, string_type& __units) const
1120     { 
1121       // These contortions are quite unfortunate.
1122       typedef moneypunct<_CharT, true>          __money_true;
1123       typedef moneypunct<_CharT, false>         __money_false;
1124       typedef money_base::part                  part;
1125       typedef typename string_type::size_type   size_type;
1126
1127       const locale __loc = __io.getloc();
1128       const __money_true& __mpt = use_facet<__money_true>(__loc); 
1129       const __money_false& __mpf = use_facet<__money_false>(__loc); 
1130       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1131
1132       const money_base::pattern __p = __intl ? __mpt.neg_format() 
1133                                              : __mpf.neg_format();
1134
1135       const string_type __pos_sign = __intl ? __mpt.positive_sign() 
1136                                             : __mpf.positive_sign();
1137       const string_type __neg_sign = __intl ? __mpt.negative_sign() 
1138                                             : __mpf.negative_sign();
1139       const char_type __d = __intl ? __mpt.decimal_point() 
1140                                    : __mpf.decimal_point();
1141       const char_type __sep = __intl ? __mpt.thousands_sep() 
1142                                      : __mpf.thousands_sep();
1143
1144       const string __grouping = __intl ? __mpt.grouping() : __mpf.grouping();
1145
1146       // Set to deduced positive or negative sign, depending.
1147       string_type __sign;
1148       // String of grouping info from thousands_sep plucked from __units.
1149       string __grouping_tmp; 
1150       // Marker for thousands_sep position.
1151       int __sep_pos = 0;
1152       // If input iterator is in a valid state.
1153       bool __testvalid = true;
1154       // Flag marking when a decimal point is found.
1155       bool __testdecfound = false; 
1156
1157       // The tentative returned string is stored here.
1158       string_type __tmp_units;
1159
1160       for (int __i = 0; __beg != __end && __i < 4 && __testvalid; ++__i)
1161         {
1162           char_type __c;
1163           const part __which = static_cast<part>(__p.field[__i]);
1164           switch (__which)
1165             {
1166             case money_base::symbol:
1167               if (__io.flags() & ios_base::showbase 
1168                   || __i < 2 || __sign.size() > 1
1169                   || ((static_cast<part>(__p.field[3]) != money_base::none)
1170                       && __i == 2)) 
1171                 {
1172                   // According to 22.2.6.1.2.2, symbol is required
1173                   // if (__io.flags() & ios_base::showbase),
1174                   // otherwise is optional and consumed only if
1175                   // other characters are needed to complete the
1176                   // format.
1177                   const string_type __symbol = __intl ? __mpt.curr_symbol()
1178                                                       : __mpf.curr_symbol();
1179                   const size_type __len = __symbol.size();
1180                   size_type __j = 0;
1181                   for (; __beg != __end && __j < __len
1182                          && *__beg == __symbol[__j]; ++__beg, ++__j);
1183                   // When (__io.flags() & ios_base::showbase)
1184                   // symbol is required.
1185                   if (__j != __len && (__io.flags() & ios_base::showbase))
1186                     __testvalid = false;
1187                 }
1188               break;
1189             case money_base::sign:                  
1190               // Sign might not exist, or be more than one character long.
1191               if (__pos_sign.size() && *__beg == __pos_sign[0])
1192                 {
1193                   __sign = __pos_sign;
1194                   ++__beg;
1195                 }
1196               else if (__neg_sign.size() && *__beg == __neg_sign[0])
1197                 {
1198                   __sign = __neg_sign;
1199                   ++__beg;
1200                 }
1201               else if (__pos_sign.size() && __neg_sign.size())
1202                 {
1203                   // Sign is mandatory.
1204                   __testvalid = false;
1205                 }
1206               break;
1207             case money_base::value:
1208               // Extract digits, remove and stash away the
1209               // grouping of found thousands separators.
1210               for (; __beg != __end; ++__beg)
1211                 if (__ctype.is(ctype_base::digit, __c = *__beg))
1212                   {
1213                     __tmp_units += __c;
1214                     ++__sep_pos;
1215                   }
1216                 else if (__c == __d && !__testdecfound)
1217                   {
1218                     __grouping_tmp += static_cast<char>(__sep_pos);
1219                     __sep_pos = 0;
1220                     __testdecfound = true;
1221                   }
1222                 else if (__c == __sep)
1223                   {
1224                     if (__grouping.size())
1225                       {
1226                         // Mark position for later analysis.
1227                         __grouping_tmp += static_cast<char>(__sep_pos);
1228                         __sep_pos = 0;
1229                       }
1230                     else
1231                       {
1232                         __testvalid = false;
1233                         break;
1234                       }
1235                   }
1236                 else
1237                   break;
1238               break;
1239             case money_base::space:
1240             case money_base::none:
1241               // Only if not at the end of the pattern.
1242               if (__i != 3)
1243                 for (; __beg != __end 
1244                        && __ctype.is(ctype_base::space, *__beg); ++__beg);
1245               break;
1246             }
1247         }
1248       
1249       // Need to get the rest of the sign characters, if they exist.
1250       if (__sign.size() > 1)
1251         {
1252           const size_type __len = __sign.size();
1253           size_type __i = 1;
1254           for (; __beg != __end && __i < __len; ++__i)
1255             for (; __beg != __end
1256                    && *__beg != __sign[__i]; ++__beg);
1257           
1258           if (__i != __len)
1259             __testvalid = false;
1260         }
1261
1262       if (__testvalid && __tmp_units.size())
1263         {
1264           const char_type __zero = __ctype.widen('0');
1265
1266           // Strip leading zeros.
1267           if (__tmp_units.size() > 1)
1268             {
1269               const size_type __first = __tmp_units.find_first_not_of(__zero);
1270               const bool __only_zeros = __first == string_type::npos;
1271               if (__first)
1272                 __tmp_units.erase(0, __only_zeros ? __tmp_units.size() - 1
1273                                                   : __first);
1274             }
1275           
1276           // 22.2.6.1.2, p4
1277           if (__sign.size() && __sign == __neg_sign
1278               && __tmp_units[0] != __zero)
1279             __tmp_units.insert(__tmp_units.begin(), __ctype.widen('-'));
1280
1281           // Test for grouping fidelity.
1282           if (__grouping.size() && __grouping_tmp.size())
1283             {
1284               if (!std::__verify_grouping(__grouping, __grouping_tmp))
1285                 __testvalid = false;
1286             }
1287           
1288           // Iff not enough digits were supplied after the decimal-point.
1289           if (__testdecfound)
1290             {
1291               const int __frac = __intl ? __mpt.frac_digits() 
1292                                         : __mpf.frac_digits();
1293               if (__frac > 0 && __sep_pos != __frac)
1294                 __testvalid = false;
1295             }
1296         }
1297       else
1298         __testvalid = false;
1299
1300       // Iff no more characters are available.      
1301       if (__beg == __end)
1302         __err |= ios_base::eofbit;
1303
1304       // Iff valid sequence is not recognized.
1305       if (!__testvalid)
1306         __err |= ios_base::failbit;
1307       else
1308         // Use the "swap trick" to copy __tmp_units into __units.
1309         __tmp_units.swap(__units);
1310
1311       return __beg; 
1312     }
1313
1314   template<typename _CharT, typename _OutIter>
1315     _OutIter
1316     money_put<_CharT, _OutIter>::
1317     do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1318            long double __units) const
1319     { 
1320       const locale __loc = __io.getloc();
1321       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1322 #ifdef _GLIBCXX_USE_C99
1323       // First try a buffer perhaps big enough.
1324       int __cs_size = 64;
1325       char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1326       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1327       // 328. Bad sprintf format modifier in money_put<>::do_put()
1328       int __len = std::__convert_from_v(__cs, __cs_size, "%.0Lf", __units, 
1329                                         _S_get_c_locale());
1330       // If the buffer was not large enough, try again with the correct size.
1331       if (__len >= __cs_size)
1332         {
1333           __cs_size = __len + 1;
1334           __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1335           __len = std::__convert_from_v(__cs, __cs_size, "%.0Lf", __units, 
1336                                         _S_get_c_locale());
1337         }
1338 #else
1339       // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'. 
1340       const int __cs_size = numeric_limits<long double>::max_exponent10 + 3;
1341       char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1342       int __len = std::__convert_from_v(__cs, 0, "%.0Lf", __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       const __timepunct<_CharT>& __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               int __mem = 0; 
1544               if (__c == 'E' || __c == 'O')
1545                 __c = __format[++__i];
1546               switch (__c)
1547                 {
1548                   const char* __cs;
1549                   _CharT __wcs[10];
1550                 case 'a':
1551                   // Abbreviated weekday name [tm_wday]
1552                   const char_type*  __days1[7];
1553                   __tp._M_days_abbreviated(__days1);
1554                   _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 7, 
1555                                   __ctype, __err);
1556                   break;
1557                 case 'A':
1558                   // Weekday name [tm_wday].
1559                   const char_type*  __days2[7];
1560                   __tp._M_days(__days2);
1561                   _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 7, 
1562                                   __ctype, __err);
1563                   break;
1564                 case 'h':
1565                 case 'b':
1566                   // Abbreviated month name [tm_mon]
1567                   const char_type*  __months1[12];
1568                   __tp._M_months_abbreviated(__months1);
1569                   _M_extract_name(__beg, __end, __tm->tm_mon, __months1, 12, 
1570                                   __ctype, __err);
1571                   break;
1572                 case 'B':
1573                   // Month name [tm_mon].
1574                   const char_type*  __months2[12];
1575                   __tp._M_months(__months2);
1576                   _M_extract_name(__beg, __end, __tm->tm_mon, __months2, 12, 
1577                                   __ctype, __err);
1578                   break;
1579                 case 'c':
1580                   // Default time and date representation.
1581                   const char_type*  __dt[2];
1582                   __tp._M_date_time_formats(__dt);
1583                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1584                                         __dt[0]);
1585                   break;
1586                 case 'd':
1587                   // Day [01, 31]. [tm_mday]
1588                   _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2, 
1589                                  __ctype, __err);
1590                   break;
1591                 case 'e':
1592                   // Day [1, 31], with single digits preceded by
1593                   // space. [tm_mday]
1594                   if (__ctype.is(ctype_base::space, *__beg))
1595                     _M_extract_num(++__beg, __end, __tm->tm_mday, 1, 9, 1,
1596                                    __ctype, __err);
1597                   else
1598                     _M_extract_num(__beg, __end, __tm->tm_mday, 10, 31, 2,
1599                                    __ctype, __err);                 
1600                   break;                    
1601                 case 'D':
1602                   // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
1603                   __cs = "%m/%d/%y";
1604                   __ctype.widen(__cs, __cs + 9, __wcs);
1605                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1606                                         __wcs);
1607                   break;
1608                 case 'H':
1609                   // Hour [00, 23]. [tm_hour]
1610                   _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
1611                                  __ctype, __err);
1612                   break;
1613                 case 'I':
1614                   // Hour [01, 12]. [tm_hour]
1615                   _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2, 
1616                                  __ctype, __err);
1617                   break;
1618                 case 'm':
1619                   // Month [01, 12]. [tm_mon]
1620                   _M_extract_num(__beg, __end, __mem, 1, 12, 2, __ctype, 
1621                                  __err);
1622                   if (!__err)
1623                     __tm->tm_mon = __mem - 1;
1624                   break;
1625                 case 'M':
1626                   // Minute [00, 59]. [tm_min]
1627                   _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
1628                                  __ctype, __err);
1629                   break;
1630                 case 'n':
1631                   if (__ctype.narrow(*__beg, 0) == '\n')
1632                     ++__beg;
1633                   else
1634                     __err |= ios_base::failbit;
1635                   break;
1636                 case 'R':
1637                   // Equivalent to (%H:%M).
1638                   __cs = "%H:%M";
1639                   __ctype.widen(__cs, __cs + 6, __wcs);
1640                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1641                                         __wcs);
1642                   break;
1643                 case 'S':
1644                   // Seconds.
1645                   _M_extract_num(__beg, __end, __tm->tm_sec, 0, 59, 2,
1646                                  __ctype, __err);
1647                   break;
1648                 case 't':
1649                   if (__ctype.narrow(*__beg, 0) == '\t')
1650                     ++__beg;
1651                   else
1652                     __err |= ios_base::failbit;
1653                   break;
1654                 case 'T':
1655                   // Equivalent to (%H:%M:%S).
1656                   __cs = "%H:%M:%S";
1657                   __ctype.widen(__cs, __cs + 9, __wcs);
1658                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1659                                         __wcs);
1660                   break;
1661                 case 'x':
1662                   // Locale's date.
1663                   const char_type*  __dates[2];
1664                   __tp._M_date_formats(__dates);
1665                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1666                                         __dates[0]);
1667                   break;
1668                 case 'X':
1669                   // Locale's time.
1670                   const char_type*  __times[2];
1671                   __tp._M_time_formats(__times);
1672                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1673                                         __times[0]);
1674                   break;
1675                 case 'y':
1676                 case 'C': // C99
1677                   // Two digit year. [tm_year]
1678                   _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2, 
1679                                  __ctype, __err);
1680                   break;
1681                 case 'Y':
1682                   // Year [1900). [tm_year]
1683                   _M_extract_num(__beg, __end, __mem, 0, 
1684                                  numeric_limits<int>::max(), 4, 
1685                                  __ctype, __err);
1686                   if (!__err)
1687                     __tm->tm_year = __mem - 1900;
1688                   break;
1689                 case 'Z':
1690                   // Timezone info.
1691                   if (__ctype.is(ctype_base::upper, *__beg))
1692                     {
1693                       int __tmp;
1694                       _M_extract_name(__beg, __end, __tmp, 
1695                                       __timepunct_cache<_CharT>::_S_timezones, 
1696                                       14, __ctype, __err);
1697                       
1698                       // GMT requires special effort.
1699                       if (__beg != __end && !__err && __tmp == 0
1700                           && (*__beg == __ctype.widen('-') 
1701                               || *__beg == __ctype.widen('+')))
1702                         {
1703                           _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
1704                                           __ctype, __err);
1705                           _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
1706                                           __ctype, __err);
1707                         }           
1708                     }
1709                   else
1710                     __err |= ios_base::failbit;
1711                   break;
1712                 default:
1713                   // Not recognized.
1714                   __err |= ios_base::failbit;
1715                 }
1716             }
1717           else
1718             {
1719               // Verify format and input match, extract and discard.
1720               if (__c == __ctype.narrow(*__beg, 0))
1721                 ++__beg;
1722               else
1723                 __err |= ios_base::failbit;
1724             }
1725         }
1726     }
1727
1728   template<typename _CharT, typename _InIter>
1729     void
1730     time_get<_CharT, _InIter>::
1731     _M_extract_num(iter_type& __beg, iter_type& __end, int& __member,
1732                    int __min, int __max, size_t __len, 
1733                    const ctype<_CharT>& __ctype, 
1734                    ios_base::iostate& __err) const
1735     {
1736       size_t __i = 0;
1737       string __digits;
1738       bool __testvalid = true;
1739       for (; __beg != __end && __i < __len 
1740              && __ctype.is(ctype_base::digit, *__beg); ++__beg, ++__i) 
1741         __digits += __ctype.narrow(*__beg, 0);
1742       if (__i == __len)
1743         {
1744           const int __value = std::atoi(__digits.c_str());
1745           if (__min <= __value && __value <= __max)
1746             __member = __value;
1747           else
1748             __testvalid = false;
1749         }
1750       else
1751         __testvalid = false;
1752       if (!__testvalid)
1753         __err |= ios_base::failbit;
1754     }
1755
1756   // Assumptions:
1757   // All elements in __names are unique.
1758   template<typename _CharT, typename _InIter>
1759     void
1760     time_get<_CharT, _InIter>::
1761     _M_extract_name(iter_type& __beg, iter_type& __end, int& __member,
1762                     const _CharT** __names, size_t __indexlen, 
1763                     const ctype<_CharT>& __ctype, 
1764                     ios_base::iostate& __err) const
1765     {
1766       typedef char_traits<_CharT>               __traits_type;
1767       int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int) 
1768                                                           * __indexlen));
1769       size_t __nmatches = 0;
1770       size_t __pos = 0;
1771       bool __testvalid = true;
1772       const char_type* __name;
1773
1774       // Look for initial matches.  
1775       // NB: Some of the locale data is in the form of all lowercase
1776       // names, and some is in the form of initially-capitalized
1777       // names. Look for both.
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                 || __c == __ctype.toupper(__names[__i1][0]))
1784               __matches[__nmatches++] = __i1;
1785         }
1786       
1787       while (__nmatches > 1)
1788         {
1789           // Find smallest matching string.
1790           size_t __minlen = 10;
1791           for (size_t __i2 = 0; __i2 < __nmatches; ++__i2)
1792             __minlen = std::min(__minlen, 
1793                                 __traits_type::length(__names[__matches[__i2]]));
1794           ++__beg;
1795           if (__pos < __minlen && __beg != __end)
1796             {
1797               ++__pos;
1798               for (size_t __i3 = 0; __i3 < __nmatches; ++__i3)
1799                 {
1800                   __name = __names[__matches[__i3]];
1801                   if (__name[__pos] != *__beg)
1802                     __matches[__i3] = __matches[--__nmatches];
1803                 }
1804             }
1805           else
1806             break;
1807         }
1808
1809       if (__nmatches == 1)
1810         {
1811           // If there was only one match, the first compare is redundant.
1812           if (__pos == 0)
1813             {
1814               ++__pos;
1815               ++__beg;
1816             }
1817
1818           // Make sure found name is completely extracted.
1819           __name = __names[__matches[0]];
1820           const size_t __len = __traits_type::length(__name);
1821           while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
1822             ++__beg, ++__pos;
1823
1824           if (__len == __pos)
1825             __member = __matches[0];
1826           else
1827             __testvalid = false;
1828         }
1829       else
1830         __testvalid = false;
1831       if (!__testvalid)
1832         __err |= ios_base::failbit;
1833     }
1834
1835   template<typename _CharT, typename _InIter>
1836     _InIter
1837     time_get<_CharT, _InIter>::
1838     do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
1839                 ios_base::iostate& __err, tm* __tm) const
1840     {
1841       _CharT __wcs[3];
1842       const char* __cs = "%X";
1843       const locale __loc = __io.getloc();
1844       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1845       __ctype.widen(__cs, __cs + 3, __wcs);
1846       _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1847       if (__beg == __end)
1848         __err |= ios_base::eofbit;
1849       return __beg;
1850     }
1851
1852   template<typename _CharT, typename _InIter>
1853     _InIter
1854     time_get<_CharT, _InIter>::
1855     do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
1856                 ios_base::iostate& __err, tm* __tm) const
1857     {
1858       _CharT __wcs[3];
1859       const char* __cs = "%x";
1860       const locale __loc = __io.getloc();
1861       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1862       __ctype.widen(__cs, __cs + 3, __wcs);
1863       _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1864       if (__beg == __end)
1865         __err |= ios_base::eofbit;
1866       return __beg;
1867     }
1868
1869   template<typename _CharT, typename _InIter>
1870     _InIter
1871     time_get<_CharT, _InIter>::
1872     do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io, 
1873                    ios_base::iostate& __err, tm* __tm) const
1874     {
1875       typedef char_traits<_CharT>               __traits_type;
1876       const locale __loc = __io.getloc();
1877       const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
1878       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1879       const char_type*  __days[7];
1880       __tp._M_days_abbreviated(__days);
1881       int __tmpwday;
1882       _M_extract_name(__beg, __end, __tmpwday, __days, 7, __ctype, __err);
1883
1884       // Check to see if non-abbreviated name exists, and extract.
1885       // NB: Assumes both _M_days and _M_days_abbreviated organized in
1886       // exact same order, first to last, such that the resulting
1887       // __days array with the same index points to a day, and that
1888       // day's abbreviated form.
1889       // NB: Also assumes that an abbreviated name is a subset of the name. 
1890       if (!__err)
1891         {
1892           size_t __pos = __traits_type::length(__days[__tmpwday]);
1893           __tp._M_days(__days);
1894           const char_type* __name = __days[__tmpwday];
1895           if (__name[__pos] == *__beg)
1896             {
1897               // Extract the rest of it.
1898               const size_t __len = __traits_type::length(__name);
1899               while (__pos < __len && __beg != __end 
1900                      && __name[__pos] == *__beg)
1901                 ++__beg, ++__pos;
1902               if (__len != __pos)
1903                 __err |= ios_base::failbit;
1904             }
1905           if (!__err)
1906             __tm->tm_wday = __tmpwday;
1907         }
1908       if (__beg == __end)
1909         __err |= ios_base::eofbit;
1910       return __beg;
1911      }
1912
1913   template<typename _CharT, typename _InIter>
1914     _InIter
1915     time_get<_CharT, _InIter>::
1916     do_get_monthname(iter_type __beg, iter_type __end,
1917                      ios_base& __io, ios_base::iostate& __err, tm* __tm) const
1918     {
1919       typedef char_traits<_CharT>               __traits_type;
1920       const locale __loc = __io.getloc();
1921       const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
1922       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1923       const char_type*  __months[12];
1924       __tp._M_months_abbreviated(__months);
1925       int __tmpmon;
1926       _M_extract_name(__beg, __end, __tmpmon, __months, 12, __ctype, __err);
1927
1928       // Check to see if non-abbreviated name exists, and extract.
1929       // NB: Assumes both _M_months and _M_months_abbreviated organized in
1930       // exact same order, first to last, such that the resulting
1931       // __months array with the same index points to a month, and that
1932       // month's abbreviated form.
1933       // NB: Also assumes that an abbreviated name is a subset of the name. 
1934       if (!__err)
1935         {
1936           size_t __pos = __traits_type::length(__months[__tmpmon]);
1937           __tp._M_months(__months);
1938           const char_type* __name = __months[__tmpmon];
1939           if (__name[__pos] == *__beg)
1940             {
1941               // Extract the rest of it.
1942               const size_t __len = __traits_type::length(__name);
1943               while (__pos < __len && __beg != __end 
1944                      && __name[__pos] == *__beg)
1945                 ++__beg, ++__pos;
1946               if (__len != __pos)
1947                 __err |= ios_base::failbit;
1948             }
1949           if (!__err)
1950             __tm->tm_mon = __tmpmon;
1951         }
1952  
1953       if (__beg == __end)
1954         __err |= ios_base::eofbit;
1955       return __beg;
1956     }
1957
1958   template<typename _CharT, typename _InIter>
1959     _InIter
1960     time_get<_CharT, _InIter>::
1961     do_get_year(iter_type __beg, iter_type __end, ios_base& __io, 
1962                 ios_base::iostate& __err, tm* __tm) const
1963     {
1964       const locale __loc = __io.getloc();
1965       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1966
1967       size_t __i = 0;
1968       string __digits;
1969       for (; __i < 4 && __beg != __end
1970              && __ctype.is(ctype_base::digit, *__beg); ++__beg, ++__i)
1971         __digits += __ctype.narrow(*__beg, 0);
1972       if (__i == 2 || __i == 4)
1973         {
1974           long __l;
1975           std::__convert_to_v(__digits.c_str(), __l, __err,
1976                               _S_get_c_locale());
1977           if (!(__err & ios_base::failbit) && __l <= INT_MAX)
1978             {
1979               __l = __i == 2 ? __l : __l - 1900; 
1980               __tm->tm_year = static_cast<int>(__l);
1981             }
1982         }
1983       else
1984         __err |= ios_base::failbit;
1985       if (__beg == __end)
1986         __err |= ios_base::eofbit;
1987       return __beg;
1988     }
1989
1990   template<typename _CharT, typename _OutIter>
1991     _OutIter
1992     time_put<_CharT, _OutIter>::
1993     put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm, 
1994         const _CharT* __beg, const _CharT* __end) const
1995     {
1996       const locale __loc = __io.getloc();
1997       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1998       for (; __beg != __end; ++__beg)
1999         if (__ctype.narrow(*__beg, 0) != '%')
2000           {
2001             *__s = *__beg;
2002             ++__s;
2003           }
2004         else if (++__beg != __end)
2005           {
2006             char __format;
2007             char __mod = 0;
2008             const char __c = __ctype.narrow(*__beg, 0);
2009             if (__c != 'E' && __c != 'O')
2010               __format = __c;
2011             else if (++__beg != __end)
2012               {
2013                 __mod = __c;
2014                 __format = __ctype.narrow(*__beg, 0);
2015               }
2016             else
2017               break;
2018             __s = this->do_put(__s, __io, __fill, __tm,
2019                                __format, __mod);
2020           }
2021         else
2022           break;
2023       return __s;
2024     }
2025
2026   template<typename _CharT, typename _OutIter>
2027     _OutIter
2028     time_put<_CharT, _OutIter>::
2029     do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm, 
2030            char __format, char __mod) const
2031     { 
2032       const locale __loc = __io.getloc();
2033       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2034       __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
2035
2036       // NB: This size is arbitrary. Should this be a data member,
2037       // initialized at construction?
2038       const size_t __maxlen = 64;
2039       char_type* __res = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen));
2040
2041       // NB: In IEE 1003.1-200x, and perhaps other locale models, it
2042       // is possible that the format character will be longer than one
2043       // character. Possibilities include 'E' or 'O' followed by a
2044       // format character: if __mod is not the default argument, assume
2045       // it's a valid modifier.
2046       char_type __fmt[4];
2047       __fmt[0] = __ctype.widen('%'); 
2048       if (!__mod)
2049         {
2050           __fmt[1] = __format;
2051           __fmt[2] = char_type();
2052         }
2053       else
2054         {
2055           __fmt[1] = __mod;
2056           __fmt[2] = __format;
2057           __fmt[3] = char_type();
2058         }
2059
2060       __tp._M_put(__res, __maxlen, __fmt, __tm);
2061
2062       // Write resulting, fully-formatted string to output iterator.
2063       return std::__write(__s, __res, char_traits<char_type>::length(__res));
2064     }
2065
2066
2067   // Generic version does nothing.
2068   template<typename _CharT>
2069     int
2070     collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
2071     { return 0; }
2072
2073   // Generic version does nothing.
2074   template<typename _CharT>
2075     size_t
2076     collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
2077     { return 0; }
2078
2079   template<typename _CharT>
2080     int
2081     collate<_CharT>::
2082     do_compare(const _CharT* __lo1, const _CharT* __hi1, 
2083                const _CharT* __lo2, const _CharT* __hi2) const
2084     { 
2085       // strcoll assumes zero-terminated strings so we make a copy
2086       // and then put a zero at the end.
2087       const string_type __one(__lo1, __hi1);
2088       const string_type __two(__lo2, __hi2);
2089
2090       const _CharT* __p = __one.c_str();
2091       const _CharT* __pend = __one.data() + __one.length();
2092       const _CharT* __q = __two.c_str();
2093       const _CharT* __qend = __two.data() + __two.length();
2094
2095       // strcoll stops when it sees a nul character so we break
2096       // the strings into zero-terminated substrings and pass those
2097       // to strcoll.
2098       for (;;)
2099         {
2100           const int __res = _M_compare(__p, __q);
2101           if (__res)
2102             return __res;
2103
2104           __p += char_traits<_CharT>::length(__p);
2105           __q += char_traits<_CharT>::length(__q);
2106           if (__p == __pend && __q == __qend)
2107             return 0;
2108           else if (__p == __pend)
2109             return -1;
2110           else if (__q == __qend)
2111             return 1;
2112
2113           __p++;
2114           __q++;
2115         }
2116     }
2117
2118   template<typename _CharT>
2119     typename collate<_CharT>::string_type
2120     collate<_CharT>::
2121     do_transform(const _CharT* __lo, const _CharT* __hi) const
2122     {
2123       // strxfrm assumes zero-terminated strings so we make a copy
2124       string_type __str(__lo, __hi);
2125
2126       const _CharT* __p = __str.c_str();
2127       const _CharT* __pend = __str.data() + __str.length();
2128
2129       size_t __len = (__hi - __lo) * 2;
2130
2131       string_type __ret;
2132
2133       // strxfrm stops when it sees a nul character so we break
2134       // the string into zero-terminated substrings and pass those
2135       // to strxfrm.
2136       for (;;)
2137         {
2138           // First try a buffer perhaps big enough.
2139           _CharT* __c =
2140             static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len));
2141           size_t __res = _M_transform(__c, __p, __len);
2142           // If the buffer was not large enough, try again with the
2143           // correct size.
2144           if (__res >= __len)
2145             {
2146               __len = __res + 1;
2147               __c = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
2148                                                           * __len));
2149               __res = _M_transform(__c, __p, __res + 1);
2150             }
2151
2152           __ret.append(__c, __res);
2153           __p += char_traits<_CharT>::length(__p);
2154           if (__p == __pend)
2155             return __ret;
2156
2157           __p++;
2158           __ret.push_back(_CharT());
2159         }
2160     }
2161
2162   template<typename _CharT>
2163     long
2164     collate<_CharT>::
2165     do_hash(const _CharT* __lo, const _CharT* __hi) const
2166     { 
2167       unsigned long __val = 0;
2168       for (; __lo < __hi; ++__lo)
2169         __val = *__lo + ((__val << 7) | 
2170                        (__val >> (numeric_limits<unsigned long>::digits - 7)));
2171       return static_cast<long>(__val);
2172     }
2173
2174   // Construct correctly padded string, as per 22.2.2.2.2
2175   // Assumes 
2176   // __newlen > __oldlen
2177   // __news is allocated for __newlen size
2178   // Used by both num_put and ostream inserters: if __num,
2179   // internal-adjusted objects are padded according to the rules below
2180   // concerning 0[xX] and +-, otherwise, exactly as right-adjusted
2181   // ones are.
2182
2183   // NB: Of the two parameters, _CharT can be deduced from the
2184   // function arguments. The other (_Traits) has to be explicitly specified.
2185   template<typename _CharT, typename _Traits>
2186     void 
2187     __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill, 
2188                                    _CharT* __news, const _CharT* __olds, 
2189                                    const streamsize __newlen, 
2190                                    const streamsize __oldlen, const bool __num)
2191     {
2192       const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
2193       const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
2194
2195       // Padding last.
2196       if (__adjust == ios_base::left)
2197         {
2198           _Traits::copy(__news, const_cast<_CharT*>(__olds), __oldlen);
2199           _Traits::assign(__news + __oldlen, __plen, __fill);
2200           return;
2201         }
2202
2203       size_t __mod = 0;
2204       if (__adjust == ios_base::internal && __num)
2205         {
2206           // Pad after the sign, if there is one.
2207           // Pad after 0[xX], if there is one.
2208           // Who came up with these rules, anyway? Jeeze.
2209           const locale& __loc = __io._M_getloc();
2210           const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
2211           const _CharT __minus = __ctype.widen('-');
2212           const _CharT __plus = __ctype.widen('+');
2213           const bool __testsign = _Traits::eq(__olds[0], __minus)
2214                                   || _Traits::eq(__olds[0], __plus);
2215
2216           const bool __testhex = _Traits::eq(__ctype.widen('0'), __olds[0]) 
2217                                  && (_Traits::eq(__ctype.widen('x'), __olds[1]) 
2218                                      || _Traits::eq(__ctype.widen('X'), __olds[1]));
2219           if (__testhex)
2220             {
2221               __news[0] = __olds[0]; 
2222               __news[1] = __olds[1];
2223               __mod = 2;
2224               __news += 2;
2225             }
2226           else if (__testsign)
2227             {
2228               __news[0] = __olds[0];
2229               __mod = 1;
2230               ++__news;
2231             }
2232           // else Padding first.
2233         }
2234       _Traits::assign(__news, __plen, __fill);
2235       _Traits::copy(__news + __plen, const_cast<_CharT*>(__olds + __mod),
2236                     __oldlen - __mod);
2237     }
2238
2239   template<typename _CharT>
2240     bool
2241     __verify_grouping(const basic_string<_CharT>& __grouping, 
2242                       const basic_string<_CharT>& __grouping_tmp)
2243     {         
2244       size_t __i = 0;
2245       size_t __j = 0;
2246       const size_t __len = __grouping.size();
2247       const size_t __n = __grouping_tmp.size();
2248       bool __test = true;
2249       
2250       // Parsed number groupings have to match the
2251       // numpunct::grouping string exactly, starting at the
2252       // right-most point of the parsed sequence of elements ...
2253       while (__test && __i < __n - 1)
2254         for (__j = 0; __test && __j < __len && __i < __n - 1; ++__j, ++__i)
2255           __test = __grouping[__j] == __grouping_tmp[__n - __i - 1];
2256       // ... but the last parsed grouping can be <= numpunct
2257       // grouping.
2258       __j == __len ? __j = 0 : __j;
2259       __test &= __grouping[__j] >= __grouping_tmp[__n - __i - 1];
2260       return __test;
2261     }
2262
2263   template<typename _CharT>
2264     _CharT*
2265     __add_grouping(_CharT* __s, _CharT __sep,  
2266                    const char* __gbeg, const char* __gend, 
2267                    const _CharT* __first, const _CharT* __last)
2268   {
2269     if (__last - __first > *__gbeg)
2270       {
2271         const bool __bump = __gbeg + 1 != __gend;
2272         __s = std::__add_grouping(__s,  __sep, __gbeg + __bump,
2273                                   __gend, __first, __last - *__gbeg);
2274         __first = __last - *__gbeg;
2275         *__s++ = __sep;
2276       }
2277     do
2278       *__s++ = *__first++;
2279     while (__first != __last);
2280     return __s;
2281   }
2282
2283   // Inhibit implicit instantiations for required instantiations,
2284   // which are defined via explicit instantiations elsewhere.  
2285   // NB: This syntax is a GNU extension.
2286 #if _GLIBCXX_EXTERN_TEMPLATE
2287   extern template class moneypunct<char, false>;
2288   extern template class moneypunct<char, true>;
2289   extern template class moneypunct_byname<char, false>;
2290   extern template class moneypunct_byname<char, true>;
2291   extern template class money_get<char>;
2292   extern template class money_put<char>;
2293   extern template class numpunct<char>;
2294   extern template class numpunct_byname<char>;
2295   extern template class num_get<char>;
2296   extern template class num_put<char>; 
2297   extern template class __timepunct<char>;
2298   extern template class time_put<char>;
2299   extern template class time_put_byname<char>;
2300   extern template class time_get<char>;
2301   extern template class time_get_byname<char>;
2302   extern template class messages<char>;
2303   extern template class messages_byname<char>;
2304   extern template class ctype_byname<char>;
2305   extern template class codecvt_byname<char, char, mbstate_t>;
2306   extern template class collate<char>;
2307   extern template class collate_byname<char>;
2308
2309   extern template
2310     const codecvt<char, char, mbstate_t>& 
2311     use_facet<codecvt<char, char, mbstate_t> >(const locale&);
2312
2313   extern template
2314     const collate<char>& 
2315     use_facet<collate<char> >(const locale&);
2316
2317   extern template
2318     const numpunct<char>& 
2319     use_facet<numpunct<char> >(const locale&);
2320
2321   extern template 
2322     const num_put<char>& 
2323     use_facet<num_put<char> >(const locale&);
2324
2325   extern template 
2326     const num_get<char>& 
2327     use_facet<num_get<char> >(const locale&);
2328
2329   extern template
2330     const moneypunct<char, true>& 
2331     use_facet<moneypunct<char, true> >(const locale&);
2332
2333   extern template
2334     const moneypunct<char, false>& 
2335     use_facet<moneypunct<char, false> >(const locale&);
2336
2337   extern template 
2338     const money_put<char>& 
2339     use_facet<money_put<char> >(const locale&);
2340
2341   extern template 
2342     const money_get<char>& 
2343     use_facet<money_get<char> >(const locale&);
2344
2345   extern template
2346     const __timepunct<char>& 
2347     use_facet<__timepunct<char> >(const locale&);
2348
2349   extern template 
2350     const time_put<char>& 
2351     use_facet<time_put<char> >(const locale&);
2352
2353   extern template 
2354     const time_get<char>& 
2355     use_facet<time_get<char> >(const locale&);
2356
2357   extern template 
2358     const messages<char>& 
2359     use_facet<messages<char> >(const locale&);
2360
2361   extern template 
2362     bool
2363     has_facet<ctype<char> >(const locale&);
2364
2365   extern template 
2366     bool
2367     has_facet<codecvt<char, char, mbstate_t> >(const locale&);
2368
2369   extern template 
2370     bool
2371     has_facet<collate<char> >(const locale&);
2372
2373   extern template 
2374     bool
2375     has_facet<numpunct<char> >(const locale&);
2376
2377   extern template 
2378     bool
2379     has_facet<num_put<char> >(const locale&);
2380
2381   extern template 
2382     bool
2383     has_facet<num_get<char> >(const locale&);
2384
2385   extern template 
2386     bool
2387     has_facet<moneypunct<char> >(const locale&);
2388
2389   extern template 
2390     bool
2391     has_facet<money_put<char> >(const locale&);
2392
2393   extern template 
2394     bool
2395     has_facet<money_get<char> >(const locale&);
2396
2397   extern template 
2398     bool
2399     has_facet<__timepunct<char> >(const locale&);
2400
2401   extern template 
2402     bool
2403     has_facet<time_put<char> >(const locale&);
2404
2405   extern template 
2406     bool
2407     has_facet<time_get<char> >(const locale&);
2408
2409   extern template 
2410     bool
2411     has_facet<messages<char> >(const locale&);
2412
2413 #ifdef _GLIBCXX_USE_WCHAR_T
2414   extern template class moneypunct<wchar_t, false>;
2415   extern template class moneypunct<wchar_t, true>;
2416   extern template class moneypunct_byname<wchar_t, false>;
2417   extern template class moneypunct_byname<wchar_t, true>;
2418   extern template class money_get<wchar_t>;
2419   extern template class money_put<wchar_t>;
2420   extern template class numpunct<wchar_t>;
2421   extern template class numpunct_byname<wchar_t>;
2422   extern template class num_get<wchar_t>;
2423   extern template class num_put<wchar_t>;
2424   extern template class __timepunct<wchar_t>;
2425   extern template class time_put<wchar_t>;
2426   extern template class time_put_byname<wchar_t>;
2427   extern template class time_get<wchar_t>;
2428   extern template class time_get_byname<wchar_t>;
2429   extern template class messages<wchar_t>;
2430   extern template class messages_byname<wchar_t>;
2431   extern template class ctype_byname<wchar_t>;
2432   extern template class codecvt_byname<wchar_t, char, mbstate_t>;
2433   extern template class collate<wchar_t>;
2434   extern template class collate_byname<wchar_t>;
2435
2436   extern template
2437     const codecvt<wchar_t, char, mbstate_t>& 
2438     use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&);
2439
2440   extern template
2441     const collate<wchar_t>& 
2442     use_facet<collate<wchar_t> >(const locale&);
2443
2444   extern template
2445     const numpunct<wchar_t>& 
2446     use_facet<numpunct<wchar_t> >(const locale&);
2447
2448   extern template 
2449     const num_put<wchar_t>& 
2450     use_facet<num_put<wchar_t> >(const locale&);
2451
2452   extern template 
2453     const num_get<wchar_t>& 
2454     use_facet<num_get<wchar_t> >(const locale&);
2455
2456   extern template
2457     const moneypunct<wchar_t, true>& 
2458     use_facet<moneypunct<wchar_t, true> >(const locale&);
2459
2460   extern template
2461     const moneypunct<wchar_t, false>& 
2462     use_facet<moneypunct<wchar_t, false> >(const locale&);
2463  
2464   extern template 
2465     const money_put<wchar_t>& 
2466     use_facet<money_put<wchar_t> >(const locale&);
2467
2468   extern template 
2469     const money_get<wchar_t>& 
2470     use_facet<money_get<wchar_t> >(const locale&);
2471
2472   extern template
2473     const __timepunct<wchar_t>& 
2474     use_facet<__timepunct<wchar_t> >(const locale&);
2475
2476   extern template 
2477     const time_put<wchar_t>& 
2478     use_facet<time_put<wchar_t> >(const locale&);
2479
2480   extern template 
2481     const time_get<wchar_t>& 
2482     use_facet<time_get<wchar_t> >(const locale&);
2483
2484   extern template 
2485     const messages<wchar_t>& 
2486     use_facet<messages<wchar_t> >(const locale&);
2487
2488  extern template 
2489     bool
2490     has_facet<ctype<wchar_t> >(const locale&);
2491
2492   extern template 
2493     bool
2494     has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
2495
2496   extern template 
2497     bool
2498     has_facet<collate<wchar_t> >(const locale&);
2499
2500   extern template 
2501     bool
2502     has_facet<numpunct<wchar_t> >(const locale&);
2503
2504   extern template 
2505     bool
2506     has_facet<num_put<wchar_t> >(const locale&);
2507
2508   extern template 
2509     bool
2510     has_facet<num_get<wchar_t> >(const locale&);
2511
2512   extern template 
2513     bool
2514     has_facet<moneypunct<wchar_t> >(const locale&);
2515
2516   extern template 
2517     bool
2518     has_facet<money_put<wchar_t> >(const locale&);
2519
2520   extern template 
2521     bool
2522     has_facet<money_get<wchar_t> >(const locale&);
2523
2524   extern template 
2525     bool
2526     has_facet<__timepunct<wchar_t> >(const locale&);
2527
2528   extern template 
2529     bool
2530     has_facet<time_put<wchar_t> >(const locale&);
2531
2532   extern template 
2533     bool
2534     has_facet<time_get<wchar_t> >(const locale&);
2535
2536   extern template 
2537     bool
2538     has_facet<messages<wchar_t> >(const locale&);
2539 #endif
2540 #endif
2541 } // namespace std
2542
2543 #endif