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