OSDN Git Service

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