OSDN Git Service

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