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