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