OSDN Git Service

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