OSDN Git Service

2001-12-11 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / locale_facets.tcc
1 // Locale support -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // Warning: this file is not meant for user inclusion.  Use <locale>.
31
32 #ifndef _CPP_BITS_LOCFACETS_TCC
33 #define _CPP_BITS_LOCFACETS_TCC 1
34
35 #include <bits/std_cerrno.h>
36 #include <bits/std_clocale.h>   // For localeconv
37 #include <bits/std_cstdlib.h>   // For strof, strtold
38 #include <bits/std_cmath.h>     // For ceil
39 #include <bits/std_limits.h>    // For numeric_limits
40 #include <bits/std_memory.h>    // For auto_ptr
41 #include <bits/streambuf_iterator.h>     // For streambuf_iterators
42 #include <bits/std_cctype.h>    // For isspace
43 #include <typeinfo>             // For bad_cast
44 #include <bits/std_vector.h>    
45
46 namespace std
47 {
48   template<typename _Facet>
49     locale
50     locale::combine(const locale& __other) const
51     {
52       _Impl* __tmp = new _Impl(*_M_impl, 1);
53       __tmp->_M_replace_facet(__other._M_impl, &_Facet::id);
54       return locale(__tmp);
55     }
56
57   template<typename _CharT, typename _Traits, typename _Alloc>
58     bool
59     locale::operator()(const basic_string<_CharT, _Traits, _Alloc>& __s1,
60                        const basic_string<_CharT, _Traits, _Alloc>& __s2) const
61     {
62       typedef std::collate<_CharT> __collate_type;
63       const __collate_type& __collate = use_facet<__collate_type>(*this);
64       return (__collate.compare(__s1.data(), __s1.data() + __s1.length(),
65                                 __s2.data(), __s2.data() + __s2.length()) < 0);
66     }
67
68   template<typename _Facet>
69     const _Facet&
70     use_facet(const locale& __loc)
71     {
72       size_t __i = _Facet::id._M_index;
73       locale::_Impl::__vec_facet* __facet = __loc._M_impl->_M_facets;
74       const locale::facet* __fp = (*__facet)[__i]; 
75       if (__fp == 0 || __i >= __facet->size())
76         __throw_bad_cast();
77       return static_cast<const _Facet&>(*__fp);
78     }
79
80   template<typename _Facet>
81     bool
82     has_facet(const locale& __loc) throw()
83     {
84       size_t __i = _Facet::id._M_index;
85       locale::_Impl::__vec_facet* __facet = __loc._M_impl->_M_facets;
86       return (__i < __facet->size() && (*__facet)[__i] != 0);
87     }
88
89
90   template<typename _CharT, typename _InIter>
91     void
92     num_get<_CharT, _InIter>::
93     _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
94                      ios_base::iostate& __err, string& __xtrc) const
95     {
96       const locale __loc = __io.getloc();
97       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
98       const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
99
100       // Check first for sign.
101       const char_type __plus = __ctype.widen('+');
102       const char_type __minus = __ctype.widen('-');
103       int __pos = 0;
104       char_type  __c = *__beg;
105       if ((__c == __plus || __c == __minus) && __beg != __end)
106         {
107           __xtrc += __ctype.narrow(__c, char());
108           ++__pos;
109           __c = *(++__beg);
110         }
111
112       // Next, strip leading zeros.
113       const char_type __zero = __ctype.widen(_S_atoms[_M_zero]);
114       bool __found_zero = false;
115       while (__c == __zero && __beg != __end)
116         {
117           __c = *(++__beg);
118           __found_zero = true;
119         }
120       if (__found_zero)
121         {
122           __xtrc += _S_atoms[_M_zero];
123           ++__pos;
124         }
125
126       // Only need acceptable digits for floating point numbers.
127       const size_t __len = _M_E - _M_zero + 1;
128       char_type  __watoms[__len];
129       __ctype.widen(_S_atoms, _S_atoms + __len, __watoms);
130       bool __found_dec = false;
131       bool __found_sci = false;
132       const char_type __dec = __np.decimal_point();
133
134       string __found_grouping;
135       const string __grouping = __np.grouping();
136       bool __check_grouping = __grouping.size();
137       int __sep_pos = 0;
138       const char_type __sep = __np.thousands_sep();
139
140       while (__beg != __end)
141         {
142           // Only look in digits.
143           typedef char_traits<_CharT>   __traits_type;
144           const char_type* __p = __traits_type::find(__watoms, 10,  __c);
145
146           // NB: strchr returns true for __c == 0x0
147           if (__p && __c)
148             {
149               // Try first for acceptable digit; record it if found.
150               ++__pos;
151               __xtrc += _S_atoms[__p - __watoms];
152               ++__sep_pos;
153               __c = *(++__beg);
154             }
155           else if (__c == __sep && __check_grouping && !__found_dec)
156             {
157               // NB: Thousands separator at the beginning of a string
158               // is a no-no, as is two consecutive thousands separators.
159               if (__sep_pos)
160                 {
161                   __found_grouping += static_cast<char>(__sep_pos);
162                   __sep_pos = 0;
163                   __c = *(++__beg);
164                 }
165               else
166                 {
167                   __err |= ios_base::failbit;
168                   break;
169                 }
170             }
171           else if (__c == __dec && !__found_dec)
172             {
173               __found_grouping += static_cast<char>(__sep_pos);
174               ++__pos;
175               __xtrc += '.';
176               __c = *(++__beg);
177               __found_dec = true;
178             }
179           else if ((__c == __watoms[_M_e] || __c == __watoms[_M_E]) 
180                    && !__found_sci && __pos)
181             {
182               // Scientific notation.
183               ++__pos;
184               __xtrc += __ctype.narrow(__c, char());
185               __c = *(++__beg);
186
187               // Remove optional plus or minus sign, if they exist.
188               if (__c == __plus || __c == __minus)
189                 {
190                   ++__pos;
191                   __xtrc += __ctype.narrow(__c, char());
192                   __c = *(++__beg);
193                 }
194               __found_sci = true;
195             }
196           else
197             // Not a valid input item.
198             break;
199         }
200
201       // Digit grouping is checked. If grouping and found_grouping don't
202       // match, then get very very upset, and set failbit.
203       if (__check_grouping && __found_grouping.size())
204         {
205           // Add the ending grouping if a decimal wasn't found.
206           if (!__found_dec)
207             __found_grouping += static_cast<char>(__sep_pos);
208           if (!__verify_grouping(__grouping, __found_grouping))
209             __err |= ios_base::failbit;
210         }
211
212       // Finish up
213       __xtrc += char();
214       if (__beg == __end)
215         __err |= ios_base::eofbit;
216     }
217
218   template<typename _CharT, typename _InIter>
219     void
220     num_get<_CharT, _InIter>::
221     _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
222                    ios_base::iostate& __err, char* __xtrc, int __max, 
223                    int& __base) const
224     {
225       const locale __loc = __io.getloc();
226       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
227       const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
228  
229       // Stage 1: determine a conversion specifier.
230       // NB: Iff __basefield == 0, this can change based on contents.
231       ios_base::fmtflags __basefield = __io.flags() & ios_base::basefield;
232       if (__basefield == ios_base::oct)
233         __base = 8;
234       else if (__basefield == ios_base::hex)
235         __base = 16;
236       else
237         __base = 10;
238
239      // Check first for sign.
240       int __pos = 0;
241       char_type  __c = *__beg;
242       if ((__c == __ctype.widen('+') || __c == __ctype.widen('-'))
243           && __beg != __end)
244         {
245           __xtrc[__pos++] = __ctype.narrow(__c, char());
246           __c = *(++__beg);
247         }
248
249       // Next, strip leading zeros and check required digits for base formats.
250       const char_type __zero = __ctype.widen(_S_atoms[_M_zero]);
251       const char_type __x = __ctype.widen('x');
252       const char_type __X = __ctype.widen('X');
253       if (__base == 10)
254         {
255           bool __found_zero = false;
256           while (__c == __zero && __beg != __end)
257             {
258               __c = *(++__beg);
259               __found_zero = true;
260             }
261           if (__found_zero)
262             {
263               __xtrc[__pos++] = _S_atoms[_M_zero];
264               if (__basefield == 0)
265                 {             
266                   if ((__c == __x || __c == __X) && __beg != __end)
267                     {
268                       __xtrc[__pos++] = __ctype.narrow(__c, char());
269                       __c = *(++__beg);
270                       __base = 16;
271                     }
272                   else 
273                     __base = 8;
274                 }
275             }
276         }
277       else if (__base == 16)
278         {
279           if (__c == __zero && __beg != __end)
280             {
281               __xtrc[__pos++] = _S_atoms[_M_zero];
282               __c = *(++__beg); 
283               if  ((__c == __x || __c == __X) && __beg != __end)
284                 {
285                   __xtrc[__pos++] = __ctype.narrow(__c, char());
286                   __c = *(++__beg);
287                 }
288             }
289         }
290
291       // At this point, base is determined. If not hex, only allow
292       // base digits as valid input.
293       size_t __len;
294       if (__base == 16)
295         __len = _M_size;
296       else
297         __len = __base;
298
299       // Figure out the maximum number of digits that can be extracted
300       // for the given type, using the determined base.
301       int __max_digits;
302       if (__base == 16)
303         __max_digits = static_cast<int>(ceil(__max * _S_scale_hex));
304       else if (__base == 8)
305         __max_digits = static_cast<int>(ceil(__max * _S_scale_oct));
306       else
307         __max_digits = __max;
308
309       // Add in what's already been extracted.
310       __max_digits += __pos;
311
312       // Extract.
313       char_type __watoms[_M_size];
314       __ctype.widen(_S_atoms, _S_atoms + __len, __watoms);
315       string __found_grouping;
316       const string __grouping = __np.grouping();
317       bool __check_grouping = __grouping.size() && __base == 10;
318       int __sep_pos = 0;
319       const char_type __sep = __np.thousands_sep();
320       while (__beg != __end && __pos <= __max_digits)
321         {
322           typedef char_traits<_CharT>   __traits_type;
323           const char_type* __p = __traits_type::find(__watoms, __len,  __c);
324
325           // NB: strchr returns true for __c == 0x0
326           if (__p && __c)
327             {
328               // Try first for acceptable digit; record it if found.
329               __xtrc[__pos++] = _S_atoms[__p - __watoms];
330               ++__sep_pos;
331               __c = *(++__beg);
332             }
333           else if (__c == __sep && __check_grouping)
334             {
335               // NB: Thousands separator at the beginning of a string
336               // is a no-no, as is two consecutive thousands separators.
337               if (__sep_pos)
338                 {
339                   __found_grouping += static_cast<char>(__sep_pos);
340                   __sep_pos = 0;
341                   __c = *(++__beg);
342                 }
343               else
344                 {
345                   __err |= ios_base::failbit;
346                   break;
347                 }
348             }
349           else
350             // Not a valid input item.
351             break;
352         }
353
354       // If one more than the maximum number of digits is extracted.
355       if (__pos > __max_digits)
356         __err |= ios_base::failbit;
357
358       // Digit grouping is checked. If grouping and found_grouping don't
359       // match, then get very very upset, and set failbit.
360       if (__check_grouping && __found_grouping.size())
361         {
362           // Add the ending grouping.
363           __found_grouping += static_cast<char>(__sep_pos);
364           if (!__verify_grouping(__grouping, __found_grouping))
365             __err |= ios_base::failbit;
366         }
367
368       // Finish up
369       __xtrc[__pos] = char();
370       if (__beg == __end)
371         __err |= ios_base::eofbit;
372     }
373
374 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
375   //17.  Bad bool parsing
376   template<typename _CharT, typename _InIter>
377     _InIter
378     num_get<_CharT, _InIter>::
379     do_get(iter_type __beg, iter_type __end, ios_base& __io,
380            ios_base::iostate& __err, bool& __v) const
381     {
382       // Parse bool values as long
383       if (!(__io.flags() & ios_base::boolalpha))
384         {
385           // NB: We can't just call do_get(long) here, as it might
386           // refer to a derived class.
387
388           // Stage 1: extract and determine the conversion specifier.
389           // Assuming leading zeros eliminated, thus the size of 32 for
390           // integral types
391           char __xtrc[32];
392           int __base;
393           _M_extract_int(__beg, __end, __io, __err, __xtrc, 
394                          numeric_limits<bool>::digits10, __base);
395
396           // Stage 2: convert and store results.
397           char* __sanity;
398           errno = 0;
399           long __l = strtol(__xtrc, &__sanity, __base);
400           if (!(__err & ios_base::failbit)
401               && __l <= 1
402               && __sanity != __xtrc && *__sanity == '\0' && errno == 0)
403             __v = __l;
404           else
405             __err |= ios_base::failbit;
406         }
407
408       // Parse bool values as alphanumeric
409       else
410         {
411           locale __loc = __io.getloc();
412           const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__loc); 
413           const char_type* __true = __np.truename().c_str();
414           const char_type* __false = __np.falsename().c_str();
415
416           const size_t __truen =  __np.truename().size() - 1;
417           const size_t __falsen =  __np.falsename().size() - 1;
418
419           for (size_t __n = 0; __beg != __end; ++__n)
420             {
421               char_type __c = *__beg++;
422               bool __testf = __n <= __falsen ? __c == __false[__n] : false;
423               bool __testt = __n <= __truen ? __c == __true[__n] : false;
424               if (!(__testf || __testt))
425                 {
426                   __err |= ios_base::failbit;
427                   break;
428                 }
429               else if (__testf && __n == __falsen)
430                 {
431                   __v = 0;
432                   break;
433                 }
434               else if (__testt && __n == __truen)
435                 {
436                   __v = 1;
437                   break;
438                 }
439             }
440           if (__beg == __end)
441             __err |= ios_base::eofbit;
442         }
443       return __beg;
444     }
445 #endif
446
447   template<typename _CharT, typename _InIter>
448     _InIter
449     num_get<_CharT, _InIter>::
450     do_get(iter_type __beg, iter_type __end, ios_base& __io,
451            ios_base::iostate& __err, long& __v) const
452     {
453       // Stage 1: extract and determine the conversion specifier.
454       // Assuming leading zeros eliminated, thus the size of 32 for
455       // integral types.
456       char __xtrc[32];
457       int __base;
458       _M_extract_int(__beg, __end, __io, __err, __xtrc, 
459                      numeric_limits<long>::digits10, __base);
460
461       // Stage 2: convert and store results.
462       char* __sanity;
463       errno = 0;
464       long __l = strtol(__xtrc, &__sanity, __base);
465       if (!(__err & ios_base::failbit)
466           && __sanity != __xtrc && *__sanity == '\0' && errno == 0)
467         __v = __l;
468       else
469         __err |= ios_base::failbit;
470       return __beg;
471     }
472
473   template<typename _CharT, typename _InIter>
474     _InIter
475     num_get<_CharT, _InIter>::
476     do_get(iter_type __beg, iter_type __end, ios_base& __io,
477            ios_base::iostate& __err, unsigned short& __v) const
478     {
479       // Stage 1: extract and determine the conversion specifier.
480       // Assuming leading zeros eliminated, thus the size of 32 for
481       // integral types.
482       char __xtrc[32];
483       int __base;
484       _M_extract_int(__beg, __end, __io, __err, __xtrc, 
485                      numeric_limits<unsigned short>::digits10, __base);
486
487       // Stage 2: convert and store results.
488       char* __sanity;
489       errno = 0;
490       unsigned long __ul = strtoul(__xtrc, &__sanity, __base);
491       if (!(__err & ios_base::failbit)
492           && __sanity != __xtrc && *__sanity == '\0' && errno == 0
493           && __ul <= USHRT_MAX)
494         __v = static_cast<unsigned short>(__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 int& __v) const
505     {
506       // Stage 1: extract and determine the conversion specifier.
507       // Assuming leading zeros eliminated, thus the size of 32 for
508       // integral types.
509       char __xtrc[32];
510       int __base;
511       _M_extract_int(__beg, __end, __io, __err, __xtrc, 
512                      numeric_limits<unsigned int>::digits10, __base);
513
514       // Stage 2: convert and store results.
515       char* __sanity;
516       errno = 0;
517       unsigned long __ul = strtoul(__xtrc, &__sanity, __base);
518       if (!(__err & ios_base::failbit)
519           && __sanity != __xtrc && *__sanity == '\0' && errno == 0
520           && __ul <= UINT_MAX)
521         __v = static_cast<unsigned int>(__ul);
522       else
523         __err |= ios_base::failbit;
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& __v) const
532     {
533       // Stage 1: extract and determine the conversion specifier.
534       // Assuming leading zeros eliminated, thus the size of 32 for
535       // integral types.
536       char __xtrc[32];
537       int __base;
538       _M_extract_int(__beg, __end, __io, __err, __xtrc, 
539                      numeric_limits<unsigned long>::digits10, __base);
540
541       // Stage 2: convert and store results.
542       char* __sanity;
543       errno = 0;
544       unsigned long __ul = strtoul(__xtrc, &__sanity, __base);
545       if (!(__err & ios_base::failbit)
546           && __sanity != __xtrc && *__sanity == '\0' && errno == 0)
547         __v = __ul;
548       else
549         __err |= ios_base::failbit;
550       return __beg;
551     }
552
553 #ifdef _GLIBCPP_USE_LONG_LONG
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, long long& __v) const
559     {
560       // Stage 1: extract and determine the conversion specifier.
561       // Assuming leading zeros eliminated, thus the size of 32 for
562       // integral types.
563       char __xtrc[32];
564       int __base;
565       _M_extract_int(__beg, __end, __io, __err, __xtrc, 
566                      numeric_limits<long long>::digits10, __base);
567
568       // Stage 2: convert and store results.
569       char* __sanity;
570       errno = 0;
571       long long __ll = strtoll(__xtrc, &__sanity, __base);
572       if (!(__err & ios_base::failbit)
573           && __sanity != __xtrc && *__sanity == '\0' && errno == 0)
574         __v = __ll;
575       else
576         __err |= ios_base::failbit;
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, unsigned long long& __v) const
585     {
586       // Stage 1: extract and determine the conversion specifier.
587       // Assuming leading zeros eliminated, thus the size of 32 for
588       // integral types.
589       char __xtrc[32];
590       int __base;
591       _M_extract_int(__beg, __end, __io, __err, __xtrc,
592                      numeric_limits<unsigned long long>::digits10, __base);
593
594       // Stage 2: convert and store results.
595       char* __sanity;
596       errno = 0;
597       unsigned long long __ull = strtoull(__xtrc, &__sanity, __base);
598       if (!(__err & ios_base::failbit)
599           && __sanity != __xtrc && *__sanity == '\0' && errno == 0)
600         __v = __ull;
601       else
602         __err |= ios_base::failbit;
603       return __beg;
604     }
605 #endif
606
607   template<typename _CharT, typename _InIter>
608     _InIter
609     num_get<_CharT, _InIter>::
610     do_get(iter_type __beg, iter_type __end, ios_base& __io, 
611            ios_base::iostate& __err, float& __v) const
612     {
613       // Stage 1: extract and determine the conversion specifier.
614       string __xtrc;
615       __xtrc.reserve(32);
616       _M_extract_float(__beg, __end, __io, __err, __xtrc);
617
618       // Stage 2: convert and store results.
619       char* __sanity;
620       errno = 0;
621 #ifdef _GLIBCPP_USE_C99
622       float __f = strtof(__xtrc.c_str(), &__sanity);
623 #else
624       float __f = static_cast<float>(strtod(__xtrc.c_str(), &__sanity));
625 #endif
626       if (!(__err & ios_base::failbit)
627           && __sanity != __xtrc.c_str() && *__sanity == '\0' && errno == 0)
628         __v = __f;
629       else
630         __err |= ios_base::failbit;
631       return __beg;
632     }
633
634   template<typename _CharT, typename _InIter>
635     _InIter
636     num_get<_CharT, _InIter>::
637     do_get(iter_type __beg, iter_type __end, ios_base& __io,
638            ios_base::iostate& __err, double& __v) const
639     {
640       // Stage 1: extract and determine the conversion specifier.
641       string __xtrc;
642       __xtrc.reserve(32);
643       _M_extract_float(__beg, __end, __io, __err, __xtrc);
644
645       // Stage 2: convert and store results.
646       char* __sanity;
647       errno = 0;
648       double __d = strtod(__xtrc.c_str(), &__sanity);
649       if (!(__err & ios_base::failbit)
650           && __sanity != __xtrc.c_str() && *__sanity == '\0' && errno == 0)
651         __v = __d;
652       else
653         __err |= ios_base::failbit;
654       return __beg;
655     }
656
657   template<typename _CharT, typename _InIter>
658     _InIter
659     num_get<_CharT, _InIter>::
660     do_get(iter_type __beg, iter_type __end, ios_base& __io,
661            ios_base::iostate& __err, long double& __v) const
662     {
663       // Stage 1: extract and determine the conversion specifier.
664       string __xtrc;
665       __xtrc.reserve(32);
666       _M_extract_float(__beg, __end, __io, __err, __xtrc);
667
668 #if defined(_GLIBCPP_USE_C99) && !defined(__hpux)
669       // Stage 2: convert and store results.
670       char* __sanity;
671       errno = 0;
672       long double __ld = strtold(__xtrc.c_str(), &__sanity);
673       if (!(__err & ios_base::failbit)
674           && __sanity != __xtrc.c_str() && *__sanity == '\0' && errno == 0)
675         __v = __ld;
676 #else
677       // Stage 2: determine a conversion specifier.
678       ios_base::fmtflags __basefield = __io.flags() & ios_base::basefield;
679       const char* __conv;
680       if (__basefield == ios_base::oct)
681         __conv = "%Lo";
682       else if (__basefield == ios_base::hex)
683         __conv = "%LX";
684       else if (__basefield == 0)
685         __conv = "%Li";
686       else
687         __conv = "%Lf";
688
689       // Stage 3: store results.
690       typedef typename char_traits<_CharT>::int_type int_type;
691       long double __ld;
692       int __p = sscanf(__xtrc.c_str(), __conv, &__ld);
693       if (!(__err & ios_base::failbit) && __p 
694           && static_cast<int_type>(__p) != char_traits<_CharT>::eof())
695         __v = __ld;
696 #endif
697       else
698         __err |= ios_base::failbit;
699       return __beg;
700     }
701
702   template<typename _CharT, typename _InIter>
703     _InIter
704     num_get<_CharT, _InIter>::
705     do_get(iter_type __beg, iter_type __end, ios_base& __io,
706            ios_base::iostate& __err, void*& __v) const
707     {
708       // Prepare for hex formatted input
709       typedef ios_base::fmtflags        fmtflags;
710       fmtflags __fmt = __io.flags();
711       fmtflags __fmtmask = ~(ios_base::showpos | ios_base::basefield
712                              | ios_base::uppercase | ios_base::internal);
713       __io.flags(__fmt & __fmtmask | (ios_base::hex | ios_base::showbase));
714
715       // Stage 1: extract and determine the conversion specifier.
716       // Assuming leading zeros eliminated, thus the size of 32 for
717       // integral types.
718       char __xtrc[32];
719       int __base;
720       _M_extract_int(__beg, __end, __io, __err, __xtrc, 
721                      numeric_limits<unsigned long>::digits10, __base);
722
723       // Stage 2: convert and store results.
724       char* __sanity;
725       errno = 0;
726       void* __vp = reinterpret_cast<void*>(strtoul(__xtrc, &__sanity, __base));
727       if (!(__err & ios_base::failbit)
728           && __sanity != __xtrc && *__sanity == '\0' && errno == 0)
729         __v = __vp;
730       else
731         __err |= ios_base::failbit;
732
733       // Reset from hex formatted input
734       __io.flags(__fmt);
735       return __beg;
736     }
737
738
739   // The following code uses sprintf() to convert floating point
740   // values for insertion into a stream.  An optimization would be to
741   // replace sprintf() with code that works directly on a wide buffer
742   // and then use __pad to do the padding. It would be good
743   // to replace sprintf() anyway to avoid accidental buffer overruns
744   // and to gain back the efficiency that C++ provides by knowing up
745   // front the type of the values to insert. This implementation
746   // follows the C++ standard fairly directly as outlined in 22.2.2.2
747   // [lib.locale.num.put]
748   template<typename _CharT, typename _OutIter>
749     template<typename _ValueT>
750       _OutIter
751       num_put<_CharT, _OutIter>::
752       _M_convert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
753                        _ValueT __v) const
754       {
755         const int __max_digits = numeric_limits<_ValueT>::digits10;
756         streamsize __prec = __io.precision();
757         // Protect against sprintf() buffer overflows.
758         if (__prec > static_cast<streamsize>(__max_digits))
759           __prec = static_cast<streamsize>(__max_digits);
760
761         // Long enough for the max format spec.
762         char __fbuf[16];
763
764         // Consider the possibility of long ios_base::fixed outputs
765         const bool __fixed = __io.flags() & ios_base::fixed;
766         const int __max_exp = numeric_limits<_ValueT>::max_exponent10;
767         // ios_base::fixed outputs may need up to __max_exp+1 chars
768         // for the integer part + up to __max_digits chars for the
769         // fractional part + 3 chars for sign, decimal point, '\0'. On
770         // the other hand, for non-fixed outputs __max_digits*3 chars
771         // are largely sufficient.
772         const int __cs_size = __fixed ? __max_exp + __max_digits + 4 
773                                       : __max_digits * 3;
774         char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
775
776         int __len;
777         // [22.2.2.2.2] Stage 1, numeric conversion to character.
778         if (_S_format_float(__io, __fbuf, __mod, __prec))
779           __len = sprintf(__cs, __fbuf, __prec, __v);
780         else
781           __len = sprintf(__cs, __fbuf, __v);
782         return _M_widen_float(__s, __io, __fill, __cs, __len);
783       }
784
785   template<typename _CharT, typename _OutIter>
786     template<typename _ValueT>
787       _OutIter
788       num_put<_CharT, _OutIter>::
789       _M_convert_int(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
790                      char __modl, _ValueT __v) const
791       {
792         // [22.2.2.2.2] Stage 1, numeric conversion to character.
793         // Leave room for "+/-," "0x," and commas. This size is
794         // arbitrary, but should work.
795         char __cs[64];
796         // Long enough for the max format spec.
797         char __fbuf[16];
798         _S_format_int(__io, __fbuf, __mod, __modl);
799         int __len = sprintf(__cs, __fbuf, __v);
800         return _M_widen_int(__s, __io, __fill, __cs, __len);
801       }
802
803   template<typename _CharT, typename _OutIter>
804     _OutIter
805     num_put<_CharT, _OutIter>::
806     _M_widen_float(_OutIter __s, ios_base& __io, _CharT __fill, char* __cs, 
807                    int __len) const
808     {
809       // [22.2.2.2.2] Stage 2, convert to char_type, using correct
810       // numpunct.decimal_point() values for '.' and adding grouping.
811       const locale __loc = __io.getloc();
812       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
813       _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
814                                                            * __len));
815       // Grouping can add (almost) as many separators as the number of
816       // digits, but no more.
817       _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
818                                                             * __len * 2));
819       __ctype.widen(__cs, __cs + __len, __ws);
820       
821       // Replace decimal point.
822       const _CharT* __p;
823       const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
824       if (__p = char_traits<_CharT>::find(__ws, __len, __ctype.widen('.')))
825         __ws[__p - __ws] = __np.decimal_point();
826
827 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
828 //282. What types does numpunct grouping refer to?
829       // Add grouping, if necessary. 
830       const string __grouping = __np.grouping();
831       ios_base::fmtflags __basefield = __io.flags() & ios_base::basefield;
832       if (__grouping.size())
833         {
834           _CharT* __p2;
835           int __declen = __p ? __p - __ws : __len;
836           __p2 = __add_grouping(__ws2, __np.thousands_sep(), 
837                                 __grouping.c_str(),
838                                 __grouping.c_str() + __grouping.size(),
839                                 __ws, __ws + __declen);
840           int __newlen = __p2 - __ws2;
841         
842           // Tack on decimal part.
843           if (__p)
844             {
845               char_traits<_CharT>::copy(__p2, __p, __len - __declen);
846               __newlen += __len - __declen;
847             }    
848
849           // Switch strings, establish correct new length.
850           __ws = __ws2;
851           __len = __newlen;
852         }
853 #endif
854       return _M_insert(__s, __io, __fill, __ws, __len);
855     }
856
857   template<typename _CharT, typename _OutIter>
858     _OutIter
859     num_put<_CharT, _OutIter>::
860     _M_widen_int(_OutIter __s, ios_base& __io, _CharT __fill, char* __cs, 
861                  int __len) const
862     {
863       // [22.2.2.2.2] Stage 2, convert to char_type, using correct
864       // numpunct.decimal_point() values for '.' and adding grouping.
865       const locale __loc = __io.getloc();
866       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
867       _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
868                                                            * __len));
869       // Grouping can add (almost) as many separators as the number of
870       // digits, but no more.
871       _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
872                                                             * __len * 2));
873       __ctype.widen(__cs, __cs + __len, __ws);
874
875       // Add grouping, if necessary.
876       const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
877       const string __grouping = __np.grouping();
878       ios_base::fmtflags __basefield = __io.flags() & ios_base::basefield;
879       bool __dec = __basefield != ios_base::oct 
880                    && __basefield != ios_base::hex;
881       if (__grouping.size() && __dec)
882         {
883           _CharT* __p;
884           __p = __add_grouping(__ws2, __np.thousands_sep(), __grouping.c_str(),
885                                __grouping.c_str() + __grouping.size(),
886                                __ws, __ws + __len);
887           __len = __p - __ws2;
888           // Switch strings.
889           __ws = __ws2;
890         }
891       return _M_insert(__s, __io, __fill, __ws, __len);
892     }
893
894   // For use by integer and floating-point types after they have been
895   // converted into a char_type string.
896   template<typename _CharT, typename _OutIter>
897     _OutIter
898     num_put<_CharT, _OutIter>::
899     _M_insert(_OutIter __s, ios_base& __io, _CharT __fill, const _CharT* __ws, 
900               int __len) const
901     {
902       // [22.2.2.2.2] Stage 3.
903       streamsize __w = __io.width();
904       _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 
905                                                             * __w));
906       if (__w > static_cast<streamsize>(__len))
907         {
908           __pad(__io, __fill, __ws2, __ws, __w, __len, true);
909           __len = static_cast<int>(__w);
910           // Switch strings.
911           __ws = __ws2;
912         }
913       __io.width(0);
914
915       // [22.2.2.2.2] Stage 4.
916       // Write resulting, fully-formatted string to output iterator.
917       for (int __j = 0; __j < __len; ++__j, ++__s)
918         *__s = __ws[__j];
919       return __s;
920     }
921
922   template<typename _CharT, typename _OutIter>
923     _OutIter
924     num_put<_CharT, _OutIter>::
925     do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
926     {
927       ios_base::fmtflags __flags = __io.flags();
928       if ((__flags & ios_base::boolalpha) == 0)
929         {
930           unsigned long __uv = __v;
931           _M_convert_int(__s, __io, __fill, 'u', char_type(), __uv);
932         }
933       else
934         {
935           locale __loc = __io.getloc();
936           const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc); 
937           const char_type* __ws;
938           int __len;
939           if (__v)
940             {
941               __ws = __np.truename().c_str();
942               __len = __np.truename().size();
943             }
944           else
945             {
946               __ws = __np.falsename().c_str();
947               __len = __np.falsename().size();
948             }
949           _M_insert(__s, __io, __fill, __ws, __len); 
950         }
951       return __s;
952     }
953
954   template<typename _CharT, typename _OutIter>
955     _OutIter
956     num_put<_CharT, _OutIter>::
957     do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
958     { return _M_convert_int(__s, __io, __fill, 'd', char_type(), __v); }
959
960   template<typename _CharT, typename _OutIter>
961     _OutIter
962     num_put<_CharT, _OutIter>::
963     do_put(iter_type __s, ios_base& __io, char_type __fill,
964            unsigned long __v) const
965     { return _M_convert_int(__s, __io, __fill, 'u', char_type(), __v); }
966
967 #ifdef _GLIBCPP_USE_LONG_LONG
968   template<typename _CharT, typename _OutIter>
969     _OutIter
970     num_put<_CharT, _OutIter>::
971     do_put(iter_type __s, ios_base& __b, char_type __fill, long long __v) const
972     { return _M_convert_int(__s, __b, __fill, 'd', 'l', __v); }
973
974   template<typename _CharT, typename _OutIter>
975     _OutIter
976     num_put<_CharT, _OutIter>::
977     do_put(iter_type __s, ios_base& __io, char_type __fill,
978            unsigned long long __v) const
979     { return _M_convert_int(__s, __io, __fill, 'u', 'l', __v); }
980 #endif
981
982   template<typename _CharT, typename _OutIter>
983     _OutIter
984     num_put<_CharT, _OutIter>::
985     do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
986     { return _M_convert_float(__s, __io, __fill, char_type(), __v); }
987
988   template<typename _CharT, typename _OutIter>
989     _OutIter
990     num_put<_CharT, _OutIter>::
991     do_put(iter_type __s, ios_base& __io, char_type __fill, 
992            long double __v) const
993     { return _M_convert_float(__s, __io, __fill, 'L', __v); }
994
995   template<typename _CharT, typename _OutIter>
996     _OutIter
997     num_put<_CharT, _OutIter>::
998     do_put(iter_type __s, ios_base& __io, char_type __fill,
999            const void* __v) const
1000     {
1001       ios_base::fmtflags __flags = __io.flags();
1002       ios_base::fmtflags __fmt = ~(ios_base::showpos | ios_base::basefield
1003                                    | ios_base::uppercase | ios_base::internal);
1004       __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase));
1005       try 
1006         {
1007           _M_convert_int(__s, __io, __fill, 'u', char_type(),
1008                          reinterpret_cast<unsigned long>(__v));
1009           __io.flags(__flags);
1010         }
1011       catch (...) 
1012         {
1013           __io.flags(__flags);
1014           __throw_exception_again;
1015         }
1016       return __s;
1017     }
1018
1019
1020   template<typename _CharT, typename _InIter>
1021     _InIter
1022     money_get<_CharT, _InIter>::
1023     do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 
1024            ios_base::iostate& __err, long double& __units) const
1025     { 
1026       string_type __str;
1027       this->do_get(__beg, __end, __intl, __io, __err, __str); 
1028
1029       const int __n = numeric_limits<long double>::digits10;
1030       char* __cs = static_cast<char*>(__builtin_alloca(sizeof(char) * __n));
1031       const locale __loc = __io.getloc();
1032       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1033       const _CharT* __wcs = __str.c_str();
1034       __ctype.narrow(__wcs, __wcs + __str.size() + 1, char(), __cs);      
1035
1036 #if defined(_GLIBCPP_USE_C99) && !defined(__hpux)
1037       char* __sanity;
1038       errno = 0;
1039       long double __ld = strtold(__cs, &__sanity);
1040       if (!(__err & ios_base::failbit)
1041           && __sanity != __cs && *__sanity == '\0' && errno == 0)
1042         __units = __ld;
1043 #else
1044       typedef typename char_traits<_CharT>::int_type int_type;
1045       long double __ld;
1046       int __p = sscanf(__cs, "%Lf", &__ld);
1047       if (!(__err & ios_base::failbit)
1048           && __p && static_cast<int_type>(__p) != char_traits<_CharT>::eof())
1049         __units = __ld;
1050 #endif
1051       return __beg;
1052     }
1053
1054   template<typename _CharT, typename _InIter>
1055     _InIter
1056     money_get<_CharT, _InIter>::
1057     do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 
1058            ios_base::iostate& __err, string_type& __units) const
1059     { 
1060       // These contortions are quite unfortunate.
1061       typedef moneypunct<_CharT, true>          __money_true;
1062       typedef moneypunct<_CharT, false>         __money_false;
1063       typedef money_base::part                  part;
1064       typedef typename string_type::size_type   size_type;
1065
1066       const locale __loc = __io.getloc();
1067       const __money_true& __mpt = use_facet<__money_true>(__loc); 
1068       const __money_false& __mpf = use_facet<__money_false>(__loc); 
1069       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1070
1071       const money_base::pattern __p = __intl ? __mpt.neg_format() 
1072                                              : __mpf.neg_format();
1073
1074       const string_type __pos_sign =__intl ? __mpt.positive_sign() 
1075                                            : __mpf.positive_sign();
1076       const string_type __neg_sign =__intl ? __mpt.negative_sign() 
1077                                            : __mpf.negative_sign();
1078       const char_type __d = __intl ? __mpt.decimal_point() 
1079                                    : __mpf.decimal_point();
1080       const char_type __sep = __intl ? __mpt.thousands_sep() 
1081                                      : __mpf.thousands_sep();
1082
1083       const string __grouping = __intl ? __mpt.grouping() : __mpf.grouping();
1084
1085       // Set to deduced positive or negative sign, depending.
1086       string_type __sign;
1087       // String of grouping info from thousands_sep plucked from __units.
1088       string __grouping_tmp; 
1089       // Marker for thousands_sep position.
1090       int __sep_pos = 0;
1091       // If input iterator is in a valid state.
1092       bool __testvalid = true;
1093       // Flag marking when a decimal point is found.
1094       bool __testdecfound = false; 
1095
1096       char_type __c = *__beg;
1097       char_type __eof = static_cast<char_type>(char_traits<char_type>::eof());
1098       for (int __i = 0; __beg != __end && __i < 4 && __testvalid; ++__i)
1099         {
1100           part __which = static_cast<part>(__p.field[__i]);
1101           switch (__which)
1102                 {
1103                 case money_base::symbol:
1104                   if (__io.flags() & ios_base::showbase)
1105                     {
1106                       // Symbol is required.
1107                       const string_type __symbol = __intl ? __mpt.curr_symbol()
1108                                                          : __mpf.curr_symbol();
1109                       size_type __len = __symbol.size();
1110                       size_type __i = 0;
1111                       while (__beg != __end 
1112                              && __i < __len && __symbol[__i] == __c)
1113                         {
1114                           __c = *(++__beg);
1115                           ++__i;
1116                         }
1117                       if (__i != __len)
1118                         __testvalid = false;
1119                     }
1120                   break;
1121                 case money_base::sign:              
1122                   // Sign might not exist, or be more than one character long. 
1123                   if (__pos_sign.size() && __neg_sign.size())
1124                   {
1125                     // Sign is mandatory.
1126                     if (__c == __pos_sign[0])
1127                       {
1128                         __sign = __pos_sign;
1129                         __c = *(++__beg);
1130                       }
1131                     else if (__c == __neg_sign[0])
1132                       {
1133                         __sign = __neg_sign;
1134                         __c = *(++__beg);
1135                       }
1136                     else
1137                       __testvalid = false;
1138                   }
1139                   else if (__pos_sign.size() && __c == __pos_sign[0])
1140                     {
1141                       __sign = __pos_sign;
1142                       __c = *(++__beg);
1143                     }
1144                   else if (__neg_sign.size() && __c == __neg_sign[0])
1145                     {
1146                       __sign = __neg_sign;
1147                       __c = *(++__beg);
1148                     }
1149                   break;
1150                 case money_base::value:
1151                   // Extract digits, remove and stash away the
1152                   // grouping of found thousands separators.
1153                   while (__beg != __end 
1154                          && (__ctype.is(ctype_base::digit, __c) 
1155                              || (__c == __d && !__testdecfound)
1156                              || __c == __sep))
1157                     {
1158                       if (__c == __d)
1159                         {
1160                           __grouping_tmp += static_cast<char>(__sep_pos);
1161                           __sep_pos = 0;
1162                           __testdecfound = true;
1163                         }
1164                       else if (__c == __sep)
1165                         {
1166                           if (__grouping.size())
1167                             {
1168                               // Mark position for later analysis.
1169                               __grouping_tmp += static_cast<char>(__sep_pos);
1170                               __sep_pos = 0;
1171                             }
1172                           else
1173                             {
1174                               __testvalid = false;
1175                               break;
1176                             }
1177                         }
1178                       else
1179                         {
1180                           __units += __c;
1181                           ++__sep_pos;
1182                         }
1183                       __c = *(++__beg);
1184                     }
1185                   break;
1186                 case money_base::space:
1187                 case money_base::none:
1188                   // Only if not at the end of the pattern.
1189                   if (__i != 3)
1190                     while (__beg != __end 
1191                            && __ctype.is(ctype_base::space, __c))
1192                       __c = *(++__beg);
1193                   break;
1194                 }
1195         }
1196
1197       // Need to get the rest of the sign characters, if they exist.
1198       if (__sign.size() > 1)
1199         {
1200           size_type __len = __sign.size();
1201           size_type __i = 1;
1202           for (; __c != __eof && __i < __len; ++__i)
1203             while (__beg != __end && __c != __sign[__i])
1204               __c = *(++__beg);
1205           
1206           if (__i != __len)
1207             __testvalid = false;
1208         }
1209
1210       // Strip leading zeros.
1211       while (__units[0] == __ctype.widen('0'))
1212         __units.erase(__units.begin());
1213
1214       if (__sign == __neg_sign)
1215         __units.insert(__units.begin(), __ctype.widen('-'));
1216
1217       // Test for grouping fidelity.
1218       if (__grouping.size() && __grouping_tmp.size())
1219         {
1220           if (!__verify_grouping(__grouping, __grouping_tmp))
1221             __testvalid = false;
1222         }
1223
1224       // Iff no more characters are available.      
1225       if (__c == __eof)
1226         __err |= ios_base::eofbit;
1227
1228       // Iff valid sequence is not recognized.
1229       if (!__testvalid || !__units.size())
1230         __err |= ios_base::failbit;
1231       return __beg; 
1232     }
1233
1234   template<typename _CharT, typename _OutIter>
1235     _OutIter
1236     money_put<_CharT, _OutIter>::
1237     do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1238            long double __units) const
1239     { 
1240       const locale __loc = __io.getloc();
1241       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1242       const int __n = numeric_limits<long double>::digits10;
1243       char* __cs = static_cast<char*>(__builtin_alloca(sizeof(char) * __n));
1244       _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __n));
1245       int __len = sprintf(__cs, "%.01Lf", __units);
1246       __ctype.widen(__cs, __cs + __len, __ws);
1247       string_type __digits(__ws);
1248       return this->do_put(__s, __intl, __io, __fill, __digits); 
1249     }
1250
1251   template<typename _CharT, typename _OutIter>
1252     _OutIter
1253     money_put<_CharT, _OutIter>::
1254     do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1255            const string_type& __digits) const
1256     { 
1257       typedef typename string_type::size_type   size_type;
1258       typedef money_base::part                  part;
1259
1260       const locale __loc = __io.getloc();
1261       const size_type __width = static_cast<size_type>(__io.width());
1262
1263       // These contortions are quite unfortunate.
1264       typedef moneypunct<_CharT, true> __money_true;
1265       typedef moneypunct<_CharT, false> __money_false;
1266       const __money_true& __mpt = use_facet<__money_true>(__loc); 
1267       const __money_false& __mpf = use_facet<__money_false>(__loc); 
1268       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1269
1270       // Determine if negative or positive formats are to be used, and
1271       // discard leading negative_sign if it is present.
1272       const char_type* __beg = __digits.data();
1273       const char_type* __end = __beg + __digits.size();
1274       money_base::pattern __p;
1275       string_type __sign;
1276       if (*__beg != __ctype.widen('-'))
1277         {
1278           __p = __intl ? __mpt.pos_format() : __mpf.pos_format();
1279           __sign =__intl ? __mpt.positive_sign() : __mpf.positive_sign();
1280         }
1281       else
1282         {
1283           __p = __intl ? __mpt.neg_format() : __mpf.neg_format();
1284           __sign =__intl ? __mpt.negative_sign() : __mpf.negative_sign();
1285           ++__beg;
1286         }
1287       
1288       // Look for valid numbers in the current ctype facet within input digits.
1289       __end = __ctype.scan_not(ctype_base::digit, __beg, __end);
1290       if (__beg != __end)
1291         {
1292           // Assume valid input, and attempt to format.
1293           // Break down input numbers into base components, as follows:
1294           //   final_value = grouped units + (decimal point) + (digits)
1295           string_type __res;
1296           string_type __value;
1297           const string_type __symbol = __intl ? __mpt.curr_symbol() 
1298                                               : __mpf.curr_symbol();
1299
1300           // Deal with decimal point, decimal digits.
1301           const int __frac = __intl ? __mpt.frac_digits() 
1302                                     : __mpf.frac_digits();
1303           if (__frac > 0)
1304             {
1305               const char_type __d = __intl ? __mpt.decimal_point() 
1306                                            : __mpf.decimal_point();
1307               if (__end - __beg >= __frac)
1308                 {
1309                   __value = string_type(__end - __frac, __end);
1310                   __value.insert(__value.begin(), __d);
1311                   __end -= __frac;
1312                 }
1313               else
1314                 {
1315                   // Have to pad zeros in the decimal position.
1316                   __value = string_type(__beg, __end);
1317                   int __paddec = __frac - (__end - __beg);
1318                   char_type __zero = __ctype.widen('0');
1319                   __value.insert(__value.begin(), __paddec, __zero);
1320                   __value.insert(__value.begin(), __d);
1321                   __beg = __end;
1322                 }
1323             }
1324
1325           // Add thousands separators to non-decimal digits, per
1326           // grouping rules.
1327           if (__beg != __end)
1328             {
1329               const string __grouping = __intl ? __mpt.grouping() 
1330                                                : __mpf.grouping();
1331               if (__grouping.size())
1332                 {
1333                   const char_type __sep = __intl ? __mpt.thousands_sep() 
1334                                                  : __mpf.thousands_sep();
1335                   const char* __gbeg = __grouping.c_str();
1336                   const char* __gend = __gbeg + __grouping.size();
1337                   const int __n = numeric_limits<long double>::digits10 * 2;
1338                   _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __n));
1339                   _CharT* __ws_end = __add_grouping(__ws2, __sep, __gbeg, 
1340                                                     __gend, __beg, __end);
1341                   __value.insert(0, __ws2, __ws_end - __ws2);
1342                 }
1343               else
1344                 __value.insert(0, string_type(__beg, __end));
1345             }
1346
1347           // Calculate length of resulting string.
1348           ios_base::fmtflags __f = __io.flags() & ios_base::adjustfield;
1349           size_type __len = __value.size() + __sign.size();
1350           __len += (__io.flags() & ios_base::showbase) ? __symbol.size() : 0;
1351           bool __testipad = __f == ios_base::internal && __len < __width;
1352
1353           // Fit formatted digits into the required pattern.
1354           for (int __i = 0; __i < 4; ++__i)
1355             {
1356               part __which = static_cast<part>(__p.field[__i]);
1357               switch (__which)
1358                 {
1359                 case money_base::symbol:
1360                   if (__io.flags() & ios_base::showbase)
1361                     __res += __symbol;
1362                   break;
1363                 case money_base::sign:              
1364                   // Sign might not exist, or be more than one
1365                   // charater long. In that case, add in the rest
1366                   // below.
1367                   if (__sign.size())
1368                     __res += __sign[0];
1369                   break;
1370                 case money_base::value:
1371                   __res += __value;
1372                   break;
1373                 case money_base::space:
1374                   // At least one space is required, but if internal
1375                   // formatting is required, an arbitrary number of
1376                   // fill spaces will be necessary.
1377                   if (__testipad)
1378                     __res += string_type(__width - __len, __fill);
1379                   else
1380                     __res += __ctype.widen(' ');
1381                   break;
1382                 case money_base::none:
1383                   if (__testipad)
1384                     __res += string_type(__width - __len, __fill);
1385                   break;
1386                 }
1387             }
1388
1389           // Special case of multi-part sign parts.
1390           if (__sign.size() > 1)
1391             __res += string_type(__sign.begin() + 1, __sign.end());
1392
1393           // Pad, if still necessary.
1394           __len = __res.size();
1395           if (__width > __len)
1396             {
1397               if (__f == ios_base::left)
1398                 // After.
1399                 __res.append(__width - __len, __fill);
1400               else
1401                 // Before.
1402                 __res.insert(0, string_type(__width - __len, __fill));
1403               __len = __width;
1404             }
1405
1406           // Write resulting, fully-formatted string to output iterator.
1407           for (size_type __j = 0; __j < __len; ++__j)
1408             __s = __res[__j];
1409         }
1410       __io.width(0);
1411       return __s; 
1412     }
1413
1414
1415   // NB: Not especially useful. Without an ios_base object or some
1416   // kind of locale reference, we are left clawing at the air where
1417   // the side of the mountain used to be...
1418   template<typename _CharT, typename _InIter>
1419     time_base::dateorder
1420     time_get<_CharT, _InIter>::do_date_order() const
1421     { return time_base::no_order; }
1422
1423   template<typename _CharT, typename _InIter>
1424     void
1425     time_get<_CharT, _InIter>::
1426     _M_extract_via_format(iter_type& __beg, iter_type& __end, ios_base& __io,
1427                           ios_base::iostate& __err, tm* __tm, 
1428                           const _CharT* __format) const
1429     {  
1430       locale __loc = __io.getloc();
1431       __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1432       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1433       size_t __len = char_traits<_CharT>::length(__format);
1434
1435       for (size_t __i = 0; __beg != __end && __i < __len && !__err; ++__i)
1436         {
1437           char __c = __format[__i];
1438           if (__c == '%')
1439             {
1440               // Verify valid formatting code, attempt to extract.
1441               __c = __format[++__i];
1442               char __mod = 0;
1443               int __mem = 0; 
1444               if (__c == 'E' || __c == 'O')
1445                 {
1446                   __mod = __c;
1447                   __c = __format[++__i];
1448                 }
1449               switch (__c)
1450                 {
1451                   const char* __cs;
1452                   _CharT __wcs[10];
1453                 case 'a':
1454                   // Abbreviated weekday name [tm_wday]
1455                   const char_type*  __days1[7];
1456                   __tp._M_days_abbreviated(__days1);
1457                   _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 7, 
1458                                   __err);
1459                   break;
1460                 case 'A':
1461                   // Weekday name [tm_wday].
1462                   const char_type*  __days2[7];
1463                   __tp._M_days(__days2);
1464                   _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 7, 
1465                                   __err);
1466                   break;
1467                 case 'h':
1468                 case 'b':
1469                   // Abbreviated month name [tm_mon]
1470                   const char_type*  __months1[12];
1471                   __tp._M_months_abbreviated(__months1);
1472                   _M_extract_name(__beg, __end, __tm->tm_mon, __months1, 12, 
1473                                   __err);
1474                   break;
1475                 case 'B':
1476                   // Month name [tm_mon].
1477                   const char_type*  __months2[12];
1478                   __tp._M_months(__months2);
1479                   _M_extract_name(__beg, __end, __tm->tm_mon, __months2, 12, 
1480                                   __err);
1481                   break;
1482                 case 'c':
1483                   // Default time and date representation.
1484                   const char_type*  __dt[2];
1485                   __tp._M_date_time_formats(__dt);
1486                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1487                                         __dt[0]);
1488                   break;
1489                 case 'd':
1490                   // Day [01, 31]. [tm_mday]
1491                   _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2, 
1492                                  __ctype, __err);
1493                   break;
1494                 case 'D':
1495                   // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
1496                   __cs = "%m/%d/%y";
1497                   __ctype.widen(__cs, __cs + 9, __wcs);
1498                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1499                                         __wcs);
1500                   break;
1501                 case 'H':
1502                   // Hour [00, 23]. [tm_hour]
1503                   _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
1504                                  __ctype, __err);
1505                   break;
1506                 case 'I':
1507                   // Hour [01, 12]. [tm_hour]
1508                   _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2,
1509                                  __ctype, __err);
1510                   break;
1511                 case 'm':
1512                   // Month [01, 12]. [tm_mon]
1513                   _M_extract_num(__beg, __end, __mem, 1, 12, 2,
1514                                  __ctype, __err);
1515                   if (!__err)
1516                     __tm->tm_mon = __mem - 1;
1517                   break;
1518                 case 'M':
1519                   // Minute [00, 59]. [tm_min]
1520                   _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
1521                                  __ctype, __err);
1522                   break;
1523                 case 'n':
1524                   if (__ctype.narrow(*__beg, 0) == '\n')
1525                     ++__beg;
1526                   else
1527                     __err |= ios_base::failbit;
1528                   break;
1529                 case 'R':
1530                   // Equivalent to (%H:%M).
1531                   __cs = "%H:%M";
1532                   __ctype.widen(__cs, __cs + 6, __wcs);
1533                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1534                                         __wcs);
1535                   break;
1536                 case 'S':
1537                   // Seconds.
1538                   _M_extract_num(__beg, __end, __tm->tm_sec, 0, 59, 2,
1539                                  __ctype, __err);
1540                   break;
1541                 case 't':
1542                   if (__ctype.narrow(*__beg, 0) == '\t')
1543                     ++__beg;
1544                   else
1545                 __err |= ios_base::failbit;
1546                   break;
1547                 case 'T':
1548                   // Equivalent to (%H:%M:%S).
1549                   __cs = "%H:%M:%S";
1550                   __ctype.widen(__cs, __cs + 9, __wcs);
1551                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1552                                         __wcs);
1553                   break;
1554                 case 'x':
1555                   // Locale's date.
1556                   const char_type*  __dates[2];
1557                   __tp._M_date_formats(__dates);
1558                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1559                                         __dates[0]);
1560                   break;
1561                 case 'X':
1562                   // Locale's time.
1563                   const char_type*  __times[2];
1564                   __tp._M_time_formats(__times);
1565                   _M_extract_via_format(__beg, __end, __io, __err, __tm, 
1566                                         __times[0]);
1567                   break;
1568                 case 'y':
1569                   // Two digit year. [tm_year]
1570                   _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2, 
1571                                  __ctype, __err);
1572                   break;
1573                 case 'Y':
1574                   // Year [1900). [tm_year]
1575                   _M_extract_num(__beg, __end, __mem, 0, 
1576                                  numeric_limits<int>::max(), 4, 
1577                                  __ctype, __err);
1578                   if (!__err)
1579                     __tm->tm_year = __mem - 1900;
1580                   break;
1581                 case 'Z':
1582                   // Timezone info.
1583                   if (__ctype.is(ctype_base::upper, *__beg))
1584                     {
1585                       int __tmp;
1586                       _M_extract_name(__beg, __end, __tmp, 
1587                                       __timepunct<_CharT>::_S_timezones, 
1588                                       14, __err);
1589                       
1590                       // GMT requires special effort.
1591                       char_type __c = *__beg;
1592                       if (!__err && __tmp == 0 
1593                           && (__c == __ctype.widen('-') 
1594                               || __c == __ctype.widen('+')))
1595                         {
1596                           _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
1597                                           __ctype, __err);
1598                           _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
1599                                           __ctype, __err);
1600                         }           
1601                           }
1602                       else
1603                         __err |= ios_base::failbit;
1604                       break;
1605                     default:
1606                       // Not recognized.
1607                       __err |= ios_base::failbit;
1608                     }
1609                 }
1610               else
1611                 {
1612                   // Verify format and input match, extract and discard.
1613                   if (__c == __ctype.narrow(*__beg, 0))
1614                     ++__beg;
1615                   else
1616                     __err |= ios_base::failbit;
1617                 }
1618         }
1619     }
1620
1621   template<typename _CharT, typename _InIter>
1622     void
1623     time_get<_CharT, _InIter>::
1624     _M_extract_num(iter_type& __beg, iter_type& __end, int& __member,
1625                    int __min, int __max, size_t __len, 
1626                    const ctype<_CharT>& __ctype, 
1627                    ios_base::iostate& __err) const
1628     {
1629       size_t __i = 0;
1630       string __digits;
1631       bool __testvalid = true;
1632       char_type __c = *__beg;
1633       while (__beg != __end && __i < __len 
1634              && __ctype.is(ctype_base::digit, __c)) 
1635         {
1636           __digits += __ctype.narrow(__c, 0);
1637           __c = *(++__beg);
1638           ++__i;
1639         }
1640       if (__i == __len)
1641         {
1642           int __value = atoi(__digits.c_str());
1643           if (__min <= __value && __value <= __max)
1644             __member = __value;
1645           else
1646             __testvalid = false;
1647         }
1648       else
1649         __testvalid = false;
1650       if (!__testvalid)
1651         __err |= ios_base::failbit;
1652     }
1653
1654   // Assumptions:
1655   // All elements in __names are unique.
1656   template<typename _CharT, typename _InIter>
1657     void
1658     time_get<_CharT, _InIter>::
1659     _M_extract_name(iter_type& __beg, iter_type& __end, int& __member,
1660                     const _CharT** __names, size_t __indexlen, 
1661                     ios_base::iostate& __err) const
1662     {
1663       typedef char_traits<char_type> __traits_type;
1664       int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int) * __indexlen));
1665       size_t __nmatches = 0;
1666       size_t __pos = 0;
1667       bool __testvalid = true;
1668       const char_type* __name;
1669
1670       char_type __c = *__beg;
1671       // Look for initial matches.
1672       for (size_t __i1 = 0; __i1 < __indexlen; ++__i1)
1673         if (__c == __names[__i1][0])
1674           __matches[__nmatches++] = __i1;
1675       
1676       while(__nmatches > 1)
1677         {
1678           // Find smallest matching string.
1679           size_t __minlen = 10;
1680           for (size_t __i2 = 0; __i2 < __nmatches; ++__i2)
1681             __minlen = min(__minlen, 
1682                            __traits_type::length(__names[__matches[__i2]]));
1683           
1684           if (__pos < __minlen && __beg != __end)
1685             {
1686               ++__pos;
1687               __c = *(++__beg);
1688               for (size_t __i3 = 0; __i3 < __nmatches; ++__i3)
1689                 {
1690                   __name = __names[__matches[__i3]];
1691                   if (__name[__pos] != __c)
1692                     __matches[__i3] = __matches[--__nmatches];
1693                 }
1694             }
1695           else
1696             break;
1697         }
1698
1699       if (__nmatches == 1)
1700         {
1701           // Make sure found name is completely extracted.
1702           __name = __names[__matches[0]];
1703           const size_t __len = __traits_type::length(__name);
1704           while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
1705             ++__beg, ++__pos;
1706
1707           if (__len == __pos)
1708             __member = __matches[0];
1709           else
1710             __testvalid = false;
1711         }
1712       else
1713         __testvalid = false;
1714       if (!__testvalid)
1715         __err |= ios_base::failbit;
1716     }
1717
1718   template<typename _CharT, typename _InIter>
1719     _InIter
1720     time_get<_CharT, _InIter>::
1721     do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
1722                 ios_base::iostate& __err, tm* __tm) const
1723     {
1724       _CharT __wcs[3];
1725       const char* __cs = "%X";
1726       locale __loc = __io.getloc();
1727       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1728       __ctype.widen(__cs, __cs + 3, __wcs);
1729       _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1730       if (__beg == __end)
1731         __err |= ios_base::eofbit;
1732       return __beg;
1733     }
1734
1735   template<typename _CharT, typename _InIter>
1736     _InIter
1737     time_get<_CharT, _InIter>::
1738     do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
1739                 ios_base::iostate& __err, tm* __tm) const
1740     {
1741       _CharT __wcs[3];
1742       const char* __cs = "%x";
1743       locale __loc = __io.getloc();
1744       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1745       __ctype.widen(__cs, __cs + 3, __wcs);
1746       _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1747       if (__beg == __end)
1748         __err |= ios_base::eofbit;
1749       return __beg;
1750     }
1751
1752   template<typename _CharT, typename _InIter>
1753     _InIter
1754     time_get<_CharT, _InIter>::
1755     do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io, 
1756                    ios_base::iostate& __err, tm* __tm) const
1757     {
1758       typedef char_traits<char_type> __traits_type;
1759       locale __loc = __io.getloc();
1760       __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1761       const char_type*  __days[7];
1762       __tp._M_days_abbreviated(__days);
1763       int __tmpwday;
1764       _M_extract_name(__beg, __end, __tmpwday, __days, 7, __err);
1765
1766       // Check to see if non-abbreviated name exists, and extract.
1767       // NB: Assumes both _M_days and _M_days_abbreviated organized in
1768       // exact same order, first to last, such that the resulting
1769       // __days array with the same index points to a day, and that
1770       // day's abbreviated form.
1771       // NB: Also assumes that an abbreviated name is a subset of the name. 
1772       if (!__err)
1773         {
1774           size_t __pos = __traits_type::length(__days[__tmpwday]);
1775           __tp._M_days(__days);
1776           const char_type* __name = __days[__tmpwday];
1777           if (__name[__pos] == *__beg)
1778             {
1779               // Extract the rest of it.
1780               const size_t __len = __traits_type::length(__name);
1781               while (__pos < __len && __beg != __end 
1782                      && __name[__pos] == *__beg)
1783                 ++__beg, ++__pos;
1784               if (__len != __pos)
1785                 __err |= ios_base::failbit;
1786             }
1787           if (!__err)
1788             __tm->tm_wday = __tmpwday;
1789         }
1790       if (__beg == __end)
1791         __err |= ios_base::eofbit;
1792       return __beg;
1793      }
1794
1795   template<typename _CharT, typename _InIter>
1796     _InIter
1797     time_get<_CharT, _InIter>::
1798     do_get_monthname(iter_type __beg, iter_type __end,
1799                      ios_base& __io, ios_base::iostate& __err, tm* __tm) const
1800     {
1801       typedef char_traits<char_type> __traits_type;
1802       locale __loc = __io.getloc();
1803       __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1804       const char_type*  __months[12];
1805       __tp._M_months_abbreviated(__months);
1806       int __tmpmon;
1807       _M_extract_name(__beg, __end, __tmpmon, __months, 12, __err);
1808
1809       // Check to see if non-abbreviated name exists, and extract.
1810       // NB: Assumes both _M_months and _M_months_abbreviated organized in
1811       // exact same order, first to last, such that the resulting
1812       // __months array with the same index points to a month, and that
1813       // month's abbreviated form.
1814       // NB: Also assumes that an abbreviated name is a subset of the name. 
1815       if (!__err)
1816         {
1817           size_t __pos = __traits_type::length(__months[__tmpmon]);
1818           __tp._M_months(__months);
1819           const char_type* __name = __months[__tmpmon];
1820           if (__name[__pos] == *__beg)
1821             {
1822               // Extract the rest of it.
1823               const size_t __len = __traits_type::length(__name);
1824               while (__pos < __len && __beg != __end 
1825                      && __name[__pos] == *__beg)
1826                 ++__beg, ++__pos;
1827               if (__len != __pos)
1828                 __err |= ios_base::failbit;
1829             }
1830           if (!__err)
1831             __tm->tm_mon = __tmpmon;
1832         }
1833  
1834       if (__beg == __end)
1835         __err |= ios_base::eofbit;
1836       return __beg;
1837     }
1838
1839   template<typename _CharT, typename _InIter>
1840     _InIter
1841     time_get<_CharT, _InIter>::
1842     do_get_year(iter_type __beg, iter_type __end, ios_base& __io, 
1843                 ios_base::iostate& __err, tm* __tm) const
1844     {
1845       locale __loc = __io.getloc();
1846       const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
1847
1848       char_type __c = *__beg;
1849       size_t __i = 0;
1850       string __digits;
1851       while (__i < 4 && __beg != __end && __ctype.is(ctype_base::digit, __c))
1852         {
1853           __digits += __ctype.narrow(__c, 0);
1854           __c = *(++__beg);
1855           ++__i;
1856         }
1857       if (__i == 2 || __i == 4)
1858         {
1859           int __year = atoi(__digits.c_str());
1860           __year = __i == 2 ? __year : __year - 1900; 
1861           __tm->tm_year = __year;
1862         }
1863       else
1864         __err |= ios_base::failbit;
1865       if (__beg == __end)
1866         __err |= ios_base::eofbit;
1867       return __beg;
1868     }
1869
1870   template<typename _CharT, typename _OutIter>
1871     _OutIter
1872     time_put<_CharT, _OutIter>::
1873     put(iter_type __s, ios_base& __io, char_type, const tm* __tm, 
1874         const _CharT* __beg, const _CharT* __end) const
1875     {
1876       locale __loc = __io.getloc();
1877       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1878       while (__beg != __end)
1879         {
1880           char __c = __ctype.narrow(*__beg, 0);
1881           ++__beg;
1882           if (__c == '%')
1883             {
1884               char __format;
1885               char __mod = 0;
1886               size_t __len = 1; 
1887               __c = __ctype.narrow(*__beg, 0);
1888               ++__beg;
1889               if (__c == 'E' || __c == 'O')
1890                 {
1891                   __mod = __c;
1892                   __format = __ctype.narrow(*__beg, 0);
1893                   ++__beg;
1894                 }
1895               else
1896                 __format = __c;
1897               this->do_put(__s, __io, char_type(), __tm, __format, __mod);
1898             }
1899           else
1900             __s = __c;
1901         }
1902       return __s;
1903     }
1904
1905   template<typename _CharT, typename _OutIter>
1906     _OutIter
1907     time_put<_CharT, _OutIter>::
1908     do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm, 
1909            char __format, char __mod) const
1910     { 
1911       locale __loc = __io.getloc();
1912       ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1913       __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1914
1915       // NB: This size is arbitrary. Should this be a data member,
1916       // initialized at construction?
1917       const size_t __maxlen = 64;
1918       char_type* __res = static_cast<char_type*>(__builtin_alloca(__maxlen));
1919
1920       // NB: In IEE 1003.1-200x, and perhaps other locale models, it
1921       // is possible that the format character will be longer than one
1922       // character. Possibilities include 'E' or 'O' followed by a
1923       // format character: if __mod is not the default argument, assume
1924       // it's a valid modifier.
1925       char_type __fmt[4];
1926       __fmt[0] = __ctype.widen('%'); 
1927       if (!__mod)
1928         {
1929           __fmt[1] = __format;
1930           __fmt[2] = char_type();
1931         }
1932       else
1933         {
1934           __fmt[1] = __mod;
1935           __fmt[2] = __format;
1936           __fmt[3] = char_type();
1937         }
1938
1939       __tp._M_put_helper(__res, __maxlen, __fmt, __tm);
1940
1941       // Write resulting, fully-formatted string to output iterator.
1942       size_t __len = char_traits<char_type>::length(__res);
1943       for (size_t __i = 0; __i < __len; ++__i)
1944         __s = __res[__i];
1945       return __s;
1946     }
1947
1948
1949   // Generic version does nothing.
1950   template<typename _CharT>
1951     int
1952     collate<_CharT>::_M_compare_helper(const _CharT*, const _CharT*) const
1953     { return 0; }
1954
1955   // Generic version does nothing.
1956   template<typename _CharT>
1957     size_t
1958     collate<_CharT>::_M_transform_helper(_CharT*, const _CharT*, size_t) const
1959     { return 0; }
1960
1961   template<typename _CharT>
1962     int
1963     collate<_CharT>::
1964     do_compare(const _CharT* __lo1, const _CharT* __hi1, 
1965                const _CharT* __lo2, const _CharT* __hi2) const
1966     { 
1967       const string_type __one(__lo1, __hi1);
1968       const string_type __two(__lo2, __hi2);
1969       return _M_compare_helper(__one.c_str(), __two.c_str());
1970     }
1971
1972  template<typename _CharT>
1973     collate<_CharT>::string_type
1974     collate<_CharT>::
1975     do_transform(const _CharT* __lo, const _CharT* __hi) const
1976     {
1977       size_t __len = __hi - __lo;
1978       _CharT* __c = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len));
1979       size_t __res = _M_transform_helper(__c, __lo, __len);
1980       if (__res >= __len)
1981         {
1982           // Try to increment size of translated string.
1983           size_t __len2 = __len * 2;
1984           _CharT* __c2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len2));
1985           __res = _M_transform_helper(__c2, __lo, __len);
1986           // XXX Throw exception if still indeterminate?
1987         }
1988       return string_type(__c);
1989     }
1990
1991  template<typename _CharT>
1992     long
1993     collate<_CharT>::
1994     do_hash(const _CharT* __lo, const _CharT* __hi) const
1995     { 
1996       unsigned long __val = 0;
1997       for (; __lo < __hi; ++__lo)
1998         __val = *__lo + ((__val << 7) | 
1999                        (__val >> (numeric_limits<unsigned long>::digits - 1)));
2000       return static_cast<long>(__val);
2001     }
2002
2003   // Construct correctly padded string, as per 22.2.2.2.2
2004   // Assumes 
2005   // __newlen > __oldlen
2006   // __news is allocated for __newlen size
2007   // Used by both num_put and ostream inserters: if __num,
2008   // internal-adjusted objects are padded according to the rules below
2009   // concerning 0[xX] and +-, otherwise, exactly as right-adjusted
2010   // ones are.
2011   template<typename _CharT, typename _Traits>
2012     void
2013     __pad(ios_base& __io, _CharT __fill, _CharT* __news, const _CharT* __olds,
2014           const streamsize __newlen, const streamsize __oldlen, 
2015           const bool __num)
2016     {
2017       typedef _CharT    char_type;
2018       typedef _Traits   traits_type;
2019       typedef typename traits_type::int_type int_type;
2020       
2021       int_type __plen = static_cast<size_t>(__newlen - __oldlen); 
2022       char_type* __pads = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __plen));
2023       traits_type::assign(__pads, __plen, __fill); 
2024
2025       char_type* __beg;
2026       char_type* __end;
2027       size_t __mod = 0;
2028       size_t __beglen; //either __plen or __oldlen
2029       ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
2030
2031       if (__adjust == ios_base::left)
2032         {
2033           // Padding last.
2034           __beg = const_cast<char_type*>(__olds);
2035           __beglen = __oldlen;
2036           __end = __pads;
2037         }
2038       else if (__adjust == ios_base::internal && __num)
2039         {
2040           // Pad after the sign, if there is one.
2041           // Pad after 0[xX], if there is one.
2042           // Who came up with these rules, anyway? Jeeze.
2043           locale __loc = __io.getloc();
2044           const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 
2045           const char_type __minus = __ctype.widen('-');
2046           const char_type __plus = __ctype.widen('+');
2047           bool __testsign = __olds[0] == __minus || __olds[0] == __plus;
2048           bool __testhex = __ctype.widen('0') == __olds[0] 
2049                            && (__ctype.widen('x') == __olds[1] 
2050                                || __ctype.widen('X') == __olds[1]);
2051           if (__testhex)
2052             {
2053               __news[0] = __olds[0]; 
2054               __news[1] = __olds[1];
2055               __mod += 2;
2056               __news += 2;
2057               __beg = __pads;
2058               __beglen = __plen;
2059               __end = const_cast<char_type*>(__olds + __mod);
2060             }
2061           else if (__testsign)
2062             {
2063               __news[0] = __olds[0] == __plus ? __plus : __minus;
2064               ++__mod;
2065               ++__news;
2066               __beg = __pads;
2067               __beglen = __plen;
2068               __end = const_cast<char_type*>(__olds + __mod);
2069             }
2070           else
2071             {
2072               // Padding first.
2073               __beg = __pads;
2074               __beglen = __plen;
2075               __end = const_cast<char_type*>(__olds);
2076             }
2077         }
2078       else
2079         {
2080           // Padding first.
2081           __beg = __pads;
2082           __beglen = __plen;
2083           __end = const_cast<char_type*>(__olds);
2084         }
2085       traits_type::copy(__news, __beg, __beglen);
2086       traits_type::copy(__news + __beglen, __end, __newlen - __beglen - __mod);
2087     }
2088
2089   // NB: Can't have default argument on non-member template, and
2090   // num_put doesn't have a _Traits template parameter, so this
2091   // forwarding template adds in the default template argument.
2092   template<typename _CharT>
2093     void
2094     __pad(ios_base& __io, _CharT __fill, _CharT* __news, const _CharT* __olds,
2095           const streamsize __newlen, const streamsize __oldlen, 
2096           const bool __num)
2097     { 
2098       return __pad<_CharT, char_traits<_CharT> >(__io, __fill, __news, __olds,
2099                                                  __newlen, __oldlen, __num); 
2100     }
2101
2102   // Used by both numeric and monetary facets.
2103   // Check to make sure that the __grouping_tmp string constructed in
2104   // money_get or num_get matches the canonical grouping for a given
2105   // locale.
2106   // __grouping_tmp is parsed L to R
2107   // 1,222,444 == __grouping_tmp of "/1/3/3"
2108   // __grouping is parsed R to L
2109   // 1,222,444 == __grouping of "/3" == "/3/3/3"
2110   template<typename _CharT>
2111     bool
2112     __verify_grouping(const basic_string<_CharT>& __grouping, 
2113                       basic_string<_CharT>& __grouping_tmp)
2114     {         
2115       int __i = 0;
2116       int __j = 0;
2117       const int __len = __grouping.size();
2118       const int __n = __grouping_tmp.size();
2119       bool __test = true;
2120       
2121       // Parsed number groupings have to match the
2122       // numpunct::grouping string exactly, starting at the
2123       // right-most point of the parsed sequence of elements ...
2124       while (__test && __i < __n - 1)
2125         for (__j = 0; __test && __j < __len && __i < __n - 1; ++__j,++__i)
2126           __test &= __grouping[__j] == __grouping_tmp[__n - __i - 1];
2127       // ... but the last parsed grouping can be <= numpunct
2128       // grouping.
2129       __j == __len ? __j = 0 : __j;
2130       __test &= __grouping[__j] >= __grouping_tmp[__n - __i - 1];
2131       return __test;
2132     }
2133
2134   // Used by both numeric and monetary facets.
2135   // Inserts "group separator" characters into an array of characters.
2136   // It's recursive, one iteration per group.  It moves the characters
2137   // in the buffer this way: "xxxx12345" -> "12,345xxx".  Call this
2138   // only with __gbeg != __gend.
2139   template<typename _CharT>
2140     _CharT*
2141     __add_grouping(_CharT* __s, _CharT __sep,  
2142                    const char* __gbeg, const char* __gend, 
2143                    const _CharT* __first, const _CharT* __last)
2144     {
2145       if (__last - __first > *__gbeg)
2146         {
2147           __s = __add_grouping(__s,  __sep, 
2148                                (__gbeg + 1 == __gend ? __gbeg : __gbeg + 1),
2149                                __gend, __first, __last - *__gbeg);
2150           __first = __last - *__gbeg;
2151           *__s++ = __sep;
2152         }
2153       do
2154         *__s++ = *__first++;
2155       while (__first != __last);
2156       return __s;
2157     }
2158 } // namespace std
2159
2160 #endif