Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / libstdc++-v3 / include / bits / locale_facets.tcc
blob93bc6d48366078cf47e7b63bc45e34a0dee07bef
1 // Locale support -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING.  If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction.  Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License.  This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 /** @file locale_facets.tcc
33  *  This is an internal header file, included by other library headers.
34  *  You should not attempt to use it directly.
35  */
37 #ifndef _LOCALE_FACETS_TCC
38 #define _LOCALE_FACETS_TCC 1
40 #pragma GCC system_header
42 _GLIBCXX_BEGIN_NAMESPACE(std)
44   // Routine to access a cache for the facet.  If the cache didn't
45   // exist before, it gets constructed on the fly.
46   template<typename _Facet>
47     struct __use_cache
48     {
49       const _Facet*
50       operator() (const locale& __loc) const;
51     };
53   // Specializations.
54   template<typename _CharT>
55     struct __use_cache<__numpunct_cache<_CharT> >
56     {
57       const __numpunct_cache<_CharT>*
58       operator() (const locale& __loc) const
59       {
60         const size_t __i = numpunct<_CharT>::id._M_id();
61         const locale::facet** __caches = __loc._M_impl->_M_caches;
62         if (!__caches[__i])
63           {
64             __numpunct_cache<_CharT>* __tmp = NULL;
65             __try
66               {
67                 __tmp = new __numpunct_cache<_CharT>;
68                 __tmp->_M_cache(__loc);
69               }
70             __catch(...)
71               {
72                 delete __tmp;
73                 __throw_exception_again;
74               }
75             __loc._M_impl->_M_install_cache(__tmp, __i);
76           }
77         return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]);
78       }
79     };
81   template<typename _CharT>
82     void
83     __numpunct_cache<_CharT>::_M_cache(const locale& __loc)
84     {
85       _M_allocated = true;
87       const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
89       _M_grouping_size = __np.grouping().size();
90       char* __grouping = new char[_M_grouping_size];
91       __np.grouping().copy(__grouping, _M_grouping_size);
92       _M_grouping = __grouping;
93       _M_use_grouping = (_M_grouping_size
94                          && static_cast<signed char>(_M_grouping[0]) > 0
95                          && (_M_grouping[0]
96                              != __gnu_cxx::__numeric_traits<char>::__max));
98       _M_truename_size = __np.truename().size();
99       _CharT* __truename = new _CharT[_M_truename_size];
100       __np.truename().copy(__truename, _M_truename_size);
101       _M_truename = __truename;
103       _M_falsename_size = __np.falsename().size();
104       _CharT* __falsename = new _CharT[_M_falsename_size];
105       __np.falsename().copy(__falsename, _M_falsename_size);
106       _M_falsename = __falsename;
108       _M_decimal_point = __np.decimal_point();
109       _M_thousands_sep = __np.thousands_sep();
111       const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
112       __ct.widen(__num_base::_S_atoms_out,
113                  __num_base::_S_atoms_out + __num_base::_S_oend, _M_atoms_out);
114       __ct.widen(__num_base::_S_atoms_in,
115                  __num_base::_S_atoms_in + __num_base::_S_iend, _M_atoms_in);
116     }
118   // Used by both numeric and monetary facets.
119   // Check to make sure that the __grouping_tmp string constructed in
120   // money_get or num_get matches the canonical grouping for a given
121   // locale.
122   // __grouping_tmp is parsed L to R
123   // 1,222,444 == __grouping_tmp of "\1\3\3"
124   // __grouping is parsed R to L
125   // 1,222,444 == __grouping of "\3" == "\3\3\3"
126   bool
127   __verify_grouping(const char* __grouping, size_t __grouping_size,
128                     const string& __grouping_tmp);
130 _GLIBCXX_BEGIN_LDBL_NAMESPACE
132   template<typename _CharT, typename _InIter>
133     _InIter
134     num_get<_CharT, _InIter>::
135     _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
136                      ios_base::iostate& __err, string& __xtrc) const
137     {
138       typedef char_traits<_CharT>                       __traits_type;
139       typedef __numpunct_cache<_CharT>                  __cache_type;
140       __use_cache<__cache_type> __uc;
141       const locale& __loc = __io._M_getloc();
142       const __cache_type* __lc = __uc(__loc);
143       const _CharT* __lit = __lc->_M_atoms_in;
144       char_type __c = char_type();
146       // True if __beg becomes equal to __end.
147       bool __testeof = __beg == __end;
149       // First check for sign.
150       if (!__testeof)
151         {
152           __c = *__beg;
153           const bool __plus = __c == __lit[__num_base::_S_iplus];
154           if ((__plus || __c == __lit[__num_base::_S_iminus])
155               && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
156               && !(__c == __lc->_M_decimal_point))
157             {
158               __xtrc += __plus ? '+' : '-';
159               if (++__beg != __end)
160                 __c = *__beg;
161               else
162                 __testeof = true;
163             }
164         }
166       // Next, look for leading zeros.
167       bool __found_mantissa = false;
168       int __sep_pos = 0;
169       while (!__testeof)
170         {
171           if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
172               || __c == __lc->_M_decimal_point)
173             break;
174           else if (__c == __lit[__num_base::_S_izero])
175             {
176               if (!__found_mantissa)
177                 {
178                   __xtrc += '0';
179                   __found_mantissa = true;
180                 }
181               ++__sep_pos;
183               if (++__beg != __end)
184                 __c = *__beg;
185               else
186                 __testeof = true;
187             }
188           else
189             break;
190         }
192       // Only need acceptable digits for floating point numbers.
193       bool __found_dec = false;
194       bool __found_sci = false;
195       string __found_grouping;
196       if (__lc->_M_use_grouping)
197         __found_grouping.reserve(32);
198       const char_type* __lit_zero = __lit + __num_base::_S_izero;
200       if (!__lc->_M_allocated)
201         // "C" locale
202         while (!__testeof)
203           {
204             const int __digit = _M_find(__lit_zero, 10, __c);
205             if (__digit != -1)
206               {
207                 __xtrc += '0' + __digit;
208                 __found_mantissa = true;
209               }
210             else if (__c == __lc->_M_decimal_point
211                      && !__found_dec && !__found_sci)
212               {
213                 __xtrc += '.';
214                 __found_dec = true;
215               }
216             else if ((__c == __lit[__num_base::_S_ie] 
217                       || __c == __lit[__num_base::_S_iE])
218                      && !__found_sci && __found_mantissa)
219               {
220                 // Scientific notation.
221                 __xtrc += 'e';
222                 __found_sci = true;
223                 
224                 // Remove optional plus or minus sign, if they exist.
225                 if (++__beg != __end)
226                   {
227                     __c = *__beg;
228                     const bool __plus = __c == __lit[__num_base::_S_iplus];
229                     if (__plus || __c == __lit[__num_base::_S_iminus])
230                       __xtrc += __plus ? '+' : '-';
231                     else
232                       continue;
233                   }
234                 else
235                   {
236                     __testeof = true;
237                     break;
238                   }
239               }
240             else
241               break;
243             if (++__beg != __end)
244               __c = *__beg;
245             else
246               __testeof = true;
247           }
248       else
249         while (!__testeof)
250           {
251             // According to 22.2.2.1.2, p8-9, first look for thousands_sep
252             // and decimal_point.
253             if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
254               {
255                 if (!__found_dec && !__found_sci)
256                   {
257                     // NB: Thousands separator at the beginning of a string
258                     // is a no-no, as is two consecutive thousands separators.
259                     if (__sep_pos)
260                       {
261                         __found_grouping += static_cast<char>(__sep_pos);
262                         __sep_pos = 0;
263                       }
264                     else
265                       {
266                         // NB: __convert_to_v will not assign __v and will
267                         // set the failbit.
268                         __xtrc.clear();
269                         break;
270                       }
271                   }
272                 else
273                   break;
274               }
275             else if (__c == __lc->_M_decimal_point)
276               {
277                 if (!__found_dec && !__found_sci)
278                   {
279                     // If no grouping chars are seen, no grouping check
280                     // is applied. Therefore __found_grouping is adjusted
281                     // only if decimal_point comes after some thousands_sep.
282                     if (__found_grouping.size())
283                       __found_grouping += static_cast<char>(__sep_pos);
284                     __xtrc += '.';
285                     __found_dec = true;
286                   }
287                 else
288                   break;
289               }
290             else
291               {
292                 const char_type* __q =
293                   __traits_type::find(__lit_zero, 10, __c);
294                 if (__q)
295                   {
296                     __xtrc += '0' + (__q - __lit_zero);
297                     __found_mantissa = true;
298                     ++__sep_pos;
299                   }
300                 else if ((__c == __lit[__num_base::_S_ie] 
301                           || __c == __lit[__num_base::_S_iE])
302                          && !__found_sci && __found_mantissa)
303                   {
304                     // Scientific notation.
305                     if (__found_grouping.size() && !__found_dec)
306                       __found_grouping += static_cast<char>(__sep_pos);
307                     __xtrc += 'e';
308                     __found_sci = true;
309                     
310                     // Remove optional plus or minus sign, if they exist.
311                     if (++__beg != __end)
312                       {
313                         __c = *__beg;
314                         const bool __plus = __c == __lit[__num_base::_S_iplus];
315                         if ((__plus || __c == __lit[__num_base::_S_iminus])
316                             && !(__lc->_M_use_grouping
317                                  && __c == __lc->_M_thousands_sep)
318                             && !(__c == __lc->_M_decimal_point))
319                       __xtrc += __plus ? '+' : '-';
320                         else
321                           continue;
322                       }
323                     else
324                       {
325                         __testeof = true;
326                         break;
327                       }
328                   }
329                 else
330                   break;
331               }
332             
333             if (++__beg != __end)
334               __c = *__beg;
335             else
336               __testeof = true;
337           }
339       // Digit grouping is checked. If grouping and found_grouping don't
340       // match, then get very very upset, and set failbit.
341       if (__found_grouping.size())
342         {
343           // Add the ending grouping if a decimal or 'e'/'E' wasn't found.
344           if (!__found_dec && !__found_sci)
345             __found_grouping += static_cast<char>(__sep_pos);
347           if (!std::__verify_grouping(__lc->_M_grouping, 
348                                       __lc->_M_grouping_size,
349                                       __found_grouping))
350             __err = ios_base::failbit;
351         }
353       return __beg;
354     }
356   template<typename _CharT, typename _InIter>
357     template<typename _ValueT>
358       _InIter
359       num_get<_CharT, _InIter>::
360       _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
361                      ios_base::iostate& __err, _ValueT& __v) const
362       {
363         typedef char_traits<_CharT>                          __traits_type;
364         using __gnu_cxx::__add_unsigned;
365         typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
366         typedef __numpunct_cache<_CharT>                     __cache_type;
367         __use_cache<__cache_type> __uc;
368         const locale& __loc = __io._M_getloc();
369         const __cache_type* __lc = __uc(__loc);
370         const _CharT* __lit = __lc->_M_atoms_in;
371         char_type __c = char_type();
373         // NB: Iff __basefield == 0, __base can change based on contents.
374         const ios_base::fmtflags __basefield = __io.flags()
375                                                & ios_base::basefield;
376         const bool __oct = __basefield == ios_base::oct;
377         int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10);
379         // True if __beg becomes equal to __end.
380         bool __testeof = __beg == __end;
382         // First check for sign.
383         bool __negative = false;
384         if (!__testeof)
385           {
386             __c = *__beg;
387             if (__gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
388               __negative = __c == __lit[__num_base::_S_iminus];
389             if ((__negative || __c == __lit[__num_base::_S_iplus])
390                 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
391                 && !(__c == __lc->_M_decimal_point))
392               {
393                 if (++__beg != __end)
394                   __c = *__beg;
395                 else
396                   __testeof = true;
397               }
398           }
400         // Next, look for leading zeros and check required digits
401         // for base formats.
402         bool __found_zero = false;
403         int __sep_pos = 0;
404         while (!__testeof)
405           {
406             if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
407                 || __c == __lc->_M_decimal_point)
408               break;
409             else if (__c == __lit[__num_base::_S_izero] 
410                      && (!__found_zero || __base == 10))
411               {
412                 __found_zero = true;
413                 ++__sep_pos;
414                 if (__basefield == 0)
415                   __base = 8;
416                 if (__base == 8)
417                   __sep_pos = 0;
418               }
419             else if (__found_zero
420                      && (__c == __lit[__num_base::_S_ix]
421                          || __c == __lit[__num_base::_S_iX]))
422               {
423                 if (__basefield == 0)
424                   __base = 16;
425                 if (__base == 16)
426                   {
427                     __found_zero = false;
428                     __sep_pos = 0;
429                   }
430                 else
431                   break;
432               }
433             else
434               break;
436             if (++__beg != __end)
437               {
438                 __c = *__beg;
439                 if (!__found_zero)
440                   break;
441               }
442             else
443               __testeof = true;
444           }
445         
446         // At this point, base is determined. If not hex, only allow
447         // base digits as valid input.
448         const size_t __len = (__base == 16 ? __num_base::_S_iend
449                               - __num_base::_S_izero : __base);
451         // Extract.
452         string __found_grouping;
453         if (__lc->_M_use_grouping)
454           __found_grouping.reserve(32);
455         bool __testfail = false;
456         bool __testoverflow = false;
457         const __unsigned_type __max = __negative
458           ? -__gnu_cxx::__numeric_traits<_ValueT>::__min
459           : __gnu_cxx::__numeric_traits<_ValueT>::__max;
460         const __unsigned_type __smax = __max / __base;
461         __unsigned_type __result = 0;
462         int __digit = 0;
463         const char_type* __lit_zero = __lit + __num_base::_S_izero;
465         if (!__lc->_M_allocated)
466           // "C" locale
467           while (!__testeof)
468             {
469               __digit = _M_find(__lit_zero, __len, __c);
470               if (__digit == -1)
471                 break;
472               
473               if (__result > __smax)
474                 __testoverflow = true;
475               else
476                 {
477                   __result *= __base;
478                   __testoverflow |= __result > __max - __digit;
479                   __result += __digit;
480                   ++__sep_pos;
481                 }
482               
483               if (++__beg != __end)
484                 __c = *__beg;
485               else
486                 __testeof = true;
487             }
488         else
489           while (!__testeof)
490             {
491               // According to 22.2.2.1.2, p8-9, first look for thousands_sep
492               // and decimal_point.
493               if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
494                 {
495                   // NB: Thousands separator at the beginning of a string
496                   // is a no-no, as is two consecutive thousands separators.
497                   if (__sep_pos)
498                     {
499                       __found_grouping += static_cast<char>(__sep_pos);
500                       __sep_pos = 0;
501                     }
502                   else
503                     {
504                       __testfail = true;
505                       break;
506                     }
507                 }
508               else if (__c == __lc->_M_decimal_point)
509                 break;
510               else
511                 {
512                   const char_type* __q =
513                     __traits_type::find(__lit_zero, __len, __c);
514                   if (!__q)
515                     break;
516                   
517                   __digit = __q - __lit_zero;
518                   if (__digit > 15)
519                     __digit -= 6;
520                   if (__result > __smax)
521                     __testoverflow = true;
522                   else
523                     {
524                       __result *= __base;
525                       __testoverflow |= __result > __max - __digit;
526                       __result += __digit;
527                       ++__sep_pos;
528                     }
529                 }
530               
531               if (++__beg != __end)
532                 __c = *__beg;
533               else
534                 __testeof = true;
535             }
536         
537         // Digit grouping is checked. If grouping and found_grouping don't
538         // match, then get very very upset, and set failbit.
539         if (__found_grouping.size())
540           {
541             // Add the ending grouping.
542             __found_grouping += static_cast<char>(__sep_pos);
544             if (!std::__verify_grouping(__lc->_M_grouping,
545                                         __lc->_M_grouping_size,
546                                         __found_grouping))
547               __err = ios_base::failbit;
548           }
550         // _GLIBCXX_RESOLVE_LIB_DEFECTS
551         // 23. Num_get overflow result.
552         if ((!__sep_pos && !__found_zero && !__found_grouping.size())
553             || __testfail)
554           {
555             __v = 0;
556             __err = ios_base::failbit;
557           }
558         else if (__testoverflow)
559           {
560             if (__negative)
561               __v = __gnu_cxx::__numeric_traits<_ValueT>::__min;
562             else
563               __v = __gnu_cxx::__numeric_traits<_ValueT>::__max;
564             __err = ios_base::failbit;
565           }
566         else
567           __v = __negative ? -__result : __result;
569         if (__testeof)
570           __err |= ios_base::eofbit;
571         return __beg;
572       }
574   // _GLIBCXX_RESOLVE_LIB_DEFECTS
575   // 17.  Bad bool parsing
576   template<typename _CharT, typename _InIter>
577     _InIter
578     num_get<_CharT, _InIter>::
579     do_get(iter_type __beg, iter_type __end, ios_base& __io,
580            ios_base::iostate& __err, bool& __v) const
581     {
582       if (!(__io.flags() & ios_base::boolalpha))
583         {
584           // Parse bool values as long.
585           // NB: We can't just call do_get(long) here, as it might
586           // refer to a derived class.
587           long __l = -1;
588           __beg = _M_extract_int(__beg, __end, __io, __err, __l);
589           if (__l == 0 || __l == 1)
590             __v = bool(__l);
591           else
592             {
593               // _GLIBCXX_RESOLVE_LIB_DEFECTS
594               // 23. Num_get overflow result.
595               __v = true;
596               __err = ios_base::failbit;
597               if (__beg == __end)
598                 __err |= ios_base::eofbit;
599             }
600         }
601       else
602         {
603           // Parse bool values as alphanumeric.
604           typedef __numpunct_cache<_CharT>  __cache_type;
605           __use_cache<__cache_type> __uc;
606           const locale& __loc = __io._M_getloc();
607           const __cache_type* __lc = __uc(__loc);
609           bool __testf = true;
610           bool __testt = true;
611           bool __donef = __lc->_M_falsename_size == 0;
612           bool __donet = __lc->_M_truename_size == 0;
613           bool __testeof = false;
614           size_t __n = 0;
615           while (!__donef || !__donet)
616             {
617               if (__beg == __end)
618                 {
619                   __testeof = true;
620                   break;
621                 }
623               const char_type __c = *__beg;
625               if (!__donef)
626                 __testf = __c == __lc->_M_falsename[__n];
628               if (!__testf && __donet)
629                 break;
631               if (!__donet)
632                 __testt = __c == __lc->_M_truename[__n];
634               if (!__testt && __donef)
635                 break;
637               if (!__testt && !__testf)
638                 break;
640               ++__n;
641               ++__beg;
643               __donef = !__testf || __n >= __lc->_M_falsename_size;
644               __donet = !__testt || __n >= __lc->_M_truename_size;
645             }
646           if (__testf && __n == __lc->_M_falsename_size && __n)
647             {
648               __v = false;
649               if (__testt && __n == __lc->_M_truename_size)
650                 __err = ios_base::failbit;
651               else
652                 __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
653             }
654           else if (__testt && __n == __lc->_M_truename_size && __n)
655             {
656               __v = true;
657               __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
658             }
659           else
660             {
661               // _GLIBCXX_RESOLVE_LIB_DEFECTS
662               // 23. Num_get overflow result.
663               __v = false;
664               __err = ios_base::failbit;
665               if (__testeof)
666                 __err |= ios_base::eofbit;
667             }
668         }
669       return __beg;
670     }
672   template<typename _CharT, typename _InIter>
673     _InIter
674     num_get<_CharT, _InIter>::
675     do_get(iter_type __beg, iter_type __end, ios_base& __io,
676            ios_base::iostate& __err, float& __v) const
677     {
678       string __xtrc;
679       __xtrc.reserve(32);
680       __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
681       std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
682       if (__beg == __end)
683         __err |= ios_base::eofbit;
684       return __beg;
685     }
687   template<typename _CharT, typename _InIter>
688     _InIter
689     num_get<_CharT, _InIter>::
690     do_get(iter_type __beg, iter_type __end, ios_base& __io,
691            ios_base::iostate& __err, double& __v) const
692     {
693       string __xtrc;
694       __xtrc.reserve(32);
695       __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
696       std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
697       if (__beg == __end)
698         __err |= ios_base::eofbit;
699       return __beg;
700     }
702 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
703   template<typename _CharT, typename _InIter>
704     _InIter
705     num_get<_CharT, _InIter>::
706     __do_get(iter_type __beg, iter_type __end, ios_base& __io,
707              ios_base::iostate& __err, double& __v) const
708     {
709       string __xtrc;
710       __xtrc.reserve(32);
711       __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
712       std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
713       if (__beg == __end)
714         __err |= ios_base::eofbit;
715       return __beg;
716     }
717 #endif
719   template<typename _CharT, typename _InIter>
720     _InIter
721     num_get<_CharT, _InIter>::
722     do_get(iter_type __beg, iter_type __end, ios_base& __io,
723            ios_base::iostate& __err, long double& __v) const
724     {
725       string __xtrc;
726       __xtrc.reserve(32);
727       __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
728       std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
729       if (__beg == __end)
730         __err |= ios_base::eofbit;
731       return __beg;
732     }
734   template<typename _CharT, typename _InIter>
735     _InIter
736     num_get<_CharT, _InIter>::
737     do_get(iter_type __beg, iter_type __end, ios_base& __io,
738            ios_base::iostate& __err, void*& __v) const
739     {
740       // Prepare for hex formatted input.
741       typedef ios_base::fmtflags        fmtflags;
742       const fmtflags __fmt = __io.flags();
743       __io.flags((__fmt & ~ios_base::basefield) | ios_base::hex);
745       typedef __gnu_cxx::__conditional_type<(sizeof(void*)
746                                              <= sizeof(unsigned long)),
747         unsigned long, unsigned long long>::__type _UIntPtrType;       
749       _UIntPtrType __ul;
750       __beg = _M_extract_int(__beg, __end, __io, __err, __ul);
752       // Reset from hex formatted input.
753       __io.flags(__fmt);
755       __v = reinterpret_cast<void*>(__ul);
756       return __beg;
757     }
759   // For use by integer and floating-point types after they have been
760   // converted into a char_type string.
761   template<typename _CharT, typename _OutIter>
762     void
763     num_put<_CharT, _OutIter>::
764     _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
765            _CharT* __new, const _CharT* __cs, int& __len) const
766     {
767       // [22.2.2.2.2] Stage 3.
768       // If necessary, pad.
769       __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new,
770                                                   __cs, __w, __len);
771       __len = static_cast<int>(__w);
772     }
774 _GLIBCXX_END_LDBL_NAMESPACE
776   template<typename _CharT, typename _ValueT>
777     int
778     __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
779                   ios_base::fmtflags __flags, bool __dec)
780     {
781       _CharT* __buf = __bufend;
782       if (__builtin_expect(__dec, true))
783         {
784           // Decimal.
785           do
786             {
787               *--__buf = __lit[(__v % 10) + __num_base::_S_odigits];
788               __v /= 10;
789             }
790           while (__v != 0);
791         }
792       else if ((__flags & ios_base::basefield) == ios_base::oct)
793         {
794           // Octal.
795           do
796             {
797               *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits];
798               __v >>= 3;
799             }
800           while (__v != 0);
801         }
802       else
803         {
804           // Hex.
805           const bool __uppercase = __flags & ios_base::uppercase;
806           const int __case_offset = __uppercase ? __num_base::_S_oudigits
807                                                 : __num_base::_S_odigits;
808           do
809             {
810               *--__buf = __lit[(__v & 0xf) + __case_offset];
811               __v >>= 4;
812             }
813           while (__v != 0);
814         }
815       return __bufend - __buf;
816     }
818 _GLIBCXX_BEGIN_LDBL_NAMESPACE
820   template<typename _CharT, typename _OutIter>
821     void
822     num_put<_CharT, _OutIter>::
823     _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep,
824                  ios_base&, _CharT* __new, _CharT* __cs, int& __len) const
825     {
826       _CharT* __p = std::__add_grouping(__new, __sep, __grouping,
827                                         __grouping_size, __cs, __cs + __len);
828       __len = __p - __new;
829     }
830   
831   template<typename _CharT, typename _OutIter>
832     template<typename _ValueT>
833       _OutIter
834       num_put<_CharT, _OutIter>::
835       _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
836                     _ValueT __v) const
837       {
838         using __gnu_cxx::__add_unsigned;
839         typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
840         typedef __numpunct_cache<_CharT>                     __cache_type;
841         __use_cache<__cache_type> __uc;
842         const locale& __loc = __io._M_getloc();
843         const __cache_type* __lc = __uc(__loc);
844         const _CharT* __lit = __lc->_M_atoms_out;
845         const ios_base::fmtflags __flags = __io.flags();
847         // Long enough to hold hex, dec, and octal representations.
848         const int __ilen = 5 * sizeof(_ValueT);
849         _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
850                                                              * __ilen));
852         // [22.2.2.2.2] Stage 1, numeric conversion to character.
853         // Result is returned right-justified in the buffer.
854         const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
855         const bool __dec = (__basefield != ios_base::oct
856                             && __basefield != ios_base::hex);
857         const __unsigned_type __u = ((__v > 0 || !__dec)
858                                      ? __unsigned_type(__v)
859                                      : -__unsigned_type(__v));
860         int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec);
861         __cs += __ilen - __len;
863         // Add grouping, if necessary.
864         if (__lc->_M_use_grouping)
865           {
866             // Grouping can add (almost) as many separators as the number
867             // of digits + space is reserved for numeric base or sign.
868             _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
869                                                                   * (__len + 1)
870                                                                   * 2));
871             _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size,
872                          __lc->_M_thousands_sep, __io, __cs2 + 2, __cs, __len);
873             __cs = __cs2 + 2;
874           }
876         // Complete Stage 1, prepend numeric base or sign.
877         if (__builtin_expect(__dec, true))
878           {
879             // Decimal.
880             if (__v >= 0)
881               {
882                 if (bool(__flags & ios_base::showpos)
883                     && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
884                   *--__cs = __lit[__num_base::_S_oplus], ++__len;
885               }
886             else
887               *--__cs = __lit[__num_base::_S_ominus], ++__len;
888           }
889         else if (bool(__flags & ios_base::showbase) && __v)
890           {
891             if (__basefield == ios_base::oct)
892               *--__cs = __lit[__num_base::_S_odigits], ++__len;
893             else
894               {
895                 // 'x' or 'X'
896                 const bool __uppercase = __flags & ios_base::uppercase;
897                 *--__cs = __lit[__num_base::_S_ox + __uppercase];
898                 // '0'
899                 *--__cs = __lit[__num_base::_S_odigits];
900                 __len += 2;
901               }
902           }
904         // Pad.
905         const streamsize __w = __io.width();
906         if (__w > static_cast<streamsize>(__len))
907           {
908             _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
909                                                                   * __w));
910             _M_pad(__fill, __w, __io, __cs3, __cs, __len);
911             __cs = __cs3;
912           }
913         __io.width(0);
915         // [22.2.2.2.2] Stage 4.
916         // Write resulting, fully-formatted string to output iterator.
917         return std::__write(__s, __cs, __len);
918       }
920   template<typename _CharT, typename _OutIter>
921     void
922     num_put<_CharT, _OutIter>::
923     _M_group_float(const char* __grouping, size_t __grouping_size,
924                    _CharT __sep, const _CharT* __p, _CharT* __new,
925                    _CharT* __cs, int& __len) const
926     {
927       // _GLIBCXX_RESOLVE_LIB_DEFECTS
928       // 282. What types does numpunct grouping refer to?
929       // Add grouping, if necessary.
930       const int __declen = __p ? __p - __cs : __len;
931       _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping,
932                                          __grouping_size,
933                                          __cs, __cs + __declen);
935       // Tack on decimal part.
936       int __newlen = __p2 - __new;
937       if (__p)
938         {
939           char_traits<_CharT>::copy(__p2, __p, __len - __declen);
940           __newlen += __len - __declen;
941         }
942       __len = __newlen;
943     }
945   // The following code uses vsnprintf (or vsprintf(), when
946   // _GLIBCXX_USE_C99 is not defined) to convert floating point values
947   // for insertion into a stream.  An optimization would be to replace
948   // them with code that works directly on a wide buffer and then use
949   // __pad to do the padding.  It would be good to replace them anyway
950   // to gain back the efficiency that C++ provides by knowing up front
951   // the type of the values to insert.  Also, sprintf is dangerous
952   // since may lead to accidental buffer overruns.  This
953   // implementation follows the C++ standard fairly directly as
954   // outlined in 22.2.2.2 [lib.locale.num.put]
955   template<typename _CharT, typename _OutIter>
956     template<typename _ValueT>
957       _OutIter
958       num_put<_CharT, _OutIter>::
959       _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
960                        _ValueT __v) const
961       {
962         typedef __numpunct_cache<_CharT>                __cache_type;
963         __use_cache<__cache_type> __uc;
964         const locale& __loc = __io._M_getloc();
965         const __cache_type* __lc = __uc(__loc);
967         // Use default precision if out of range.
968         const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision();
970         const int __max_digits =
971           __gnu_cxx::__numeric_traits<_ValueT>::__digits10;
973         // [22.2.2.2.2] Stage 1, numeric conversion to character.
974         int __len;
975         // Long enough for the max format spec.
976         char __fbuf[16];
977         __num_base::_S_format_float(__io, __fbuf, __mod);
979 #ifdef _GLIBCXX_USE_C99
980         // First try a buffer perhaps big enough (most probably sufficient
981         // for non-ios_base::fixed outputs)
982         int __cs_size = __max_digits * 3;
983         char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
984         __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
985                                       __fbuf, __prec, __v);
987         // If the buffer was not large enough, try again with the correct size.
988         if (__len >= __cs_size)
989           {
990             __cs_size = __len + 1;
991             __cs = static_cast<char*>(__builtin_alloca(__cs_size));
992             __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
993                                           __fbuf, __prec, __v);
994           }
995 #else
996         // Consider the possibility of long ios_base::fixed outputs
997         const bool __fixed = __io.flags() & ios_base::fixed;
998         const int __max_exp =
999           __gnu_cxx::__numeric_traits<_ValueT>::__max_exponent10;
1001         // The size of the output string is computed as follows.
1002         // ios_base::fixed outputs may need up to __max_exp + 1 chars
1003         // for the integer part + __prec chars for the fractional part
1004         // + 3 chars for sign, decimal point, '\0'. On the other hand,
1005         // for non-fixed outputs __max_digits * 2 + __prec chars are
1006         // largely sufficient.
1007         const int __cs_size = __fixed ? __max_exp + __prec + 4
1008                                       : __max_digits * 2 + __prec;
1009         char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1010         __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf, 
1011                                       __prec, __v);
1012 #endif
1014         // [22.2.2.2.2] Stage 2, convert to char_type, using correct
1015         // numpunct.decimal_point() values for '.' and adding grouping.
1016         const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1017         
1018         _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1019                                                              * __len));
1020         __ctype.widen(__cs, __cs + __len, __ws);
1021         
1022         // Replace decimal point.
1023         _CharT* __wp = 0;
1024         const char* __p = char_traits<char>::find(__cs, __len, '.');
1025         if (__p)
1026           {
1027             __wp = __ws + (__p - __cs);
1028             *__wp = __lc->_M_decimal_point;
1029           }
1030         
1031         // Add grouping, if necessary.
1032         // N.B. Make sure to not group things like 2e20, i.e., no decimal
1033         // point, scientific notation.
1034         if (__lc->_M_use_grouping
1035             && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9'
1036                                       && __cs[1] >= '0' && __cs[2] >= '0')))
1037           {
1038             // Grouping can add (almost) as many separators as the
1039             // number of digits, but no more.
1040             _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1041                                                                   * __len * 2));
1042             
1043             streamsize __off = 0;
1044             if (__cs[0] == '-' || __cs[0] == '+')
1045               {
1046                 __off = 1;
1047                 __ws2[0] = __ws[0];
1048                 __len -= 1;
1049               }
1050             
1051             _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size,
1052                            __lc->_M_thousands_sep, __wp, __ws2 + __off,
1053                            __ws + __off, __len);
1054             __len += __off;
1055             
1056             __ws = __ws2;
1057           }
1059         // Pad.
1060         const streamsize __w = __io.width();
1061         if (__w > static_cast<streamsize>(__len))
1062           {
1063             _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1064                                                                   * __w));
1065             _M_pad(__fill, __w, __io, __ws3, __ws, __len);
1066             __ws = __ws3;
1067           }
1068         __io.width(0);
1069         
1070         // [22.2.2.2.2] Stage 4.
1071         // Write resulting, fully-formatted string to output iterator.
1072         return std::__write(__s, __ws, __len);
1073       }
1074   
1075   template<typename _CharT, typename _OutIter>
1076     _OutIter
1077     num_put<_CharT, _OutIter>::
1078     do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
1079     {
1080       const ios_base::fmtflags __flags = __io.flags();
1081       if ((__flags & ios_base::boolalpha) == 0)
1082         {
1083           const long __l = __v;
1084           __s = _M_insert_int(__s, __io, __fill, __l);
1085         }
1086       else
1087         {
1088           typedef __numpunct_cache<_CharT>              __cache_type;
1089           __use_cache<__cache_type> __uc;
1090           const locale& __loc = __io._M_getloc();
1091           const __cache_type* __lc = __uc(__loc);
1093           const _CharT* __name = __v ? __lc->_M_truename
1094                                      : __lc->_M_falsename;
1095           int __len = __v ? __lc->_M_truename_size
1096                           : __lc->_M_falsename_size;
1098           const streamsize __w = __io.width();
1099           if (__w > static_cast<streamsize>(__len))
1100             {
1101               const streamsize __plen = __w - __len;
1102               _CharT* __ps
1103                 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1104                                                         * __plen));
1106               char_traits<_CharT>::assign(__ps, __plen, __fill);
1107               __io.width(0);
1109               if ((__flags & ios_base::adjustfield) == ios_base::left)
1110                 {
1111                   __s = std::__write(__s, __name, __len);
1112                   __s = std::__write(__s, __ps, __plen);
1113                 }
1114               else
1115                 {
1116                   __s = std::__write(__s, __ps, __plen);
1117                   __s = std::__write(__s, __name, __len);
1118                 }
1119               return __s;
1120             }
1121           __io.width(0);
1122           __s = std::__write(__s, __name, __len);
1123         }
1124       return __s;
1125     }
1127   template<typename _CharT, typename _OutIter>
1128     _OutIter
1129     num_put<_CharT, _OutIter>::
1130     do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1131     { return _M_insert_float(__s, __io, __fill, char(), __v); }
1133 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1134   template<typename _CharT, typename _OutIter>
1135     _OutIter
1136     num_put<_CharT, _OutIter>::
1137     __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1138     { return _M_insert_float(__s, __io, __fill, char(), __v); }
1139 #endif
1141   template<typename _CharT, typename _OutIter>
1142     _OutIter
1143     num_put<_CharT, _OutIter>::
1144     do_put(iter_type __s, ios_base& __io, char_type __fill,
1145            long double __v) const
1146     { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1148   template<typename _CharT, typename _OutIter>
1149     _OutIter
1150     num_put<_CharT, _OutIter>::
1151     do_put(iter_type __s, ios_base& __io, char_type __fill,
1152            const void* __v) const
1153     {
1154       const ios_base::fmtflags __flags = __io.flags();
1155       const ios_base::fmtflags __fmt = ~(ios_base::basefield
1156                                          | ios_base::uppercase);
1157       __io.flags((__flags & __fmt) | (ios_base::hex | ios_base::showbase));
1159       typedef __gnu_cxx::__conditional_type<(sizeof(const void*)
1160                                              <= sizeof(unsigned long)),
1161         unsigned long, unsigned long long>::__type _UIntPtrType;       
1163       __s = _M_insert_int(__s, __io, __fill,
1164                           reinterpret_cast<_UIntPtrType>(__v));
1165       __io.flags(__flags);
1166       return __s;
1167     }
1169 _GLIBCXX_END_LDBL_NAMESPACE
1171   // Construct correctly padded string, as per 22.2.2.2.2
1172   // Assumes
1173   // __newlen > __oldlen
1174   // __news is allocated for __newlen size
1176   // NB: Of the two parameters, _CharT can be deduced from the
1177   // function arguments. The other (_Traits) has to be explicitly specified.
1178   template<typename _CharT, typename _Traits>
1179     void
1180     __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
1181                                    _CharT* __news, const _CharT* __olds,
1182                                    streamsize __newlen, streamsize __oldlen)
1183     {
1184       const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
1185       const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
1187       // Padding last.
1188       if (__adjust == ios_base::left)
1189         {
1190           _Traits::copy(__news, __olds, __oldlen);
1191           _Traits::assign(__news + __oldlen, __plen, __fill);
1192           return;
1193         }
1195       size_t __mod = 0;
1196       if (__adjust == ios_base::internal)
1197         {
1198           // Pad after the sign, if there is one.
1199           // Pad after 0[xX], if there is one.
1200           // Who came up with these rules, anyway? Jeeze.
1201           const locale& __loc = __io._M_getloc();
1202           const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1204           if (__ctype.widen('-') == __olds[0]
1205               || __ctype.widen('+') == __olds[0])
1206             {
1207               __news[0] = __olds[0];
1208               __mod = 1;
1209               ++__news;
1210             }
1211           else if (__ctype.widen('0') == __olds[0]
1212                    && __oldlen > 1
1213                    && (__ctype.widen('x') == __olds[1]
1214                        || __ctype.widen('X') == __olds[1]))
1215             {
1216               __news[0] = __olds[0];
1217               __news[1] = __olds[1];
1218               __mod = 2;
1219               __news += 2;
1220             }
1221           // else Padding first.
1222         }
1223       _Traits::assign(__news, __plen, __fill);
1224       _Traits::copy(__news + __plen, __olds + __mod, __oldlen - __mod);
1225     }
1227   template<typename _CharT>
1228     _CharT*
1229     __add_grouping(_CharT* __s, _CharT __sep,
1230                    const char* __gbeg, size_t __gsize,
1231                    const _CharT* __first, const _CharT* __last)
1232     {
1233       size_t __idx = 0;
1234       size_t __ctr = 0;
1236       while (__last - __first > __gbeg[__idx]
1237              && static_cast<signed char>(__gbeg[__idx]) > 0
1238              && __gbeg[__idx] != __gnu_cxx::__numeric_traits<char>::__max)
1239         {
1240           __last -= __gbeg[__idx];
1241           __idx < __gsize - 1 ? ++__idx : ++__ctr;
1242         }
1244       while (__first != __last)
1245         *__s++ = *__first++;
1247       while (__ctr--)
1248         {
1249           *__s++ = __sep;         
1250           for (char __i = __gbeg[__idx]; __i > 0; --__i)
1251             *__s++ = *__first++;
1252         }
1254       while (__idx--)
1255         {
1256           *__s++ = __sep;         
1257           for (char __i = __gbeg[__idx]; __i > 0; --__i)
1258             *__s++ = *__first++;
1259         }
1261       return __s;
1262     }
1264   // Inhibit implicit instantiations for required instantiations,
1265   // which are defined via explicit instantiations elsewhere.
1266   // NB: This syntax is a GNU extension.
1267 #if _GLIBCXX_EXTERN_TEMPLATE
1268   extern template class numpunct<char>;
1269   extern template class numpunct_byname<char>;
1270   extern template class _GLIBCXX_LDBL_NAMESPACE num_get<char>;
1271   extern template class _GLIBCXX_LDBL_NAMESPACE num_put<char>;
1272   extern template class ctype_byname<char>;
1274   extern template
1275     const ctype<char>&
1276     use_facet<ctype<char> >(const locale&);
1278   extern template
1279     const numpunct<char>&
1280     use_facet<numpunct<char> >(const locale&);
1282   extern template
1283     const num_put<char>&
1284     use_facet<num_put<char> >(const locale&);
1286   extern template
1287     const num_get<char>&
1288     use_facet<num_get<char> >(const locale&);
1290   extern template
1291     bool
1292     has_facet<ctype<char> >(const locale&);
1294   extern template
1295     bool
1296     has_facet<numpunct<char> >(const locale&);
1298   extern template
1299     bool
1300     has_facet<num_put<char> >(const locale&);
1302   extern template
1303     bool
1304     has_facet<num_get<char> >(const locale&);
1306 #ifdef _GLIBCXX_USE_WCHAR_T
1307   extern template class numpunct<wchar_t>;
1308   extern template class numpunct_byname<wchar_t>;
1309   extern template class _GLIBCXX_LDBL_NAMESPACE num_get<wchar_t>;
1310   extern template class _GLIBCXX_LDBL_NAMESPACE num_put<wchar_t>;
1311   extern template class ctype_byname<wchar_t>;
1313   extern template
1314     const ctype<wchar_t>&
1315     use_facet<ctype<wchar_t> >(const locale&);
1317   extern template
1318     const numpunct<wchar_t>&
1319     use_facet<numpunct<wchar_t> >(const locale&);
1321   extern template
1322     const num_put<wchar_t>&
1323     use_facet<num_put<wchar_t> >(const locale&);
1325   extern template
1326     const num_get<wchar_t>&
1327     use_facet<num_get<wchar_t> >(const locale&);
1329  extern template
1330     bool
1331     has_facet<ctype<wchar_t> >(const locale&);
1333   extern template
1334     bool
1335     has_facet<numpunct<wchar_t> >(const locale&);
1337   extern template
1338     bool
1339     has_facet<num_put<wchar_t> >(const locale&);
1341   extern template
1342     bool
1343     has_facet<num_get<wchar_t> >(const locale&);
1344 #endif
1345 #endif
1347 _GLIBCXX_END_NAMESPACE
1349 #endif