OSDN Git Service

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