OSDN Git Service

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