OSDN Git Service

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