OSDN Git Service

Add NIOS2 support. Code from SourceyG++.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / locale_facets.h
1 // Locale support -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
21
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
25 // <http://www.gnu.org/licenses/>.
26
27 /** @file locale_facets.h
28  *  This is an internal header file, included by other library headers.
29  *  You should not attempt to use it directly.
30  */
31
32 //
33 // ISO C++ 14882: 22.1  Locales
34 //
35
36 #ifndef _LOCALE_FACETS_H
37 #define _LOCALE_FACETS_H 1
38
39 #pragma GCC system_header
40
41 #include <cwctype>      // For wctype_t
42 #include <cctype>
43 #include <bits/ctype_base.h>    
44 #include <iosfwd>
45 #include <bits/ios_base.h>  // For ios_base, ios_base::iostate
46 #include <streambuf>
47 #include <bits/cpp_type_traits.h>
48 #include <ext/type_traits.h>
49 #include <ext/numeric_traits.h>
50 #include <bits/streambuf_iterator.h>
51
52 _GLIBCXX_BEGIN_NAMESPACE(std)
53
54   // NB: Don't instantiate required wchar_t facets if no wchar_t support.
55 #ifdef _GLIBCXX_USE_WCHAR_T
56 # define  _GLIBCXX_NUM_FACETS 28
57 #else
58 # define  _GLIBCXX_NUM_FACETS 14
59 #endif
60
61   // Convert string to numeric value of type _Tp and store results.
62   // NB: This is specialized for all required types, there is no
63   // generic definition.
64   template<typename _Tp>
65     void
66     __convert_to_v(const char*, _Tp&, ios_base::iostate&,
67                    const __c_locale&) throw();
68
69   // Explicit specializations for required types.
70   template<>
71     void
72     __convert_to_v(const char*, float&, ios_base::iostate&,
73                    const __c_locale&) throw();
74
75   template<>
76     void
77     __convert_to_v(const char*, double&, ios_base::iostate&,
78                    const __c_locale&) throw();
79
80   template<>
81     void
82     __convert_to_v(const char*, long double&, ios_base::iostate&,
83                    const __c_locale&) throw();
84
85   // NB: __pad is a struct, rather than a function, so it can be
86   // partially-specialized.
87   template<typename _CharT, typename _Traits>
88     struct __pad
89     {
90       static void
91       _S_pad(ios_base& __io, _CharT __fill, _CharT* __news,
92              const _CharT* __olds, streamsize __newlen, streamsize __oldlen);
93     };
94
95   // Used by both numeric and monetary facets.
96   // Inserts "group separator" characters into an array of characters.
97   // It's recursive, one iteration per group.  It moves the characters
98   // in the buffer this way: "xxxx12345" -> "12,345xxx".  Call this
99   // only with __gsize != 0.
100   template<typename _CharT>
101     _CharT*
102     __add_grouping(_CharT* __s, _CharT __sep,
103                    const char* __gbeg, size_t __gsize,
104                    const _CharT* __first, const _CharT* __last);
105
106   // This template permits specializing facet output code for
107   // ostreambuf_iterator.  For ostreambuf_iterator, sputn is
108   // significantly more efficient than incrementing iterators.
109   template<typename _CharT>
110     inline
111     ostreambuf_iterator<_CharT>
112     __write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len)
113     {
114       __s._M_put(__ws, __len);
115       return __s;
116     }
117
118   // This is the unspecialized form of the template.
119   template<typename _CharT, typename _OutIter>
120     inline
121     _OutIter
122     __write(_OutIter __s, const _CharT* __ws, int __len)
123     {
124       for (int __j = 0; __j < __len; __j++, ++__s)
125         *__s = __ws[__j];
126       return __s;
127     }
128
129
130   // 22.2.1.1  Template class ctype
131   // Include host and configuration specific ctype enums for ctype_base.
132
133   /**
134    *  @brief  Common base for ctype facet
135    *
136    *  This template class provides implementations of the public functions
137    *  that forward to the protected virtual functions.
138    *
139    *  This template also provides abstract stubs for the protected virtual
140    *  functions.
141   */
142   template<typename _CharT>
143     class __ctype_abstract_base : public locale::facet, public ctype_base
144     {
145     public:
146       // Types:
147       /// Typedef for the template parameter
148       typedef _CharT char_type;
149
150       /**
151        *  @brief  Test char_type classification.
152        *
153        *  This function finds a mask M for @a c and compares it to mask @a m.
154        *  It does so by returning the value of ctype<char_type>::do_is().
155        *
156        *  @param c  The char_type to compare the mask of.
157        *  @param m  The mask to compare against.
158        *  @return  (M & m) != 0.
159       */
160       bool
161       is(mask __m, char_type __c) const
162       { return this->do_is(__m, __c); }
163
164       /**
165        *  @brief  Return a mask array.
166        *
167        *  This function finds the mask for each char_type in the range [lo,hi)
168        *  and successively writes it to vec.  vec must have as many elements
169        *  as the char array.  It does so by returning the value of
170        *  ctype<char_type>::do_is().
171        *
172        *  @param lo  Pointer to start of range.
173        *  @param hi  Pointer to end of range.
174        *  @param vec  Pointer to an array of mask storage.
175        *  @return  @a hi.
176       */
177       const char_type*
178       is(const char_type *__lo, const char_type *__hi, mask *__vec) const
179       { return this->do_is(__lo, __hi, __vec); }
180
181       /**
182        *  @brief  Find char_type matching a mask
183        *
184        *  This function searches for and returns the first char_type c in
185        *  [lo,hi) for which is(m,c) is true.  It does so by returning
186        *  ctype<char_type>::do_scan_is().
187        *
188        *  @param m  The mask to compare against.
189        *  @param lo  Pointer to start of range.
190        *  @param hi  Pointer to end of range.
191        *  @return  Pointer to matching char_type if found, else @a hi.
192       */
193       const char_type*
194       scan_is(mask __m, const char_type* __lo, const char_type* __hi) const
195       { return this->do_scan_is(__m, __lo, __hi); }
196
197       /**
198        *  @brief  Find char_type not matching a mask
199        *
200        *  This function searches for and returns the first char_type c in
201        *  [lo,hi) for which is(m,c) is false.  It does so by returning
202        *  ctype<char_type>::do_scan_not().
203        *
204        *  @param m  The mask to compare against.
205        *  @param lo  Pointer to first char in range.
206        *  @param hi  Pointer to end of range.
207        *  @return  Pointer to non-matching char if found, else @a hi.
208       */
209       const char_type*
210       scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
211       { return this->do_scan_not(__m, __lo, __hi); }
212
213       /**
214        *  @brief  Convert to uppercase.
215        *
216        *  This function converts the argument to uppercase if possible.
217        *  If not possible (for example, '2'), returns the argument.  It does
218        *  so by returning ctype<char_type>::do_toupper().
219        *
220        *  @param c  The char_type to convert.
221        *  @return  The uppercase char_type if convertible, else @a c.
222       */
223       char_type
224       toupper(char_type __c) const
225       { return this->do_toupper(__c); }
226
227       /**
228        *  @brief  Convert array to uppercase.
229        *
230        *  This function converts each char_type in the range [lo,hi) to
231        *  uppercase if possible.  Other elements remain untouched.  It does so
232        *  by returning ctype<char_type>:: do_toupper(lo, hi).
233        *
234        *  @param lo  Pointer to start of range.
235        *  @param hi  Pointer to end of range.
236        *  @return  @a hi.
237       */
238       const char_type*
239       toupper(char_type *__lo, const char_type* __hi) const
240       { return this->do_toupper(__lo, __hi); }
241
242       /**
243        *  @brief  Convert to lowercase.
244        *
245        *  This function converts the argument to lowercase if possible.  If
246        *  not possible (for example, '2'), returns the argument.  It does so
247        *  by returning ctype<char_type>::do_tolower(c).
248        *
249        *  @param c  The char_type to convert.
250        *  @return  The lowercase char_type if convertible, else @a c.
251       */
252       char_type
253       tolower(char_type __c) const
254       { return this->do_tolower(__c); }
255
256       /**
257        *  @brief  Convert array to lowercase.
258        *
259        *  This function converts each char_type in the range [lo,hi) to
260        *  lowercase if possible.  Other elements remain untouched.  It does so
261        *  by returning ctype<char_type>:: do_tolower(lo, hi).
262        *
263        *  @param lo  Pointer to start of range.
264        *  @param hi  Pointer to end of range.
265        *  @return  @a hi.
266       */
267       const char_type*
268       tolower(char_type* __lo, const char_type* __hi) const
269       { return this->do_tolower(__lo, __hi); }
270
271       /**
272        *  @brief  Widen char to char_type
273        *
274        *  This function converts the char argument to char_type using the
275        *  simplest reasonable transformation.  It does so by returning
276        *  ctype<char_type>::do_widen(c).
277        *
278        *  Note: this is not what you want for codepage conversions.  See
279        *  codecvt for that.
280        *
281        *  @param c  The char to convert.
282        *  @return  The converted char_type.
283       */
284       char_type
285       widen(char __c) const
286       { return this->do_widen(__c); }
287
288       /**
289        *  @brief  Widen array to char_type
290        *
291        *  This function converts each char in the input to char_type using the
292        *  simplest reasonable transformation.  It does so by returning
293        *  ctype<char_type>::do_widen(c).
294        *
295        *  Note: this is not what you want for codepage conversions.  See
296        *  codecvt for that.
297        *
298        *  @param lo  Pointer to start of range.
299        *  @param hi  Pointer to end of range.
300        *  @param to  Pointer to the destination array.
301        *  @return  @a hi.
302       */
303       const char*
304       widen(const char* __lo, const char* __hi, char_type* __to) const
305       { return this->do_widen(__lo, __hi, __to); }
306
307       /**
308        *  @brief  Narrow char_type to char
309        *
310        *  This function converts the char_type to char using the simplest
311        *  reasonable transformation.  If the conversion fails, dfault is
312        *  returned instead.  It does so by returning
313        *  ctype<char_type>::do_narrow(c).
314        *
315        *  Note: this is not what you want for codepage conversions.  See
316        *  codecvt for that.
317        *
318        *  @param c  The char_type to convert.
319        *  @param dfault  Char to return if conversion fails.
320        *  @return  The converted char.
321       */
322       char
323       narrow(char_type __c, char __dfault) const
324       { return this->do_narrow(__c, __dfault); }
325
326       /**
327        *  @brief  Narrow array to char array
328        *
329        *  This function converts each char_type in the input to char using the
330        *  simplest reasonable transformation and writes the results to the
331        *  destination array.  For any char_type in the input that cannot be
332        *  converted, @a dfault is used instead.  It does so by returning
333        *  ctype<char_type>::do_narrow(lo, hi, dfault, to).
334        *
335        *  Note: this is not what you want for codepage conversions.  See
336        *  codecvt for that.
337        *
338        *  @param lo  Pointer to start of range.
339        *  @param hi  Pointer to end of range.
340        *  @param dfault  Char to use if conversion fails.
341        *  @param to  Pointer to the destination array.
342        *  @return  @a hi.
343       */
344       const char_type*
345       narrow(const char_type* __lo, const char_type* __hi,
346               char __dfault, char *__to) const
347       { return this->do_narrow(__lo, __hi, __dfault, __to); }
348
349     protected:
350       explicit
351       __ctype_abstract_base(size_t __refs = 0): facet(__refs) { }
352
353       virtual
354       ~__ctype_abstract_base() { }
355
356       /**
357        *  @brief  Test char_type classification.
358        *
359        *  This function finds a mask M for @a c and compares it to mask @a m.
360        *
361        *  do_is() is a hook for a derived facet to change the behavior of
362        *  classifying.  do_is() must always return the same result for the
363        *  same input.
364        *
365        *  @param c  The char_type to find the mask of.
366        *  @param m  The mask to compare against.
367        *  @return  (M & m) != 0.
368       */
369       virtual bool
370       do_is(mask __m, char_type __c) const = 0;
371
372       /**
373        *  @brief  Return a mask array.
374        *
375        *  This function finds the mask for each char_type in the range [lo,hi)
376        *  and successively writes it to vec.  vec must have as many elements
377        *  as the input.
378        *
379        *  do_is() is a hook for a derived facet to change the behavior of
380        *  classifying.  do_is() must always return the same result for the
381        *  same input.
382        *
383        *  @param lo  Pointer to start of range.
384        *  @param hi  Pointer to end of range.
385        *  @param vec  Pointer to an array of mask storage.
386        *  @return  @a hi.
387       */
388       virtual const char_type*
389       do_is(const char_type* __lo, const char_type* __hi,
390             mask* __vec) const = 0;
391
392       /**
393        *  @brief  Find char_type matching mask
394        *
395        *  This function searches for and returns the first char_type c in
396        *  [lo,hi) for which is(m,c) is true.
397        *
398        *  do_scan_is() is a hook for a derived facet to change the behavior of
399        *  match searching.  do_is() must always return the same result for the
400        *  same input.
401        *
402        *  @param m  The mask to compare against.
403        *  @param lo  Pointer to start of range.
404        *  @param hi  Pointer to end of range.
405        *  @return  Pointer to a matching char_type if found, else @a hi.
406       */
407       virtual const char_type*
408       do_scan_is(mask __m, const char_type* __lo,
409                  const char_type* __hi) const = 0;
410
411       /**
412        *  @brief  Find char_type not matching mask
413        *
414        *  This function searches for and returns a pointer to the first
415        *  char_type c of [lo,hi) for which is(m,c) is false.
416        *
417        *  do_scan_is() is a hook for a derived facet to change the behavior of
418        *  match searching.  do_is() must always return the same result for the
419        *  same input.
420        *
421        *  @param m  The mask to compare against.
422        *  @param lo  Pointer to start of range.
423        *  @param hi  Pointer to end of range.
424        *  @return  Pointer to a non-matching char_type if found, else @a hi.
425       */
426       virtual const char_type*
427       do_scan_not(mask __m, const char_type* __lo,
428                   const char_type* __hi) const = 0;
429
430       /**
431        *  @brief  Convert to uppercase.
432        *
433        *  This virtual function converts the char_type argument to uppercase
434        *  if possible.  If not possible (for example, '2'), returns the
435        *  argument.
436        *
437        *  do_toupper() is a hook for a derived facet to change the behavior of
438        *  uppercasing.  do_toupper() must always return the same result for
439        *  the same input.
440        *
441        *  @param c  The char_type to convert.
442        *  @return  The uppercase char_type if convertible, else @a c.
443       */
444       virtual char_type
445       do_toupper(char_type) const = 0;
446
447       /**
448        *  @brief  Convert array to uppercase.
449        *
450        *  This virtual function converts each char_type in the range [lo,hi)
451        *  to uppercase if possible.  Other elements remain untouched.
452        *
453        *  do_toupper() is a hook for a derived facet to change the behavior of
454        *  uppercasing.  do_toupper() must always return the same result for
455        *  the same input.
456        *
457        *  @param lo  Pointer to start of range.
458        *  @param hi  Pointer to end of range.
459        *  @return  @a hi.
460       */
461       virtual const char_type*
462       do_toupper(char_type* __lo, const char_type* __hi) const = 0;
463
464       /**
465        *  @brief  Convert to lowercase.
466        *
467        *  This virtual function converts the argument to lowercase if
468        *  possible.  If not possible (for example, '2'), returns the argument.
469        *
470        *  do_tolower() is a hook for a derived facet to change the behavior of
471        *  lowercasing.  do_tolower() must always return the same result for
472        *  the same input.
473        *
474        *  @param c  The char_type to convert.
475        *  @return  The lowercase char_type if convertible, else @a c.
476       */
477       virtual char_type
478       do_tolower(char_type) const = 0;
479
480       /**
481        *  @brief  Convert array to lowercase.
482        *
483        *  This virtual function converts each char_type in the range [lo,hi)
484        *  to lowercase if possible.  Other elements remain untouched.
485        *
486        *  do_tolower() is a hook for a derived facet to change the behavior of
487        *  lowercasing.  do_tolower() must always return the same result for
488        *  the same input.
489        *
490        *  @param lo  Pointer to start of range.
491        *  @param hi  Pointer to end of range.
492        *  @return  @a hi.
493       */
494       virtual const char_type*
495       do_tolower(char_type* __lo, const char_type* __hi) const = 0;
496
497       /**
498        *  @brief  Widen char
499        *
500        *  This virtual function converts the char to char_type using the
501        *  simplest reasonable transformation.
502        *
503        *  do_widen() is a hook for a derived facet to change the behavior of
504        *  widening.  do_widen() must always return the same result for the
505        *  same input.
506        *
507        *  Note: this is not what you want for codepage conversions.  See
508        *  codecvt for that.
509        *
510        *  @param c  The char to convert.
511        *  @return  The converted char_type
512       */
513       virtual char_type
514       do_widen(char) const = 0;
515
516       /**
517        *  @brief  Widen char array
518        *
519        *  This function converts each char in the input to char_type using the
520        *  simplest reasonable transformation.
521        *
522        *  do_widen() is a hook for a derived facet to change the behavior of
523        *  widening.  do_widen() must always return the same result for the
524        *  same input.
525        *
526        *  Note: this is not what you want for codepage conversions.  See
527        *  codecvt for that.
528        *
529        *  @param lo  Pointer to start range.
530        *  @param hi  Pointer to end of range.
531        *  @param to  Pointer to the destination array.
532        *  @return  @a hi.
533       */
534       virtual const char*
535       do_widen(const char* __lo, const char* __hi,
536                char_type* __dest) const = 0;
537
538       /**
539        *  @brief  Narrow char_type to char
540        *
541        *  This virtual function converts the argument to char using the
542        *  simplest reasonable transformation.  If the conversion fails, dfault
543        *  is returned instead.
544        *
545        *  do_narrow() is a hook for a derived facet to change the behavior of
546        *  narrowing.  do_narrow() must always return the same result for the
547        *  same input.
548        *
549        *  Note: this is not what you want for codepage conversions.  See
550        *  codecvt for that.
551        *
552        *  @param c  The char_type to convert.
553        *  @param dfault  Char to return if conversion fails.
554        *  @return  The converted char.
555       */
556       virtual char
557       do_narrow(char_type, char __dfault) const = 0;
558
559       /**
560        *  @brief  Narrow char_type array to char
561        *
562        *  This virtual function converts each char_type in the range [lo,hi) to
563        *  char using the simplest reasonable transformation and writes the
564        *  results to the destination array.  For any element in the input that
565        *  cannot be converted, @a dfault is used instead.
566        *
567        *  do_narrow() is a hook for a derived facet to change the behavior of
568        *  narrowing.  do_narrow() must always return the same result for the
569        *  same input.
570        *
571        *  Note: this is not what you want for codepage conversions.  See
572        *  codecvt for that.
573        *
574        *  @param lo  Pointer to start of range.
575        *  @param hi  Pointer to end of range.
576        *  @param dfault  Char to use if conversion fails.
577        *  @param to  Pointer to the destination array.
578        *  @return  @a hi.
579       */
580       virtual const char_type*
581       do_narrow(const char_type* __lo, const char_type* __hi,
582                 char __dfault, char* __dest) const = 0;
583     };
584
585   /**
586    *  @brief  Primary class template ctype facet.
587    *  @ingroup locales
588    *
589    *  This template class defines classification and conversion functions for
590    *  character sets.  It wraps cctype functionality.  Ctype gets used by
591    *  streams for many I/O operations.
592    *
593    *  This template provides the protected virtual functions the developer
594    *  will have to replace in a derived class or specialization to make a
595    *  working facet.  The public functions that access them are defined in
596    *  __ctype_abstract_base, to allow for implementation flexibility.  See
597    *  ctype<wchar_t> for an example.  The functions are documented in
598    *  __ctype_abstract_base.
599    *
600    *  Note: implementations are provided for all the protected virtual
601    *  functions, but will likely not be useful.
602   */
603   template<typename _CharT>
604     class ctype : public __ctype_abstract_base<_CharT>
605     {
606     public:
607       // Types:
608       typedef _CharT                    char_type;
609       typedef typename __ctype_abstract_base<_CharT>::mask mask;
610
611       /// The facet id for ctype<char_type>
612       static locale::id                 id;
613
614       explicit
615       ctype(size_t __refs = 0) : __ctype_abstract_base<_CharT>(__refs) { }
616
617    protected:
618       virtual
619       ~ctype();
620
621       virtual bool
622       do_is(mask __m, char_type __c) const;
623
624       virtual const char_type*
625       do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
626
627       virtual const char_type*
628       do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
629
630       virtual const char_type*
631       do_scan_not(mask __m, const char_type* __lo,
632                   const char_type* __hi) const;
633
634       virtual char_type
635       do_toupper(char_type __c) const;
636
637       virtual const char_type*
638       do_toupper(char_type* __lo, const char_type* __hi) const;
639
640       virtual char_type
641       do_tolower(char_type __c) const;
642
643       virtual const char_type*
644       do_tolower(char_type* __lo, const char_type* __hi) const;
645
646       virtual char_type
647       do_widen(char __c) const;
648
649       virtual const char*
650       do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
651
652       virtual char
653       do_narrow(char_type, char __dfault) const;
654
655       virtual const char_type*
656       do_narrow(const char_type* __lo, const char_type* __hi,
657                 char __dfault, char* __dest) const;
658     };
659
660   template<typename _CharT>
661     locale::id ctype<_CharT>::id;
662
663   /**
664    *  @brief  The ctype<char> specialization.
665    *  @ingroup locales
666    *
667    *  This class defines classification and conversion functions for
668    *  the char type.  It gets used by char streams for many I/O
669    *  operations.  The char specialization provides a number of
670    *  optimizations as well.
671   */
672   template<>
673     class ctype<char> : public locale::facet, public ctype_base
674     {
675     public:
676       // Types:
677       /// Typedef for the template parameter char.
678       typedef char              char_type;
679
680     protected:
681       // Data Members:
682       __c_locale                _M_c_locale_ctype;
683       bool                      _M_del;
684       __to_type                 _M_toupper;
685       __to_type                 _M_tolower;
686       const mask*               _M_table;
687       mutable char              _M_widen_ok;
688       mutable char              _M_widen[1 + static_cast<unsigned char>(-1)];
689       mutable char              _M_narrow[1 + static_cast<unsigned char>(-1)];
690       mutable char              _M_narrow_ok;   // 0 uninitialized, 1 init,
691                                                 // 2 memcpy can't be used
692
693     public:
694       /// The facet id for ctype<char>
695       static locale::id        id;
696       /// The size of the mask table.  It is SCHAR_MAX + 1.
697       static const size_t      table_size = 1 + static_cast<unsigned char>(-1);
698
699       /**
700        *  @brief  Constructor performs initialization.
701        *
702        *  This is the constructor provided by the standard.
703        *
704        *  @param table If non-zero, table is used as the per-char mask.
705        *               Else classic_table() is used.
706        *  @param del   If true, passes ownership of table to this facet.
707        *  @param refs  Passed to the base facet class.
708       */
709       explicit
710       ctype(const mask* __table = 0, bool __del = false, size_t __refs = 0);
711
712       /**
713        *  @brief  Constructor performs static initialization.
714        *
715        *  This constructor is used to construct the initial C locale facet.
716        *
717        *  @param cloc  Handle to C locale data.
718        *  @param table If non-zero, table is used as the per-char mask.
719        *  @param del   If true, passes ownership of table to this facet.
720        *  @param refs  Passed to the base facet class.
721       */
722       explicit
723       ctype(__c_locale __cloc, const mask* __table = 0, bool __del = false,
724             size_t __refs = 0);
725
726       /**
727        *  @brief  Test char classification.
728        *
729        *  This function compares the mask table[c] to @a m.
730        *
731        *  @param c  The char to compare the mask of.
732        *  @param m  The mask to compare against.
733        *  @return  True if m & table[c] is true, false otherwise.
734       */
735       inline bool
736       is(mask __m, char __c) const;
737
738       /**
739        *  @brief  Return a mask array.
740        *
741        *  This function finds the mask for each char in the range [lo, hi) and
742        *  successively writes it to vec.  vec must have as many elements as
743        *  the char array.
744        *
745        *  @param lo  Pointer to start of range.
746        *  @param hi  Pointer to end of range.
747        *  @param vec  Pointer to an array of mask storage.
748        *  @return  @a hi.
749       */
750       inline const char*
751       is(const char* __lo, const char* __hi, mask* __vec) const;
752
753       /**
754        *  @brief  Find char matching a mask
755        *
756        *  This function searches for and returns the first char in [lo,hi) for
757        *  which is(m,char) is true.
758        *
759        *  @param m  The mask to compare against.
760        *  @param lo  Pointer to start of range.
761        *  @param hi  Pointer to end of range.
762        *  @return  Pointer to a matching char if found, else @a hi.
763       */
764       inline const char*
765       scan_is(mask __m, const char* __lo, const char* __hi) const;
766
767       /**
768        *  @brief  Find char not matching a mask
769        *
770        *  This function searches for and returns a pointer to the first char
771        *  in [lo,hi) for which is(m,char) is false.
772        *
773        *  @param m  The mask to compare against.
774        *  @param lo  Pointer to start of range.
775        *  @param hi  Pointer to end of range.
776        *  @return  Pointer to a non-matching char if found, else @a hi.
777       */
778       inline const char*
779       scan_not(mask __m, const char* __lo, const char* __hi) const;
780
781       /**
782        *  @brief  Convert to uppercase.
783        *
784        *  This function converts the char argument to uppercase if possible.
785        *  If not possible (for example, '2'), returns the argument.
786        *
787        *  toupper() acts as if it returns ctype<char>::do_toupper(c).
788        *  do_toupper() must always return the same result for the same input.
789        *
790        *  @param c  The char to convert.
791        *  @return  The uppercase char if convertible, else @a c.
792       */
793       char_type
794       toupper(char_type __c) const
795       { return this->do_toupper(__c); }
796
797       /**
798        *  @brief  Convert array to uppercase.
799        *
800        *  This function converts each char in the range [lo,hi) to uppercase
801        *  if possible.  Other chars remain untouched.
802        *
803        *  toupper() acts as if it returns ctype<char>:: do_toupper(lo, hi).
804        *  do_toupper() must always return the same result for the same input.
805        *
806        *  @param lo  Pointer to first char in range.
807        *  @param hi  Pointer to end of range.
808        *  @return  @a hi.
809       */
810       const char_type*
811       toupper(char_type *__lo, const char_type* __hi) const
812       { return this->do_toupper(__lo, __hi); }
813
814       /**
815        *  @brief  Convert to lowercase.
816        *
817        *  This function converts the char argument to lowercase if possible.
818        *  If not possible (for example, '2'), returns the argument.
819        *
820        *  tolower() acts as if it returns ctype<char>::do_tolower(c).
821        *  do_tolower() must always return the same result for the same input.
822        *
823        *  @param c  The char to convert.
824        *  @return  The lowercase char if convertible, else @a c.
825       */
826       char_type
827       tolower(char_type __c) const
828       { return this->do_tolower(__c); }
829
830       /**
831        *  @brief  Convert array to lowercase.
832        *
833        *  This function converts each char in the range [lo,hi) to lowercase
834        *  if possible.  Other chars remain untouched.
835        *
836        *  tolower() acts as if it returns ctype<char>:: do_tolower(lo, hi).
837        *  do_tolower() must always return the same result for the same input.
838        *
839        *  @param lo  Pointer to first char in range.
840        *  @param hi  Pointer to end of range.
841        *  @return  @a hi.
842       */
843       const char_type*
844       tolower(char_type* __lo, const char_type* __hi) const
845       { return this->do_tolower(__lo, __hi); }
846
847       /**
848        *  @brief  Widen char
849        *
850        *  This function converts the char to char_type using the simplest
851        *  reasonable transformation.  For an underived ctype<char> facet, the
852        *  argument will be returned unchanged.
853        *
854        *  This function works as if it returns ctype<char>::do_widen(c).
855        *  do_widen() must always return the same result for the same input.
856        *
857        *  Note: this is not what you want for codepage conversions.  See
858        *  codecvt for that.
859        *
860        *  @param c  The char to convert.
861        *  @return  The converted character.
862       */
863       char_type
864       widen(char __c) const
865       {
866         if (_M_widen_ok)
867           return _M_widen[static_cast<unsigned char>(__c)];
868         this->_M_widen_init();
869         return this->do_widen(__c);
870       }
871
872       /**
873        *  @brief  Widen char array
874        *
875        *  This function converts each char in the input to char using the
876        *  simplest reasonable transformation.  For an underived ctype<char>
877        *  facet, the argument will be copied unchanged.
878        *
879        *  This function works as if it returns ctype<char>::do_widen(c).
880        *  do_widen() must always return the same result for the same input.
881        *
882        *  Note: this is not what you want for codepage conversions.  See
883        *  codecvt for that.
884        *
885        *  @param lo  Pointer to first char in range.
886        *  @param hi  Pointer to end of range.
887        *  @param to  Pointer to the destination array.
888        *  @return  @a hi.
889       */
890       const char*
891       widen(const char* __lo, const char* __hi, char_type* __to) const
892       {
893         if (_M_widen_ok == 1)
894           {
895             __builtin_memcpy(__to, __lo, __hi - __lo);
896             return __hi;
897           }
898         if (!_M_widen_ok)
899           _M_widen_init();
900         return this->do_widen(__lo, __hi, __to);
901       }
902
903       /**
904        *  @brief  Narrow char
905        *
906        *  This function converts the char to char using the simplest
907        *  reasonable transformation.  If the conversion fails, dfault is
908        *  returned instead.  For an underived ctype<char> facet, @a c
909        *  will be returned unchanged.
910        *
911        *  This function works as if it returns ctype<char>::do_narrow(c).
912        *  do_narrow() must always return the same result for the same input.
913        *
914        *  Note: this is not what you want for codepage conversions.  See
915        *  codecvt for that.
916        *
917        *  @param c  The char to convert.
918        *  @param dfault  Char to return if conversion fails.
919        *  @return  The converted character.
920       */
921       char
922       narrow(char_type __c, char __dfault) const
923       {
924         if (_M_narrow[static_cast<unsigned char>(__c)])
925           return _M_narrow[static_cast<unsigned char>(__c)];
926         const char __t = do_narrow(__c, __dfault);
927         if (__t != __dfault)
928           _M_narrow[static_cast<unsigned char>(__c)] = __t;
929         return __t;
930       }
931
932       /**
933        *  @brief  Narrow char array
934        *
935        *  This function converts each char in the input to char using the
936        *  simplest reasonable transformation and writes the results to the
937        *  destination array.  For any char in the input that cannot be
938        *  converted, @a dfault is used instead.  For an underived ctype<char>
939        *  facet, the argument will be copied unchanged.
940        *
941        *  This function works as if it returns ctype<char>::do_narrow(lo, hi,
942        *  dfault, to).  do_narrow() must always return the same result for the
943        *  same input.
944        *
945        *  Note: this is not what you want for codepage conversions.  See
946        *  codecvt for that.
947        *
948        *  @param lo  Pointer to start of range.
949        *  @param hi  Pointer to end of range.
950        *  @param dfault  Char to use if conversion fails.
951        *  @param to  Pointer to the destination array.
952        *  @return  @a hi.
953       */
954       const char_type*
955       narrow(const char_type* __lo, const char_type* __hi,
956              char __dfault, char *__to) const
957       {
958         if (__builtin_expect(_M_narrow_ok == 1, true))
959           {
960             __builtin_memcpy(__to, __lo, __hi - __lo);
961             return __hi;
962           }
963         if (!_M_narrow_ok)
964           _M_narrow_init();
965         return this->do_narrow(__lo, __hi, __dfault, __to);
966       }
967
968       // _GLIBCXX_RESOLVE_LIB_DEFECTS
969       // DR 695. ctype<char>::classic_table() not accessible.
970       /// Returns a pointer to the mask table provided to the constructor, or
971       /// the default from classic_table() if none was provided.
972       const mask*
973       table() const throw()
974       { return _M_table; }
975
976       /// Returns a pointer to the C locale mask table.
977       static const mask*
978       classic_table() throw();
979     protected:
980
981       /**
982        *  @brief  Destructor.
983        *
984        *  This function deletes table() if @a del was true in the
985        *  constructor.
986       */
987       virtual
988       ~ctype();
989
990       /**
991        *  @brief  Convert to uppercase.
992        *
993        *  This virtual function converts the char argument to uppercase if
994        *  possible.  If not possible (for example, '2'), returns the argument.
995        *
996        *  do_toupper() is a hook for a derived facet to change the behavior of
997        *  uppercasing.  do_toupper() must always return the same result for
998        *  the same input.
999        *
1000        *  @param c  The char to convert.
1001        *  @return  The uppercase char if convertible, else @a c.
1002       */
1003       virtual char_type
1004       do_toupper(char_type) const;
1005
1006       /**
1007        *  @brief  Convert array to uppercase.
1008        *
1009        *  This virtual function converts each char in the range [lo,hi) to
1010        *  uppercase if possible.  Other chars remain untouched.
1011        *
1012        *  do_toupper() is a hook for a derived facet to change the behavior of
1013        *  uppercasing.  do_toupper() must always return the same result for
1014        *  the same input.
1015        *
1016        *  @param lo  Pointer to start of range.
1017        *  @param hi  Pointer to end of range.
1018        *  @return  @a hi.
1019       */
1020       virtual const char_type*
1021       do_toupper(char_type* __lo, const char_type* __hi) const;
1022
1023       /**
1024        *  @brief  Convert to lowercase.
1025        *
1026        *  This virtual function converts the char argument to lowercase if
1027        *  possible.  If not possible (for example, '2'), returns the argument.
1028        *
1029        *  do_tolower() is a hook for a derived facet to change the behavior of
1030        *  lowercasing.  do_tolower() must always return the same result for
1031        *  the same input.
1032        *
1033        *  @param c  The char to convert.
1034        *  @return  The lowercase char if convertible, else @a c.
1035       */
1036       virtual char_type
1037       do_tolower(char_type) const;
1038
1039       /**
1040        *  @brief  Convert array to lowercase.
1041        *
1042        *  This virtual function converts each char in the range [lo,hi) to
1043        *  lowercase if possible.  Other chars remain untouched.
1044        *
1045        *  do_tolower() is a hook for a derived facet to change the behavior of
1046        *  lowercasing.  do_tolower() must always return the same result for
1047        *  the same input.
1048        *
1049        *  @param lo  Pointer to first char in range.
1050        *  @param hi  Pointer to end of range.
1051        *  @return  @a hi.
1052       */
1053       virtual const char_type*
1054       do_tolower(char_type* __lo, const char_type* __hi) const;
1055
1056       /**
1057        *  @brief  Widen char
1058        *
1059        *  This virtual function converts the char to char using the simplest
1060        *  reasonable transformation.  For an underived ctype<char> facet, the
1061        *  argument will be returned unchanged.
1062        *
1063        *  do_widen() is a hook for a derived facet to change the behavior of
1064        *  widening.  do_widen() must always return the same result for the
1065        *  same input.
1066        *
1067        *  Note: this is not what you want for codepage conversions.  See
1068        *  codecvt for that.
1069        *
1070        *  @param c  The char to convert.
1071        *  @return  The converted character.
1072       */
1073       virtual char_type
1074       do_widen(char __c) const
1075       { return __c; }
1076
1077       /**
1078        *  @brief  Widen char array
1079        *
1080        *  This function converts each char in the range [lo,hi) to char using
1081        *  the simplest reasonable transformation.  For an underived
1082        *  ctype<char> facet, the argument will be copied unchanged.
1083        *
1084        *  do_widen() is a hook for a derived facet to change the behavior of
1085        *  widening.  do_widen() must always return the same result for the
1086        *  same input.
1087        *
1088        *  Note: this is not what you want for codepage conversions.  See
1089        *  codecvt for that.
1090        *
1091        *  @param lo  Pointer to start of range.
1092        *  @param hi  Pointer to end of range.
1093        *  @param to  Pointer to the destination array.
1094        *  @return  @a hi.
1095       */
1096       virtual const char*
1097       do_widen(const char* __lo, const char* __hi, char_type* __dest) const
1098       {
1099         __builtin_memcpy(__dest, __lo, __hi - __lo);
1100         return __hi;
1101       }
1102
1103       /**
1104        *  @brief  Narrow char
1105        *
1106        *  This virtual function converts the char to char using the simplest
1107        *  reasonable transformation.  If the conversion fails, dfault is
1108        *  returned instead.  For an underived ctype<char> facet, @a c will be
1109        *  returned unchanged.
1110        *
1111        *  do_narrow() is a hook for a derived facet to change the behavior of
1112        *  narrowing.  do_narrow() must always return the same result for the
1113        *  same input.
1114        *
1115        *  Note: this is not what you want for codepage conversions.  See
1116        *  codecvt for that.
1117        *
1118        *  @param c  The char to convert.
1119        *  @param dfault  Char to return if conversion fails.
1120        *  @return  The converted char.
1121       */
1122       virtual char
1123       do_narrow(char_type __c, char) const
1124       { return __c; }
1125
1126       /**
1127        *  @brief  Narrow char array to char array
1128        *
1129        *  This virtual function converts each char in the range [lo,hi) to
1130        *  char using the simplest reasonable transformation and writes the
1131        *  results to the destination array.  For any char in the input that
1132        *  cannot be converted, @a dfault is used instead.  For an underived
1133        *  ctype<char> facet, the argument will be copied unchanged.
1134        *
1135        *  do_narrow() is a hook for a derived facet to change the behavior of
1136        *  narrowing.  do_narrow() must always return the same result for the
1137        *  same input.
1138        *
1139        *  Note: this is not what you want for codepage conversions.  See
1140        *  codecvt for that.
1141        *
1142        *  @param lo  Pointer to start of range.
1143        *  @param hi  Pointer to end of range.
1144        *  @param dfault  Char to use if conversion fails.
1145        *  @param to  Pointer to the destination array.
1146        *  @return  @a hi.
1147       */
1148       virtual const char_type*
1149       do_narrow(const char_type* __lo, const char_type* __hi,
1150                 char, char* __dest) const
1151       {
1152         __builtin_memcpy(__dest, __lo, __hi - __lo);
1153         return __hi;
1154       }
1155
1156     private:
1157       void _M_narrow_init() const;
1158       void _M_widen_init() const;
1159     };
1160
1161 #ifdef _GLIBCXX_USE_WCHAR_T
1162   /**
1163    *  @brief  The ctype<wchar_t> specialization.
1164    *  @ingroup locales
1165    *
1166    *  This class defines classification and conversion functions for the
1167    *  wchar_t type.  It gets used by wchar_t streams for many I/O operations.
1168    *  The wchar_t specialization provides a number of optimizations as well.
1169    *
1170    *  ctype<wchar_t> inherits its public methods from
1171    *  __ctype_abstract_base<wchar_t>.
1172   */
1173   template<>
1174     class ctype<wchar_t> : public __ctype_abstract_base<wchar_t>
1175     {
1176     public:
1177       // Types:
1178       /// Typedef for the template parameter wchar_t.
1179       typedef wchar_t           char_type;
1180       typedef wctype_t          __wmask_type;
1181
1182     protected:
1183       __c_locale                _M_c_locale_ctype;
1184
1185       // Pre-computed narrowed and widened chars.
1186       bool                      _M_narrow_ok;
1187       char                      _M_narrow[128];
1188       wint_t                    _M_widen[1 + static_cast<unsigned char>(-1)];
1189
1190       // Pre-computed elements for do_is.
1191       mask                      _M_bit[16];
1192       __wmask_type              _M_wmask[16];
1193
1194     public:
1195       // Data Members:
1196       /// The facet id for ctype<wchar_t>
1197       static locale::id         id;
1198
1199       /**
1200        *  @brief  Constructor performs initialization.
1201        *
1202        *  This is the constructor provided by the standard.
1203        *
1204        *  @param refs  Passed to the base facet class.
1205       */
1206       explicit
1207       ctype(size_t __refs = 0);
1208
1209       /**
1210        *  @brief  Constructor performs static initialization.
1211        *
1212        *  This constructor is used to construct the initial C locale facet.
1213        *
1214        *  @param cloc  Handle to C locale data.
1215        *  @param refs  Passed to the base facet class.
1216       */
1217       explicit
1218       ctype(__c_locale __cloc, size_t __refs = 0);
1219
1220     protected:
1221       __wmask_type
1222       _M_convert_to_wmask(const mask __m) const throw();
1223
1224       /// Destructor
1225       virtual
1226       ~ctype();
1227
1228       /**
1229        *  @brief  Test wchar_t classification.
1230        *
1231        *  This function finds a mask M for @a c and compares it to mask @a m.
1232        *
1233        *  do_is() is a hook for a derived facet to change the behavior of
1234        *  classifying.  do_is() must always return the same result for the
1235        *  same input.
1236        *
1237        *  @param c  The wchar_t to find the mask of.
1238        *  @param m  The mask to compare against.
1239        *  @return  (M & m) != 0.
1240       */
1241       virtual bool
1242       do_is(mask __m, char_type __c) const;
1243
1244       /**
1245        *  @brief  Return a mask array.
1246        *
1247        *  This function finds the mask for each wchar_t in the range [lo,hi)
1248        *  and successively writes it to vec.  vec must have as many elements
1249        *  as the input.
1250        *
1251        *  do_is() is a hook for a derived facet to change the behavior of
1252        *  classifying.  do_is() must always return the same result for the
1253        *  same input.
1254        *
1255        *  @param lo  Pointer to start of range.
1256        *  @param hi  Pointer to end of range.
1257        *  @param vec  Pointer to an array of mask storage.
1258        *  @return  @a hi.
1259       */
1260       virtual const char_type*
1261       do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
1262
1263       /**
1264        *  @brief  Find wchar_t matching mask
1265        *
1266        *  This function searches for and returns the first wchar_t c in
1267        *  [lo,hi) for which is(m,c) is true.
1268        *
1269        *  do_scan_is() is a hook for a derived facet to change the behavior of
1270        *  match searching.  do_is() must always return the same result for the
1271        *  same input.
1272        *
1273        *  @param m  The mask to compare against.
1274        *  @param lo  Pointer to start of range.
1275        *  @param hi  Pointer to end of range.
1276        *  @return  Pointer to a matching wchar_t if found, else @a hi.
1277       */
1278       virtual const char_type*
1279       do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
1280
1281       /**
1282        *  @brief  Find wchar_t not matching mask
1283        *
1284        *  This function searches for and returns a pointer to the first
1285        *  wchar_t c of [lo,hi) for which is(m,c) is false.
1286        *
1287        *  do_scan_is() is a hook for a derived facet to change the behavior of
1288        *  match searching.  do_is() must always return the same result for the
1289        *  same input.
1290        *
1291        *  @param m  The mask to compare against.
1292        *  @param lo  Pointer to start of range.
1293        *  @param hi  Pointer to end of range.
1294        *  @return  Pointer to a non-matching wchar_t if found, else @a hi.
1295       */
1296       virtual const char_type*
1297       do_scan_not(mask __m, const char_type* __lo,
1298                   const char_type* __hi) const;
1299
1300       /**
1301        *  @brief  Convert to uppercase.
1302        *
1303        *  This virtual function converts the wchar_t argument to uppercase if
1304        *  possible.  If not possible (for example, '2'), returns the argument.
1305        *
1306        *  do_toupper() is a hook for a derived facet to change the behavior of
1307        *  uppercasing.  do_toupper() must always return the same result for
1308        *  the same input.
1309        *
1310        *  @param c  The wchar_t to convert.
1311        *  @return  The uppercase wchar_t if convertible, else @a c.
1312       */
1313       virtual char_type
1314       do_toupper(char_type) const;
1315
1316       /**
1317        *  @brief  Convert array to uppercase.
1318        *
1319        *  This virtual function converts each wchar_t in the range [lo,hi) to
1320        *  uppercase if possible.  Other elements remain untouched.
1321        *
1322        *  do_toupper() is a hook for a derived facet to change the behavior of
1323        *  uppercasing.  do_toupper() must always return the same result for
1324        *  the same input.
1325        *
1326        *  @param lo  Pointer to start of range.
1327        *  @param hi  Pointer to end of range.
1328        *  @return  @a hi.
1329       */
1330       virtual const char_type*
1331       do_toupper(char_type* __lo, const char_type* __hi) const;
1332
1333       /**
1334        *  @brief  Convert to lowercase.
1335        *
1336        *  This virtual function converts the argument to lowercase if
1337        *  possible.  If not possible (for example, '2'), returns the argument.
1338        *
1339        *  do_tolower() is a hook for a derived facet to change the behavior of
1340        *  lowercasing.  do_tolower() must always return the same result for
1341        *  the same input.
1342        *
1343        *  @param c  The wchar_t to convert.
1344        *  @return  The lowercase wchar_t if convertible, else @a c.
1345       */
1346       virtual char_type
1347       do_tolower(char_type) const;
1348
1349       /**
1350        *  @brief  Convert array to lowercase.
1351        *
1352        *  This virtual function converts each wchar_t in the range [lo,hi) to
1353        *  lowercase if possible.  Other elements remain untouched.
1354        *
1355        *  do_tolower() is a hook for a derived facet to change the behavior of
1356        *  lowercasing.  do_tolower() must always return the same result for
1357        *  the same input.
1358        *
1359        *  @param lo  Pointer to start of range.
1360        *  @param hi  Pointer to end of range.
1361        *  @return  @a hi.
1362       */
1363       virtual const char_type*
1364       do_tolower(char_type* __lo, const char_type* __hi) const;
1365
1366       /**
1367        *  @brief  Widen char to wchar_t
1368        *
1369        *  This virtual function converts the char to wchar_t using the
1370        *  simplest reasonable transformation.  For an underived ctype<wchar_t>
1371        *  facet, the argument will be cast to wchar_t.
1372        *
1373        *  do_widen() is a hook for a derived facet to change the behavior of
1374        *  widening.  do_widen() must always return the same result for the
1375        *  same input.
1376        *
1377        *  Note: this is not what you want for codepage conversions.  See
1378        *  codecvt for that.
1379        *
1380        *  @param c  The char to convert.
1381        *  @return  The converted wchar_t.
1382       */
1383       virtual char_type
1384       do_widen(char) const;
1385
1386       /**
1387        *  @brief  Widen char array to wchar_t array
1388        *
1389        *  This function converts each char in the input to wchar_t using the
1390        *  simplest reasonable transformation.  For an underived ctype<wchar_t>
1391        *  facet, the argument will be copied, casting each element to wchar_t.
1392        *
1393        *  do_widen() is a hook for a derived facet to change the behavior of
1394        *  widening.  do_widen() must always return the same result for the
1395        *  same input.
1396        *
1397        *  Note: this is not what you want for codepage conversions.  See
1398        *  codecvt for that.
1399        *
1400        *  @param lo  Pointer to start range.
1401        *  @param hi  Pointer to end of range.
1402        *  @param to  Pointer to the destination array.
1403        *  @return  @a hi.
1404       */
1405       virtual const char*
1406       do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
1407
1408       /**
1409        *  @brief  Narrow wchar_t to char
1410        *
1411        *  This virtual function converts the argument to char using
1412        *  the simplest reasonable transformation.  If the conversion
1413        *  fails, dfault is returned instead.  For an underived
1414        *  ctype<wchar_t> facet, @a c will be cast to char and
1415        *  returned.
1416        *
1417        *  do_narrow() is a hook for a derived facet to change the
1418        *  behavior of narrowing.  do_narrow() must always return the
1419        *  same result for the same input.
1420        *
1421        *  Note: this is not what you want for codepage conversions.  See
1422        *  codecvt for that.
1423        *
1424        *  @param c  The wchar_t to convert.
1425        *  @param dfault  Char to return if conversion fails.
1426        *  @return  The converted char.
1427       */
1428       virtual char
1429       do_narrow(char_type, char __dfault) const;
1430
1431       /**
1432        *  @brief  Narrow wchar_t array to char array
1433        *
1434        *  This virtual function converts each wchar_t in the range [lo,hi) to
1435        *  char using the simplest reasonable transformation and writes the
1436        *  results to the destination array.  For any wchar_t in the input that
1437        *  cannot be converted, @a dfault is used instead.  For an underived
1438        *  ctype<wchar_t> facet, the argument will be copied, casting each
1439        *  element to char.
1440        *
1441        *  do_narrow() is a hook for a derived facet to change the behavior of
1442        *  narrowing.  do_narrow() must always return the same result for the
1443        *  same input.
1444        *
1445        *  Note: this is not what you want for codepage conversions.  See
1446        *  codecvt for that.
1447        *
1448        *  @param lo  Pointer to start of range.
1449        *  @param hi  Pointer to end of range.
1450        *  @param dfault  Char to use if conversion fails.
1451        *  @param to  Pointer to the destination array.
1452        *  @return  @a hi.
1453       */
1454       virtual const char_type*
1455       do_narrow(const char_type* __lo, const char_type* __hi,
1456                 char __dfault, char* __dest) const;
1457
1458       // For use at construction time only.
1459       void
1460       _M_initialize_ctype() throw();
1461     };
1462 #endif //_GLIBCXX_USE_WCHAR_T
1463
1464   /// class ctype_byname [22.2.1.2].
1465   template<typename _CharT>
1466     class ctype_byname : public ctype<_CharT>
1467     {
1468     public:
1469       typedef typename ctype<_CharT>::mask  mask;
1470
1471       explicit
1472       ctype_byname(const char* __s, size_t __refs = 0);
1473
1474     protected:
1475       virtual
1476       ~ctype_byname() { };
1477     };
1478
1479   /// 22.2.1.4  Class ctype_byname specializations.
1480   template<>
1481     class ctype_byname<char> : public ctype<char>
1482     {
1483     public:
1484       explicit
1485       ctype_byname(const char* __s, size_t __refs = 0);
1486
1487     protected:
1488       virtual
1489       ~ctype_byname();
1490     };
1491
1492 #ifdef _GLIBCXX_USE_WCHAR_T
1493   template<>
1494     class ctype_byname<wchar_t> : public ctype<wchar_t>
1495     {
1496     public:
1497       explicit
1498       ctype_byname(const char* __s, size_t __refs = 0);
1499
1500     protected:
1501       virtual
1502       ~ctype_byname();
1503     };
1504 #endif
1505
1506 _GLIBCXX_END_NAMESPACE
1507
1508 // Include host and configuration specific ctype inlines.
1509 #include <bits/ctype_inline.h>
1510
1511 _GLIBCXX_BEGIN_NAMESPACE(std)
1512
1513   // 22.2.2  The numeric category.
1514   class __num_base
1515   {
1516   public:
1517     // NB: Code depends on the order of _S_atoms_out elements.
1518     // Below are the indices into _S_atoms_out.
1519     enum
1520       {
1521         _S_ominus,
1522         _S_oplus,
1523         _S_ox,
1524         _S_oX,
1525         _S_odigits,
1526         _S_odigits_end = _S_odigits + 16,
1527         _S_oudigits = _S_odigits_end,
1528         _S_oudigits_end = _S_oudigits + 16,
1529         _S_oe = _S_odigits + 14,  // For scientific notation, 'e'
1530         _S_oE = _S_oudigits + 14, // For scientific notation, 'E'
1531         _S_oend = _S_oudigits_end
1532       };
1533
1534     // A list of valid numeric literals for output.  This array
1535     // contains chars that will be passed through the current locale's
1536     // ctype<_CharT>.widen() and then used to render numbers.
1537     // For the standard "C" locale, this is
1538     // "-+xX0123456789abcdef0123456789ABCDEF".
1539     static const char* _S_atoms_out;
1540
1541     // String literal of acceptable (narrow) input, for num_get.
1542     // "-+xX0123456789abcdefABCDEF"
1543     static const char* _S_atoms_in;
1544
1545     enum
1546     {
1547       _S_iminus,
1548       _S_iplus,
1549       _S_ix,
1550       _S_iX,
1551       _S_izero,
1552       _S_ie = _S_izero + 14,
1553       _S_iE = _S_izero + 20,
1554       _S_iend = 26
1555     };
1556
1557     // num_put
1558     // Construct and return valid scanf format for floating point types.
1559     static void
1560     _S_format_float(const ios_base& __io, char* __fptr, char __mod) throw();
1561   };
1562
1563   template<typename _CharT>
1564     struct __numpunct_cache : public locale::facet
1565     {
1566       const char*                       _M_grouping;
1567       size_t                            _M_grouping_size;
1568       bool                              _M_use_grouping;
1569       const _CharT*                     _M_truename;
1570       size_t                            _M_truename_size;
1571       const _CharT*                     _M_falsename;
1572       size_t                            _M_falsename_size;
1573       _CharT                            _M_decimal_point;
1574       _CharT                            _M_thousands_sep;
1575
1576       // A list of valid numeric literals for output: in the standard
1577       // "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF".
1578       // This array contains the chars after having been passed
1579       // through the current locale's ctype<_CharT>.widen().
1580       _CharT                            _M_atoms_out[__num_base::_S_oend];
1581
1582       // A list of valid numeric literals for input: in the standard
1583       // "C" locale, this is "-+xX0123456789abcdefABCDEF"
1584       // This array contains the chars after having been passed
1585       // through the current locale's ctype<_CharT>.widen().
1586       _CharT                            _M_atoms_in[__num_base::_S_iend];
1587
1588       bool                              _M_allocated;
1589
1590       __numpunct_cache(size_t __refs = 0) : facet(__refs),
1591       _M_grouping(NULL), _M_grouping_size(0), _M_use_grouping(false),
1592       _M_truename(NULL), _M_truename_size(0), _M_falsename(NULL),
1593       _M_falsename_size(0), _M_decimal_point(_CharT()),
1594       _M_thousands_sep(_CharT()), _M_allocated(false)
1595       { }
1596
1597       ~__numpunct_cache();
1598
1599       void
1600       _M_cache(const locale& __loc);
1601
1602     private:
1603       __numpunct_cache&
1604       operator=(const __numpunct_cache&);
1605       
1606       explicit
1607       __numpunct_cache(const __numpunct_cache&);
1608     };
1609
1610   template<typename _CharT>
1611     __numpunct_cache<_CharT>::~__numpunct_cache()
1612     {
1613       if (_M_allocated)
1614         {
1615           delete [] _M_grouping;
1616           delete [] _M_truename;
1617           delete [] _M_falsename;
1618         }
1619     }
1620
1621   /**
1622    *  @brief  Primary class template numpunct.
1623    *  @ingroup locales
1624    *
1625    *  This facet stores several pieces of information related to printing and
1626    *  scanning numbers, such as the decimal point character.  It takes a
1627    *  template parameter specifying the char type.  The numpunct facet is
1628    *  used by streams for many I/O operations involving numbers.
1629    *
1630    *  The numpunct template uses protected virtual functions to provide the
1631    *  actual results.  The public accessors forward the call to the virtual
1632    *  functions.  These virtual functions are hooks for developers to
1633    *  implement the behavior they require from a numpunct facet.
1634   */
1635   template<typename _CharT>
1636     class numpunct : public locale::facet
1637     {
1638     public:
1639       // Types:
1640       //@{
1641       /// Public typedefs
1642       typedef _CharT                    char_type;
1643       typedef basic_string<_CharT>      string_type;
1644       //@}
1645       typedef __numpunct_cache<_CharT>  __cache_type;
1646
1647     protected:
1648       __cache_type*                     _M_data;
1649
1650     public:
1651       /// Numpunct facet id.
1652       static locale::id                 id;
1653
1654       /**
1655        *  @brief  Numpunct constructor.
1656        *
1657        *  @param  refs  Refcount to pass to the base class.
1658        */
1659       explicit
1660       numpunct(size_t __refs = 0) : facet(__refs), _M_data(NULL)
1661       { _M_initialize_numpunct(); }
1662
1663       /**
1664        *  @brief  Internal constructor.  Not for general use.
1665        *
1666        *  This is a constructor for use by the library itself to set up the
1667        *  predefined locale facets.
1668        *
1669        *  @param  cache  __numpunct_cache object.
1670        *  @param  refs  Refcount to pass to the base class.
1671        */
1672       explicit
1673       numpunct(__cache_type* __cache, size_t __refs = 0)
1674       : facet(__refs), _M_data(__cache)
1675       { _M_initialize_numpunct(); }
1676
1677       /**
1678        *  @brief  Internal constructor.  Not for general use.
1679        *
1680        *  This is a constructor for use by the library itself to set up new
1681        *  locales.
1682        *
1683        *  @param  cloc  The C locale.
1684        *  @param  refs  Refcount to pass to the base class.
1685        */
1686       explicit
1687       numpunct(__c_locale __cloc, size_t __refs = 0)
1688       : facet(__refs), _M_data(NULL)
1689       { _M_initialize_numpunct(__cloc); }
1690
1691       /**
1692        *  @brief  Return decimal point character.
1693        *
1694        *  This function returns a char_type to use as a decimal point.  It
1695        *  does so by returning returning
1696        *  numpunct<char_type>::do_decimal_point().
1697        *
1698        *  @return  @a char_type representing a decimal point.
1699       */
1700       char_type
1701       decimal_point() const
1702       { return this->do_decimal_point(); }
1703
1704       /**
1705        *  @brief  Return thousands separator character.
1706        *
1707        *  This function returns a char_type to use as a thousands
1708        *  separator.  It does so by returning returning
1709        *  numpunct<char_type>::do_thousands_sep().
1710        *
1711        *  @return  char_type representing a thousands separator.
1712       */
1713       char_type
1714       thousands_sep() const
1715       { return this->do_thousands_sep(); }
1716
1717       /**
1718        *  @brief  Return grouping specification.
1719        *
1720        *  This function returns a string representing groupings for the
1721        *  integer part of a number.  Groupings indicate where thousands
1722        *  separators should be inserted in the integer part of a number.
1723        *
1724        *  Each char in the return string is interpret as an integer
1725        *  rather than a character.  These numbers represent the number
1726        *  of digits in a group.  The first char in the string
1727        *  represents the number of digits in the least significant
1728        *  group.  If a char is negative, it indicates an unlimited
1729        *  number of digits for the group.  If more chars from the
1730        *  string are required to group a number, the last char is used
1731        *  repeatedly.
1732        *
1733        *  For example, if the grouping() returns "\003\002" and is
1734        *  applied to the number 123456789, this corresponds to
1735        *  12,34,56,789.  Note that if the string was "32", this would
1736        *  put more than 50 digits into the least significant group if
1737        *  the character set is ASCII.
1738        *
1739        *  The string is returned by calling
1740        *  numpunct<char_type>::do_grouping().
1741        *
1742        *  @return  string representing grouping specification.
1743       */
1744       string
1745       grouping() const
1746       { return this->do_grouping(); }
1747
1748       /**
1749        *  @brief  Return string representation of bool true.
1750        *
1751        *  This function returns a string_type containing the text
1752        *  representation for true bool variables.  It does so by calling
1753        *  numpunct<char_type>::do_truename().
1754        *
1755        *  @return  string_type representing printed form of true.
1756       */
1757       string_type
1758       truename() const
1759       { return this->do_truename(); }
1760
1761       /**
1762        *  @brief  Return string representation of bool false.
1763        *
1764        *  This function returns a string_type containing the text
1765        *  representation for false bool variables.  It does so by calling
1766        *  numpunct<char_type>::do_falsename().
1767        *
1768        *  @return  string_type representing printed form of false.
1769       */
1770       string_type
1771       falsename() const
1772       { return this->do_falsename(); }
1773
1774     protected:
1775       /// Destructor.
1776       virtual
1777       ~numpunct();
1778
1779       /**
1780        *  @brief  Return decimal point character.
1781        *
1782        *  Returns a char_type to use as a decimal point.  This function is a
1783        *  hook for derived classes to change the value returned.
1784        *
1785        *  @return  @a char_type representing a decimal point.
1786       */
1787       virtual char_type
1788       do_decimal_point() const
1789       { return _M_data->_M_decimal_point; }
1790
1791       /**
1792        *  @brief  Return thousands separator character.
1793        *
1794        *  Returns a char_type to use as a thousands separator.  This function
1795        *  is a hook for derived classes to change the value returned.
1796        *
1797        *  @return  @a char_type representing a thousands separator.
1798       */
1799       virtual char_type
1800       do_thousands_sep() const
1801       { return _M_data->_M_thousands_sep; }
1802
1803       /**
1804        *  @brief  Return grouping specification.
1805        *
1806        *  Returns a string representing groupings for the integer part of a
1807        *  number.  This function is a hook for derived classes to change the
1808        *  value returned.  @see grouping() for details.
1809        *
1810        *  @return  String representing grouping specification.
1811       */
1812       virtual string
1813       do_grouping() const
1814       { return _M_data->_M_grouping; }
1815
1816       /**
1817        *  @brief  Return string representation of bool true.
1818        *
1819        *  Returns a string_type containing the text representation for true
1820        *  bool variables.  This function is a hook for derived classes to
1821        *  change the value returned.
1822        *
1823        *  @return  string_type representing printed form of true.
1824       */
1825       virtual string_type
1826       do_truename() const
1827       { return _M_data->_M_truename; }
1828
1829       /**
1830        *  @brief  Return string representation of bool false.
1831        *
1832        *  Returns a string_type containing the text representation for false
1833        *  bool variables.  This function is a hook for derived classes to
1834        *  change the value returned.
1835        *
1836        *  @return  string_type representing printed form of false.
1837       */
1838       virtual string_type
1839       do_falsename() const
1840       { return _M_data->_M_falsename; }
1841
1842       // For use at construction time only.
1843       void
1844       _M_initialize_numpunct(__c_locale __cloc = NULL);
1845     };
1846
1847   template<typename _CharT>
1848     locale::id numpunct<_CharT>::id;
1849
1850   template<>
1851     numpunct<char>::~numpunct();
1852
1853   template<>
1854     void
1855     numpunct<char>::_M_initialize_numpunct(__c_locale __cloc);
1856
1857 #ifdef _GLIBCXX_USE_WCHAR_T
1858   template<>
1859     numpunct<wchar_t>::~numpunct();
1860
1861   template<>
1862     void
1863     numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc);
1864 #endif
1865
1866   /// class numpunct_byname [22.2.3.2].
1867   template<typename _CharT>
1868     class numpunct_byname : public numpunct<_CharT>
1869     {
1870     public:
1871       typedef _CharT                    char_type;
1872       typedef basic_string<_CharT>      string_type;
1873
1874       explicit
1875       numpunct_byname(const char* __s, size_t __refs = 0)
1876       : numpunct<_CharT>(__refs)
1877       {
1878         if (__builtin_strcmp(__s, "C") != 0
1879             && __builtin_strcmp(__s, "POSIX") != 0)
1880           {
1881             __c_locale __tmp;
1882             this->_S_create_c_locale(__tmp, __s);
1883             this->_M_initialize_numpunct(__tmp);
1884             this->_S_destroy_c_locale(__tmp);
1885           }
1886       }
1887
1888     protected:
1889       virtual
1890       ~numpunct_byname() { }
1891     };
1892
1893 _GLIBCXX_BEGIN_LDBL_NAMESPACE
1894
1895   /**
1896    *  @brief  Primary class template num_get.
1897    *  @ingroup locales
1898    *
1899    *  This facet encapsulates the code to parse and return a number
1900    *  from a string.  It is used by the istream numeric extraction
1901    *  operators.
1902    *
1903    *  The num_get template uses protected virtual functions to provide the
1904    *  actual results.  The public accessors forward the call to the virtual
1905    *  functions.  These virtual functions are hooks for developers to
1906    *  implement the behavior they require from the num_get facet.
1907   */
1908   template<typename _CharT, typename _InIter>
1909     class num_get : public locale::facet
1910     {
1911     public:
1912       // Types:
1913       //@{
1914       /// Public typedefs
1915       typedef _CharT                    char_type;
1916       typedef _InIter                   iter_type;
1917       //@}
1918
1919       /// Numpunct facet id.
1920       static locale::id                 id;
1921
1922       /**
1923        *  @brief  Constructor performs initialization.
1924        *
1925        *  This is the constructor provided by the standard.
1926        *
1927        *  @param refs  Passed to the base facet class.
1928       */
1929       explicit
1930       num_get(size_t __refs = 0) : facet(__refs) { }
1931
1932       /**
1933        *  @brief  Numeric parsing.
1934        *
1935        *  Parses the input stream into the bool @a v.  It does so by calling
1936        *  num_get::do_get().
1937        *
1938        *  If ios_base::boolalpha is set, attempts to read
1939        *  ctype<CharT>::truename() or ctype<CharT>::falsename().  Sets
1940        *  @a v to true or false if successful.  Sets err to
1941        *  ios_base::failbit if reading the string fails.  Sets err to
1942        *  ios_base::eofbit if the stream is emptied.
1943        *
1944        *  If ios_base::boolalpha is not set, proceeds as with reading a long,
1945        *  except if the value is 1, sets @a v to true, if the value is 0, sets
1946        *  @a v to false, and otherwise set err to ios_base::failbit.
1947        *
1948        *  @param  in  Start of input stream.
1949        *  @param  end  End of input stream.
1950        *  @param  io  Source of locale and flags.
1951        *  @param  err  Error flags to set.
1952        *  @param  v  Value to format and insert.
1953        *  @return  Iterator after reading.
1954       */
1955       iter_type
1956       get(iter_type __in, iter_type __end, ios_base& __io,
1957           ios_base::iostate& __err, bool& __v) const
1958       { return this->do_get(__in, __end, __io, __err, __v); }
1959
1960       //@{
1961       /**
1962        *  @brief  Numeric parsing.
1963        *
1964        *  Parses the input stream into the integral variable @a v.  It does so
1965        *  by calling num_get::do_get().
1966        *
1967        *  Parsing is affected by the flag settings in @a io.
1968        *
1969        *  The basic parse is affected by the value of io.flags() &
1970        *  ios_base::basefield.  If equal to ios_base::oct, parses like the
1971        *  scanf %o specifier.  Else if equal to ios_base::hex, parses like %X
1972        *  specifier.  Else if basefield equal to 0, parses like the %i
1973        *  specifier.  Otherwise, parses like %d for signed and %u for unsigned
1974        *  types.  The matching type length modifier is also used.
1975        *
1976        *  Digit grouping is interpreted according to numpunct::grouping() and
1977        *  numpunct::thousands_sep().  If the pattern of digit groups isn't
1978        *  consistent, sets err to ios_base::failbit.
1979        *
1980        *  If parsing the string yields a valid value for @a v, @a v is set.
1981        *  Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
1982        *  Sets err to ios_base::eofbit if the stream is emptied.
1983        *
1984        *  @param  in  Start of input stream.
1985        *  @param  end  End of input stream.
1986        *  @param  io  Source of locale and flags.
1987        *  @param  err  Error flags to set.
1988        *  @param  v  Value to format and insert.
1989        *  @return  Iterator after reading.
1990       */
1991       iter_type
1992       get(iter_type __in, iter_type __end, ios_base& __io,
1993           ios_base::iostate& __err, long& __v) const
1994       { return this->do_get(__in, __end, __io, __err, __v); }
1995
1996       iter_type
1997       get(iter_type __in, iter_type __end, ios_base& __io,
1998           ios_base::iostate& __err, unsigned short& __v) const
1999       { return this->do_get(__in, __end, __io, __err, __v); }
2000
2001       iter_type
2002       get(iter_type __in, iter_type __end, ios_base& __io,
2003           ios_base::iostate& __err, unsigned int& __v)   const
2004       { return this->do_get(__in, __end, __io, __err, __v); }
2005
2006       iter_type
2007       get(iter_type __in, iter_type __end, ios_base& __io,
2008           ios_base::iostate& __err, unsigned long& __v)  const
2009       { return this->do_get(__in, __end, __io, __err, __v); }
2010
2011 #ifdef _GLIBCXX_USE_LONG_LONG
2012       iter_type
2013       get(iter_type __in, iter_type __end, ios_base& __io,
2014           ios_base::iostate& __err, long long& __v) const
2015       { return this->do_get(__in, __end, __io, __err, __v); }
2016
2017       iter_type
2018       get(iter_type __in, iter_type __end, ios_base& __io,
2019           ios_base::iostate& __err, unsigned long long& __v)  const
2020       { return this->do_get(__in, __end, __io, __err, __v); }
2021 #endif
2022       //@}
2023
2024       //@{
2025       /**
2026        *  @brief  Numeric parsing.
2027        *
2028        *  Parses the input stream into the integral variable @a v.  It does so
2029        *  by calling num_get::do_get().
2030        *
2031        *  The input characters are parsed like the scanf %g specifier.  The
2032        *  matching type length modifier is also used.
2033        *
2034        *  The decimal point character used is numpunct::decimal_point().
2035        *  Digit grouping is interpreted according to numpunct::grouping() and
2036        *  numpunct::thousands_sep().  If the pattern of digit groups isn't
2037        *  consistent, sets err to ios_base::failbit.
2038        *
2039        *  If parsing the string yields a valid value for @a v, @a v is set.
2040        *  Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2041        *  Sets err to ios_base::eofbit if the stream is emptied.
2042        *
2043        *  @param  in  Start of input stream.
2044        *  @param  end  End of input stream.
2045        *  @param  io  Source of locale and flags.
2046        *  @param  err  Error flags to set.
2047        *  @param  v  Value to format and insert.
2048        *  @return  Iterator after reading.
2049       */
2050       iter_type
2051       get(iter_type __in, iter_type __end, ios_base& __io,
2052           ios_base::iostate& __err, float& __v) const
2053       { return this->do_get(__in, __end, __io, __err, __v); }
2054
2055       iter_type
2056       get(iter_type __in, iter_type __end, ios_base& __io,
2057           ios_base::iostate& __err, double& __v) const
2058       { return this->do_get(__in, __end, __io, __err, __v); }
2059
2060       iter_type
2061       get(iter_type __in, iter_type __end, ios_base& __io,
2062           ios_base::iostate& __err, long double& __v) const
2063       { return this->do_get(__in, __end, __io, __err, __v); }
2064       //@}
2065
2066       /**
2067        *  @brief  Numeric parsing.
2068        *
2069        *  Parses the input stream into the pointer variable @a v.  It does so
2070        *  by calling num_get::do_get().
2071        *
2072        *  The input characters are parsed like the scanf %p specifier.
2073        *
2074        *  Digit grouping is interpreted according to numpunct::grouping() and
2075        *  numpunct::thousands_sep().  If the pattern of digit groups isn't
2076        *  consistent, sets err to ios_base::failbit.
2077        *
2078        *  Note that the digit grouping effect for pointers is a bit ambiguous
2079        *  in the standard and shouldn't be relied on.  See DR 344.
2080        *
2081        *  If parsing the string yields a valid value for @a v, @a v is set.
2082        *  Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2083        *  Sets err to ios_base::eofbit if the stream is emptied.
2084        *
2085        *  @param  in  Start of input stream.
2086        *  @param  end  End of input stream.
2087        *  @param  io  Source of locale and flags.
2088        *  @param  err  Error flags to set.
2089        *  @param  v  Value to format and insert.
2090        *  @return  Iterator after reading.
2091       */
2092       iter_type
2093       get(iter_type __in, iter_type __end, ios_base& __io,
2094           ios_base::iostate& __err, void*& __v) const
2095       { return this->do_get(__in, __end, __io, __err, __v); }
2096
2097     protected:
2098       /// Destructor.
2099       virtual ~num_get() { }
2100
2101       iter_type
2102       _M_extract_float(iter_type, iter_type, ios_base&, ios_base::iostate&,
2103                        string&) const;
2104
2105       template<typename _ValueT>
2106         iter_type
2107         _M_extract_int(iter_type, iter_type, ios_base&, ios_base::iostate&,
2108                        _ValueT&) const;
2109
2110       template<typename _CharT2>
2111       typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, int>::__type
2112         _M_find(const _CharT2*, size_t __len, _CharT2 __c) const
2113         {
2114           int __ret = -1;
2115           if (__len <= 10)
2116             {
2117               if (__c >= _CharT2('0') && __c < _CharT2(_CharT2('0') + __len))
2118                 __ret = __c - _CharT2('0');
2119             }
2120           else
2121             {
2122               if (__c >= _CharT2('0') && __c <= _CharT2('9'))
2123                 __ret = __c - _CharT2('0');
2124               else if (__c >= _CharT2('a') && __c <= _CharT2('f'))
2125                 __ret = 10 + (__c - _CharT2('a'));
2126               else if (__c >= _CharT2('A') && __c <= _CharT2('F'))
2127                 __ret = 10 + (__c - _CharT2('A'));
2128             }
2129           return __ret;
2130         }
2131
2132       template<typename _CharT2>
2133       typename __gnu_cxx::__enable_if<!__is_char<_CharT2>::__value, 
2134                                       int>::__type
2135         _M_find(const _CharT2* __zero, size_t __len, _CharT2 __c) const
2136         {
2137           int __ret = -1;
2138           const char_type* __q = char_traits<_CharT2>::find(__zero, __len, __c);
2139           if (__q)
2140             {
2141               __ret = __q - __zero;
2142               if (__ret > 15)
2143                 __ret -= 6;
2144             }
2145           return __ret;
2146         }
2147
2148       //@{
2149       /**
2150        *  @brief  Numeric parsing.
2151        *
2152        *  Parses the input stream into the variable @a v.  This function is a
2153        *  hook for derived classes to change the value returned.  @see get()
2154        *  for more details.
2155        *
2156        *  @param  in  Start of input stream.
2157        *  @param  end  End of input stream.
2158        *  @param  io  Source of locale and flags.
2159        *  @param  err  Error flags to set.
2160        *  @param  v  Value to format and insert.
2161        *  @return  Iterator after reading.
2162       */
2163       virtual iter_type
2164       do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const;
2165
2166       virtual iter_type
2167       do_get(iter_type __beg, iter_type __end, ios_base& __io,
2168              ios_base::iostate& __err, long& __v) const
2169       { return _M_extract_int(__beg, __end, __io, __err, __v); }
2170
2171       virtual iter_type
2172       do_get(iter_type __beg, iter_type __end, ios_base& __io,
2173              ios_base::iostate& __err, unsigned short& __v) const
2174       { return _M_extract_int(__beg, __end, __io, __err, __v); }
2175
2176       virtual iter_type
2177       do_get(iter_type __beg, iter_type __end, ios_base& __io,
2178              ios_base::iostate& __err, unsigned int& __v) const
2179       { return _M_extract_int(__beg, __end, __io, __err, __v); }
2180
2181       virtual iter_type
2182       do_get(iter_type __beg, iter_type __end, ios_base& __io,
2183              ios_base::iostate& __err, unsigned long& __v) const
2184       { return _M_extract_int(__beg, __end, __io, __err, __v); }
2185
2186 #ifdef _GLIBCXX_USE_LONG_LONG
2187       virtual iter_type
2188       do_get(iter_type __beg, iter_type __end, ios_base& __io,
2189              ios_base::iostate& __err, long long& __v) const
2190       { return _M_extract_int(__beg, __end, __io, __err, __v); }        
2191
2192       virtual iter_type
2193       do_get(iter_type __beg, iter_type __end, ios_base& __io,
2194              ios_base::iostate& __err, unsigned long long& __v) const
2195       { return _M_extract_int(__beg, __end, __io, __err, __v); }
2196 #endif
2197
2198       virtual iter_type
2199       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2200              float&) const;
2201
2202       virtual iter_type
2203       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2204              double&) const;
2205
2206       // XXX GLIBCXX_ABI Deprecated
2207 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2208       virtual iter_type
2209       __do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2210                double&) const;
2211 #else
2212       virtual iter_type
2213       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2214              long double&) const;
2215 #endif
2216
2217       virtual iter_type
2218       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2219              void*&) const;
2220
2221       // XXX GLIBCXX_ABI Deprecated
2222 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2223       virtual iter_type
2224       do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2225              long double&) const;
2226 #endif
2227       //@}
2228     };
2229
2230   template<typename _CharT, typename _InIter>
2231     locale::id num_get<_CharT, _InIter>::id;
2232
2233
2234   /**
2235    *  @brief  Primary class template num_put.
2236    *  @ingroup locales
2237    *
2238    *  This facet encapsulates the code to convert a number to a string.  It is
2239    *  used by the ostream numeric insertion operators.
2240    *
2241    *  The num_put template uses protected virtual functions to provide the
2242    *  actual results.  The public accessors forward the call to the virtual
2243    *  functions.  These virtual functions are hooks for developers to
2244    *  implement the behavior they require from the num_put facet.
2245   */
2246   template<typename _CharT, typename _OutIter>
2247     class num_put : public locale::facet
2248     {
2249     public:
2250       // Types:
2251       //@{
2252       /// Public typedefs
2253       typedef _CharT            char_type;
2254       typedef _OutIter          iter_type;
2255       //@}
2256
2257       /// Numpunct facet id.
2258       static locale::id         id;
2259
2260       /**
2261        *  @brief  Constructor performs initialization.
2262        *
2263        *  This is the constructor provided by the standard.
2264        *
2265        *  @param refs  Passed to the base facet class.
2266       */
2267       explicit
2268       num_put(size_t __refs = 0) : facet(__refs) { }
2269
2270       /**
2271        *  @brief  Numeric formatting.
2272        *
2273        *  Formats the boolean @a v and inserts it into a stream.  It does so
2274        *  by calling num_put::do_put().
2275        *
2276        *  If ios_base::boolalpha is set, writes ctype<CharT>::truename() or
2277        *  ctype<CharT>::falsename().  Otherwise formats @a v as an int.
2278        *
2279        *  @param  s  Stream to write to.
2280        *  @param  io  Source of locale and flags.
2281        *  @param  fill  Char_type to use for filling.
2282        *  @param  v  Value to format and insert.
2283        *  @return  Iterator after writing.
2284       */
2285       iter_type
2286       put(iter_type __s, ios_base& __f, char_type __fill, bool __v) const
2287       { return this->do_put(__s, __f, __fill, __v); }
2288
2289       //@{
2290       /**
2291        *  @brief  Numeric formatting.
2292        *
2293        *  Formats the integral value @a v and inserts it into a
2294        *  stream.  It does so by calling num_put::do_put().
2295        *
2296        *  Formatting is affected by the flag settings in @a io.
2297        *
2298        *  The basic format is affected by the value of io.flags() &
2299        *  ios_base::basefield.  If equal to ios_base::oct, formats like the
2300        *  printf %o specifier.  Else if equal to ios_base::hex, formats like
2301        *  %x or %X with ios_base::uppercase unset or set respectively.
2302        *  Otherwise, formats like %d, %ld, %lld for signed and %u, %lu, %llu
2303        *  for unsigned values.  Note that if both oct and hex are set, neither
2304        *  will take effect.
2305        *
2306        *  If ios_base::showpos is set, '+' is output before positive values.
2307        *  If ios_base::showbase is set, '0' precedes octal values (except 0)
2308        *  and '0[xX]' precedes hex values.
2309        *
2310        *  Thousands separators are inserted according to numpunct::grouping()
2311        *  and numpunct::thousands_sep().  The decimal point character used is
2312        *  numpunct::decimal_point().
2313        *
2314        *  If io.width() is non-zero, enough @a fill characters are inserted to
2315        *  make the result at least that wide.  If
2316        *  (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2317        *  padded at the end.  If ios_base::internal, then padding occurs
2318        *  immediately after either a '+' or '-' or after '0x' or '0X'.
2319        *  Otherwise, padding occurs at the beginning.
2320        *
2321        *  @param  s  Stream to write to.
2322        *  @param  io  Source of locale and flags.
2323        *  @param  fill  Char_type to use for filling.
2324        *  @param  v  Value to format and insert.
2325        *  @return  Iterator after writing.
2326       */
2327       iter_type
2328       put(iter_type __s, ios_base& __f, char_type __fill, long __v) const
2329       { return this->do_put(__s, __f, __fill, __v); }
2330
2331       iter_type
2332       put(iter_type __s, ios_base& __f, char_type __fill,
2333           unsigned long __v) const
2334       { return this->do_put(__s, __f, __fill, __v); }
2335
2336 #ifdef _GLIBCXX_USE_LONG_LONG
2337       iter_type
2338       put(iter_type __s, ios_base& __f, char_type __fill, long long __v) const
2339       { return this->do_put(__s, __f, __fill, __v); }
2340
2341       iter_type
2342       put(iter_type __s, ios_base& __f, char_type __fill,
2343           unsigned long long __v) const
2344       { return this->do_put(__s, __f, __fill, __v); }
2345 #endif
2346       //@}
2347
2348       //@{
2349       /**
2350        *  @brief  Numeric formatting.
2351        *
2352        *  Formats the floating point value @a v and inserts it into a stream.
2353        *  It does so by calling num_put::do_put().
2354        *
2355        *  Formatting is affected by the flag settings in @a io.
2356        *
2357        *  The basic format is affected by the value of io.flags() &
2358        *  ios_base::floatfield.  If equal to ios_base::fixed, formats like the
2359        *  printf %f specifier.  Else if equal to ios_base::scientific, formats
2360        *  like %e or %E with ios_base::uppercase unset or set respectively.
2361        *  Otherwise, formats like %g or %G depending on uppercase.  Note that
2362        *  if both fixed and scientific are set, the effect will also be like
2363        *  %g or %G.
2364        *
2365        *  The output precision is given by io.precision().  This precision is
2366        *  capped at numeric_limits::digits10 + 2 (different for double and
2367        *  long double).  The default precision is 6.
2368        *
2369        *  If ios_base::showpos is set, '+' is output before positive values.
2370        *  If ios_base::showpoint is set, a decimal point will always be
2371        *  output.
2372        *
2373        *  Thousands separators are inserted according to numpunct::grouping()
2374        *  and numpunct::thousands_sep().  The decimal point character used is
2375        *  numpunct::decimal_point().
2376        *
2377        *  If io.width() is non-zero, enough @a fill characters are inserted to
2378        *  make the result at least that wide.  If
2379        *  (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2380        *  padded at the end.  If ios_base::internal, then padding occurs
2381        *  immediately after either a '+' or '-' or after '0x' or '0X'.
2382        *  Otherwise, padding occurs at the beginning.
2383        *
2384        *  @param  s  Stream to write to.
2385        *  @param  io  Source of locale and flags.
2386        *  @param  fill  Char_type to use for filling.
2387        *  @param  v  Value to format and insert.
2388        *  @return  Iterator after writing.
2389       */
2390       iter_type
2391       put(iter_type __s, ios_base& __f, char_type __fill, double __v) const
2392       { return this->do_put(__s, __f, __fill, __v); }
2393
2394       iter_type
2395       put(iter_type __s, ios_base& __f, char_type __fill,
2396           long double __v) const
2397       { return this->do_put(__s, __f, __fill, __v); }
2398       //@}
2399
2400       /**
2401        *  @brief  Numeric formatting.
2402        *
2403        *  Formats the pointer value @a v and inserts it into a stream.  It
2404        *  does so by calling num_put::do_put().
2405        *
2406        *  This function formats @a v as an unsigned long with ios_base::hex
2407        *  and ios_base::showbase set.
2408        *
2409        *  @param  s  Stream to write to.
2410        *  @param  io  Source of locale and flags.
2411        *  @param  fill  Char_type to use for filling.
2412        *  @param  v  Value to format and insert.
2413        *  @return  Iterator after writing.
2414       */
2415       iter_type
2416       put(iter_type __s, ios_base& __f, char_type __fill,
2417           const void* __v) const
2418       { return this->do_put(__s, __f, __fill, __v); }
2419
2420     protected:
2421       template<typename _ValueT>
2422         iter_type
2423         _M_insert_float(iter_type, ios_base& __io, char_type __fill,
2424                         char __mod, _ValueT __v) const;
2425
2426       void
2427       _M_group_float(const char* __grouping, size_t __grouping_size,
2428                      char_type __sep, const char_type* __p, char_type* __new,
2429                      char_type* __cs, int& __len) const;
2430
2431       template<typename _ValueT>
2432         iter_type
2433         _M_insert_int(iter_type, ios_base& __io, char_type __fill,
2434                       _ValueT __v) const;
2435
2436       void
2437       _M_group_int(const char* __grouping, size_t __grouping_size,
2438                    char_type __sep, ios_base& __io, char_type* __new,
2439                    char_type* __cs, int& __len) const;
2440
2441       void
2442       _M_pad(char_type __fill, streamsize __w, ios_base& __io,
2443              char_type* __new, const char_type* __cs, int& __len) const;
2444
2445       /// Destructor.
2446       virtual
2447       ~num_put() { };
2448
2449       //@{
2450       /**
2451        *  @brief  Numeric formatting.
2452        *
2453        *  These functions do the work of formatting numeric values and
2454        *  inserting them into a stream. This function is a hook for derived
2455        *  classes to change the value returned.
2456        *
2457        *  @param  s  Stream to write to.
2458        *  @param  io  Source of locale and flags.
2459        *  @param  fill  Char_type to use for filling.
2460        *  @param  v  Value to format and insert.
2461        *  @return  Iterator after writing.
2462       */
2463       virtual iter_type
2464       do_put(iter_type, ios_base&, char_type __fill, bool __v) const;
2465
2466       virtual iter_type
2467       do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
2468       { return _M_insert_int(__s, __io, __fill, __v); } 
2469
2470       virtual iter_type
2471       do_put(iter_type __s, ios_base& __io, char_type __fill,
2472              unsigned long __v) const
2473       { return _M_insert_int(__s, __io, __fill, __v); }
2474
2475 #ifdef _GLIBCXX_USE_LONG_LONG
2476       virtual iter_type
2477       do_put(iter_type __s, ios_base& __io, char_type __fill,
2478              long long __v) const
2479       { return _M_insert_int(__s, __io, __fill, __v); }
2480
2481       virtual iter_type
2482       do_put(iter_type __s, ios_base& __io, char_type __fill,
2483              unsigned long long __v) const
2484       { return _M_insert_int(__s, __io, __fill, __v); }
2485 #endif
2486
2487       virtual iter_type
2488       do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2489
2490       // XXX GLIBCXX_ABI Deprecated
2491 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2492       virtual iter_type
2493       __do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2494 #else
2495       virtual iter_type
2496       do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2497 #endif
2498
2499       virtual iter_type
2500       do_put(iter_type, ios_base&, char_type __fill, const void* __v) const;
2501
2502       // XXX GLIBCXX_ABI Deprecated
2503 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2504       virtual iter_type
2505       do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2506 #endif
2507       //@}
2508     };
2509
2510   template <typename _CharT, typename _OutIter>
2511     locale::id num_put<_CharT, _OutIter>::id;
2512
2513 _GLIBCXX_END_LDBL_NAMESPACE
2514
2515   // Subclause convenience interfaces, inlines.
2516   // NB: These are inline because, when used in a loop, some compilers
2517   // can hoist the body out of the loop; then it's just as fast as the
2518   // C is*() function.
2519
2520   /// Convenience interface to ctype.is(ctype_base::space, __c).
2521   template<typename _CharT>
2522     inline bool
2523     isspace(_CharT __c, const locale& __loc)
2524     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); }
2525
2526   /// Convenience interface to ctype.is(ctype_base::print, __c).
2527   template<typename _CharT>
2528     inline bool
2529     isprint(_CharT __c, const locale& __loc)
2530     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); }
2531
2532   /// Convenience interface to ctype.is(ctype_base::cntrl, __c).
2533   template<typename _CharT>
2534     inline bool
2535     iscntrl(_CharT __c, const locale& __loc)
2536     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); }
2537
2538   /// Convenience interface to ctype.is(ctype_base::upper, __c).
2539   template<typename _CharT>
2540     inline bool
2541     isupper(_CharT __c, const locale& __loc)
2542     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); }
2543
2544   /// Convenience interface to ctype.is(ctype_base::lower, __c).
2545   template<typename _CharT>
2546     inline bool 
2547     islower(_CharT __c, const locale& __loc)
2548     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); }
2549
2550   /// Convenience interface to ctype.is(ctype_base::alpha, __c).
2551   template<typename _CharT>
2552     inline bool
2553     isalpha(_CharT __c, const locale& __loc)
2554     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); }
2555
2556   /// Convenience interface to ctype.is(ctype_base::digit, __c).
2557   template<typename _CharT>
2558     inline bool
2559     isdigit(_CharT __c, const locale& __loc)
2560     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); }
2561
2562   /// Convenience interface to ctype.is(ctype_base::punct, __c).
2563   template<typename _CharT>
2564     inline bool
2565     ispunct(_CharT __c, const locale& __loc)
2566     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); }
2567
2568   /// Convenience interface to ctype.is(ctype_base::xdigit, __c).
2569   template<typename _CharT>
2570     inline bool
2571     isxdigit(_CharT __c, const locale& __loc)
2572     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); }
2573
2574   /// Convenience interface to ctype.is(ctype_base::alnum, __c).
2575   template<typename _CharT>
2576     inline bool
2577     isalnum(_CharT __c, const locale& __loc)
2578     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); }
2579
2580   /// Convenience interface to ctype.is(ctype_base::graph, __c).
2581   template<typename _CharT>
2582     inline bool
2583     isgraph(_CharT __c, const locale& __loc)
2584     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); }
2585
2586   /// Convenience interface to ctype.toupper(__c).
2587   template<typename _CharT>
2588     inline _CharT
2589     toupper(_CharT __c, const locale& __loc)
2590     { return use_facet<ctype<_CharT> >(__loc).toupper(__c); }
2591
2592   /// Convenience interface to ctype.tolower(__c).
2593   template<typename _CharT>
2594     inline _CharT
2595     tolower(_CharT __c, const locale& __loc)
2596     { return use_facet<ctype<_CharT> >(__loc).tolower(__c); }
2597
2598 _GLIBCXX_END_NAMESPACE
2599
2600 #ifndef _GLIBCXX_EXPORT_TEMPLATE
2601 # include <bits/locale_facets.tcc>
2602 #endif
2603
2604 #endif