OSDN Git Service

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