Import a stripped down version of gcc-4.1.1
[dragonfly.git] / contrib / gcc-4.1 / libstdc++-v3 / include / bits / locale_facets.h
1 // Locale support -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 //
32 // ISO C++ 14882: 22.1  Locales
33 //
34
35 /** @file locale_facets.h
36  *  This is an internal header file, included by other library headers.
37  *  You should not attempt to use it directly.
38  */
39
40 #ifndef _LOCALE_FACETS_H
41 #define _LOCALE_FACETS_H 1
42
43 #pragma GCC system_header
44
45 #include <ctime>        // For struct tm
46 #include <cwctype>      // For wctype_t
47 #include <iosfwd>
48 #include <bits/ios_base.h>  // For ios_base, ios_base::iostate
49 #include <streambuf>
50
51 namespace std
52 {
53   // NB: Don't instantiate required wchar_t facets if no wchar_t support.
54 #ifdef _GLIBCXX_USE_WCHAR_T
55 # define  _GLIBCXX_NUM_FACETS 28
56 #else
57 # define  _GLIBCXX_NUM_FACETS 14
58 #endif
59
60   // Convert string to numeric value of type _Tv and store results.
61   // NB: This is specialized for all required types, there is no
62   // generic definition.
63   template<typename _Tv>
64     void
65     __convert_to_v(const char* __in, _Tv& __out, ios_base::iostate& __err,
66                    const __c_locale& __cloc);
67
68   // Explicit specializations for required types.
69   template<>
70     void
71     __convert_to_v(const char*, float&, ios_base::iostate&,
72                    const __c_locale&);
73
74   template<>
75     void
76     __convert_to_v(const char*, double&, ios_base::iostate&,
77                    const __c_locale&);
78
79   template<>
80     void
81     __convert_to_v(const char*, long double&, ios_base::iostate&,
82                    const __c_locale&);
83
84   // NB: __pad is a struct, rather than a function, so it can be
85   // partially-specialized.
86   template<typename _CharT, typename _Traits>
87     struct __pad
88     {
89       static void
90       _S_pad(ios_base& __io, _CharT __fill, _CharT* __news,
91              const _CharT* __olds, const streamsize __newlen,
92              const streamsize __oldlen, const bool __num);
93     };
94
95   // Used by both numeric and monetary facets.
96   // Inserts "group separator" characters into an array of characters.
97   // It's recursive, one iteration per group.  It moves the characters
98   // in the buffer this way: "xxxx12345" -> "12,345xxx".  Call this
99   // only with __glen != 0.
100   template<typename _CharT>
101     _CharT*
102     __add_grouping(_CharT* __s, _CharT __sep,
103                    const char* __gbeg, size_t __gsize,
104                    const _CharT* __first, const _CharT* __last);
105
106   // This template permits specializing facet output code for
107   // ostreambuf_iterator.  For ostreambuf_iterator, sputn is
108   // significantly more efficient than incrementing iterators.
109   template<typename _CharT>
110     inline
111     ostreambuf_iterator<_CharT>
112     __write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len)
113     {
114       __s._M_put(__ws, __len);
115       return __s;
116     }
117
118   // This is the unspecialized form of the template.
119   template<typename _CharT, typename _OutIter>
120     inline
121     _OutIter
122     __write(_OutIter __s, const _CharT* __ws, int __len)
123     {
124       for (int __j = 0; __j < __len; __j++, ++__s)
125         *__s = __ws[__j];
126       return __s;
127     }
128
129
130   // 22.2.1.1  Template class ctype
131   // Include host and configuration specific ctype enums for ctype_base.
132   #include <bits/ctype_base.h>
133
134   // Common base for ctype<_CharT>.
135   /**
136    *  @brief  Common base for ctype facet
137    *
138    *  This template class provides implementations of the public functions
139    *  that forward to the protected virtual functions.
140    *
141    *  This template also provides abtract stubs for the protected virtual
142    *  functions.
143   */
144   template<typename _CharT>
145     class __ctype_abstract_base : public locale::facet, public ctype_base
146     {
147     public:
148       // Types:
149       /// Typedef for the template parameter
150       typedef _CharT char_type;
151
152       /**
153        *  @brief  Test char_type classification.
154        *
155        *  This function finds a mask M for @a c and compares it to mask @a m.
156        *  It does so by returning the value of ctype<char_type>::do_is().
157        *
158        *  @param c  The char_type to compare the mask of.
159        *  @param m  The mask to compare against.
160        *  @return  (M & m) != 0.
161       */
162       bool
163       is(mask __m, char_type __c) const
164       { return this->do_is(__m, __c); }
165
166       /**
167        *  @brief  Return a mask array.
168        *
169        *  This function finds the mask for each char_type in the range [lo,hi)
170        *  and successively writes it to vec.  vec must have as many elements
171        *  as the char array.  It does so by returning the value of
172        *  ctype<char_type>::do_is().
173        *
174        *  @param lo  Pointer to start of range.
175        *  @param hi  Pointer to end of range.
176        *  @param vec  Pointer to an array of mask storage.
177        *  @return  @a hi.
178       */
179       const char_type*
180       is(const char_type *__lo, const char_type *__hi, mask *__vec) const
181       { return this->do_is(__lo, __hi, __vec); }
182
183       /**
184        *  @brief  Find char_type matching a mask
185        *
186        *  This function searches for and returns the first char_type c in
187        *  [lo,hi) for which is(m,c) is true.  It does so by returning
188        *  ctype<char_type>::do_scan_is().
189        *
190        *  @param m  The mask to compare against.
191        *  @param lo  Pointer to start of range.
192        *  @param hi  Pointer to end of range.
193        *  @return  Pointer to matching char_type if found, else @a hi.
194       */
195       const char_type*
196       scan_is(mask __m, const char_type* __lo, const char_type* __hi) const
197       { return this->do_scan_is(__m, __lo, __hi); }
198
199       /**
200        *  @brief  Find char_type not matching a mask
201        *
202        *  This function searches for and returns the first char_type c in
203        *  [lo,hi) for which is(m,c) is false.  It does so by returning
204        *  ctype<char_type>::do_scan_not().
205        *
206        *  @param m  The mask to compare against.
207        *  @param lo  Pointer to first char in range.
208        *  @param hi  Pointer to end of range.
209        *  @return  Pointer to non-matching char if found, else @a hi.
210       */
211       const char_type*
212       scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
213       { return this->do_scan_not(__m, __lo, __hi); }
214
215       /**
216        *  @brief  Convert to uppercase.
217        *
218        *  This function converts the argument to uppercase if possible.
219        *  If not possible (for example, '2'), returns the argument.  It does
220        *  so by returning ctype<char_type>::do_toupper().
221        *
222        *  @param c  The char_type to convert.
223        *  @return  The uppercase char_type if convertible, else @a c.
224       */
225       char_type
226       toupper(char_type __c) const
227       { return this->do_toupper(__c); }
228
229       /**
230        *  @brief  Convert array to uppercase.
231        *
232        *  This function converts each char_type in the range [lo,hi) to
233        *  uppercase if possible.  Other elements remain untouched.  It does so
234        *  by returning ctype<char_type>:: do_toupper(lo, hi).
235        *
236        *  @param lo  Pointer to start of range.
237        *  @param hi  Pointer to end of range.
238        *  @return  @a hi.
239       */
240       const char_type*
241       toupper(char_type *__lo, const char_type* __hi) const
242       { return this->do_toupper(__lo, __hi); }
243
244       /**
245        *  @brief  Convert to lowercase.
246        *
247        *  This function converts the argument to lowercase if possible.  If
248        *  not possible (for example, '2'), returns the argument.  It does so
249        *  by returning ctype<char_type>::do_tolower(c).
250        *
251        *  @param c  The char_type to convert.
252        *  @return  The lowercase char_type if convertible, else @a c.
253       */
254       char_type
255       tolower(char_type __c) const
256       { return this->do_tolower(__c); }
257
258       /**
259        *  @brief  Convert array to lowercase.
260        *
261        *  This function converts each char_type in the range [lo,hi) to
262        *  lowercase if possible.  Other elements remain untouched.  It does so
263        *  by returning ctype<char_type>:: do_tolower(lo, hi).
264        *
265        *  @param lo  Pointer to start of range.
266        *  @param hi  Pointer to end of range.
267        *  @return  @a hi.
268       */
269       const char_type*
270       tolower(char_type* __lo, const char_type* __hi) const
271       { return this->do_tolower(__lo, __hi); }
272
273       /**
274        *  @brief  Widen char to char_type
275        *
276        *  This function converts the char argument to char_type using the
277        *  simplest reasonable transformation.  It does so by returning
278        *  ctype<char_type>::do_widen(c).
279        *
280        *  Note: this is not what you want for codepage conversions.  See
281        *  codecvt for that.
282        *
283        *  @param c  The char to convert.
284        *  @return  The converted char_type.
285       */
286       char_type
287       widen(char __c) const
288       { return this->do_widen(__c); }
289
290       /**
291        *  @brief  Widen array to char_type
292        *
293        *  This function converts each char in the input to char_type using the
294        *  simplest reasonable transformation.  It does so by returning
295        *  ctype<char_type>::do_widen(c).
296        *
297        *  Note: this is not what you want for codepage conversions.  See
298        *  codecvt for that.
299        *
300        *  @param lo  Pointer to start of range.
301        *  @param hi  Pointer to end of range.
302        *  @param to  Pointer to the destination array.
303        *  @return  @a hi.
304       */
305       const char*
306       widen(const char* __lo, const char* __hi, char_type* __to) const
307       { return this->do_widen(__lo, __hi, __to); }
308
309       /**
310        *  @brief  Narrow char_type to char
311        *
312        *  This function converts the char_type to char using the simplest
313        *  reasonable transformation.  If the conversion fails, dfault is
314        *  returned instead.  It does so by returning
315        *  ctype<char_type>::do_narrow(c).
316        *
317        *  Note: this is not what you want for codepage conversions.  See
318        *  codecvt for that.
319        *
320        *  @param c  The char_type to convert.
321        *  @param dfault  Char to return if conversion fails.
322        *  @return  The converted char.
323       */
324       char
325       narrow(char_type __c, char __dfault) const
326       { return this->do_narrow(__c, __dfault); }
327
328       /**
329        *  @brief  Narrow array to char array
330        *
331        *  This function converts each char_type in the input to char using the
332        *  simplest reasonable transformation and writes the results to the
333        *  destination array.  For any char_type in the input that cannot be
334        *  converted, @a dfault is used instead.  It does so by returning
335        *  ctype<char_type>::do_narrow(lo, hi, dfault, to).
336        *
337        *  Note: this is not what you want for codepage conversions.  See
338        *  codecvt for that.
339        *
340        *  @param lo  Pointer to start of range.
341        *  @param hi  Pointer to end of range.
342        *  @param dfault  Char to use if conversion fails.
343        *  @param to  Pointer to the destination array.
344        *  @return  @a hi.
345       */
346       const char_type*
347       narrow(const char_type* __lo, const char_type* __hi,
348               char __dfault, char *__to) const
349       { return this->do_narrow(__lo, __hi, __dfault, __to); }
350
351     protected:
352       explicit
353       __ctype_abstract_base(size_t __refs = 0): facet(__refs) { }
354
355       virtual
356       ~__ctype_abstract_base() { }
357
358       /**
359        *  @brief  Test char_type classification.
360        *
361        *  This function finds a mask M for @a c and compares it to mask @a m.
362        *
363        *  do_is() is a hook for a derived facet to change the behavior of
364        *  classifying.  do_is() must always return the same result for the
365        *  same input.
366        *
367        *  @param c  The char_type to find the mask of.
368        *  @param m  The mask to compare against.
369        *  @return  (M & m) != 0.
370       */
371       virtual bool
372       do_is(mask __m, char_type __c) const = 0;
373
374       /**
375        *  @brief  Return a mask array.
376        *
377        *  This function finds the mask for each char_type in the range [lo,hi)
378        *  and successively writes it to vec.  vec must have as many elements
379        *  as the input.
380        *
381        *  do_is() is a hook for a derived facet to change the behavior of
382        *  classifying.  do_is() must always return the same result for the
383        *  same input.
384        *
385        *  @param lo  Pointer to start of range.
386        *  @param hi  Pointer to end of range.
387        *  @param vec  Pointer to an array of mask storage.
388        *  @return  @a hi.
389       */
390       virtual const char_type*
391       do_is(const char_type* __lo, const char_type* __hi,
392             mask* __vec) const = 0;
393
394       /**
395        *  @brief  Find char_type matching mask
396        *
397        *  This function searches for and returns the first char_type c in
398        *  [lo,hi) for which is(m,c) is true.
399        *
400        *  do_scan_is() is a hook for a derived facet to change the behavior of
401        *  match searching.  do_is() must always return the same result for the
402        *  same input.
403        *
404        *  @param m  The mask to compare against.
405        *  @param lo  Pointer to start of range.
406        *  @param hi  Pointer to end of range.
407        *  @return  Pointer to a matching char_type if found, else @a hi.
408       */
409       virtual const char_type*
410       do_scan_is(mask __m, const char_type* __lo,
411                  const char_type* __hi) const = 0;
412
413       /**
414        *  @brief  Find char_type not matching mask
415        *
416        *  This function searches for and returns a pointer to the first
417        *  char_type c of [lo,hi) for which is(m,c) is false.
418        *
419        *  do_scan_is() is a hook for a derived facet to change the behavior of
420        *  match searching.  do_is() must always return the same result for the
421        *  same input.
422        *
423        *  @param m  The mask to compare against.
424        *  @param lo  Pointer to start of range.
425        *  @param hi  Pointer to end of range.
426        *  @return  Pointer to a non-matching char_type if found, else @a hi.
427       */
428       virtual const char_type*
429       do_scan_not(mask __m, const char_type* __lo,
430                   const char_type* __hi) const = 0;
431
432       /**
433        *  @brief  Convert to uppercase.
434        *
435        *  This virtual function converts the char_type argument to uppercase
436        *  if possible.  If not possible (for example, '2'), returns the
437        *  argument.
438        *
439        *  do_toupper() is a hook for a derived facet to change the behavior of
440        *  uppercasing.  do_toupper() must always return the same result for
441        *  the same input.
442        *
443        *  @param c  The char_type to convert.
444        *  @return  The uppercase char_type if convertible, else @a c.
445       */
446       virtual char_type
447       do_toupper(char_type) const = 0;
448
449       /**
450        *  @brief  Convert array to uppercase.
451        *
452        *  This virtual function converts each char_type in the range [lo,hi)
453        *  to uppercase if possible.  Other elements remain untouched.
454        *
455        *  do_toupper() is a hook for a derived facet to change the behavior of
456        *  uppercasing.  do_toupper() must always return the same result for
457        *  the same input.
458        *
459        *  @param lo  Pointer to start of range.
460        *  @param hi  Pointer to end of range.
461        *  @return  @a hi.
462       */
463       virtual const char_type*
464       do_toupper(char_type* __lo, const char_type* __hi) const = 0;
465
466       /**
467        *  @brief  Convert to lowercase.
468        *
469        *  This virtual function converts the argument to lowercase if
470        *  possible.  If not possible (for example, '2'), returns the argument.
471        *
472        *  do_tolower() is a hook for a derived facet to change the behavior of
473        *  lowercasing.  do_tolower() must always return the same result for
474        *  the same input.
475        *
476        *  @param c  The char_type to convert.
477        *  @return  The lowercase char_type if convertible, else @a c.
478       */
479       virtual char_type
480       do_tolower(char_type) const = 0;
481
482       /**
483        *  @brief  Convert array to lowercase.
484        *
485        *  This virtual function converts each char_type in the range [lo,hi)
486        *  to lowercase if possible.  Other elements remain untouched.
487        *
488        *  do_tolower() is a hook for a derived facet to change the behavior of
489        *  lowercasing.  do_tolower() must always return the same result for
490        *  the same input.
491        *
492        *  @param lo  Pointer to start of range.
493        *  @param hi  Pointer to end of range.
494        *  @return  @a hi.
495       */
496       virtual const char_type*
497       do_tolower(char_type* __lo, const char_type* __hi) const = 0;
498
499       /**
500        *  @brief  Widen char
501        *
502        *  This virtual function converts the char to char_type using the
503        *  simplest reasonable transformation.
504        *
505        *  do_widen() is a hook for a derived facet to change the behavior of
506        *  widening.  do_widen() must always return the same result for the
507        *  same input.
508        *
509        *  Note: this is not what you want for codepage conversions.  See
510        *  codecvt for that.
511        *
512        *  @param c  The char to convert.
513        *  @return  The converted char_type
514       */
515       virtual char_type
516       do_widen(char) const = 0;
517
518       /**
519        *  @brief  Widen char array
520        *
521        *  This function converts each char in the input to char_type using the
522        *  simplest reasonable transformation.
523        *
524        *  do_widen() is a hook for a derived facet to change the behavior of
525        *  widening.  do_widen() must always return the same result for the
526        *  same input.
527        *
528        *  Note: this is not what you want for codepage conversions.  See
529        *  codecvt for that.
530        *
531        *  @param lo  Pointer to start range.
532        *  @param hi  Pointer to end of range.
533        *  @param to  Pointer to the destination array.
534        *  @return  @a hi.
535       */
536       virtual const char*
537       do_widen(const char* __lo, const char* __hi,
538                char_type* __dest) const = 0;
539
540       /**
541        *  @brief  Narrow char_type to char
542        *
543        *  This virtual function converts the argument to char using the
544        *  simplest reasonable transformation.  If the conversion fails, dfault
545        *  is returned instead.
546        *
547        *  do_narrow() is a hook for a derived facet to change the behavior of
548        *  narrowing.  do_narrow() must always return the same result for the
549        *  same input.
550        *
551        *  Note: this is not what you want for codepage conversions.  See
552        *  codecvt for that.
553        *
554        *  @param c  The char_type to convert.
555        *  @param dfault  Char to return if conversion fails.
556        *  @return  The converted char.
557       */
558       virtual char
559       do_narrow(char_type, char __dfault) const = 0;
560
561       /**
562        *  @brief  Narrow char_type array to char
563        *
564        *  This virtual function converts each char_type in the range [lo,hi) to
565        *  char using the simplest reasonable transformation and writes the
566        *  results to the destination array.  For any element in the input that
567        *  cannot be converted, @a dfault is used instead.
568        *
569        *  do_narrow() is a hook for a derived facet to change the behavior of
570        *  narrowing.  do_narrow() must always return the same result for the
571        *  same input.
572        *
573        *  Note: this is not what you want for codepage conversions.  See
574        *  codecvt for that.
575        *
576        *  @param lo  Pointer to start of range.
577        *  @param hi  Pointer to end of range.
578        *  @param dfault  Char to use if conversion fails.
579        *  @param to  Pointer to the destination array.
580        *  @return  @a hi.
581       */
582       virtual const char_type*
583       do_narrow(const char_type* __lo, const char_type* __hi,
584                 char __dfault, char* __dest) const = 0;
585     };
586
587   // NB: Generic, mostly useless implementation.
588   /**
589    *  @brief  Template ctype facet
590    *
591    *  This template class defines classification and conversion functions for
592    *  character sets.  It wraps <cctype> functionality.  Ctype gets used by
593    *  streams for many I/O operations.
594    *
595    *  This template provides the protected virtual functions the developer
596    *  will have to replace in a derived class or specialization to make a
597    *  working facet.  The public functions that access them are defined in
598    *  __ctype_abstract_base, to allow for implementation flexibility.  See
599    *  ctype<wchar_t> for an example.  The functions are documented in
600    *  __ctype_abstract_base.
601    *
602    *  Note: implementations are provided for all the protected virtual
603    *  functions, but will likely not be useful.
604   */
605   template<typename _CharT>
606     class ctype : public __ctype_abstract_base<_CharT>
607     {
608     public:
609       // Types:
610       typedef _CharT                    char_type;
611       typedef typename __ctype_abstract_base<_CharT>::mask mask;
612
613       /// The facet id for ctype<char_type>
614       static locale::id                 id;
615
616       explicit
617       ctype(size_t __refs = 0) : __ctype_abstract_base<_CharT>(__refs) { }
618
619    protected:
620       virtual
621       ~ctype();
622
623       virtual bool
624       do_is(mask __m, char_type __c) const;
625
626       virtual const char_type*
627       do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
628
629       virtual const char_type*
630       do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
631
632       virtual const char_type*
633       do_scan_not(mask __m, const char_type* __lo,
634                   const char_type* __hi) const;
635
636       virtual char_type
637       do_toupper(char_type __c) const;
638
639       virtual const char_type*
640       do_toupper(char_type* __lo, const char_type* __hi) const;
641
642       virtual char_type
643       do_tolower(char_type __c) const;
644
645       virtual const char_type*
646       do_tolower(char_type* __lo, const char_type* __hi) const;
647
648       virtual char_type
649       do_widen(char __c) const;
650
651       virtual const char*
652       do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
653
654       virtual char
655       do_narrow(char_type, char __dfault) const;
656
657       virtual const char_type*
658       do_narrow(const char_type* __lo, const char_type* __hi,
659                 char __dfault, char* __dest) const;
660     };
661
662   template<typename _CharT>
663     locale::id ctype<_CharT>::id;
664
665   // 22.2.1.3  ctype<char> specialization.
666   /**
667    *  @brief  The ctype<char> specialization.
668    *
669    *  This class defines classification and conversion functions for
670    *  the char type.  It gets used by char streams for many I/O
671    *  operations.  The char specialization provides a number of
672    *  optimizations as well.
673   */
674   template<>
675     class ctype<char> : public locale::facet, public ctype_base
676     {
677     public:
678       // Types:
679       /// Typedef for the template parameter char.
680       typedef char              char_type;
681
682     protected:
683       // Data Members:
684       __c_locale                _M_c_locale_ctype;
685       bool                      _M_del;
686       __to_type                 _M_toupper;
687       __to_type                 _M_tolower;
688       const mask*               _M_table;
689       mutable char              _M_widen_ok;
690       mutable char              _M_widen[1 + static_cast<unsigned char>(-1)];
691       mutable char              _M_narrow[1 + static_cast<unsigned char>(-1)];
692       mutable char              _M_narrow_ok;   // 0 uninitialized, 1 init,
693                                                 // 2 memcpy can't be used
694
695     public:
696       /// The facet id for ctype<char>
697       static locale::id        id;
698       /// The size of the mask table.  It is SCHAR_MAX + 1.
699       static const size_t      table_size = 1 + static_cast<unsigned char>(-1);
700
701       /**
702        *  @brief  Constructor performs initialization.
703        *
704        *  This is the constructor provided by the standard.
705        *
706        *  @param table If non-zero, table is used as the per-char mask.
707        *               Else classic_table() is used.
708        *  @param del   If true, passes ownership of table to this facet.
709        *  @param refs  Passed to the base facet class.
710       */
711       explicit
712       ctype(const mask* __table = 0, bool __del = false, size_t __refs = 0);
713
714       /**
715        *  @brief  Constructor performs static initialization.
716        *
717        *  This constructor is used to construct the initial C locale facet.
718        *
719        *  @param cloc  Handle to C locale data.
720        *  @param table If non-zero, table is used as the per-char mask.
721        *  @param del   If true, passes ownership of table to this facet.
722        *  @param refs  Passed to the base facet class.
723       */
724       explicit
725       ctype(__c_locale __cloc, const mask* __table = 0, bool __del = false,
726             size_t __refs = 0);
727
728       /**
729        *  @brief  Test char classification.
730        *
731        *  This function compares the mask table[c] to @a m.
732        *
733        *  @param c  The char to compare the mask of.
734        *  @param m  The mask to compare against.
735        *  @return  True if m & table[c] is true, false otherwise.
736       */
737       inline bool
738       is(mask __m, char __c) const;
739
740       /**
741        *  @brief  Return a mask array.
742        *
743        *  This function finds the mask for each char in the range [lo, hi) and
744        *  successively writes it to vec.  vec must have as many elements as
745        *  the char array.
746        *
747        *  @param lo  Pointer to start of range.
748        *  @param hi  Pointer to end of range.
749        *  @param vec  Pointer to an array of mask storage.
750        *  @return  @a hi.
751       */
752       inline const char*
753       is(const char* __lo, const char* __hi, mask* __vec) const;
754
755       /**
756        *  @brief  Find char matching a mask
757        *
758        *  This function searches for and returns the first char in [lo,hi) for
759        *  which is(m,char) is true.
760        *
761        *  @param m  The mask to compare against.
762        *  @param lo  Pointer to start of range.
763        *  @param hi  Pointer to end of range.
764        *  @return  Pointer to a matching char if found, else @a hi.
765       */
766       inline const char*
767       scan_is(mask __m, const char* __lo, const char* __hi) const;
768
769       /**
770        *  @brief  Find char not matching a mask
771        *
772        *  This function searches for and returns a pointer to the first char
773        *  in [lo,hi) for which is(m,char) is false.
774        *
775        *  @param m  The mask to compare against.
776        *  @param lo  Pointer to start of range.
777        *  @param hi  Pointer to end of range.
778        *  @return  Pointer to a non-matching char if found, else @a hi.
779       */
780       inline const char*
781       scan_not(mask __m, const char* __lo, const char* __hi) const;
782
783       /**
784        *  @brief  Convert to uppercase.
785        *
786        *  This function converts the char argument to uppercase if possible.
787        *  If not possible (for example, '2'), returns the argument.
788        *
789        *  toupper() acts as if it returns ctype<char>::do_toupper(c).
790        *  do_toupper() must always return the same result for the same input.
791        *
792        *  @param c  The char to convert.
793        *  @return  The uppercase char if convertible, else @a c.
794       */
795       char_type
796       toupper(char_type __c) const
797       { return this->do_toupper(__c); }
798
799       /**
800        *  @brief  Convert array to uppercase.
801        *
802        *  This function converts each char in the range [lo,hi) to uppercase
803        *  if possible.  Other chars remain untouched.
804        *
805        *  toupper() acts as if it returns ctype<char>:: do_toupper(lo, hi).
806        *  do_toupper() must always return the same result for the same input.
807        *
808        *  @param lo  Pointer to first char in range.
809        *  @param hi  Pointer to end of range.
810        *  @return  @a hi.
811       */
812       const char_type*
813       toupper(char_type *__lo, const char_type* __hi) const
814       { return this->do_toupper(__lo, __hi); }
815
816       /**
817        *  @brief  Convert to lowercase.
818        *
819        *  This function converts the char argument to lowercase if possible.
820        *  If not possible (for example, '2'), returns the argument.
821        *
822        *  tolower() acts as if it returns ctype<char>::do_tolower(c).
823        *  do_tolower() must always return the same result for the same input.
824        *
825        *  @param c  The char to convert.
826        *  @return  The lowercase char if convertible, else @a c.
827       */
828       char_type
829       tolower(char_type __c) const
830       { return this->do_tolower(__c); }
831
832       /**
833        *  @brief  Convert array to lowercase.
834        *
835        *  This function converts each char in the range [lo,hi) to lowercase
836        *  if possible.  Other chars remain untouched.
837        *
838        *  tolower() acts as if it returns ctype<char>:: do_tolower(lo, hi).
839        *  do_tolower() must always return the same result for the same input.
840        *
841        *  @param lo  Pointer to first char in range.
842        *  @param hi  Pointer to end of range.
843        *  @return  @a hi.
844       */
845       const char_type*
846       tolower(char_type* __lo, const char_type* __hi) const
847       { return this->do_tolower(__lo, __hi); }
848
849       /**
850        *  @brief  Widen char
851        *
852        *  This function converts the char to char_type using the simplest
853        *  reasonable transformation.  For an underived ctype<char> facet, the
854        *  argument will be returned unchanged.
855        *
856        *  This function works as if it returns ctype<char>::do_widen(c).
857        *  do_widen() must always return the same result for the same input.
858        *
859        *  Note: this is not what you want for codepage conversions.  See
860        *  codecvt for that.
861        *
862        *  @param c  The char to convert.
863        *  @return  The converted character.
864       */
865       char_type
866       widen(char __c) const
867       {
868         if (_M_widen_ok)
869           return _M_widen[static_cast<unsigned char>(__c)];
870         this->_M_widen_init();
871         return this->do_widen(__c);
872       }
873
874       /**
875        *  @brief  Widen char array
876        *
877        *  This function converts each char in the input to char using the
878        *  simplest reasonable transformation.  For an underived ctype<char>
879        *  facet, the argument will be copied unchanged.
880        *
881        *  This function works as if it returns ctype<char>::do_widen(c).
882        *  do_widen() must always return the same result for the same input.
883        *
884        *  Note: this is not what you want for codepage conversions.  See
885        *  codecvt for that.
886        *
887        *  @param lo  Pointer to first char in range.
888        *  @param hi  Pointer to end of range.
889        *  @param to  Pointer to the destination array.
890        *  @return  @a hi.
891       */
892       const char*
893       widen(const char* __lo, const char* __hi, char_type* __to) const
894       {
895         if (_M_widen_ok == 1)
896           {
897             memcpy(__to, __lo, __hi - __lo);
898             return __hi;
899           }
900         if (!_M_widen_ok)
901           _M_widen_init();
902         return this->do_widen(__lo, __hi, __to);
903       }
904
905       /**
906        *  @brief  Narrow char
907        *
908        *  This function converts the char to char using the simplest
909        *  reasonable transformation.  If the conversion fails, dfault is
910        *  returned instead.  For an underived ctype<char> facet, @a c
911        *  will be returned unchanged.
912        *
913        *  This function works as if it returns ctype<char>::do_narrow(c).
914        *  do_narrow() must always return the same result for the same input.
915        *
916        *  Note: this is not what you want for codepage conversions.  See
917        *  codecvt for that.
918        *
919        *  @param c  The char to convert.
920        *  @param dfault  Char to return if conversion fails.
921        *  @return  The converted character.
922       */
923       char
924       narrow(char_type __c, char __dfault) const
925       {
926         if (_M_narrow[static_cast<unsigned char>(__c)])
927           return _M_narrow[static_cast<unsigned char>(__c)];
928         const char __t = do_narrow(__c, __dfault);
929         if (__t != __dfault)
930           _M_narrow[static_cast<unsigned char>(__c)] = __t;
931         return __t;
932       }
933
934       /**
935        *  @brief  Narrow char array
936        *
937        *  This function converts each char in the input to char using the
938        *  simplest reasonable transformation and writes the results to the
939        *  destination array.  For any char in the input that cannot be
940        *  converted, @a dfault is used instead.  For an underived ctype<char>
941        *  facet, the argument will be copied unchanged.
942        *
943        *  This function works as if it returns ctype<char>::do_narrow(lo, hi,
944        *  dfault, to).  do_narrow() must always return the same result for the
945        *  same input.
946        *
947        *  Note: this is not what you want for codepage conversions.  See
948        *  codecvt for that.
949        *
950        *  @param lo  Pointer to start of range.
951        *  @param hi  Pointer to end of range.
952        *  @param dfault  Char to use if conversion fails.
953        *  @param to  Pointer to the destination array.
954        *  @return  @a hi.
955       */
956       const char_type*
957       narrow(const char_type* __lo, const char_type* __hi,
958              char __dfault, char *__to) const
959       {
960         if (__builtin_expect(_M_narrow_ok == 1, true))
961           {
962             memcpy(__to, __lo, __hi - __lo);
963             return __hi;
964           }
965         if (!_M_narrow_ok)
966           _M_narrow_init();
967         return this->do_narrow(__lo, __hi, __dfault, __to);
968       }
969
970     protected:
971       /// Returns a pointer to the mask table provided to the constructor, or
972       /// the default from classic_table() if none was provided.
973       const mask*
974       table() const throw()
975       { return _M_table; }
976
977       /// Returns a pointer to the C locale mask table.
978       static const mask*
979       classic_table() throw();
980
981       /**
982        *  @brief  Destructor.
983        *
984        *  This function deletes table() if @a del was true in the
985        *  constructor.
986       */
987       virtual
988       ~ctype();
989
990       /**
991        *  @brief  Convert to uppercase.
992        *
993        *  This virtual function converts the char argument to uppercase if
994        *  possible.  If not possible (for example, '2'), returns the argument.
995        *
996        *  do_toupper() is a hook for a derived facet to change the behavior of
997        *  uppercasing.  do_toupper() must always return the same result for
998        *  the same input.
999        *
1000        *  @param c  The char to convert.
1001        *  @return  The uppercase char if convertible, else @a c.
1002       */
1003       virtual char_type
1004       do_toupper(char_type) const;
1005
1006       /**
1007        *  @brief  Convert array to uppercase.
1008        *
1009        *  This virtual function converts each char in the range [lo,hi) to
1010        *  uppercase if possible.  Other chars remain untouched.
1011        *
1012        *  do_toupper() is a hook for a derived facet to change the behavior of
1013        *  uppercasing.  do_toupper() must always return the same result for
1014        *  the same input.
1015        *
1016        *  @param lo  Pointer to start of range.
1017        *  @param hi  Pointer to end of range.
1018        *  @return  @a hi.
1019       */
1020       virtual const char_type*
1021       do_toupper(char_type* __lo, const char_type* __hi) const;
1022
1023       /**
1024        *  @brief  Convert to lowercase.
1025        *
1026        *  This virtual function converts the char argument to lowercase if
1027        *  possible.  If not possible (for example, '2'), returns the argument.
1028        *
1029        *  do_tolower() is a hook for a derived facet to change the behavior of
1030        *  lowercasing.  do_tolower() must always return the same result for
1031        *  the same input.
1032        *
1033        *  @param c  The char to convert.
1034        *  @return  The lowercase char if convertible, else @a c.
1035       */
1036       virtual char_type
1037       do_tolower(char_type) const;
1038
1039       /**
1040        *  @brief  Convert array to lowercase.
1041        *
1042        *  This virtual function converts each char in the range [lo,hi) to
1043        *  lowercase if possible.  Other chars remain untouched.
1044        *
1045        *  do_tolower() is a hook for a derived facet to change the behavior of
1046        *  lowercasing.  do_tolower() must always return the same result for
1047        *  the same input.
1048        *
1049        *  @param lo  Pointer to first char in range.
1050        *  @param hi  Pointer to end of range.
1051        *  @return  @a hi.
1052       */
1053       virtual const char_type*
1054       do_tolower(char_type* __lo, const char_type* __hi) const;
1055
1056       /**
1057        *  @brief  Widen char
1058        *
1059        *  This virtual function converts the char to char using the simplest
1060        *  reasonable transformation.  For an underived ctype<char> facet, the
1061        *  argument will be returned unchanged.
1062        *
1063        *  do_widen() is a hook for a derived facet to change the behavior of
1064        *  widening.  do_widen() must always return the same result for the
1065        *  same input.
1066        *
1067        *  Note: this is not what you want for codepage conversions.  See
1068        *  codecvt for that.
1069        *
1070        *  @param c  The char to convert.
1071        *  @return  The converted character.
1072       */
1073       virtual char_type
1074       do_widen(char __c) const
1075       { return __c; }
1076
1077       /**
1078        *  @brief  Widen char array
1079        *
1080        *  This function converts each char in the range [lo,hi) to char using
1081        *  the simplest reasonable transformation.  For an underived
1082        *  ctype<char> facet, the argument will be copied unchanged.
1083        *
1084        *  do_widen() is a hook for a derived facet to change the behavior of
1085        *  widening.  do_widen() must always return the same result for the
1086        *  same input.
1087        *
1088        *  Note: this is not what you want for codepage conversions.  See
1089        *  codecvt for that.
1090        *
1091        *  @param lo  Pointer to start of range.
1092        *  @param hi  Pointer to end of range.
1093        *  @param to  Pointer to the destination array.
1094        *  @return  @a hi.
1095       */
1096       virtual const char*
1097       do_widen(const char* __lo, const char* __hi, char_type* __dest) const
1098       {
1099         memcpy(__dest, __lo, __hi - __lo);
1100         return __hi;
1101       }
1102
1103       /**
1104        *  @brief  Narrow char
1105        *
1106        *  This virtual function converts the char to char using the simplest
1107        *  reasonable transformation.  If the conversion fails, dfault is
1108        *  returned instead.  For an underived ctype<char> facet, @a c will be
1109        *  returned unchanged.
1110        *
1111        *  do_narrow() is a hook for a derived facet to change the behavior of
1112        *  narrowing.  do_narrow() must always return the same result for the
1113        *  same input.
1114        *
1115        *  Note: this is not what you want for codepage conversions.  See
1116        *  codecvt for that.
1117        *
1118        *  @param c  The char to convert.
1119        *  @param dfault  Char to return if conversion fails.
1120        *  @return  The converted char.
1121       */
1122       virtual char
1123       do_narrow(char_type __c, char) const
1124       { return __c; }
1125
1126       /**
1127        *  @brief  Narrow char array to char array
1128        *
1129        *  This virtual function converts each char in the range [lo,hi) to
1130        *  char using the simplest reasonable transformation and writes the
1131        *  results to the destination array.  For any char in the input that
1132        *  cannot be converted, @a dfault is used instead.  For an underived
1133        *  ctype<char> facet, the argument will be copied unchanged.
1134        *
1135        *  do_narrow() is a hook for a derived facet to change the behavior of
1136        *  narrowing.  do_narrow() must always return the same result for the
1137        *  same input.
1138        *
1139        *  Note: this is not what you want for codepage conversions.  See
1140        *  codecvt for that.
1141        *
1142        *  @param lo  Pointer to start of range.
1143        *  @param hi  Pointer to end of range.
1144        *  @param dfault  Char to use if conversion fails.
1145        *  @param to  Pointer to the destination array.
1146        *  @return  @a hi.
1147       */
1148       virtual const char_type*
1149       do_narrow(const char_type* __lo, const char_type* __hi,
1150                 char, char* __dest) const
1151       {
1152         memcpy(__dest, __lo, __hi - __lo);
1153         return __hi;
1154       }
1155
1156     private:
1157
1158       void _M_widen_init() const
1159       {
1160         char __tmp[sizeof(_M_widen)];
1161         for (size_t __i = 0; __i < sizeof(_M_widen); ++__i)
1162           __tmp[__i] = __i;
1163         do_widen(__tmp, __tmp + sizeof(__tmp), _M_widen);
1164
1165         _M_widen_ok = 1;
1166         // Set _M_widen_ok to 2 if memcpy can't be used.
1167         if (memcmp(__tmp, _M_widen, sizeof(_M_widen)))
1168           _M_widen_ok = 2;
1169       }
1170
1171       // Fill in the narrowing cache and flag whether all values are
1172       // valid or not.  _M_narrow_ok is set to 2 if memcpy can't
1173       // be used.
1174       void _M_narrow_init() const
1175       {
1176         char __tmp[sizeof(_M_narrow)];
1177         for (size_t __i = 0; __i < sizeof(_M_narrow); ++__i)
1178           __tmp[__i] = __i;
1179         do_narrow(__tmp, __tmp + sizeof(__tmp), 0, _M_narrow);
1180
1181         _M_narrow_ok = 1;
1182         if (memcmp(__tmp, _M_narrow, sizeof(_M_narrow)))
1183           _M_narrow_ok = 2;
1184         else
1185           {
1186             // Deal with the special case of zero: renarrow with a
1187             // different default and compare.
1188             char __c;
1189             do_narrow(__tmp, __tmp + 1, 1, &__c);
1190             if (__c == 1)
1191               _M_narrow_ok = 2;
1192           }
1193       }
1194     };
1195
1196   template<>
1197     const ctype<char>&
1198     use_facet<ctype<char> >(const locale& __loc);
1199
1200 #ifdef _GLIBCXX_USE_WCHAR_T
1201   // 22.2.1.3  ctype<wchar_t> specialization
1202   /**
1203    *  @brief  The ctype<wchar_t> specialization.
1204    *
1205    *  This class defines classification and conversion functions for the
1206    *  wchar_t type.  It gets used by wchar_t streams for many I/O operations.
1207    *  The wchar_t specialization provides a number of optimizations as well.
1208    *
1209    *  ctype<wchar_t> inherits its public methods from
1210    *  __ctype_abstract_base<wchar_t>.
1211   */
1212   template<>
1213     class ctype<wchar_t> : public __ctype_abstract_base<wchar_t>
1214     {
1215     public:
1216       // Types:
1217       /// Typedef for the template parameter wchar_t.
1218       typedef wchar_t           char_type;
1219       typedef wctype_t          __wmask_type;
1220
1221     protected:
1222       __c_locale                _M_c_locale_ctype;
1223
1224       // Pre-computed narrowed and widened chars.
1225       bool                      _M_narrow_ok;
1226       char                      _M_narrow[128];
1227       wint_t                    _M_widen[1 + static_cast<unsigned char>(-1)];
1228
1229       // Pre-computed elements for do_is.
1230       mask                      _M_bit[16];
1231       __wmask_type              _M_wmask[16];
1232
1233     public:
1234       // Data Members:
1235       /// The facet id for ctype<wchar_t>
1236       static locale::id         id;
1237
1238       /**
1239        *  @brief  Constructor performs initialization.
1240        *
1241        *  This is the constructor provided by the standard.
1242        *
1243        *  @param refs  Passed to the base facet class.
1244       */
1245       explicit
1246       ctype(size_t __refs = 0);
1247
1248       /**
1249        *  @brief  Constructor performs static initialization.
1250        *
1251        *  This constructor is used to construct the initial C locale facet.
1252        *
1253        *  @param cloc  Handle to C locale data.
1254        *  @param refs  Passed to the base facet class.
1255       */
1256       explicit
1257       ctype(__c_locale __cloc, size_t __refs = 0);
1258
1259     protected:
1260       __wmask_type
1261       _M_convert_to_wmask(const mask __m) const;
1262
1263       /// Destructor
1264       virtual
1265       ~ctype();
1266
1267       /**
1268        *  @brief  Test wchar_t classification.
1269        *
1270        *  This function finds a mask M for @a c and compares it to mask @a m.
1271        *
1272        *  do_is() is a hook for a derived facet to change the behavior of
1273        *  classifying.  do_is() must always return the same result for the
1274        *  same input.
1275        *
1276        *  @param c  The wchar_t to find the mask of.
1277        *  @param m  The mask to compare against.
1278        *  @return  (M & m) != 0.
1279       */
1280       virtual bool
1281       do_is(mask __m, char_type __c) const;
1282
1283       /**
1284        *  @brief  Return a mask array.
1285        *
1286        *  This function finds the mask for each wchar_t in the range [lo,hi)
1287        *  and successively writes it to vec.  vec must have as many elements
1288        *  as the input.
1289        *
1290        *  do_is() is a hook for a derived facet to change the behavior of
1291        *  classifying.  do_is() must always return the same result for the
1292        *  same input.
1293        *
1294        *  @param lo  Pointer to start of range.
1295        *  @param hi  Pointer to end of range.
1296        *  @param vec  Pointer to an array of mask storage.
1297        *  @return  @a hi.
1298       */
1299       virtual const char_type*
1300       do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
1301
1302       /**
1303        *  @brief  Find wchar_t matching mask
1304        *
1305        *  This function searches for and returns the first wchar_t c in
1306        *  [lo,hi) for which is(m,c) is true.
1307        *
1308        *  do_scan_is() is a hook for a derived facet to change the behavior of
1309        *  match searching.  do_is() must always return the same result for the
1310        *  same input.
1311        *
1312        *  @param m  The mask to compare against.
1313        *  @param lo  Pointer to start of range.
1314        *  @param hi  Pointer to end of range.
1315        *  @return  Pointer to a matching wchar_t if found, else @a hi.
1316       */
1317       virtual const char_type*
1318       do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
1319
1320       /**
1321        *  @brief  Find wchar_t not matching mask
1322        *
1323        *  This function searches for and returns a pointer to the first
1324        *  wchar_t c of [lo,hi) for which is(m,c) is false.
1325        *
1326        *  do_scan_is() is a hook for a derived facet to change the behavior of
1327        *  match searching.  do_is() must always return the same result for the
1328        *  same input.
1329        *
1330        *  @param m  The mask to compare against.
1331        *  @param lo  Pointer to start of range.
1332        *  @param hi  Pointer to end of range.
1333        *  @return  Pointer to a non-matching wchar_t if found, else @a hi.
1334       */
1335       virtual const char_type*
1336       do_scan_not(mask __m, const char_type* __lo,
1337                   const char_type* __hi) const;
1338
1339       /**
1340        *  @brief  Convert to uppercase.
1341        *
1342        *  This virtual function converts the wchar_t argument to uppercase if
1343        *  possible.  If not possible (for example, '2'), returns the argument.
1344        *
1345        *  do_toupper() is a hook for a derived facet to change the behavior of
1346        *  uppercasing.  do_toupper() must always return the same result for
1347        *  the same input.
1348        *
1349        *  @param c  The wchar_t to convert.
1350        *  @return  The uppercase wchar_t if convertible, else @a c.
1351       */
1352       virtual char_type
1353       do_toupper(char_type) const;
1354
1355       /**
1356        *  @brief  Convert array to uppercase.
1357        *
1358        *  This virtual function converts each wchar_t in the range [lo,hi) to
1359        *  uppercase if possible.  Other elements remain untouched.
1360        *
1361        *  do_toupper() is a hook for a derived facet to change the behavior of
1362        *  uppercasing.  do_toupper() must always return the same result for
1363        *  the same input.
1364        *
1365        *  @param lo  Pointer to start of range.
1366        *  @param hi  Pointer to end of range.
1367        *  @return  @a hi.
1368       */
1369       virtual const char_type*
1370       do_toupper(char_type* __lo, const char_type* __hi) const;
1371
1372       /**
1373        *  @brief  Convert to lowercase.
1374        *
1375        *  This virtual function converts the argument to lowercase if
1376        *  possible.  If not possible (for example, '2'), returns the argument.
1377        *
1378        *  do_tolower() is a hook for a derived facet to change the behavior of
1379        *  lowercasing.  do_tolower() must always return the same result for
1380        *  the same input.
1381        *
1382        *  @param c  The wchar_t to convert.
1383        *  @return  The lowercase wchar_t if convertible, else @a c.
1384       */
1385       virtual char_type
1386       do_tolower(char_type) const;
1387
1388       /**
1389        *  @brief  Convert array to lowercase.
1390        *
1391        *  This virtual function converts each wchar_t in the range [lo,hi) to
1392        *  lowercase if possible.  Other elements remain untouched.
1393        *
1394        *  do_tolower() is a hook for a derived facet to change the behavior of
1395        *  lowercasing.  do_tolower() must always return the same result for
1396        *  the same input.
1397        *
1398        *  @param lo  Pointer to start of range.
1399        *  @param hi  Pointer to end of range.
1400        *  @return  @a hi.
1401       */
1402       virtual const char_type*
1403       do_tolower(char_type* __lo, const char_type* __hi) const;
1404
1405       /**
1406        *  @brief  Widen char to wchar_t
1407        *
1408        *  This virtual function converts the char to wchar_t using the
1409        *  simplest reasonable transformation.  For an underived ctype<wchar_t>
1410        *  facet, the argument will be cast to wchar_t.
1411        *
1412        *  do_widen() is a hook for a derived facet to change the behavior of
1413        *  widening.  do_widen() must always return the same result for the
1414        *  same input.
1415        *
1416        *  Note: this is not what you want for codepage conversions.  See
1417        *  codecvt for that.
1418        *
1419        *  @param c  The char to convert.
1420        *  @return  The converted wchar_t.
1421       */
1422       virtual char_type
1423       do_widen(char) const;
1424
1425       /**
1426        *  @brief  Widen char array to wchar_t array
1427        *
1428        *  This function converts each char in the input to wchar_t using the
1429        *  simplest reasonable transformation.  For an underived ctype<wchar_t>
1430        *  facet, the argument will be copied, casting each element to wchar_t.
1431        *
1432        *  do_widen() is a hook for a derived facet to change the behavior of
1433        *  widening.  do_widen() must always return the same result for the
1434        *  same input.
1435        *
1436        *  Note: this is not what you want for codepage conversions.  See
1437        *  codecvt for that.
1438        *
1439        *  @param lo  Pointer to start range.
1440        *  @param hi  Pointer to end of range.
1441        *  @param to  Pointer to the destination array.
1442        *  @return  @a hi.
1443       */
1444       virtual const char*
1445       do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
1446
1447       /**
1448        *  @brief  Narrow wchar_t to char
1449        *
1450        *  This virtual function converts the argument to char using
1451        *  the simplest reasonable transformation.  If the conversion
1452        *  fails, dfault is returned instead.  For an underived
1453        *  ctype<wchar_t> facet, @a c will be cast to char and
1454        *  returned.
1455        *
1456        *  do_narrow() is a hook for a derived facet to change the
1457        *  behavior of narrowing.  do_narrow() must always return the
1458        *  same result for the same input.
1459        *
1460        *  Note: this is not what you want for codepage conversions.  See
1461        *  codecvt for that.
1462        *
1463        *  @param c  The wchar_t to convert.
1464        *  @param dfault  Char to return if conversion fails.
1465        *  @return  The converted char.
1466       */
1467       virtual char
1468       do_narrow(char_type, char __dfault) const;
1469
1470       /**
1471        *  @brief  Narrow wchar_t array to char array
1472        *
1473        *  This virtual function converts each wchar_t in the range [lo,hi) to
1474        *  char using the simplest reasonable transformation and writes the
1475        *  results to the destination array.  For any wchar_t in the input that
1476        *  cannot be converted, @a dfault is used instead.  For an underived
1477        *  ctype<wchar_t> facet, the argument will be copied, casting each
1478        *  element to char.
1479        *
1480        *  do_narrow() is a hook for a derived facet to change the behavior of
1481        *  narrowing.  do_narrow() must always return the same result for the
1482        *  same input.
1483        *
1484        *  Note: this is not what you want for codepage conversions.  See
1485        *  codecvt for that.
1486        *
1487        *  @param lo  Pointer to start of range.
1488        *  @param hi  Pointer to end of range.
1489        *  @param dfault  Char to use if conversion fails.
1490        *  @param to  Pointer to the destination array.
1491        *  @return  @a hi.
1492       */
1493       virtual const char_type*
1494       do_narrow(const char_type* __lo, const char_type* __hi,
1495                 char __dfault, char* __dest) const;
1496
1497       // For use at construction time only.
1498       void
1499       _M_initialize_ctype();
1500     };
1501
1502   template<>
1503     const ctype<wchar_t>&
1504     use_facet<ctype<wchar_t> >(const locale& __loc);
1505 #endif //_GLIBCXX_USE_WCHAR_T
1506
1507   // Include host and configuration specific ctype inlines.
1508   #include <bits/ctype_inline.h>
1509
1510   /// @brief  class ctype_byname [22.2.1.2].
1511   template<typename _CharT>
1512     class ctype_byname : public ctype<_CharT>
1513     {
1514     public:
1515       typedef _CharT            char_type;
1516
1517       explicit
1518       ctype_byname(const char* __s, size_t __refs = 0);
1519
1520     protected:
1521       virtual
1522       ~ctype_byname() { };
1523     };
1524
1525   /// 22.2.1.4  Class ctype_byname specializations.
1526   template<>
1527     ctype_byname<char>::ctype_byname(const char*, size_t refs);
1528
1529   template<>
1530     ctype_byname<wchar_t>::ctype_byname(const char*, size_t refs);
1531
1532   // 22.2.1.5  Template class codecvt
1533   #include <bits/codecvt.h>
1534
1535   // 22.2.2  The numeric category.
1536   class __num_base
1537   {
1538   public:
1539     // NB: Code depends on the order of _S_atoms_out elements.
1540     // Below are the indices into _S_atoms_out.
1541     enum
1542       {
1543         _S_ominus,
1544         _S_oplus,
1545         _S_ox,
1546         _S_oX,
1547         _S_odigits,
1548         _S_odigits_end = _S_odigits + 16,
1549         _S_oudigits = _S_odigits_end,
1550         _S_oudigits_end = _S_oudigits + 16,
1551         _S_oe = _S_odigits + 14,  // For scientific notation, 'e'
1552         _S_oE = _S_oudigits + 14, // For scientific notation, 'E'
1553         _S_oend = _S_oudigits_end
1554       };
1555
1556     // A list of valid numeric literals for output.  This array
1557     // contains chars that will be passed through the current locale's
1558     // ctype<_CharT>.widen() and then used to render numbers.
1559     // For the standard "C" locale, this is
1560     // "-+xX0123456789abcdef0123456789ABCDEF".
1561     static const char* _S_atoms_out;
1562
1563     // String literal of acceptable (narrow) input, for num_get.
1564     // "-+xX0123456789abcdefABCDEF"
1565     static const char* _S_atoms_in;
1566
1567     enum
1568     {
1569       _S_iminus,
1570       _S_iplus,
1571       _S_ix,
1572       _S_iX,
1573       _S_izero,
1574       _S_ie = _S_izero + 14,
1575       _S_iE = _S_izero + 20,
1576       _S_iend = 26
1577     };
1578
1579     // num_put
1580     // Construct and return valid scanf format for floating point types.
1581     static void
1582     _S_format_float(const ios_base& __io, char* __fptr, char __mod);
1583   };
1584
1585   template<typename _CharT>
1586     struct __numpunct_cache : public locale::facet
1587     {
1588       const char*                       _M_grouping;
1589       size_t                            _M_grouping_size;
1590       bool                              _M_use_grouping;
1591       const _CharT*                     _M_truename;
1592       size_t                            _M_truename_size;
1593       const _CharT*                     _M_falsename;
1594       size_t                            _M_falsename_size;
1595       _CharT                            _M_decimal_point;
1596       _CharT                            _M_thousands_sep;
1597
1598       // A list of valid numeric literals for output: in the standard
1599       // "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF".
1600       // This array contains the chars after having been passed
1601       // through the current locale's ctype<_CharT>.widen().
1602       _CharT                            _M_atoms_out[__num_base::_S_oend];
1603
1604       // A list of valid numeric literals for input: in the standard
1605       // "C" locale, this is "-+xX0123456789abcdefABCDEF"
1606       // This array contains the chars after having been passed
1607       // through the current locale's ctype<_CharT>.widen().
1608       _CharT                            _M_atoms_in[__num_base::_S_iend];
1609
1610       bool                              _M_allocated;
1611
1612       __numpunct_cache(size_t __refs = 0) : facet(__refs),
1613       _M_grouping(NULL), _M_grouping_size(0), _M_use_grouping(false),
1614       _M_truename(NULL), _M_truename_size(0), _M_falsename(NULL),
1615       _M_falsename_size(0), _M_decimal_point(_CharT()),
1616       _M_thousands_sep(_CharT()), _M_allocated(false)
1617       { }
1618
1619       ~__numpunct_cache();
1620
1621       void
1622       _M_cache(const locale& __loc);
1623
1624     private:
1625       __numpunct_cache&
1626       operator=(const __numpunct_cache&);
1627       
1628       explicit
1629       __numpunct_cache(const __numpunct_cache&);
1630     };
1631
1632   template<typename _CharT>
1633     __numpunct_cache<_CharT>::~__numpunct_cache()
1634     {
1635       if (_M_allocated)
1636         {
1637           delete [] _M_grouping;
1638           delete [] _M_truename;
1639           delete [] _M_falsename;
1640         }
1641     }
1642
1643   /**
1644    *  @brief  Numpunct facet.
1645    *
1646    *  This facet stores several pieces of information related to printing and
1647    *  scanning numbers, such as the decimal point character.  It takes a
1648    *  template parameter specifying the char type.  The numpunct facet is
1649    *  used by streams for many I/O operations involving numbers.
1650    *
1651    *  The numpunct template uses protected virtual functions to provide the
1652    *  actual results.  The public accessors forward the call to the virtual
1653    *  functions.  These virtual functions are hooks for developers to
1654    *  implement the behavior they require from a numpunct facet.
1655   */
1656   template<typename _CharT>
1657     class numpunct : public locale::facet
1658     {
1659     public:
1660       // Types:
1661       //@{
1662       /// Public typedefs
1663       typedef _CharT                    char_type;
1664       typedef basic_string<_CharT>      string_type;
1665       //@}
1666       typedef __numpunct_cache<_CharT>  __cache_type;
1667
1668     protected:
1669       __cache_type*                     _M_data;
1670
1671     public:
1672       /// Numpunct facet id.
1673       static locale::id                 id;
1674
1675       /**
1676        *  @brief  Numpunct constructor.
1677        *
1678        *  @param  refs  Refcount to pass to the base class.
1679        */
1680       explicit
1681       numpunct(size_t __refs = 0) : facet(__refs), _M_data(NULL)
1682       { _M_initialize_numpunct(); }
1683
1684       /**
1685        *  @brief  Internal constructor.  Not for general use.
1686        *
1687        *  This is a constructor for use by the library itself to set up the
1688        *  predefined locale facets.
1689        *
1690        *  @param  cache  __numpunct_cache object.
1691        *  @param  refs  Refcount to pass to the base class.
1692        */
1693       explicit
1694       numpunct(__cache_type* __cache, size_t __refs = 0)
1695       : facet(__refs), _M_data(__cache)
1696       { _M_initialize_numpunct(); }
1697
1698       /**
1699        *  @brief  Internal constructor.  Not for general use.
1700        *
1701        *  This is a constructor for use by the library itself to set up new
1702        *  locales.
1703        *
1704        *  @param  cloc  The "C" locale.
1705        *  @param  refs  Refcount to pass to the base class.
1706        */
1707       explicit
1708       numpunct(__c_locale __cloc, size_t __refs = 0)
1709       : facet(__refs), _M_data(NULL)
1710       { _M_initialize_numpunct(__cloc); }
1711
1712       /**
1713        *  @brief  Return decimal point character.
1714        *
1715        *  This function returns a char_type to use as a decimal point.  It
1716        *  does so by returning returning
1717        *  numpunct<char_type>::do_decimal_point().
1718        *
1719        *  @return  @a char_type representing a decimal point.
1720       */
1721       char_type
1722       decimal_point() const
1723       { return this->do_decimal_point(); }
1724
1725       /**
1726        *  @brief  Return thousands separator character.
1727        *
1728        *  This function returns a char_type to use as a thousands
1729        *  separator.  It does so by returning returning
1730        *  numpunct<char_type>::do_thousands_sep().
1731        *
1732        *  @return  char_type representing a thousands separator.
1733       */
1734       char_type
1735       thousands_sep() const
1736       { return this->do_thousands_sep(); }
1737
1738       /**
1739        *  @brief  Return grouping specification.
1740        *
1741        *  This function returns a string representing groupings for the
1742        *  integer part of a number.  Groupings indicate where thousands
1743        *  separators should be inserted in the integer part of a number.
1744        *
1745        *  Each char in the return string is interpret as an integer
1746        *  rather than a character.  These numbers represent the number
1747        *  of digits in a group.  The first char in the string
1748        *  represents the number of digits in the least significant
1749        *  group.  If a char is negative, it indicates an unlimited
1750        *  number of digits for the group.  If more chars from the
1751        *  string are required to group a number, the last char is used
1752        *  repeatedly.
1753        *
1754        *  For example, if the grouping() returns "\003\002" and is
1755        *  applied to the number 123456789, this corresponds to
1756        *  12,34,56,789.  Note that if the string was "32", this would
1757        *  put more than 50 digits into the least significant group if
1758        *  the character set is ASCII.
1759        *
1760        *  The string is returned by calling
1761        *  numpunct<char_type>::do_grouping().
1762        *
1763        *  @return  string representing grouping specification.
1764       */
1765       string
1766       grouping() const
1767       { return this->do_grouping(); }
1768
1769       /**
1770        *  @brief  Return string representation of bool true.
1771        *
1772        *  This function returns a string_type containing the text
1773        *  representation for true bool variables.  It does so by calling
1774        *  numpunct<char_type>::do_truename().
1775        *
1776        *  @return  string_type representing printed form of true.
1777       */
1778       string_type
1779       truename() const
1780       { return this->do_truename(); }
1781
1782       /**
1783        *  @brief  Return string representation of bool false.
1784        *
1785        *  This function returns a string_type containing the text
1786        *  representation for false bool variables.  It does so by calling
1787        *  numpunct<char_type>::do_falsename().
1788        *
1789        *  @return  string_type representing printed form of false.
1790       */
1791       string_type
1792       falsename() const
1793       { return this->do_falsename(); }
1794
1795     protected:
1796       /// Destructor.
1797       virtual
1798       ~numpunct();
1799
1800       /**
1801        *  @brief  Return decimal point character.
1802        *
1803        *  Returns a char_type to use as a decimal point.  This function is a
1804        *  hook for derived classes to change the value returned.
1805        *
1806        *  @return  @a char_type representing a decimal point.
1807       */
1808       virtual char_type
1809       do_decimal_point() const
1810       { return _M_data->_M_decimal_point; }
1811
1812       /**
1813        *  @brief  Return thousands separator character.
1814        *
1815        *  Returns a char_type to use as a thousands separator.  This function
1816        *  is a hook for derived classes to change the value returned.
1817        *
1818        *  @return  @a char_type representing a thousands separator.
1819       */
1820       virtual char_type
1821       do_thousands_sep() const
1822       { return _M_data->_M_thousands_sep; }
1823
1824       /**
1825        *  @brief  Return grouping specification.
1826        *
1827        *  Returns a string representing groupings for the integer part of a
1828        *  number.  This function is a hook for derived classes to change the
1829        *  value returned.  @see grouping() for details.
1830        *
1831        *  @return  String representing grouping specification.
1832       */
1833       virtual string
1834       do_grouping() const
1835       { return _M_data->_M_grouping; }
1836
1837       /**
1838        *  @brief  Return string representation of bool true.
1839        *
1840        *  Returns a string_type containing the text representation for true
1841        *  bool variables.  This function is a hook for derived classes to
1842        *  change the value returned.
1843        *
1844        *  @return  string_type representing printed form of true.
1845       */
1846       virtual string_type
1847       do_truename() const
1848       { return _M_data->_M_truename; }
1849
1850       /**
1851        *  @brief  Return string representation of bool false.
1852        *
1853        *  Returns a string_type containing the text representation for false
1854        *  bool variables.  This function is a hook for derived classes to
1855        *  change the value returned.
1856        *
1857        *  @return  string_type representing printed form of false.
1858       */
1859       virtual string_type
1860       do_falsename() const
1861       { return _M_data->_M_falsename; }
1862
1863       // For use at construction time only.
1864       void
1865       _M_initialize_numpunct(__c_locale __cloc = NULL);
1866     };
1867
1868   template<typename _CharT>
1869     locale::id numpunct<_CharT>::id;
1870
1871   template<>
1872     numpunct<char>::~numpunct();
1873
1874   template<>
1875     void
1876     numpunct<char>::_M_initialize_numpunct(__c_locale __cloc);
1877
1878 #ifdef _GLIBCXX_USE_WCHAR_T
1879   template<>
1880     numpunct<wchar_t>::~numpunct();
1881
1882   template<>
1883     void
1884     numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc);
1885 #endif
1886
1887   /// @brief  class numpunct_byname [22.2.3.2].
1888   template<typename _CharT>
1889     class numpunct_byname : public numpunct<_CharT>
1890     {
1891     public:
1892       typedef _CharT                    char_type;
1893       typedef basic_string<_CharT>      string_type;
1894
1895       explicit
1896       numpunct_byname(const char* __s, size_t __refs = 0)
1897       : numpunct<_CharT>(__refs)
1898       {
1899         if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
1900           {
1901             __c_locale __tmp;
1902             this->_S_create_c_locale(__tmp, __s);
1903             this->_M_initialize_numpunct(__tmp);
1904             this->_S_destroy_c_locale(__tmp);
1905           }
1906       }
1907
1908     protected:
1909       virtual
1910       ~numpunct_byname() { }
1911     };
1912
1913   /**
1914    *  @brief  Facet for parsing number strings.
1915    *
1916    *  This facet encapsulates the code to parse and return a number
1917    *  from a string.  It is used by the istream numeric extraction
1918    *  operators.
1919    *
1920    *  The num_get template uses protected virtual functions to provide the
1921    *  actual results.  The public accessors forward the call to the virtual
1922    *  functions.  These virtual functions are hooks for developers to
1923    *  implement the behavior they require from the num_get facet.
1924   */
1925   template<typename _CharT, typename _InIter>
1926     class num_get : public locale::facet
1927     {
1928     public:
1929       // Types:
1930       //@{
1931       /// Public typedefs
1932       typedef _CharT                    char_type;
1933       typedef _InIter                   iter_type;
1934       //@}
1935
1936       /// Numpunct facet id.
1937       static locale::id                 id;
1938
1939       /**
1940        *  @brief  Constructor performs initialization.
1941        *
1942        *  This is the constructor provided by the standard.
1943        *
1944        *  @param refs  Passed to the base facet class.
1945       */
1946       explicit
1947       num_get(size_t __refs = 0) : facet(__refs) { }
1948
1949       /**
1950        *  @brief  Numeric parsing.
1951        *
1952        *  Parses the input stream into the bool @a v.  It does so by calling
1953        *  num_get::do_get().
1954        *
1955        *  If ios_base::boolalpha is set, attempts to read
1956        *  ctype<CharT>::truename() or ctype<CharT>::falsename().  Sets
1957        *  @a v to true or false if successful.  Sets err to
1958        *  ios_base::failbit if reading the string fails.  Sets err to
1959        *  ios_base::eofbit if the stream is emptied.
1960        *
1961        *  If ios_base::boolalpha is not set, proceeds as with reading a long,
1962        *  except if the value is 1, sets @a v to true, if the value is 0, sets
1963        *  @a v to false, and otherwise set err to ios_base::failbit.
1964        *
1965        *  @param  in  Start of input stream.
1966        *  @param  end  End of input stream.
1967        *  @param  io  Source of locale and flags.
1968        *  @param  err  Error flags to set.
1969        *  @param  v  Value to format and insert.
1970        *  @return  Iterator after reading.
1971       */
1972       iter_type
1973       get(iter_type __in, iter_type __end, ios_base& __io,
1974           ios_base::iostate& __err, bool& __v) const
1975       { return this->do_get(__in, __end, __io, __err, __v); }
1976
1977       //@{
1978       /**
1979        *  @brief  Numeric parsing.
1980        *
1981        *  Parses the input stream into the integral variable @a v.  It does so
1982        *  by calling num_get::do_get().
1983        *
1984        *  Parsing is affected by the flag settings in @a io.
1985        *
1986        *  The basic parse is affected by the value of io.flags() &
1987        *  ios_base::basefield.  If equal to ios_base::oct, parses like the
1988        *  scanf %o specifier.  Else if equal to ios_base::hex, parses like %X
1989        *  specifier.  Else if basefield equal to 0, parses like the %i
1990        *  specifier.  Otherwise, parses like %d for signed and %u for unsigned
1991        *  types.  The matching type length modifier is also used.
1992        *
1993        *  Digit grouping is intrepreted according to numpunct::grouping() and
1994        *  numpunct::thousands_sep().  If the pattern of digit groups isn't
1995        *  consistent, sets err to ios_base::failbit.
1996        *
1997        *  If parsing the string yields a valid value for @a v, @a v is set.
1998        *  Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
1999        *  Sets err to ios_base::eofbit if the stream is emptied.
2000        *
2001        *  @param  in  Start of input stream.
2002        *  @param  end  End of input stream.
2003        *  @param  io  Source of locale and flags.
2004        *  @param  err  Error flags to set.
2005        *  @param  v  Value to format and insert.
2006        *  @return  Iterator after reading.
2007       */
2008       iter_type
2009       get(iter_type __in, iter_type __end, ios_base& __io,
2010           ios_base::iostate& __err, long& __v) const
2011       { return this->do_get(__in, __end, __io, __err, __v); }
2012
2013       iter_type
2014       get(iter_type __in, iter_type __end, ios_base& __io,
2015           ios_base::iostate& __err, unsigned short& __v) const
2016       { return this->do_get(__in, __end, __io, __err, __v); }
2017
2018       iter_type
2019       get(iter_type __in, iter_type __end, ios_base& __io,
2020           ios_base::iostate& __err, unsigned int& __v)   const
2021       { return this->do_get(__in, __end, __io, __err, __v); }
2022
2023       iter_type
2024       get(iter_type __in, iter_type __end, ios_base& __io,
2025           ios_base::iostate& __err, unsigned long& __v)  const
2026       { return this->do_get(__in, __end, __io, __err, __v); }
2027
2028 #ifdef _GLIBCXX_USE_LONG_LONG
2029       iter_type
2030       get(iter_type __in, iter_type __end, ios_base& __io,
2031           ios_base::iostate& __err, long long& __v) const
2032       { return this->do_get(__in, __end, __io, __err, __v); }
2033
2034       iter_type
2035       get(iter_type __in, iter_type __end, ios_base& __io,
2036           ios_base::iostate& __err, unsigned long long& __v)  const
2037       { return this->do_get(__in, __end, __io, __err, __v); }
2038 #endif
2039       //@}
2040
2041       //@{
2042       /**
2043        *  @brief  Numeric parsing.
2044        *
2045        *  Parses the input stream into the integral variable @a v.  It does so
2046        *  by calling num_get::do_get().
2047        *
2048        *  The input characters are parsed like the scanf %g specifier.  The
2049        *  matching type length modifier is also used.
2050        *
2051        *  The decimal point character used is numpunct::decimal_point().
2052        *  Digit grouping is intrepreted according to numpunct::grouping() and
2053        *  numpunct::thousands_sep().  If the pattern of digit groups isn't
2054        *  consistent, sets err to ios_base::failbit.
2055        *
2056        *  If parsing the string yields a valid value for @a v, @a v is set.
2057        *  Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2058        *  Sets err to ios_base::eofbit if the stream is emptied.
2059        *
2060        *  @param  in  Start of input stream.
2061        *  @param  end  End of input stream.
2062        *  @param  io  Source of locale and flags.
2063        *  @param  err  Error flags to set.
2064        *  @param  v  Value to format and insert.
2065        *  @return  Iterator after reading.
2066       */
2067       iter_type
2068       get(iter_type __in, iter_type __end, ios_base& __io,
2069           ios_base::iostate& __err, float& __v) const
2070       { return this->do_get(__in, __end, __io, __err, __v); }
2071
2072       iter_type
2073       get(iter_type __in, iter_type __end, ios_base& __io,
2074           ios_base::iostate& __err, double& __v) const
2075       { return this->do_get(__in, __end, __io, __err, __v); }
2076
2077       iter_type
2078       get(iter_type __in, iter_type __end, ios_base& __io,
2079           ios_base::iostate& __err, long double& __v) const
2080       { return this->do_get(__in, __end, __io, __err, __v); }
2081       //@}
2082
2083       /**
2084        *  @brief  Numeric parsing.
2085        *
2086        *  Parses the input stream into the pointer variable @a v.  It does so
2087        *  by calling num_get::do_get().
2088        *
2089        *  The input characters are parsed like the scanf %p specifier.
2090        *
2091        *  Digit grouping is intrepreted according to numpunct::grouping() and
2092        *  numpunct::thousands_sep().  If the pattern of digit groups isn't
2093        *  consistent, sets err to ios_base::failbit.
2094        *
2095        *  Note that the digit grouping effect for pointers is a bit ambiguous
2096        *  in the standard and shouldn't be relied on.  See DR 344.
2097        *
2098        *  If parsing the string yields a valid value for @a v, @a v is set.
2099        *  Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2100        *  Sets err to ios_base::eofbit if the stream is emptied.
2101        *
2102        *  @param  in  Start of input stream.
2103        *  @param  end  End of input stream.
2104        *  @param  io  Source of locale and flags.
2105        *  @param  err  Error flags to set.
2106        *  @param  v  Value to format and insert.
2107        *  @return  Iterator after reading.
2108       */
2109       iter_type
2110       get(iter_type __in, iter_type __end, ios_base& __io,
2111           ios_base::iostate& __err, void*& __v) const
2112       { return this->do_get(__in, __end, __io, __err, __v); }
2113
2114     protected:
2115       /// Destructor.
2116       virtual ~num_get() { }
2117
2118       iter_type
2119       _M_extract_float(iter_type, iter_type, ios_base&, ios_base::iostate&,
2120                        string& __xtrc) const;
2121
2122       template<typename _ValueT>
2123         iter_type
2124         _M_extract_int(iter_type, iter_type, ios_base&, ios_base::iostate&,
2125                        _ValueT& __v) const;
2126
2127       //@{
2128       /**
2129        *  @brief  Numeric parsing.
2130        *
2131        *  Parses the input stream into the variable @a v.  This function is a
2132        *  hook for derived classes to change the value returned.  @see get()
2133        *  for more details.
2134        *
2135        *  @param  in  Start of input stream.
2136        *  @param  end  End of input stream.
2137        *  @param  io  Source of locale and flags.
2138        *  @param  err  Error flags to set.
2139        *  @param  v  Value to format and insert.
2140        *  @return  Iterator after reading.
2141       */
2142       virtual iter_type
2143       do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const;
2144
2145
2146       virtual iter_type
2147       do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, long&) const;
2148
2149       virtual iter_type
2150       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2151               unsigned short&) const;
2152
2153       virtual iter_type
2154       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2155              unsigned int&) const;
2156
2157       virtual iter_type
2158       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2159              unsigned long&) const;
2160
2161 #ifdef _GLIBCXX_USE_LONG_LONG
2162       virtual iter_type
2163       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2164              long long&) const;
2165
2166       virtual iter_type
2167       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2168              unsigned long long&) const;
2169 #endif
2170
2171       virtual iter_type
2172       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2173              float&) const;
2174
2175       virtual iter_type
2176       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2177              double&) const;
2178
2179       virtual iter_type
2180       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2181              long double&) const;
2182
2183       virtual iter_type
2184       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2185              void*&) const;
2186       //@}
2187     };
2188
2189   template<typename _CharT, typename _InIter>
2190     locale::id num_get<_CharT, _InIter>::id;
2191
2192
2193   /**
2194    *  @brief  Facet for converting numbers to strings.
2195    *
2196    *  This facet encapsulates the code to convert a number to a string.  It is
2197    *  used by the ostream numeric insertion operators.
2198    *
2199    *  The num_put template uses protected virtual functions to provide the
2200    *  actual results.  The public accessors forward the call to the virtual
2201    *  functions.  These virtual functions are hooks for developers to
2202    *  implement the behavior they require from the num_put facet.
2203   */
2204   template<typename _CharT, typename _OutIter>
2205     class num_put : public locale::facet
2206     {
2207     public:
2208       // Types:
2209       //@{
2210       /// Public typedefs
2211       typedef _CharT            char_type;
2212       typedef _OutIter          iter_type;
2213       //@}
2214
2215       /// Numpunct facet id.
2216       static locale::id         id;
2217
2218       /**
2219        *  @brief  Constructor performs initialization.
2220        *
2221        *  This is the constructor provided by the standard.
2222        *
2223        *  @param refs  Passed to the base facet class.
2224       */
2225       explicit
2226       num_put(size_t __refs = 0) : facet(__refs) { }
2227
2228       /**
2229        *  @brief  Numeric formatting.
2230        *
2231        *  Formats the boolean @a v and inserts it into a stream.  It does so
2232        *  by calling num_put::do_put().
2233        *
2234        *  If ios_base::boolalpha is set, writes ctype<CharT>::truename() or
2235        *  ctype<CharT>::falsename().  Otherwise formats @a v as an int.
2236        *
2237        *  @param  s  Stream to write to.
2238        *  @param  io  Source of locale and flags.
2239        *  @param  fill  Char_type to use for filling.
2240        *  @param  v  Value to format and insert.
2241        *  @return  Iterator after writing.
2242       */
2243       iter_type
2244       put(iter_type __s, ios_base& __f, char_type __fill, bool __v) const
2245       { return this->do_put(__s, __f, __fill, __v); }
2246
2247       //@{
2248       /**
2249        *  @brief  Numeric formatting.
2250        *
2251        *  Formats the integral value @a v and inserts it into a
2252        *  stream.  It does so by calling num_put::do_put().
2253        *
2254        *  Formatting is affected by the flag settings in @a io.
2255        *
2256        *  The basic format is affected by the value of io.flags() &
2257        *  ios_base::basefield.  If equal to ios_base::oct, formats like the
2258        *  printf %o specifier.  Else if equal to ios_base::hex, formats like
2259        *  %x or %X with ios_base::uppercase unset or set respectively.
2260        *  Otherwise, formats like %d, %ld, %lld for signed and %u, %lu, %llu
2261        *  for unsigned values.  Note that if both oct and hex are set, neither
2262        *  will take effect.
2263        *
2264        *  If ios_base::showpos is set, '+' is output before positive values.
2265        *  If ios_base::showbase is set, '0' precedes octal values (except 0)
2266        *  and '0[xX]' precedes hex values.
2267        *
2268        *  Thousands separators are inserted according to numpunct::grouping()
2269        *  and numpunct::thousands_sep().  The decimal point character used is
2270        *  numpunct::decimal_point().
2271        *
2272        *  If io.width() is non-zero, enough @a fill characters are inserted to
2273        *  make the result at least that wide.  If
2274        *  (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2275        *  padded at the end.  If ios_base::internal, then padding occurs
2276        *  immediately after either a '+' or '-' or after '0x' or '0X'.
2277        *  Otherwise, padding occurs at the beginning.
2278        *
2279        *  @param  s  Stream to write to.
2280        *  @param  io  Source of locale and flags.
2281        *  @param  fill  Char_type to use for filling.
2282        *  @param  v  Value to format and insert.
2283        *  @return  Iterator after writing.
2284       */
2285       iter_type
2286       put(iter_type __s, ios_base& __f, char_type __fill, long __v) const
2287       { return this->do_put(__s, __f, __fill, __v); }
2288
2289       iter_type
2290       put(iter_type __s, ios_base& __f, char_type __fill,
2291           unsigned long __v) const
2292       { return this->do_put(__s, __f, __fill, __v); }
2293
2294 #ifdef _GLIBCXX_USE_LONG_LONG
2295       iter_type
2296       put(iter_type __s, ios_base& __f, char_type __fill, long long __v) const
2297       { return this->do_put(__s, __f, __fill, __v); }
2298
2299       iter_type
2300       put(iter_type __s, ios_base& __f, char_type __fill,
2301           unsigned long long __v) const
2302       { return this->do_put(__s, __f, __fill, __v); }
2303 #endif
2304       //@}
2305
2306       //@{
2307       /**
2308        *  @brief  Numeric formatting.
2309        *
2310        *  Formats the floating point value @a v and inserts it into a stream.
2311        *  It does so by calling num_put::do_put().
2312        *
2313        *  Formatting is affected by the flag settings in @a io.
2314        *
2315        *  The basic format is affected by the value of io.flags() &
2316        *  ios_base::floatfield.  If equal to ios_base::fixed, formats like the
2317        *  printf %f specifier.  Else if equal to ios_base::scientific, formats
2318        *  like %e or %E with ios_base::uppercase unset or set respectively.
2319        *  Otherwise, formats like %g or %G depending on uppercase.  Note that
2320        *  if both fixed and scientific are set, the effect will also be like
2321        *  %g or %G.
2322        *
2323        *  The output precision is given by io.precision().  This precision is
2324        *  capped at numeric_limits::digits10 + 2 (different for double and
2325        *  long double).  The default precision is 6.
2326        *
2327        *  If ios_base::showpos is set, '+' is output before positive values.
2328        *  If ios_base::showpoint is set, a decimal point will always be
2329        *  output.
2330        *
2331        *  Thousands separators are inserted according to numpunct::grouping()
2332        *  and numpunct::thousands_sep().  The decimal point character used is
2333        *  numpunct::decimal_point().
2334        *
2335        *  If io.width() is non-zero, enough @a fill characters are inserted to
2336        *  make the result at least that wide.  If
2337        *  (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2338        *  padded at the end.  If ios_base::internal, then padding occurs
2339        *  immediately after either a '+' or '-' or after '0x' or '0X'.
2340        *  Otherwise, padding occurs at the beginning.
2341        *
2342        *  @param  s  Stream to write to.
2343        *  @param  io  Source of locale and flags.
2344        *  @param  fill  Char_type to use for filling.
2345        *  @param  v  Value to format and insert.
2346        *  @return  Iterator after writing.
2347       */
2348       iter_type
2349       put(iter_type __s, ios_base& __f, char_type __fill, double __v) const
2350       { return this->do_put(__s, __f, __fill, __v); }
2351
2352       iter_type
2353       put(iter_type __s, ios_base& __f, char_type __fill,
2354           long double __v) const
2355       { return this->do_put(__s, __f, __fill, __v); }
2356       //@}
2357
2358       /**
2359        *  @brief  Numeric formatting.
2360        *
2361        *  Formats the pointer value @a v and inserts it into a stream.  It
2362        *  does so by calling num_put::do_put().
2363        *
2364        *  This function formats @a v as an unsigned long with ios_base::hex
2365        *  and ios_base::showbase set.
2366        *
2367        *  @param  s  Stream to write to.
2368        *  @param  io  Source of locale and flags.
2369        *  @param  fill  Char_type to use for filling.
2370        *  @param  v  Value to format and insert.
2371        *  @return  Iterator after writing.
2372       */
2373       iter_type
2374       put(iter_type __s, ios_base& __f, char_type __fill,
2375           const void* __v) const
2376       { return this->do_put(__s, __f, __fill, __v); }
2377
2378     protected:
2379       template<typename _ValueT>
2380         iter_type
2381         _M_insert_float(iter_type, ios_base& __io, char_type __fill,
2382                         char __mod, _ValueT __v) const;
2383
2384       void
2385       _M_group_float(const char* __grouping, size_t __grouping_size,
2386                      char_type __sep, const char_type* __p, char_type* __new,
2387                      char_type* __cs, int& __len) const;
2388
2389       template<typename _ValueT>
2390         iter_type
2391         _M_insert_int(iter_type, ios_base& __io, char_type __fill,
2392                       _ValueT __v) const;
2393
2394       void
2395       _M_group_int(const char* __grouping, size_t __grouping_size,
2396                    char_type __sep, ios_base& __io, char_type* __new,
2397                    char_type* __cs, int& __len) const;
2398
2399       void
2400       _M_pad(char_type __fill, streamsize __w, ios_base& __io,
2401              char_type* __new, const char_type* __cs, int& __len) const;
2402
2403       /// Destructor.
2404       virtual
2405       ~num_put() { };
2406
2407       //@{
2408       /**
2409        *  @brief  Numeric formatting.
2410        *
2411        *  These functions do the work of formatting numeric values and
2412        *  inserting them into a stream. This function is a hook for derived
2413        *  classes to change the value returned.
2414        *
2415        *  @param  s  Stream to write to.
2416        *  @param  io  Source of locale and flags.
2417        *  @param  fill  Char_type to use for filling.
2418        *  @param  v  Value to format and insert.
2419        *  @return  Iterator after writing.
2420       */
2421       virtual iter_type
2422       do_put(iter_type, ios_base&, char_type __fill, bool __v) const;
2423
2424       virtual iter_type
2425       do_put(iter_type, ios_base&, char_type __fill, long __v) const;
2426
2427       virtual iter_type
2428       do_put(iter_type, ios_base&, char_type __fill, unsigned long) const;
2429
2430 #ifdef _GLIBCXX_USE_LONG_LONG
2431       virtual iter_type
2432       do_put(iter_type, ios_base&, char_type __fill, long long __v) const;
2433
2434       virtual iter_type
2435       do_put(iter_type, ios_base&, char_type __fill, unsigned long long) const;
2436 #endif
2437
2438       virtual iter_type
2439       do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2440
2441       virtual iter_type
2442       do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2443
2444       virtual iter_type
2445       do_put(iter_type, ios_base&, char_type __fill, const void* __v) const;
2446       //@}
2447     };
2448
2449   template <typename _CharT, typename _OutIter>
2450     locale::id num_put<_CharT, _OutIter>::id;
2451
2452
2453   /**
2454    *  @brief  Facet for localized string comparison.
2455    *
2456    *  This facet encapsulates the code to compare strings in a localized
2457    *  manner.
2458    *
2459    *  The collate template uses protected virtual functions to provide
2460    *  the actual results.  The public accessors forward the call to
2461    *  the virtual functions.  These virtual functions are hooks for
2462    *  developers to implement the behavior they require from the
2463    *  collate facet.
2464   */
2465   template<typename _CharT>
2466     class collate : public locale::facet
2467     {
2468     public:
2469       // Types:
2470       //@{
2471       /// Public typedefs
2472       typedef _CharT                    char_type;
2473       typedef basic_string<_CharT>      string_type;
2474       //@}
2475
2476     protected:
2477       // Underlying "C" library locale information saved from
2478       // initialization, needed by collate_byname as well.
2479       __c_locale                        _M_c_locale_collate;
2480
2481     public:
2482       /// Numpunct facet id.
2483       static locale::id                 id;
2484
2485       /**
2486        *  @brief  Constructor performs initialization.
2487        *
2488        *  This is the constructor provided by the standard.
2489        *
2490        *  @param refs  Passed to the base facet class.
2491       */
2492       explicit
2493       collate(size_t __refs = 0)
2494       : facet(__refs), _M_c_locale_collate(_S_get_c_locale())
2495       { }
2496
2497       /**
2498        *  @brief  Internal constructor. Not for general use.
2499        *
2500        *  This is a constructor for use by the library itself to set up new
2501        *  locales.
2502        *
2503        *  @param cloc  The "C" locale.
2504        *  @param refs  Passed to the base facet class.
2505       */
2506       explicit
2507       collate(__c_locale __cloc, size_t __refs = 0)
2508       : facet(__refs), _M_c_locale_collate(_S_clone_c_locale(__cloc))
2509       { }
2510
2511       /**
2512        *  @brief  Compare two strings.
2513        *
2514        *  This function compares two strings and returns the result by calling
2515        *  collate::do_compare().
2516        *
2517        *  @param lo1  Start of string 1.
2518        *  @param hi1  End of string 1.
2519        *  @param lo2  Start of string 2.
2520        *  @param hi2  End of string 2.
2521        *  @return  1 if string1 > string2, -1 if string1 < string2, else 0.
2522       */
2523       int
2524       compare(const _CharT* __lo1, const _CharT* __hi1,
2525               const _CharT* __lo2, const _CharT* __hi2) const
2526       { return this->do_compare(__lo1, __hi1, __lo2, __hi2); }
2527
2528       /**
2529        *  @brief  Transform string to comparable form.
2530        *
2531        *  This function is a wrapper for strxfrm functionality.  It takes the
2532        *  input string and returns a modified string that can be directly
2533        *  compared to other transformed strings.  In the "C" locale, this
2534        *  function just returns a copy of the input string.  In some other
2535        *  locales, it may replace two chars with one, change a char for
2536        *  another, etc.  It does so by returning collate::do_transform().
2537        *
2538        *  @param lo  Start of string.
2539        *  @param hi  End of string.
2540        *  @return  Transformed string_type.
2541       */
2542       string_type
2543       transform(const _CharT* __lo, const _CharT* __hi) const
2544       { return this->do_transform(__lo, __hi); }
2545
2546       /**
2547        *  @brief  Return hash of a string.
2548        *
2549        *  This function computes and returns a hash on the input string.  It
2550        *  does so by returning collate::do_hash().
2551        *
2552        *  @param lo  Start of string.
2553        *  @param hi  End of string.
2554        *  @return  Hash value.
2555       */
2556       long
2557       hash(const _CharT* __lo, const _CharT* __hi) const
2558       { return this->do_hash(__lo, __hi); }
2559
2560       // Used to abstract out _CharT bits in virtual member functions, below.
2561       int
2562       _M_compare(const _CharT*, const _CharT*) const;
2563
2564       size_t
2565       _M_transform(_CharT*, const _CharT*, size_t) const;
2566
2567   protected:
2568       /// Destructor.
2569       virtual
2570       ~collate()
2571       { _S_destroy_c_locale(_M_c_locale_collate); }
2572
2573       /**
2574        *  @brief  Compare two strings.
2575        *
2576        *  This function is a hook for derived classes to change the value
2577        *  returned.  @see compare().
2578        *
2579        *  @param lo1  Start of string 1.
2580        *  @param hi1  End of string 1.
2581        *  @param lo2  Start of string 2.
2582        *  @param hi2  End of string 2.
2583        *  @return  1 if string1 > string2, -1 if string1 < string2, else 0.
2584       */
2585       virtual int
2586       do_compare(const _CharT* __lo1, const _CharT* __hi1,
2587                  const _CharT* __lo2, const _CharT* __hi2) const;
2588
2589       /**
2590        *  @brief  Transform string to comparable form.
2591        *
2592        *  This function is a hook for derived classes to change the value
2593        *  returned.
2594        *
2595        *  @param lo1  Start of string 1.
2596        *  @param hi1  End of string 1.
2597        *  @param lo2  Start of string 2.
2598        *  @param hi2  End of string 2.
2599        *  @return  1 if string1 > string2, -1 if string1 < string2, else 0.
2600       */
2601       virtual string_type
2602       do_transform(const _CharT* __lo, const _CharT* __hi) const;
2603
2604       /**
2605        *  @brief  Return hash of a string.
2606        *
2607        *  This function computes and returns a hash on the input string.  This
2608        *  function is a hook for derived classes to change the value returned.
2609        *
2610        *  @param lo  Start of string.
2611        *  @param hi  End of string.
2612        *  @return  Hash value.
2613       */
2614       virtual long
2615       do_hash(const _CharT* __lo, const _CharT* __hi) const;
2616     };
2617
2618   template<typename _CharT>
2619     locale::id collate<_CharT>::id;
2620
2621   // Specializations.
2622   template<>
2623     int
2624     collate<char>::_M_compare(const char*, const char*) const;
2625
2626   template<>
2627     size_t
2628     collate<char>::_M_transform(char*, const char*, size_t) const;
2629
2630 #ifdef _GLIBCXX_USE_WCHAR_T
2631   template<>
2632     int
2633     collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const;
2634
2635   template<>
2636     size_t
2637     collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const;
2638 #endif
2639
2640   /// @brief  class collate_byname [22.2.4.2].
2641   template<typename _CharT>
2642     class collate_byname : public collate<_CharT>
2643     {
2644     public:
2645       //@{
2646       /// Public typedefs
2647       typedef _CharT               char_type;
2648       typedef basic_string<_CharT> string_type;
2649       //@}
2650
2651       explicit
2652       collate_byname(const char* __s, size_t __refs = 0)
2653       : collate<_CharT>(__refs)
2654       {
2655         if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
2656           {
2657             this->_S_destroy_c_locale(this->_M_c_locale_collate);
2658             this->_S_create_c_locale(this->_M_c_locale_collate, __s);
2659           }
2660       }
2661
2662     protected:
2663       virtual
2664       ~collate_byname() { }
2665     };
2666
2667
2668   /**
2669    *  @brief  Time format ordering data.
2670    *
2671    *  This class provides an enum representing different orderings of day,
2672    *  month, and year.
2673   */
2674   class time_base
2675   {
2676   public:
2677     enum dateorder { no_order, dmy, mdy, ymd, ydm };
2678   };
2679
2680   template<typename _CharT>
2681     struct __timepunct_cache : public locale::facet
2682     {
2683       // List of all known timezones, with GMT first.
2684       static const _CharT*              _S_timezones[14];
2685
2686       const _CharT*                     _M_date_format;
2687       const _CharT*                     _M_date_era_format;
2688       const _CharT*                     _M_time_format;
2689       const _CharT*                     _M_time_era_format;
2690       const _CharT*                     _M_date_time_format;
2691       const _CharT*                     _M_date_time_era_format;
2692       const _CharT*                     _M_am;
2693       const _CharT*                     _M_pm;
2694       const _CharT*                     _M_am_pm_format;
2695
2696       // Day names, starting with "C"'s Sunday.
2697       const _CharT*                     _M_day1;
2698       const _CharT*                     _M_day2;
2699       const _CharT*                     _M_day3;
2700       const _CharT*                     _M_day4;
2701       const _CharT*                     _M_day5;
2702       const _CharT*                     _M_day6;
2703       const _CharT*                     _M_day7;
2704
2705       // Abbreviated day names, starting with "C"'s Sun.
2706       const _CharT*                     _M_aday1;
2707       const _CharT*                     _M_aday2;
2708       const _CharT*                     _M_aday3;
2709       const _CharT*                     _M_aday4;
2710       const _CharT*                     _M_aday5;
2711       const _CharT*                     _M_aday6;
2712       const _CharT*                     _M_aday7;
2713
2714       // Month names, starting with "C"'s January.
2715       const _CharT*                     _M_month01;
2716       const _CharT*                     _M_month02;
2717       const _CharT*                     _M_month03;
2718       const _CharT*                     _M_month04;
2719       const _CharT*                     _M_month05;
2720       const _CharT*                     _M_month06;
2721       const _CharT*                     _M_month07;
2722       const _CharT*                     _M_month08;
2723       const _CharT*                     _M_month09;
2724       const _CharT*                     _M_month10;
2725       const _CharT*                     _M_month11;
2726       const _CharT*                     _M_month12;
2727
2728       // Abbreviated month names, starting with "C"'s Jan.
2729       const _CharT*                     _M_amonth01;
2730       const _CharT*                     _M_amonth02;
2731       const _CharT*                     _M_amonth03;
2732       const _CharT*                     _M_amonth04;
2733       const _CharT*                     _M_amonth05;
2734       const _CharT*                     _M_amonth06;
2735       const _CharT*                     _M_amonth07;
2736       const _CharT*                     _M_amonth08;
2737       const _CharT*                     _M_amonth09;
2738       const _CharT*                     _M_amonth10;
2739       const _CharT*                     _M_amonth11;
2740       const _CharT*                     _M_amonth12;
2741
2742       bool                              _M_allocated;
2743
2744       __timepunct_cache(size_t __refs = 0) : facet(__refs),
2745       _M_date_format(NULL), _M_date_era_format(NULL), _M_time_format(NULL),
2746       _M_time_era_format(NULL), _M_date_time_format(NULL),
2747       _M_date_time_era_format(NULL), _M_am(NULL), _M_pm(NULL),
2748       _M_am_pm_format(NULL), _M_day1(NULL), _M_day2(NULL), _M_day3(NULL),
2749       _M_day4(NULL), _M_day5(NULL), _M_day6(NULL), _M_day7(NULL),
2750       _M_aday1(NULL), _M_aday2(NULL), _M_aday3(NULL), _M_aday4(NULL),
2751       _M_aday5(NULL), _M_aday6(NULL), _M_aday7(NULL), _M_month01(NULL),
2752       _M_month02(NULL), _M_month03(NULL), _M_month04(NULL), _M_month05(NULL),
2753       _M_month06(NULL), _M_month07(NULL), _M_month08(NULL), _M_month09(NULL),
2754       _M_month10(NULL), _M_month11(NULL), _M_month12(NULL), _M_amonth01(NULL),
2755       _M_amonth02(NULL), _M_amonth03(NULL), _M_amonth04(NULL),
2756       _M_amonth05(NULL), _M_amonth06(NULL), _M_amonth07(NULL),
2757       _M_amonth08(NULL), _M_amonth09(NULL), _M_amonth10(NULL),
2758       _M_amonth11(NULL), _M_amonth12(NULL), _M_allocated(false)
2759       { }
2760
2761       ~__timepunct_cache();
2762
2763       void
2764       _M_cache(const locale& __loc);
2765
2766     private:
2767       __timepunct_cache&
2768       operator=(const __timepunct_cache&);
2769       
2770       explicit
2771       __timepunct_cache(const __timepunct_cache&);
2772     };
2773
2774   template<typename _CharT>
2775     __timepunct_cache<_CharT>::~__timepunct_cache()
2776     {
2777       if (_M_allocated)
2778         {
2779           // Unused.
2780         }
2781     }
2782
2783   // Specializations.
2784   template<>
2785     const char*
2786     __timepunct_cache<char>::_S_timezones[14];
2787
2788 #ifdef _GLIBCXX_USE_WCHAR_T
2789   template<>
2790     const wchar_t*
2791     __timepunct_cache<wchar_t>::_S_timezones[14];
2792 #endif
2793
2794   // Generic.
2795   template<typename _CharT>
2796     const _CharT* __timepunct_cache<_CharT>::_S_timezones[14];
2797
2798   template<typename _CharT>
2799     class __timepunct : public locale::facet
2800     {
2801     public:
2802       // Types:
2803       typedef _CharT                    __char_type;
2804       typedef basic_string<_CharT>      __string_type;
2805       typedef __timepunct_cache<_CharT> __cache_type;
2806
2807     protected:
2808       __cache_type*                     _M_data;
2809       __c_locale                        _M_c_locale_timepunct;
2810       const char*                       _M_name_timepunct;
2811
2812     public:
2813       /// Numpunct facet id.
2814       static locale::id                 id;
2815
2816       explicit
2817       __timepunct(size_t __refs = 0);
2818
2819       explicit
2820       __timepunct(__cache_type* __cache, size_t __refs = 0);
2821
2822       /**
2823        *  @brief  Internal constructor. Not for general use.
2824        *
2825        *  This is a constructor for use by the library itself to set up new
2826        *  locales.
2827        *
2828        *  @param cloc  The "C" locale.
2829        *  @param s  The name of a locale.
2830        *  @param refs  Passed to the base facet class.
2831       */
2832       explicit
2833       __timepunct(__c_locale __cloc, const char* __s, size_t __refs = 0);
2834
2835       // FIXME: for error checking purposes _M_put should return the return
2836       // value of strftime/wcsftime.
2837       void
2838       _M_put(_CharT* __s, size_t __maxlen, const _CharT* __format,
2839              const tm* __tm) const;
2840
2841       void
2842       _M_date_formats(const _CharT** __date) const
2843       {
2844         // Always have default first.
2845         __date[0] = _M_data->_M_date_format;
2846         __date[1] = _M_data->_M_date_era_format;
2847       }
2848
2849       void
2850       _M_time_formats(const _CharT** __time) const
2851       {
2852         // Always have default first.
2853         __time[0] = _M_data->_M_time_format;
2854         __time[1] = _M_data->_M_time_era_format;
2855       }
2856
2857       void
2858       _M_date_time_formats(const _CharT** __dt) const
2859       {
2860         // Always have default first.
2861         __dt[0] = _M_data->_M_date_time_format;
2862         __dt[1] = _M_data->_M_date_time_era_format;
2863       }
2864
2865       void
2866       _M_am_pm_format(const _CharT* __ampm) const
2867       { __ampm = _M_data->_M_am_pm_format; }
2868
2869       void
2870       _M_am_pm(const _CharT** __ampm) const
2871       {
2872         __ampm[0] = _M_data->_M_am;
2873         __ampm[1] = _M_data->_M_pm;
2874       }
2875
2876       void
2877       _M_days(const _CharT** __days) const
2878       {
2879         __days[0] = _M_data->_M_day1;
2880         __days[1] = _M_data->_M_day2;
2881         __days[2] = _M_data->_M_day3;
2882         __days[3] = _M_data->_M_day4;
2883         __days[4] = _M_data->_M_day5;
2884         __days[5] = _M_data->_M_day6;
2885         __days[6] = _M_data->_M_day7;
2886       }
2887
2888       void
2889       _M_days_abbreviated(const _CharT** __days) const
2890       {
2891         __days[0] = _M_data->_M_aday1;
2892         __days[1] = _M_data->_M_aday2;
2893         __days[2] = _M_data->_M_aday3;
2894         __days[3] = _M_data->_M_aday4;
2895         __days[4] = _M_data->_M_aday5;
2896         __days[5] = _M_data->_M_aday6;
2897         __days[6] = _M_data->_M_aday7;
2898       }
2899
2900       void
2901       _M_months(const _CharT** __months) const
2902       {
2903         __months[0] = _M_data->_M_month01;
2904         __months[1] = _M_data->_M_month02;
2905         __months[2] = _M_data->_M_month03;
2906         __months[3] = _M_data->_M_month04;
2907         __months[4] = _M_data->_M_month05;
2908         __months[5] = _M_data->_M_month06;
2909         __months[6] = _M_data->_M_month07;
2910         __months[7] = _M_data->_M_month08;
2911         __months[8] = _M_data->_M_month09;
2912         __months[9] = _M_data->_M_month10;
2913         __months[10] = _M_data->_M_month11;
2914         __months[11] = _M_data->_M_month12;
2915       }
2916
2917       void
2918       _M_months_abbreviated(const _CharT** __months) const
2919       {
2920         __months[0] = _M_data->_M_amonth01;
2921         __months[1] = _M_data->_M_amonth02;
2922         __months[2] = _M_data->_M_amonth03;
2923         __months[3] = _M_data->_M_amonth04;
2924         __months[4] = _M_data->_M_amonth05;
2925         __months[5] = _M_data->_M_amonth06;
2926         __months[6] = _M_data->_M_amonth07;
2927         __months[7] = _M_data->_M_amonth08;
2928         __months[8] = _M_data->_M_amonth09;
2929         __months[9] = _M_data->_M_amonth10;
2930         __months[10] = _M_data->_M_amonth11;
2931         __months[11] = _M_data->_M_amonth12;
2932       }
2933
2934     protected:
2935       virtual
2936       ~__timepunct();
2937
2938       // For use at construction time only.
2939       void
2940       _M_initialize_timepunct(__c_locale __cloc = NULL);
2941     };
2942
2943   template<typename _CharT>
2944     locale::id __timepunct<_CharT>::id;
2945
2946   // Specializations.
2947   template<>
2948     void
2949     __timepunct<char>::_M_initialize_timepunct(__c_locale __cloc);
2950
2951   template<>
2952     void
2953     __timepunct<char>::_M_put(char*, size_t, const char*, const tm*) const;
2954
2955 #ifdef _GLIBCXX_USE_WCHAR_T
2956   template<>
2957     void
2958     __timepunct<wchar_t>::_M_initialize_timepunct(__c_locale __cloc);
2959
2960   template<>
2961     void
2962     __timepunct<wchar_t>::_M_put(wchar_t*, size_t, const wchar_t*,
2963                                  const tm*) const;
2964 #endif
2965
2966   // Include host and configuration specific timepunct functions.
2967   #include <bits/time_members.h>
2968
2969   /**
2970    *  @brief  Facet for parsing dates and times.
2971    *
2972    *  This facet encapsulates the code to parse and return a date or
2973    *  time from a string.  It is used by the istream numeric
2974    *  extraction operators.
2975    *
2976    *  The time_get template uses protected virtual functions to provide the
2977    *  actual results.  The public accessors forward the call to the virtual
2978    *  functions.  These virtual functions are hooks for developers to
2979    *  implement the behavior they require from the time_get facet.
2980   */
2981   template<typename _CharT, typename _InIter>
2982     class time_get : public locale::facet, public time_base
2983     {
2984     public:
2985       // Types:
2986       //@{
2987       /// Public typedefs
2988       typedef _CharT                    char_type;
2989       typedef _InIter                   iter_type;
2990       //@}
2991       typedef basic_string<_CharT>      __string_type;
2992
2993       /// Numpunct facet id.
2994       static locale::id                 id;
2995
2996       /**
2997        *  @brief  Constructor performs initialization.
2998        *
2999        *  This is the constructor provided by the standard.
3000        *
3001        *  @param refs  Passed to the base facet class.
3002       */
3003       explicit
3004       time_get(size_t __refs = 0)
3005       : facet (__refs) { }
3006
3007       /**
3008        *  @brief  Return preferred order of month, day, and year.
3009        *
3010        *  This function returns an enum from timebase::dateorder giving the
3011        *  preferred ordering if the format "x" given to time_put::put() only
3012        *  uses month, day, and year.  If the format "x" for the associated
3013        *  locale uses other fields, this function returns
3014        *  timebase::dateorder::noorder.
3015        *
3016        *  NOTE: The library always returns noorder at the moment.
3017        *
3018        *  @return  A member of timebase::dateorder.
3019       */
3020       dateorder
3021       date_order()  const
3022       { return this->do_date_order(); }
3023
3024       /**
3025        *  @brief  Parse input time string.
3026        *
3027        *  This function parses a time according to the format "x" and puts the
3028        *  results into a user-supplied struct tm.  The result is returned by
3029        *  calling time_get::do_get_time().
3030        *
3031        *  If there is a valid time string according to format "x", @a tm will
3032        *  be filled in accordingly and the returned iterator will point to the
3033        *  first character beyond the time string.  If an error occurs before
3034        *  the end, err |= ios_base::failbit.  If parsing reads all the
3035        *  characters, err |= ios_base::eofbit.
3036        *
3037        *  @param  beg  Start of string to parse.
3038        *  @param  end  End of string to parse.
3039        *  @param  io  Source of the locale.
3040        *  @param  err  Error flags to set.
3041        *  @param  tm  Pointer to struct tm to fill in.
3042        *  @return  Iterator to first char beyond time string.
3043       */
3044       iter_type
3045       get_time(iter_type __beg, iter_type __end, ios_base& __io,
3046                ios_base::iostate& __err, tm* __tm)  const
3047       { return this->do_get_time(__beg, __end, __io, __err, __tm); }
3048
3049       /**
3050        *  @brief  Parse input date string.
3051        *
3052        *  This function parses a date according to the format "X" and puts the
3053        *  results into a user-supplied struct tm.  The result is returned by
3054        *  calling time_get::do_get_date().
3055        *
3056        *  If there is a valid date string according to format "X", @a tm will
3057        *  be filled in accordingly and the returned iterator will point to the
3058        *  first character beyond the date string.  If an error occurs before
3059        *  the end, err |= ios_base::failbit.  If parsing reads all the
3060        *  characters, err |= ios_base::eofbit.
3061        *
3062        *  @param  beg  Start of string to parse.
3063        *  @param  end  End of string to parse.
3064        *  @param  io  Source of the locale.
3065        *  @param  err  Error flags to set.
3066        *  @param  tm  Pointer to struct tm to fill in.
3067        *  @return  Iterator to first char beyond date string.
3068       */
3069       iter_type
3070       get_date(iter_type __beg, iter_type __end, ios_base& __io,
3071                ios_base::iostate& __err, tm* __tm)  const
3072       { return this->do_get_date(__beg, __end, __io, __err, __tm); }
3073
3074       /**
3075        *  @brief  Parse input weekday string.
3076        *
3077        *  This function parses a weekday name and puts the results into a
3078        *  user-supplied struct tm.  The result is returned by calling
3079        *  time_get::do_get_weekday().
3080        *
3081        *  Parsing starts by parsing an abbreviated weekday name.  If a valid
3082        *  abbreviation is followed by a character that would lead to the full
3083        *  weekday name, parsing continues until the full name is found or an
3084        *  error occurs.  Otherwise parsing finishes at the end of the
3085        *  abbreviated name.
3086        *
3087        *  If an error occurs before the end, err |= ios_base::failbit.  If
3088        *  parsing reads all the characters, err |= ios_base::eofbit.
3089        *
3090        *  @param  beg  Start of string to parse.
3091        *  @param  end  End of string to parse.
3092        *  @param  io  Source of the locale.
3093        *  @param  err  Error flags to set.
3094        *  @param  tm  Pointer to struct tm to fill in.
3095        *  @return  Iterator to first char beyond weekday name.
3096       */
3097       iter_type
3098       get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
3099                   ios_base::iostate& __err, tm* __tm) const
3100       { return this->do_get_weekday(__beg, __end, __io, __err, __tm); }
3101
3102       /**
3103        *  @brief  Parse input month string.
3104        *
3105        *  This function parses a month name and puts the results into a
3106        *  user-supplied struct tm.  The result is returned by calling
3107        *  time_get::do_get_monthname().
3108        *
3109        *  Parsing starts by parsing an abbreviated month name.  If a valid
3110        *  abbreviation is followed by a character that would lead to the full
3111        *  month name, parsing continues until the full name is found or an
3112        *  error occurs.  Otherwise parsing finishes at the end of the
3113        *  abbreviated name.
3114        *
3115        *  If an error occurs before the end, err |= ios_base::failbit.  If
3116        *  parsing reads all the characters, err |=
3117        *  ios_base::eofbit.
3118        *
3119        *  @param  beg  Start of string to parse.
3120        *  @param  end  End of string to parse.
3121        *  @param  io  Source of the locale.
3122        *  @param  err  Error flags to set.
3123        *  @param  tm  Pointer to struct tm to fill in.
3124        *  @return  Iterator to first char beyond month name.
3125       */
3126       iter_type
3127       get_monthname(iter_type __beg, iter_type __end, ios_base& __io,
3128                     ios_base::iostate& __err, tm* __tm) const
3129       { return this->do_get_monthname(__beg, __end, __io, __err, __tm); }
3130
3131       /**
3132        *  @brief  Parse input year string.
3133        *
3134        *  This function reads up to 4 characters to parse a year string and
3135        *  puts the results into a user-supplied struct tm.  The result is
3136        *  returned by calling time_get::do_get_year().
3137        *
3138        *  4 consecutive digits are interpreted as a full year.  If there are
3139        *  exactly 2 consecutive digits, the library interprets this as the
3140        *  number of years since 1900.
3141        *
3142        *  If an error occurs before the end, err |= ios_base::failbit.  If
3143        *  parsing reads all the characters, err |= ios_base::eofbit.
3144        *
3145        *  @param  beg  Start of string to parse.
3146        *  @param  end  End of string to parse.
3147        *  @param  io  Source of the locale.
3148        *  @param  err  Error flags to set.
3149        *  @param  tm  Pointer to struct tm to fill in.
3150        *  @return  Iterator to first char beyond year.
3151       */
3152       iter_type
3153       get_year(iter_type __beg, iter_type __end, ios_base& __io,
3154                ios_base::iostate& __err, tm* __tm) const
3155       { return this->do_get_year(__beg, __end, __io, __err, __tm); }
3156
3157     protected:
3158       /// Destructor.
3159       virtual
3160       ~time_get() { }
3161
3162       /**
3163        *  @brief  Return preferred order of month, day, and year.
3164        *
3165        *  This function returns an enum from timebase::dateorder giving the
3166        *  preferred ordering if the format "x" given to time_put::put() only
3167        *  uses month, day, and year.  This function is a hook for derived
3168        *  classes to change the value returned.
3169        *
3170        *  @return  A member of timebase::dateorder.
3171       */
3172       virtual dateorder
3173       do_date_order() const;
3174
3175       /**
3176        *  @brief  Parse input time string.
3177        *
3178        *  This function parses a time according to the format "x" and puts the
3179        *  results into a user-supplied struct tm.  This function is a hook for
3180        *  derived classes to change the value returned.  @see get_time() for
3181        *  details.
3182        *
3183        *  @param  beg  Start of string to parse.
3184        *  @param  end  End of string to parse.
3185        *  @param  io  Source of the locale.
3186        *  @param  err  Error flags to set.
3187        *  @param  tm  Pointer to struct tm to fill in.
3188        *  @return  Iterator to first char beyond time string.
3189       */
3190       virtual iter_type
3191       do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
3192                   ios_base::iostate& __err, tm* __tm) const;
3193
3194       /**
3195        *  @brief  Parse input date string.
3196        *
3197        *  This function parses a date according to the format "X" and puts the
3198        *  results into a user-supplied struct tm.  This function is a hook for
3199        *  derived classes to change the value returned.  @see get_date() for
3200        *  details.
3201        *
3202        *  @param  beg  Start of string to parse.
3203        *  @param  end  End of string to parse.
3204        *  @param  io  Source of the locale.
3205        *  @param  err  Error flags to set.
3206        *  @param  tm  Pointer to struct tm to fill in.
3207        *  @return  Iterator to first char beyond date string.
3208       */
3209       virtual iter_type
3210       do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
3211                   ios_base::iostate& __err, tm* __tm) const;
3212
3213       /**
3214        *  @brief  Parse input weekday string.
3215        *
3216        *  This function parses a weekday name and puts the results into a
3217        *  user-supplied struct tm.  This function is a hook for derived
3218        *  classes to change the value returned.  @see get_weekday() for
3219        *  details.
3220        *
3221        *  @param  beg  Start of string to parse.
3222        *  @param  end  End of string to parse.
3223        *  @param  io  Source of the locale.
3224        *  @param  err  Error flags to set.
3225        *  @param  tm  Pointer to struct tm to fill in.
3226        *  @return  Iterator to first char beyond weekday name.
3227       */
3228       virtual iter_type
3229       do_get_weekday(iter_type __beg, iter_type __end, ios_base&,
3230                      ios_base::iostate& __err, tm* __tm) const;
3231
3232       /**
3233        *  @brief  Parse input month string.
3234        *
3235        *  This function parses a month name and puts the results into a
3236        *  user-supplied struct tm.  This function is a hook for derived
3237        *  classes to change the value returned.  @see get_monthname() for
3238        *  details.
3239        *
3240        *  @param  beg  Start of string to parse.
3241        *  @param  end  End of string to parse.
3242        *  @param  io  Source of the locale.
3243        *  @param  err  Error flags to set.
3244        *  @param  tm  Pointer to struct tm to fill in.
3245        *  @return  Iterator to first char beyond month name.
3246       */
3247       virtual iter_type
3248       do_get_monthname(iter_type __beg, iter_type __end, ios_base&,
3249                        ios_base::iostate& __err, tm* __tm) const;
3250
3251       /**
3252        *  @brief  Parse input year string.
3253        *
3254        *  This function reads up to 4 characters to parse a year string and
3255        *  puts the results into a user-supplied struct tm.  This function is a
3256        *  hook for derived classes to change the value returned.  @see
3257        *  get_year() for details.
3258        *
3259        *  @param  beg  Start of string to parse.
3260        *  @param  end  End of string to parse.
3261        *  @param  io  Source of the locale.
3262        *  @param  err  Error flags to set.
3263        *  @param  tm  Pointer to struct tm to fill in.
3264        *  @return  Iterator to first char beyond year.
3265       */
3266       virtual iter_type
3267       do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
3268                   ios_base::iostate& __err, tm* __tm) const;
3269
3270       // Extract numeric component of length __len.
3271       iter_type
3272       _M_extract_num(iter_type __beg, iter_type __end, int& __member,
3273                      int __min, int __max, size_t __len,
3274                      ios_base& __io, ios_base::iostate& __err) const;
3275
3276       // Extract day or month name, or any unique array of string
3277       // literals in a const _CharT* array.
3278       iter_type
3279       _M_extract_name(iter_type __beg, iter_type __end, int& __member,
3280                       const _CharT** __names, size_t __indexlen,
3281                       ios_base& __io, ios_base::iostate& __err) const;
3282
3283       // Extract on a component-by-component basis, via __format argument.
3284       iter_type
3285       _M_extract_via_format(iter_type __beg, iter_type __end, ios_base& __io,
3286                             ios_base::iostate& __err, tm* __tm,
3287                             const _CharT* __format) const;
3288     };
3289
3290   template<typename _CharT, typename _InIter>
3291     locale::id time_get<_CharT, _InIter>::id;
3292
3293   /// @brief  class time_get_byname [22.2.5.2].
3294   template<typename _CharT, typename _InIter>
3295     class time_get_byname : public time_get<_CharT, _InIter>
3296     {
3297     public:
3298       // Types:
3299       typedef _CharT                    char_type;
3300       typedef _InIter                   iter_type;
3301
3302       explicit
3303       time_get_byname(const char*, size_t __refs = 0)
3304       : time_get<_CharT, _InIter>(__refs) { }
3305
3306     protected:
3307       virtual
3308       ~time_get_byname() { }
3309     };
3310
3311   /**
3312    *  @brief  Facet for outputting dates and times.
3313    *
3314    *  This facet encapsulates the code to format and output dates and times
3315    *  according to formats used by strftime().
3316    *
3317    *  The time_put template uses protected virtual functions to provide the
3318    *  actual results.  The public accessors forward the call to the virtual
3319    *  functions.  These virtual functions are hooks for developers to
3320    *  implement the behavior they require from the time_put facet.
3321   */
3322   template<typename _CharT, typename _OutIter>
3323     class time_put : public locale::facet
3324     {
3325     public:
3326       // Types:
3327       //@{
3328       /// Public typedefs
3329       typedef _CharT                    char_type;
3330       typedef _OutIter                  iter_type;
3331       //@}
3332
3333       /// Numpunct facet id.
3334       static locale::id                 id;
3335
3336       /**
3337        *  @brief  Constructor performs initialization.
3338        *
3339        *  This is the constructor provided by the standard.
3340        *
3341        *  @param refs  Passed to the base facet class.
3342       */
3343       explicit
3344       time_put(size_t __refs = 0)
3345       : facet(__refs) { }
3346
3347       /**
3348        *  @brief  Format and output a time or date.
3349        *
3350        *  This function formats the data in struct tm according to the
3351        *  provided format string.  The format string is interpreted as by
3352        *  strftime().
3353        *
3354        *  @param  s  The stream to write to.
3355        *  @param  io  Source of locale.
3356        *  @param  fill  char_type to use for padding.
3357        *  @param  tm  Struct tm with date and time info to format.
3358        *  @param  beg  Start of format string.
3359        *  @param  end  End of format string.
3360        *  @return  Iterator after writing.
3361        */
3362       iter_type
3363       put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
3364           const _CharT* __beg, const _CharT* __end) const;
3365
3366       /**
3367        *  @brief  Format and output a time or date.
3368        *
3369        *  This function formats the data in struct tm according to the
3370        *  provided format char and optional modifier.  The format and modifier
3371        *  are interpreted as by strftime().  It does so by returning
3372        *  time_put::do_put().
3373        *
3374        *  @param  s  The stream to write to.
3375        *  @param  io  Source of locale.
3376        *  @param  fill  char_type to use for padding.
3377        *  @param  tm  Struct tm with date and time info to format.
3378        *  @param  format  Format char.
3379        *  @param  mod  Optional modifier char.
3380        *  @return  Iterator after writing.
3381        */
3382       iter_type
3383       put(iter_type __s, ios_base& __io, char_type __fill,
3384           const tm* __tm, char __format, char __mod = 0) const
3385       { return this->do_put(__s, __io, __fill, __tm, __format, __mod); }
3386
3387     protected:
3388       /// Destructor.
3389       virtual
3390       ~time_put()
3391       { }
3392
3393       /**
3394        *  @brief  Format and output a time or date.
3395        *
3396        *  This function formats the data in struct tm according to the
3397        *  provided format char and optional modifier.  This function is a hook
3398        *  for derived classes to change the value returned.  @see put() for
3399        *  more details.
3400        *
3401        *  @param  s  The stream to write to.
3402        *  @param  io  Source of locale.
3403        *  @param  fill  char_type to use for padding.
3404        *  @param  tm  Struct tm with date and time info to format.
3405        *  @param  format  Format char.
3406        *  @param  mod  Optional modifier char.
3407        *  @return  Iterator after writing.
3408        */
3409       virtual iter_type
3410       do_put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
3411              char __format, char __mod) const;
3412     };
3413
3414   template<typename _CharT, typename _OutIter>
3415     locale::id time_put<_CharT, _OutIter>::id;
3416
3417   /// @brief  class time_put_byname [22.2.5.4].
3418   template<typename _CharT, typename _OutIter>
3419     class time_put_byname : public time_put<_CharT, _OutIter>
3420     {
3421     public:
3422       // Types:
3423       typedef _CharT                    char_type;
3424       typedef _OutIter                  iter_type;
3425
3426       explicit
3427       time_put_byname(const char*, size_t __refs = 0)
3428       : time_put<_CharT, _OutIter>(__refs)
3429       { };
3430
3431     protected:
3432       virtual
3433       ~time_put_byname() { }
3434     };
3435
3436
3437   /**
3438    *  @brief  Money format ordering data.
3439    *
3440    *  This class contains an ordered array of 4 fields to represent the
3441    *  pattern for formatting a money amount.  Each field may contain one entry
3442    *  from the part enum.  symbol, sign, and value must be present and the
3443    *  remaining field must contain either none or space.  @see
3444    *  moneypunct::pos_format() and moneypunct::neg_format() for details of how
3445    *  these fields are interpreted.
3446   */
3447   class money_base
3448   {
3449   public:
3450     enum part { none, space, symbol, sign, value };
3451     struct pattern { char field[4]; };
3452
3453     static const pattern _S_default_pattern;
3454
3455     enum
3456     {
3457       _S_minus,
3458       _S_zero,
3459       _S_end = 11
3460     };
3461
3462     // String literal of acceptable (narrow) input/output, for
3463     // money_get/money_put. "-0123456789"
3464     static const char* _S_atoms;
3465
3466     // Construct and return valid pattern consisting of some combination of:
3467     // space none symbol sign value
3468     static pattern
3469     _S_construct_pattern(char __precedes, char __space, char __posn);
3470   };
3471
3472   template<typename _CharT, bool _Intl>
3473     struct __moneypunct_cache : public locale::facet
3474     {
3475       const char*                       _M_grouping;
3476       size_t                            _M_grouping_size;
3477       bool                              _M_use_grouping;
3478       _CharT                            _M_decimal_point;
3479       _CharT                            _M_thousands_sep;
3480       const _CharT*                     _M_curr_symbol;
3481       size_t                            _M_curr_symbol_size;
3482       const _CharT*                     _M_positive_sign;
3483       size_t                            _M_positive_sign_size;
3484       const _CharT*                     _M_negative_sign;
3485       size_t                            _M_negative_sign_size;
3486       int                               _M_frac_digits;
3487       money_base::pattern               _M_pos_format;
3488       money_base::pattern               _M_neg_format;
3489
3490       // A list of valid numeric literals for input and output: in the standard
3491       // "C" locale, this is "-0123456789". This array contains the chars after
3492       // having been passed through the current locale's ctype<_CharT>.widen().
3493       _CharT                            _M_atoms[money_base::_S_end];
3494
3495       bool                              _M_allocated;
3496
3497       __moneypunct_cache(size_t __refs = 0) : facet(__refs),
3498       _M_grouping(NULL), _M_grouping_size(0), _M_use_grouping(false),
3499       _M_decimal_point(_CharT()), _M_thousands_sep(_CharT()),
3500       _M_curr_symbol(NULL), _M_curr_symbol_size(0),
3501       _M_positive_sign(NULL), _M_positive_sign_size(0),
3502       _M_negative_sign(NULL), _M_negative_sign_size(0),
3503       _M_frac_digits(0),
3504       _M_pos_format(money_base::pattern()),
3505       _M_neg_format(money_base::pattern()), _M_allocated(false)
3506       { }
3507
3508       ~__moneypunct_cache();
3509
3510       void
3511       _M_cache(const locale& __loc);
3512
3513     private:
3514       __moneypunct_cache&
3515       operator=(const __moneypunct_cache&);
3516       
3517       explicit
3518       __moneypunct_cache(const __moneypunct_cache&);
3519     };
3520
3521   template<typename _CharT, bool _Intl>
3522     __moneypunct_cache<_CharT, _Intl>::~__moneypunct_cache()
3523     {
3524       if (_M_allocated)
3525         {
3526           delete [] _M_grouping;
3527           delete [] _M_curr_symbol;
3528           delete [] _M_positive_sign;
3529           delete [] _M_negative_sign;
3530         }
3531     }
3532
3533   /**
3534    *  @brief  Facet for formatting data for money amounts.
3535    *
3536    *  This facet encapsulates the punctuation, grouping and other formatting
3537    *  features of money amount string representations.
3538   */
3539   template<typename _CharT, bool _Intl>
3540     class moneypunct : public locale::facet, public money_base
3541     {
3542     public:
3543       // Types:
3544       //@{
3545       /// Public typedefs
3546       typedef _CharT                    char_type;
3547       typedef basic_string<_CharT>      string_type;
3548       //@}
3549       typedef __moneypunct_cache<_CharT, _Intl>     __cache_type;
3550
3551     private:
3552       __cache_type*                     _M_data;
3553
3554     public:
3555       /// This value is provided by the standard, but no reason for its
3556       /// existence.
3557       static const bool                 intl = _Intl;
3558       /// Numpunct facet id.
3559       static locale::id                 id;
3560
3561       /**
3562        *  @brief  Constructor performs initialization.
3563        *
3564        *  This is the constructor provided by the standard.
3565        *
3566        *  @param refs  Passed to the base facet class.
3567       */
3568       explicit
3569       moneypunct(size_t __refs = 0) : facet(__refs), _M_data(NULL)
3570       { _M_initialize_moneypunct(); }
3571
3572       /**
3573        *  @brief  Constructor performs initialization.
3574        *
3575        *  This is an internal constructor.
3576        *
3577        *  @param cache  Cache for optimization.
3578        *  @param refs  Passed to the base facet class.
3579       */
3580       explicit
3581       moneypunct(__cache_type* __cache, size_t __refs = 0)
3582       : facet(__refs), _M_data(__cache)
3583       { _M_initialize_moneypunct(); }
3584
3585       /**
3586        *  @brief  Internal constructor. Not for general use.
3587        *
3588        *  This is a constructor for use by the library itself to set up new
3589        *  locales.
3590        *
3591        *  @param cloc  The "C" locale.
3592        *  @param s  The name of a locale.
3593        *  @param refs  Passed to the base facet class.
3594       */
3595       explicit
3596       moneypunct(__c_locale __cloc, const char* __s, size_t __refs = 0)
3597       : facet(__refs), _M_data(NULL)
3598       { _M_initialize_moneypunct(__cloc, __s); }
3599
3600       /**
3601        *  @brief  Return decimal point character.
3602        *
3603        *  This function returns a char_type to use as a decimal point.  It
3604        *  does so by returning returning
3605        *  moneypunct<char_type>::do_decimal_point().
3606        *
3607        *  @return  @a char_type representing a decimal point.
3608       */
3609       char_type
3610       decimal_point() const
3611       { return this->do_decimal_point(); }
3612
3613       /**
3614        *  @brief  Return thousands separator character.
3615        *
3616        *  This function returns a char_type to use as a thousands
3617        *  separator.  It does so by returning returning
3618        *  moneypunct<char_type>::do_thousands_sep().
3619        *
3620        *  @return  char_type representing a thousands separator.
3621       */
3622       char_type
3623       thousands_sep() const
3624       { return this->do_thousands_sep(); }
3625
3626       /**
3627        *  @brief  Return grouping specification.
3628        *
3629        *  This function returns a string representing groupings for the
3630        *  integer part of an amount.  Groupings indicate where thousands
3631        *  separators should be inserted.
3632        *
3633        *  Each char in the return string is interpret as an integer rather
3634        *  than a character.  These numbers represent the number of digits in a
3635        *  group.  The first char in the string represents the number of digits
3636        *  in the least significant group.  If a char is negative, it indicates
3637        *  an unlimited number of digits for the group.  If more chars from the
3638        *  string are required to group a number, the last char is used
3639        *  repeatedly.
3640        *
3641        *  For example, if the grouping() returns "\003\002" and is applied to
3642        *  the number 123456789, this corresponds to 12,34,56,789.  Note that
3643        *  if the string was "32", this would put more than 50 digits into the
3644        *  least significant group if the character set is ASCII.
3645        *
3646        *  The string is returned by calling
3647        *  moneypunct<char_type>::do_grouping().
3648        *
3649        *  @return  string representing grouping specification.
3650       */
3651       string
3652       grouping() const
3653       { return this->do_grouping(); }
3654
3655       /**
3656        *  @brief  Return currency symbol string.
3657        *
3658        *  This function returns a string_type to use as a currency symbol.  It
3659        *  does so by returning returning
3660        *  moneypunct<char_type>::do_curr_symbol().
3661        *
3662        *  @return  @a string_type representing a currency symbol.
3663       */
3664       string_type
3665       curr_symbol() const
3666       { return this->do_curr_symbol(); }
3667
3668       /**
3669        *  @brief  Return positive sign string.
3670        *
3671        *  This function returns a string_type to use as a sign for positive
3672        *  amounts.  It does so by returning returning
3673        *  moneypunct<char_type>::do_positive_sign().
3674        *
3675        *  If the return value contains more than one character, the first
3676        *  character appears in the position indicated by pos_format() and the
3677        *  remainder appear at the end of the formatted string.
3678        *
3679        *  @return  @a string_type representing a positive sign.
3680       */
3681       string_type
3682       positive_sign() const
3683       { return this->do_positive_sign(); }
3684
3685       /**
3686        *  @brief  Return negative sign string.
3687        *
3688        *  This function returns a string_type to use as a sign for negative
3689        *  amounts.  It does so by returning returning
3690        *  moneypunct<char_type>::do_negative_sign().
3691        *
3692        *  If the return value contains more than one character, the first
3693        *  character appears in the position indicated by neg_format() and the
3694        *  remainder appear at the end of the formatted string.
3695        *
3696        *  @return  @a string_type representing a negative sign.
3697       */
3698       string_type
3699       negative_sign() const
3700       { return this->do_negative_sign(); }
3701
3702       /**
3703        *  @brief  Return number of digits in fraction.
3704        *
3705        *  This function returns the exact number of digits that make up the
3706        *  fractional part of a money amount.  It does so by returning
3707        *  returning moneypunct<char_type>::do_frac_digits().
3708        *
3709        *  The fractional part of a money amount is optional.  But if it is
3710        *  present, there must be frac_digits() digits.
3711        *
3712        *  @return  Number of digits in amount fraction.
3713       */
3714       int
3715       frac_digits() const
3716       { return this->do_frac_digits(); }
3717
3718       //@{
3719       /**
3720        *  @brief  Return pattern for money values.
3721        *
3722        *  This function returns a pattern describing the formatting of a
3723        *  positive or negative valued money amount.  It does so by returning
3724        *  returning moneypunct<char_type>::do_pos_format() or
3725        *  moneypunct<char_type>::do_neg_format().
3726        *
3727        *  The pattern has 4 fields describing the ordering of symbol, sign,
3728        *  value, and none or space.  There must be one of each in the pattern.
3729        *  The none and space enums may not appear in the first field and space
3730        *  may not appear in the final field.
3731        *
3732        *  The parts of a money string must appear in the order indicated by
3733        *  the fields of the pattern.  The symbol field indicates that the
3734        *  value of curr_symbol() may be present.  The sign field indicates
3735        *  that the value of positive_sign() or negative_sign() must be
3736        *  present.  The value field indicates that the absolute value of the
3737        *  money amount is present.  none indicates 0 or more whitespace
3738        *  characters, except at the end, where it permits no whitespace.
3739        *  space indicates that 1 or more whitespace characters must be
3740        *  present.
3741        *
3742        *  For example, for the US locale and pos_format() pattern
3743        *  {symbol,sign,value,none}, curr_symbol() == '$' positive_sign() ==
3744        *  '+', and value 10.01, and options set to force the symbol, the
3745        *  corresponding string is "$+10.01".
3746        *
3747        *  @return  Pattern for money values.
3748       */
3749       pattern
3750       pos_format() const
3751       { return this->do_pos_format(); }
3752
3753       pattern
3754       neg_format() const
3755       { return this->do_neg_format(); }
3756       //@}
3757
3758     protected:
3759       /// Destructor.
3760       virtual
3761       ~moneypunct();
3762
3763       /**
3764        *  @brief  Return decimal point character.
3765        *
3766        *  Returns a char_type to use as a decimal point.  This function is a
3767        *  hook for derived classes to change the value returned.
3768        *
3769        *  @return  @a char_type representing a decimal point.
3770       */
3771       virtual char_type
3772       do_decimal_point() const
3773       { return _M_data->_M_decimal_point; }
3774
3775       /**
3776        *  @brief  Return thousands separator character.
3777        *
3778        *  Returns a char_type to use as a thousands separator.  This function
3779        *  is a hook for derived classes to change the value returned.
3780        *
3781        *  @return  @a char_type representing a thousands separator.
3782       */
3783       virtual char_type
3784       do_thousands_sep() const
3785       { return _M_data->_M_thousands_sep; }
3786
3787       /**
3788        *  @brief  Return grouping specification.
3789        *
3790        *  Returns a string representing groupings for the integer part of a
3791        *  number.  This function is a hook for derived classes to change the
3792        *  value returned.  @see grouping() for details.
3793        *
3794        *  @return  String representing grouping specification.
3795       */
3796       virtual string
3797       do_grouping() const
3798       { return _M_data->_M_grouping; }
3799
3800       /**
3801        *  @brief  Return currency symbol string.
3802        *
3803        *  This function returns a string_type to use as a currency symbol.
3804        *  This function is a hook for derived classes to change the value
3805        *  returned.  @see curr_symbol() for details.
3806        *
3807        *  @return  @a string_type representing a currency symbol.
3808       */
3809       virtual string_type
3810       do_curr_symbol()   const
3811       { return _M_data->_M_curr_symbol; }
3812
3813       /**
3814        *  @brief  Return positive sign string.
3815        *
3816        *  This function returns a string_type to use as a sign for positive
3817        *  amounts.  This function is a hook for derived classes to change the
3818        *  value returned.  @see positive_sign() for details.
3819        *
3820        *  @return  @a string_type representing a positive sign.
3821       */
3822       virtual string_type
3823       do_positive_sign() const
3824       { return _M_data->_M_positive_sign; }
3825
3826       /**
3827        *  @brief  Return negative sign string.
3828        *
3829        *  This function returns a string_type to use as a sign for negative
3830        *  amounts.  This function is a hook for derived classes to change the
3831        *  value returned.  @see negative_sign() for details.
3832        *
3833        *  @return  @a string_type representing a negative sign.
3834       */
3835       virtual string_type
3836       do_negative_sign() const
3837       { return _M_data->_M_negative_sign; }
3838
3839       /**
3840        *  @brief  Return number of digits in fraction.
3841        *
3842        *  This function returns the exact number of digits that make up the
3843        *  fractional part of a money amount.  This function is a hook for
3844        *  derived classes to change the value returned.  @see frac_digits()
3845        *  for details.
3846        *
3847        *  @return  Number of digits in amount fraction.
3848       */
3849       virtual int
3850       do_frac_digits() const
3851       { return _M_data->_M_frac_digits; }
3852
3853       /**
3854        *  @brief  Return pattern for money values.
3855        *
3856        *  This function returns a pattern describing the formatting of a
3857        *  positive valued money amount.  This function is a hook for derived
3858        *  classes to change the value returned.  @see pos_format() for
3859        *  details.
3860        *
3861        *  @return  Pattern for money values.
3862       */
3863       virtual pattern
3864       do_pos_format() const
3865       { return _M_data->_M_pos_format; }
3866
3867       /**
3868        *  @brief  Return pattern for money values.
3869        *
3870        *  This function returns a pattern describing the formatting of a
3871        *  negative valued money amount.  This function is a hook for derived
3872        *  classes to change the value returned.  @see neg_format() for
3873        *  details.
3874        *
3875        *  @return  Pattern for money values.
3876       */
3877       virtual pattern
3878       do_neg_format() const
3879       { return _M_data->_M_neg_format; }
3880
3881       // For use at construction time only.
3882        void
3883        _M_initialize_moneypunct(__c_locale __cloc = NULL,
3884                                 const char* __name = NULL);
3885     };
3886
3887   template<typename _CharT, bool _Intl>
3888     locale::id moneypunct<_CharT, _Intl>::id;
3889
3890   template<typename _CharT, bool _Intl>
3891     const bool moneypunct<_CharT, _Intl>::intl;
3892
3893   template<>
3894     moneypunct<char, true>::~moneypunct();
3895
3896   template<>
3897     moneypunct<char, false>::~moneypunct();
3898
3899   template<>
3900     void
3901     moneypunct<char, true>::_M_initialize_moneypunct(__c_locale, const char*);
3902
3903   template<>
3904     void
3905     moneypunct<char, false>::_M_initialize_moneypunct(__c_locale, const char*);
3906
3907 #ifdef _GLIBCXX_USE_WCHAR_T
3908   template<>
3909     moneypunct<wchar_t, true>::~moneypunct();
3910
3911   template<>
3912     moneypunct<wchar_t, false>::~moneypunct();
3913
3914   template<>
3915     void
3916     moneypunct<wchar_t, true>::_M_initialize_moneypunct(__c_locale,
3917                                                         const char*);
3918
3919   template<>
3920     void
3921     moneypunct<wchar_t, false>::_M_initialize_moneypunct(__c_locale,
3922                                                          const char*);
3923 #endif
3924
3925   /// @brief  class moneypunct_byname [22.2.6.4].
3926   template<typename _CharT, bool _Intl>
3927     class moneypunct_byname : public moneypunct<_CharT, _Intl>
3928     {
3929     public:
3930       typedef _CharT                    char_type;
3931       typedef basic_string<_CharT>      string_type;
3932
3933       static const bool intl = _Intl;
3934
3935       explicit
3936       moneypunct_byname(const char* __s, size_t __refs = 0)
3937       : moneypunct<_CharT, _Intl>(__refs)
3938       {
3939         if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
3940           {
3941             __c_locale __tmp;
3942             this->_S_create_c_locale(__tmp, __s);
3943             this->_M_initialize_moneypunct(__tmp);
3944             this->_S_destroy_c_locale(__tmp);
3945           }
3946       }
3947
3948     protected:
3949       virtual
3950       ~moneypunct_byname() { }
3951     };
3952
3953   template<typename _CharT, bool _Intl>
3954     const bool moneypunct_byname<_CharT, _Intl>::intl;
3955
3956   /**
3957    *  @brief  Facet for parsing monetary amounts.
3958    *
3959    *  This facet encapsulates the code to parse and return a monetary
3960    *  amount from a string.
3961    *
3962    *  The money_get template uses protected virtual functions to
3963    *  provide the actual results.  The public accessors forward the
3964    *  call to the virtual functions.  These virtual functions are
3965    *  hooks for developers to implement the behavior they require from
3966    *  the money_get facet.
3967   */
3968   template<typename _CharT, typename _InIter>
3969     class money_get : public locale::facet
3970     {
3971     public:
3972       // Types:
3973       //@{
3974       /// Public typedefs
3975       typedef _CharT                    char_type;
3976       typedef _InIter                   iter_type;
3977       typedef basic_string<_CharT>      string_type;
3978       //@}
3979
3980       /// Numpunct facet id.
3981       static locale::id                 id;
3982
3983       /**
3984        *  @brief  Constructor performs initialization.
3985        *
3986        *  This is the constructor provided by the standard.
3987        *
3988        *  @param refs  Passed to the base facet class.
3989       */
3990       explicit
3991       money_get(size_t __refs = 0) : facet(__refs) { }
3992
3993       /**
3994        *  @brief  Read and parse a monetary value.
3995        *
3996        *  This function reads characters from @a s, interprets them as a
3997        *  monetary value according to moneypunct and ctype facets retrieved
3998        *  from io.getloc(), and returns the result in @a units as an integral
3999        *  value moneypunct::frac_digits() * the actual amount.  For example,
4000        *  the string $10.01 in a US locale would store 1001 in @a units.
4001        *
4002        *  Any characters not part of a valid money amount are not consumed.
4003        *
4004        *  If a money value cannot be parsed from the input stream, sets
4005        *  err=(err|io.failbit).  If the stream is consumed before finishing
4006        *  parsing,  sets err=(err|io.failbit|io.eofbit).  @a units is
4007        *  unchanged if parsing fails.
4008        *
4009        *  This function works by returning the result of do_get().
4010        *
4011        *  @param  s  Start of characters to parse.
4012        *  @param  end  End of characters to parse.
4013        *  @param  intl  Parameter to use_facet<moneypunct<CharT,intl> >.
4014        *  @param  io  Source of facets and io state.
4015        *  @param  err  Error field to set if parsing fails.
4016        *  @param  units  Place to store result of parsing.
4017        *  @return  Iterator referencing first character beyond valid money
4018        *           amount.
4019        */
4020       iter_type
4021       get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4022           ios_base::iostate& __err, long double& __units) const
4023       { return this->do_get(__s, __end, __intl, __io, __err, __units); }
4024
4025       /**
4026        *  @brief  Read and parse a monetary value.
4027        *
4028        *  This function reads characters from @a s, interprets them as a
4029        *  monetary value according to moneypunct and ctype facets retrieved
4030        *  from io.getloc(), and returns the result in @a digits.  For example,
4031        *  the string $10.01 in a US locale would store "1001" in @a digits.
4032        *
4033        *  Any characters not part of a valid money amount are not consumed.
4034        *
4035        *  If a money value cannot be parsed from the input stream, sets
4036        *  err=(err|io.failbit).  If the stream is consumed before finishing
4037        *  parsing,  sets err=(err|io.failbit|io.eofbit).
4038        *
4039        *  This function works by returning the result of do_get().
4040        *
4041        *  @param  s  Start of characters to parse.
4042        *  @param  end  End of characters to parse.
4043        *  @param  intl  Parameter to use_facet<moneypunct<CharT,intl> >.
4044        *  @param  io  Source of facets and io state.
4045        *  @param  err  Error field to set if parsing fails.
4046        *  @param  digits  Place to store result of parsing.
4047        *  @return  Iterator referencing first character beyond valid money
4048        *           amount.
4049        */
4050       iter_type
4051       get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4052           ios_base::iostate& __err, string_type& __digits) const
4053       { return this->do_get(__s, __end, __intl, __io, __err, __digits); }
4054
4055     protected:
4056       /// Destructor.
4057       virtual
4058       ~money_get() { }
4059
4060       /**
4061        *  @brief  Read and parse a monetary value.
4062        *
4063        *  This function reads and parses characters representing a monetary
4064        *  value.  This function is a hook for derived classes to change the
4065        *  value returned.  @see get() for details.
4066        */
4067       virtual iter_type
4068       do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4069              ios_base::iostate& __err, long double& __units) const;
4070
4071       /**
4072        *  @brief  Read and parse a monetary value.
4073        *
4074        *  This function reads and parses characters representing a monetary
4075        *  value.  This function is a hook for derived classes to change the
4076        *  value returned.  @see get() for details.
4077        */
4078       virtual iter_type
4079       do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4080              ios_base::iostate& __err, string_type& __digits) const;
4081
4082       template<bool _Intl>
4083         iter_type
4084         _M_extract(iter_type __s, iter_type __end, ios_base& __io,
4085                    ios_base::iostate& __err, string& __digits) const;     
4086     };
4087
4088   template<typename _CharT, typename _InIter>
4089     locale::id money_get<_CharT, _InIter>::id;
4090
4091   /**
4092    *  @brief  Facet for outputting monetary amounts.
4093    *
4094    *  This facet encapsulates the code to format and output a monetary
4095    *  amount.
4096    *
4097    *  The money_put template uses protected virtual functions to
4098    *  provide the actual results.  The public accessors forward the
4099    *  call to the virtual functions.  These virtual functions are
4100    *  hooks for developers to implement the behavior they require from
4101    *  the money_put facet.
4102   */
4103   template<typename _CharT, typename _OutIter>
4104     class money_put : public locale::facet
4105     {
4106     public:
4107       //@{
4108       /// Public typedefs
4109       typedef _CharT                    char_type;
4110       typedef _OutIter                  iter_type;
4111       typedef basic_string<_CharT>      string_type;
4112       //@}
4113
4114       /// Numpunct facet id.
4115       static locale::id                 id;
4116
4117       /**
4118        *  @brief  Constructor performs initialization.
4119        *
4120        *  This is the constructor provided by the standard.
4121        *
4122        *  @param refs  Passed to the base facet class.
4123       */
4124       explicit
4125       money_put(size_t __refs = 0) : facet(__refs) { }
4126
4127       /**
4128        *  @brief  Format and output a monetary value.
4129        *
4130        *  This function formats @a units as a monetary value according to
4131        *  moneypunct and ctype facets retrieved from io.getloc(), and writes
4132        *  the resulting characters to @a s.  For example, the value 1001 in a
4133        *  US locale would write "$10.01" to @a s.
4134        *
4135        *  This function works by returning the result of do_put().
4136        *
4137        *  @param  s  The stream to write to.
4138        *  @param  intl  Parameter to use_facet<moneypunct<CharT,intl> >.
4139        *  @param  io  Source of facets and io state.
4140        *  @param  fill  char_type to use for padding.
4141        *  @param  units  Place to store result of parsing.
4142        *  @return  Iterator after writing.
4143        */
4144       iter_type
4145       put(iter_type __s, bool __intl, ios_base& __io,
4146           char_type __fill, long double __units) const
4147       { return this->do_put(__s, __intl, __io, __fill, __units); }
4148
4149       /**
4150        *  @brief  Format and output a monetary value.
4151        *
4152        *  This function formats @a digits as a monetary value according to
4153        *  moneypunct and ctype facets retrieved from io.getloc(), and writes
4154        *  the resulting characters to @a s.  For example, the string "1001" in
4155        *  a US locale would write "$10.01" to @a s.
4156        *
4157        *  This function works by returning the result of do_put().
4158        *
4159        *  @param  s  The stream to write to.
4160        *  @param  intl  Parameter to use_facet<moneypunct<CharT,intl> >.
4161        *  @param  io  Source of facets and io state.
4162        *  @param  fill  char_type to use for padding.
4163        *  @param  units  Place to store result of parsing.
4164        *  @return  Iterator after writing.
4165        */
4166       iter_type
4167       put(iter_type __s, bool __intl, ios_base& __io,
4168           char_type __fill, const string_type& __digits) const
4169       { return this->do_put(__s, __intl, __io, __fill, __digits); }
4170
4171     protected:
4172       /// Destructor.
4173       virtual
4174       ~money_put() { }
4175
4176       /**
4177        *  @brief  Format and output a monetary value.
4178        *
4179        *  This function formats @a units as a monetary value according to
4180        *  moneypunct and ctype facets retrieved from io.getloc(), and writes
4181        *  the resulting characters to @a s.  For example, the value 1001 in a
4182        *  US locale would write "$10.01" to @a s.
4183        *
4184        *  This function is a hook for derived classes to change the value
4185        *  returned.  @see put().
4186        *
4187        *  @param  s  The stream to write to.
4188        *  @param  intl  Parameter to use_facet<moneypunct<CharT,intl> >.
4189        *  @param  io  Source of facets and io state.
4190        *  @param  fill  char_type to use for padding.
4191        *  @param  units  Place to store result of parsing.
4192        *  @return  Iterator after writing.
4193        */
4194       virtual iter_type
4195       do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4196              long double __units) const;
4197
4198       /**
4199        *  @brief  Format and output a monetary value.
4200        *
4201        *  This function formats @a digits as a monetary value according to
4202        *  moneypunct and ctype facets retrieved from io.getloc(), and writes
4203        *  the resulting characters to @a s.  For example, the string "1001" in
4204        *  a US locale would write "$10.01" to @a s.
4205        *
4206        *  This function is a hook for derived classes to change the value
4207        *  returned.  @see put().
4208        *
4209        *  @param  s  The stream to write to.
4210        *  @param  intl  Parameter to use_facet<moneypunct<CharT,intl> >.
4211        *  @param  io  Source of facets and io state.
4212        *  @param  fill  char_type to use for padding.
4213        *  @param  units  Place to store result of parsing.
4214        *  @return  Iterator after writing.
4215        */
4216       virtual iter_type
4217       do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4218              const string_type& __digits) const;
4219
4220       template<bool _Intl>
4221         iter_type
4222         _M_insert(iter_type __s, ios_base& __io, char_type __fill,
4223                   const string_type& __digits) const;
4224     };
4225
4226   template<typename _CharT, typename _OutIter>
4227     locale::id money_put<_CharT, _OutIter>::id;
4228
4229   /**
4230    *  @brief  Messages facet base class providing catalog typedef.
4231    */
4232   struct messages_base
4233   {
4234     typedef int catalog;
4235   };
4236
4237   /**
4238    *  @brief  Facet for handling message catalogs
4239    *
4240    *  This facet encapsulates the code to retrieve messages from
4241    *  message catalogs.  The only thing defined by the standard for this facet
4242    *  is the interface.  All underlying functionality is
4243    *  implementation-defined.
4244    *
4245    *  This library currently implements 3 versions of the message facet.  The
4246    *  first version (gnu) is a wrapper around gettext, provided by libintl.
4247    *  The second version (ieee) is a wrapper around catgets.  The final
4248    *  version (default) does no actual translation.  These implementations are
4249    *  only provided for char and wchar_t instantiations.
4250    *
4251    *  The messages template uses protected virtual functions to
4252    *  provide the actual results.  The public accessors forward the
4253    *  call to the virtual functions.  These virtual functions are
4254    *  hooks for developers to implement the behavior they require from
4255    *  the messages facet.
4256   */
4257   template<typename _CharT>
4258     class messages : public locale::facet, public messages_base
4259     {
4260     public:
4261       // Types:
4262       //@{
4263       /// Public typedefs
4264       typedef _CharT                    char_type;
4265       typedef basic_string<_CharT>      string_type;
4266       //@}
4267
4268     protected:
4269       // Underlying "C" library locale information saved from
4270       // initialization, needed by messages_byname as well.
4271       __c_locale                        _M_c_locale_messages;
4272       const char*                       _M_name_messages;
4273
4274     public:
4275       /// Numpunct facet id.
4276       static locale::id                 id;
4277
4278       /**
4279        *  @brief  Constructor performs initialization.
4280        *
4281        *  This is the constructor provided by the standard.
4282        *
4283        *  @param refs  Passed to the base facet class.
4284       */
4285       explicit
4286       messages(size_t __refs = 0);
4287
4288       // Non-standard.
4289       /**
4290        *  @brief  Internal constructor.  Not for general use.
4291        *
4292        *  This is a constructor for use by the library itself to set up new
4293        *  locales.
4294        *
4295        *  @param  cloc  The "C" locale.
4296        *  @param  s  The name of a locale.
4297        *  @param  refs  Refcount to pass to the base class.
4298        */
4299       explicit
4300       messages(__c_locale __cloc, const char* __s, size_t __refs = 0);
4301
4302       /*
4303        *  @brief  Open a message catalog.
4304        *
4305        *  This function opens and returns a handle to a message catalog by
4306        *  returning do_open(s, loc).
4307        *
4308        *  @param  s  The catalog to open.
4309        *  @param  loc  Locale to use for character set conversions.
4310        *  @return  Handle to the catalog or value < 0 if open fails.
4311       */
4312       catalog
4313       open(const basic_string<char>& __s, const locale& __loc) const
4314       { return this->do_open(__s, __loc); }
4315
4316       // Non-standard and unorthodox, yet effective.
4317       /*
4318        *  @brief  Open a message catalog.
4319        *
4320        *  This non-standard function opens and returns a handle to a message
4321        *  catalog by returning do_open(s, loc).  The third argument provides a
4322        *  message catalog root directory for gnu gettext and is ignored
4323        *  otherwise.
4324        *
4325        *  @param  s  The catalog to open.
4326        *  @param  loc  Locale to use for character set conversions.
4327        *  @param  dir  Message catalog root directory.
4328        *  @return  Handle to the catalog or value < 0 if open fails.
4329       */
4330       catalog
4331       open(const basic_string<char>&, const locale&, const char*) const;
4332
4333       /*
4334        *  @brief  Look up a string in a message catalog.
4335        *
4336        *  This function retrieves and returns a message from a catalog by
4337        *  returning do_get(c, set, msgid, s).
4338        *
4339        *  For gnu, @a set and @a msgid are ignored.  Returns gettext(s).
4340        *  For default, returns s. For ieee, returns catgets(c,set,msgid,s).
4341        *
4342        *  @param  c  The catalog to access.
4343        *  @param  set  Implementation-defined.
4344        *  @param  msgid  Implementation-defined.
4345        *  @param  s  Default return value if retrieval fails.
4346        *  @return  Retrieved message or @a s if get fails.
4347       */
4348       string_type
4349       get(catalog __c, int __set, int __msgid, const string_type& __s) const
4350       { return this->do_get(__c, __set, __msgid, __s); }
4351
4352       /*
4353        *  @brief  Close a message catalog.
4354        *
4355        *  Closes catalog @a c by calling do_close(c).
4356        *
4357        *  @param  c  The catalog to close.
4358       */
4359       void
4360       close(catalog __c) const
4361       { return this->do_close(__c); }
4362
4363     protected:
4364       /// Destructor.
4365       virtual
4366       ~messages();
4367
4368       /*
4369        *  @brief  Open a message catalog.
4370        *
4371        *  This function opens and returns a handle to a message catalog in an
4372        *  implementation-defined manner.  This function is a hook for derived
4373        *  classes to change the value returned.
4374        *
4375        *  @param  s  The catalog to open.
4376        *  @param  loc  Locale to use for character set conversions.
4377        *  @return  Handle to the opened catalog, value < 0 if open failed.
4378       */
4379       virtual catalog
4380       do_open(const basic_string<char>&, const locale&) const;
4381
4382       /*
4383        *  @brief  Look up a string in a message catalog.
4384        *
4385        *  This function retrieves and returns a message from a catalog in an
4386        *  implementation-defined manner.  This function is a hook for derived
4387        *  classes to change the value returned.
4388        *
4389        *  For gnu, @a set and @a msgid are ignored.  Returns gettext(s).
4390        *  For default, returns s. For ieee, returns catgets(c,set,msgid,s).
4391        *
4392        *  @param  c  The catalog to access.
4393        *  @param  set  Implementation-defined.
4394        *  @param  msgid  Implementation-defined.
4395        *  @param  s  Default return value if retrieval fails.
4396        *  @return  Retrieved message or @a s if get fails.
4397       */
4398       virtual string_type
4399       do_get(catalog, int, int, const string_type& __dfault) const;
4400
4401       /*
4402        *  @brief  Close a message catalog.
4403        *
4404        *  @param  c  The catalog to close.
4405       */
4406       virtual void
4407       do_close(catalog) const;
4408
4409       // Returns a locale and codeset-converted string, given a char* message.
4410       char*
4411       _M_convert_to_char(const string_type& __msg) const
4412       {
4413         // XXX
4414         return reinterpret_cast<char*>(const_cast<_CharT*>(__msg.c_str()));
4415       }
4416
4417       // Returns a locale and codeset-converted string, given a char* message.
4418       string_type
4419       _M_convert_from_char(char*) const
4420       {
4421 #if 0
4422         // Length of message string without terminating null.
4423         size_t __len = char_traits<char>::length(__msg) - 1;
4424
4425         // "everybody can easily convert the string using
4426         // mbsrtowcs/wcsrtombs or with iconv()"
4427
4428         // Convert char* to _CharT in locale used to open catalog.
4429         // XXX need additional template parameter on messages class for this..
4430         // typedef typename codecvt<char, _CharT, _StateT> __codecvt_type;
4431         typedef typename codecvt<char, _CharT, mbstate_t> __codecvt_type;
4432
4433         __codecvt_type::state_type __state;
4434         // XXX may need to initialize state.
4435         //initialize_state(__state._M_init());
4436
4437         char* __from_next;
4438         // XXX what size for this string?
4439         _CharT* __to = static_cast<_CharT*>(__builtin_alloca(__len + 1));
4440         const __codecvt_type& __cvt = use_facet<__codecvt_type>(_M_locale_conv);
4441         __cvt.out(__state, __msg, __msg + __len, __from_next,
4442                   __to, __to + __len + 1, __to_next);
4443         return string_type(__to);
4444 #endif
4445 #if 0
4446         typedef ctype<_CharT> __ctype_type;
4447         // const __ctype_type& __cvt = use_facet<__ctype_type>(_M_locale_msg);
4448         const __ctype_type& __cvt = use_facet<__ctype_type>(locale());
4449         // XXX Again, proper length of converted string an issue here.
4450         // For now, assume the converted length is not larger.
4451         _CharT* __dest = static_cast<_CharT*>(__builtin_alloca(__len + 1));
4452         __cvt.widen(__msg, __msg + __len, __dest);
4453         return basic_string<_CharT>(__dest);
4454 #endif
4455         return string_type();
4456       }
4457      };
4458
4459   template<typename _CharT>
4460     locale::id messages<_CharT>::id;
4461
4462   // Specializations for required instantiations.
4463   template<>
4464     string
4465     messages<char>::do_get(catalog, int, int, const string&) const;
4466
4467 #ifdef _GLIBCXX_USE_WCHAR_T
4468   template<>
4469     wstring
4470     messages<wchar_t>::do_get(catalog, int, int, const wstring&) const;
4471 #endif
4472
4473    /// @brief class messages_byname [22.2.7.2].
4474    template<typename _CharT>
4475     class messages_byname : public messages<_CharT>
4476     {
4477     public:
4478       typedef _CharT                    char_type;
4479       typedef basic_string<_CharT>      string_type;
4480
4481       explicit
4482       messages_byname(const char* __s, size_t __refs = 0);
4483
4484     protected:
4485       virtual
4486       ~messages_byname()
4487       { }
4488     };
4489
4490   // Include host and configuration specific messages functions.
4491   #include <bits/messages_members.h>
4492
4493
4494   // Subclause convenience interfaces, inlines.
4495   // NB: These are inline because, when used in a loop, some compilers
4496   // can hoist the body out of the loop; then it's just as fast as the
4497   // C is*() function.
4498
4499   /// Convenience interface to ctype.is(ctype_base::space, __c).
4500   template<typename _CharT>
4501     inline bool
4502     isspace(_CharT __c, const locale& __loc)
4503     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); }
4504
4505   /// Convenience interface to ctype.is(ctype_base::print, __c).
4506   template<typename _CharT>
4507     inline bool
4508     isprint(_CharT __c, const locale& __loc)
4509     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); }
4510
4511   /// Convenience interface to ctype.is(ctype_base::cntrl, __c).
4512   template<typename _CharT>
4513     inline bool
4514     iscntrl(_CharT __c, const locale& __loc)
4515     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); }
4516
4517   /// Convenience interface to ctype.is(ctype_base::upper, __c).
4518   template<typename _CharT>
4519     inline bool
4520     isupper(_CharT __c, const locale& __loc)
4521     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); }
4522
4523   /// Convenience interface to ctype.is(ctype_base::lower, __c).
4524   template<typename _CharT>
4525     inline bool 
4526     islower(_CharT __c, const locale& __loc)
4527     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); }
4528
4529   /// Convenience interface to ctype.is(ctype_base::alpha, __c).
4530   template<typename _CharT>
4531     inline bool
4532     isalpha(_CharT __c, const locale& __loc)
4533     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); }
4534
4535   /// Convenience interface to ctype.is(ctype_base::digit, __c).
4536   template<typename _CharT>
4537     inline bool
4538     isdigit(_CharT __c, const locale& __loc)
4539     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); }
4540
4541   /// Convenience interface to ctype.is(ctype_base::punct, __c).
4542   template<typename _CharT>
4543     inline bool
4544     ispunct(_CharT __c, const locale& __loc)
4545     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); }
4546
4547   /// Convenience interface to ctype.is(ctype_base::xdigit, __c).
4548   template<typename _CharT>
4549     inline bool
4550     isxdigit(_CharT __c, const locale& __loc)
4551     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); }
4552
4553   /// Convenience interface to ctype.is(ctype_base::alnum, __c).
4554   template<typename _CharT>
4555     inline bool
4556     isalnum(_CharT __c, const locale& __loc)
4557     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); }
4558
4559   /// Convenience interface to ctype.is(ctype_base::graph, __c).
4560   template<typename _CharT>
4561     inline bool
4562     isgraph(_CharT __c, const locale& __loc)
4563     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); }
4564
4565   /// Convenience interface to ctype.toupper(__c).
4566   template<typename _CharT>
4567     inline _CharT
4568     toupper(_CharT __c, const locale& __loc)
4569     { return use_facet<ctype<_CharT> >(__loc).toupper(__c); }
4570
4571   /// Convenience interface to ctype.tolower(__c).
4572   template<typename _CharT>
4573     inline _CharT
4574     tolower(_CharT __c, const locale& __loc)
4575     { return use_facet<ctype<_CharT> >(__loc).tolower(__c); }
4576 } // namespace std
4577
4578 #endif