OSDN Git Service

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