libstdc++
locale_facets.h
Go to the documentation of this file.
00001 // Locale support -*- C++ -*-
00002 
00003 // Copyright (C) 1997-2013 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/locale_facets.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{locale}
00028  */
00029 
00030 //
00031 // ISO C++ 14882: 22.1  Locales
00032 //
00033 
00034 #ifndef _LOCALE_FACETS_H
00035 #define _LOCALE_FACETS_H 1
00036 
00037 #pragma GCC system_header
00038 
00039 #include <cwctype>  // For wctype_t
00040 #include <cctype>
00041 #include <bits/ctype_base.h>
00042 #include <iosfwd>
00043 #include <bits/ios_base.h>  // For ios_base, ios_base::iostate
00044 #include <streambuf>
00045 #include <bits/cpp_type_traits.h>
00046 #include <ext/type_traits.h>
00047 #include <ext/numeric_traits.h>
00048 #include <bits/streambuf_iterator.h>
00049 
00050 namespace std _GLIBCXX_VISIBILITY(default)
00051 {
00052 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00053 
00054   // NB: Don't instantiate required wchar_t facets if no wchar_t support.
00055 #ifdef _GLIBCXX_USE_WCHAR_T
00056 # define  _GLIBCXX_NUM_FACETS 28
00057 #else
00058 # define  _GLIBCXX_NUM_FACETS 14
00059 #endif
00060 
00061   // Convert string to numeric value of type _Tp and store results.
00062   // NB: This is specialized for all required types, there is no
00063   // generic definition.
00064   template<typename _Tp>
00065     void
00066     __convert_to_v(const char*, _Tp&, ios_base::iostate&,
00067            const __c_locale&) throw();
00068 
00069   // Explicit specializations for required types.
00070   template<>
00071     void
00072     __convert_to_v(const char*, float&, ios_base::iostate&,
00073            const __c_locale&) throw();
00074 
00075   template<>
00076     void
00077     __convert_to_v(const char*, double&, ios_base::iostate&,
00078            const __c_locale&) throw();
00079 
00080   template<>
00081     void
00082     __convert_to_v(const char*, long double&, ios_base::iostate&,
00083            const __c_locale&) throw();
00084 
00085   // NB: __pad is a struct, rather than a function, so it can be
00086   // partially-specialized.
00087   template<typename _CharT, typename _Traits>
00088     struct __pad
00089     {
00090       static void
00091       _S_pad(ios_base& __io, _CharT __fill, _CharT* __news,
00092          const _CharT* __olds, streamsize __newlen, streamsize __oldlen);
00093     };
00094 
00095   // Used by both numeric and monetary facets.
00096   // Inserts "group separator" characters into an array of characters.
00097   // It's recursive, one iteration per group.  It moves the characters
00098   // in the buffer this way: "xxxx12345" -> "12,345xxx".  Call this
00099   // only with __gsize != 0.
00100   template<typename _CharT>
00101     _CharT*
00102     __add_grouping(_CharT* __s, _CharT __sep,
00103            const char* __gbeg, size_t __gsize,
00104            const _CharT* __first, const _CharT* __last);
00105 
00106   // This template permits specializing facet output code for
00107   // ostreambuf_iterator.  For ostreambuf_iterator, sputn is
00108   // significantly more efficient than incrementing iterators.
00109   template<typename _CharT>
00110     inline
00111     ostreambuf_iterator<_CharT>
00112     __write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len)
00113     {
00114       __s._M_put(__ws, __len);
00115       return __s;
00116     }
00117 
00118   // This is the unspecialized form of the template.
00119   template<typename _CharT, typename _OutIter>
00120     inline
00121     _OutIter
00122     __write(_OutIter __s, const _CharT* __ws, int __len)
00123     {
00124       for (int __j = 0; __j < __len; __j++, ++__s)
00125     *__s = __ws[__j];
00126       return __s;
00127     }
00128 
00129 
00130   // 22.2.1.1  Template class ctype
00131   // Include host and configuration specific ctype enums for ctype_base.
00132 
00133   /**
00134    *  @brief  Common base for ctype facet
00135    *
00136    *  This template class provides implementations of the public functions
00137    *  that forward to the protected virtual functions.
00138    *
00139    *  This template also provides abstract stubs for the protected virtual
00140    *  functions.
00141   */
00142   template<typename _CharT>
00143     class __ctype_abstract_base : public locale::facet, public ctype_base
00144     {
00145     public:
00146       // Types:
00147       /// Typedef for the template parameter
00148       typedef _CharT char_type;
00149 
00150       /**
00151        *  @brief  Test char_type classification.
00152        *
00153        *  This function finds a mask M for @a __c and compares it to
00154        *  mask @a __m.  It does so by returning the value of
00155        *  ctype<char_type>::do_is().
00156        *
00157        *  @param __c  The char_type to compare the mask of.
00158        *  @param __m  The mask to compare against.
00159        *  @return  (M & __m) != 0.
00160       */
00161       bool
00162       is(mask __m, char_type __c) const
00163       { return this->do_is(__m, __c); }
00164 
00165       /**
00166        *  @brief  Return a mask array.
00167        *
00168        *  This function finds the mask for each char_type in the range [lo,hi)
00169        *  and successively writes it to vec.  vec must have as many elements
00170        *  as the char array.  It does so by returning the value of
00171        *  ctype<char_type>::do_is().
00172        *
00173        *  @param __lo  Pointer to start of range.
00174        *  @param __hi  Pointer to end of range.
00175        *  @param __vec  Pointer to an array of mask storage.
00176        *  @return  @a __hi.
00177       */
00178       const char_type*
00179       is(const char_type *__lo, const char_type *__hi, mask *__vec) const
00180       { return this->do_is(__lo, __hi, __vec); }
00181 
00182       /**
00183        *  @brief  Find char_type matching a mask
00184        *
00185        *  This function searches for and returns the first char_type c in
00186        *  [lo,hi) for which is(m,c) is true.  It does so by returning
00187        *  ctype<char_type>::do_scan_is().
00188        *
00189        *  @param __m  The mask to compare against.
00190        *  @param __lo  Pointer to start of range.
00191        *  @param __hi  Pointer to end of range.
00192        *  @return  Pointer to matching char_type if found, else @a __hi.
00193       */
00194       const char_type*
00195       scan_is(mask __m, const char_type* __lo, const char_type* __hi) const
00196       { return this->do_scan_is(__m, __lo, __hi); }
00197 
00198       /**
00199        *  @brief  Find char_type not matching a mask
00200        *
00201        *  This function searches for and returns the first char_type c in
00202        *  [lo,hi) for which is(m,c) is false.  It does so by returning
00203        *  ctype<char_type>::do_scan_not().
00204        *
00205        *  @param __m  The mask to compare against.
00206        *  @param __lo  Pointer to first char in range.
00207        *  @param __hi  Pointer to end of range.
00208        *  @return  Pointer to non-matching char if found, else @a __hi.
00209       */
00210       const char_type*
00211       scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
00212       { return this->do_scan_not(__m, __lo, __hi); }
00213 
00214       /**
00215        *  @brief  Convert to uppercase.
00216        *
00217        *  This function converts the argument to uppercase if possible.
00218        *  If not possible (for example, '2'), returns the argument.  It does
00219        *  so by returning ctype<char_type>::do_toupper().
00220        *
00221        *  @param __c  The char_type to convert.
00222        *  @return  The uppercase char_type if convertible, else @a __c.
00223       */
00224       char_type
00225       toupper(char_type __c) const
00226       { return this->do_toupper(__c); }
00227 
00228       /**
00229        *  @brief  Convert array to uppercase.
00230        *
00231        *  This function converts each char_type in the range [lo,hi) to
00232        *  uppercase if possible.  Other elements remain untouched.  It does so
00233        *  by returning ctype<char_type>:: do_toupper(lo, hi).
00234        *
00235        *  @param __lo  Pointer to start of range.
00236        *  @param __hi  Pointer to end of range.
00237        *  @return  @a __hi.
00238       */
00239       const char_type*
00240       toupper(char_type *__lo, const char_type* __hi) const
00241       { return this->do_toupper(__lo, __hi); }
00242 
00243       /**
00244        *  @brief  Convert to lowercase.
00245        *
00246        *  This function converts the argument to lowercase if possible.  If
00247        *  not possible (for example, '2'), returns the argument.  It does so
00248        *  by returning ctype<char_type>::do_tolower(c).
00249        *
00250        *  @param __c  The char_type to convert.
00251        *  @return  The lowercase char_type if convertible, else @a __c.
00252       */
00253       char_type
00254       tolower(char_type __c) const
00255       { return this->do_tolower(__c); }
00256 
00257       /**
00258        *  @brief  Convert array to lowercase.
00259        *
00260        *  This function converts each char_type in the range [__lo,__hi) to
00261        *  lowercase if possible.  Other elements remain untouched.  It does so
00262        *  by returning ctype<char_type>:: do_tolower(__lo, __hi).
00263        *
00264        *  @param __lo  Pointer to start of range.
00265        *  @param __hi  Pointer to end of range.
00266        *  @return  @a __hi.
00267       */
00268       const char_type*
00269       tolower(char_type* __lo, const char_type* __hi) const
00270       { return this->do_tolower(__lo, __hi); }
00271 
00272       /**
00273        *  @brief  Widen char to char_type
00274        *
00275        *  This function converts the char argument to char_type using the
00276        *  simplest reasonable transformation.  It does so by returning
00277        *  ctype<char_type>::do_widen(c).
00278        *
00279        *  Note: this is not what you want for codepage conversions.  See
00280        *  codecvt for that.
00281        *
00282        *  @param __c  The char to convert.
00283        *  @return  The converted char_type.
00284       */
00285       char_type
00286       widen(char __c) const
00287       { return this->do_widen(__c); }
00288 
00289       /**
00290        *  @brief  Widen array to char_type
00291        *
00292        *  This function converts each char in the input to char_type using the
00293        *  simplest reasonable transformation.  It does so by returning
00294        *  ctype<char_type>::do_widen(c).
00295        *
00296        *  Note: this is not what you want for codepage conversions.  See
00297        *  codecvt for that.
00298        *
00299        *  @param __lo  Pointer to start of range.
00300        *  @param __hi  Pointer to end of range.
00301        *  @param __to  Pointer to the destination array.
00302        *  @return  @a __hi.
00303       */
00304       const char*
00305       widen(const char* __lo, const char* __hi, char_type* __to) const
00306       { return this->do_widen(__lo, __hi, __to); }
00307 
00308       /**
00309        *  @brief  Narrow char_type to char
00310        *
00311        *  This function converts the char_type to char using the simplest
00312        *  reasonable transformation.  If the conversion fails, dfault is
00313        *  returned instead.  It does so by returning
00314        *  ctype<char_type>::do_narrow(__c).
00315        *
00316        *  Note: this is not what you want for codepage conversions.  See
00317        *  codecvt for that.
00318        *
00319        *  @param __c  The char_type to convert.
00320        *  @param __dfault  Char to return if conversion fails.
00321        *  @return  The converted char.
00322       */
00323       char
00324       narrow(char_type __c, char __dfault) const
00325       { return this->do_narrow(__c, __dfault); }
00326 
00327       /**
00328        *  @brief  Narrow array to char array
00329        *
00330        *  This function converts each char_type in the input to char using the
00331        *  simplest reasonable transformation and writes the results to the
00332        *  destination array.  For any char_type in the input that cannot be
00333        *  converted, @a dfault is used instead.  It does so by returning
00334        *  ctype<char_type>::do_narrow(__lo, __hi, __dfault, __to).
00335        *
00336        *  Note: this is not what you want for codepage conversions.  See
00337        *  codecvt for that.
00338        *
00339        *  @param __lo  Pointer to start of range.
00340        *  @param __hi  Pointer to end of range.
00341        *  @param __dfault  Char to use if conversion fails.
00342        *  @param __to  Pointer to the destination array.
00343        *  @return  @a __hi.
00344       */
00345       const char_type*
00346       narrow(const char_type* __lo, const char_type* __hi,
00347           char __dfault, char* __to) const
00348       { return this->do_narrow(__lo, __hi, __dfault, __to); }
00349 
00350     protected:
00351       explicit
00352       __ctype_abstract_base(size_t __refs = 0): facet(__refs) { }
00353 
00354       virtual
00355       ~__ctype_abstract_base() { }
00356 
00357       /**
00358        *  @brief  Test char_type classification.
00359        *
00360        *  This function finds a mask M for @a c and compares it to mask @a m.
00361        *
00362        *  do_is() is a hook for a derived facet to change the behavior of
00363        *  classifying.  do_is() must always return the same result for the
00364        *  same input.
00365        *
00366        *  @param __c  The char_type to find the mask of.
00367        *  @param __m  The mask to compare against.
00368        *  @return  (M & __m) != 0.
00369       */
00370       virtual bool
00371       do_is(mask __m, char_type __c) const = 0;
00372 
00373       /**
00374        *  @brief  Return a mask array.
00375        *
00376        *  This function finds the mask for each char_type in the range [lo,hi)
00377        *  and successively writes it to vec.  vec must have as many elements
00378        *  as the input.
00379        *
00380        *  do_is() is a hook for a derived facet to change the behavior of
00381        *  classifying.  do_is() must always return the same result for the
00382        *  same input.
00383        *
00384        *  @param __lo  Pointer to start of range.
00385        *  @param __hi  Pointer to end of range.
00386        *  @param __vec  Pointer to an array of mask storage.
00387        *  @return  @a __hi.
00388       */
00389       virtual const char_type*
00390       do_is(const char_type* __lo, const char_type* __hi,
00391         mask* __vec) const = 0;
00392 
00393       /**
00394        *  @brief  Find char_type matching mask
00395        *
00396        *  This function searches for and returns the first char_type c in
00397        *  [__lo,__hi) for which is(__m,c) is true.
00398        *
00399        *  do_scan_is() is a hook for a derived facet to change the behavior of
00400        *  match searching.  do_is() must always return the same result for the
00401        *  same input.
00402        *
00403        *  @param __m  The mask to compare against.
00404        *  @param __lo  Pointer to start of range.
00405        *  @param __hi  Pointer to end of range.
00406        *  @return  Pointer to a matching char_type if found, else @a __hi.
00407       */
00408       virtual const char_type*
00409       do_scan_is(mask __m, const char_type* __lo,
00410          const char_type* __hi) const = 0;
00411 
00412       /**
00413        *  @brief  Find char_type not matching mask
00414        *
00415        *  This function searches for and returns a pointer to the first
00416        *  char_type c of [lo,hi) for which is(m,c) is false.
00417        *
00418        *  do_scan_is() is a hook for a derived facet to change the behavior of
00419        *  match searching.  do_is() must always return the same result for the
00420        *  same input.
00421        *
00422        *  @param __m  The mask to compare against.
00423        *  @param __lo  Pointer to start of range.
00424        *  @param __hi  Pointer to end of range.
00425        *  @return  Pointer to a non-matching char_type if found, else @a __hi.
00426       */
00427       virtual const char_type*
00428       do_scan_not(mask __m, const char_type* __lo,
00429           const char_type* __hi) const = 0;
00430 
00431       /**
00432        *  @brief  Convert to uppercase.
00433        *
00434        *  This virtual function converts the char_type argument to uppercase
00435        *  if possible.  If not possible (for example, '2'), returns the
00436        *  argument.
00437        *
00438        *  do_toupper() is a hook for a derived facet to change the behavior of
00439        *  uppercasing.  do_toupper() must always return the same result for
00440        *  the same input.
00441        *
00442        *  @param __c  The char_type to convert.
00443        *  @return  The uppercase char_type if convertible, else @a __c.
00444       */
00445       virtual char_type
00446       do_toupper(char_type __c) const = 0;
00447 
00448       /**
00449        *  @brief  Convert array to uppercase.
00450        *
00451        *  This virtual function converts each char_type in the range [__lo,__hi)
00452        *  to uppercase if possible.  Other elements remain untouched.
00453        *
00454        *  do_toupper() is a hook for a derived facet to change the behavior of
00455        *  uppercasing.  do_toupper() must always return the same result for
00456        *  the same input.
00457        *
00458        *  @param __lo  Pointer to start of range.
00459        *  @param __hi  Pointer to end of range.
00460        *  @return  @a __hi.
00461       */
00462       virtual const char_type*
00463       do_toupper(char_type* __lo, const char_type* __hi) const = 0;
00464 
00465       /**
00466        *  @brief  Convert to lowercase.
00467        *
00468        *  This virtual function converts the argument to lowercase if
00469        *  possible.  If not possible (for example, '2'), returns the argument.
00470        *
00471        *  do_tolower() is a hook for a derived facet to change the behavior of
00472        *  lowercasing.  do_tolower() must always return the same result for
00473        *  the same input.
00474        *
00475        *  @param __c  The char_type to convert.
00476        *  @return  The lowercase char_type if convertible, else @a __c.
00477       */
00478       virtual char_type
00479       do_tolower(char_type __c) const = 0;
00480 
00481       /**
00482        *  @brief  Convert array to lowercase.
00483        *
00484        *  This virtual function converts each char_type in the range [__lo,__hi)
00485        *  to lowercase if possible.  Other elements remain untouched.
00486        *
00487        *  do_tolower() is a hook for a derived facet to change the behavior of
00488        *  lowercasing.  do_tolower() must always return the same result for
00489        *  the same input.
00490        *
00491        *  @param __lo  Pointer to start of range.
00492        *  @param __hi  Pointer to end of range.
00493        *  @return  @a __hi.
00494       */
00495       virtual const char_type*
00496       do_tolower(char_type* __lo, const char_type* __hi) const = 0;
00497 
00498       /**
00499        *  @brief  Widen char
00500        *
00501        *  This virtual function converts the char to char_type using the
00502        *  simplest reasonable transformation.
00503        *
00504        *  do_widen() is a hook for a derived facet to change the behavior of
00505        *  widening.  do_widen() must always return the same result for the
00506        *  same input.
00507        *
00508        *  Note: this is not what you want for codepage conversions.  See
00509        *  codecvt for that.
00510        *
00511        *  @param __c  The char to convert.
00512        *  @return  The converted char_type
00513       */
00514       virtual char_type
00515       do_widen(char __c) const = 0;
00516 
00517       /**
00518        *  @brief  Widen char array
00519        *
00520        *  This function converts each char in the input to char_type using the
00521        *  simplest reasonable transformation.
00522        *
00523        *  do_widen() is a hook for a derived facet to change the behavior of
00524        *  widening.  do_widen() must always return the same result for the
00525        *  same input.
00526        *
00527        *  Note: this is not what you want for codepage conversions.  See
00528        *  codecvt for that.
00529        *
00530        *  @param __lo  Pointer to start range.
00531        *  @param __hi  Pointer to end of range.
00532        *  @param __to  Pointer to the destination array.
00533        *  @return  @a __hi.
00534       */
00535       virtual const char*
00536       do_widen(const char* __lo, const char* __hi, char_type* __to) const = 0;
00537 
00538       /**
00539        *  @brief  Narrow char_type to char
00540        *
00541        *  This virtual function converts the argument to char using the
00542        *  simplest reasonable transformation.  If the conversion fails, dfault
00543        *  is returned instead.
00544        *
00545        *  do_narrow() is a hook for a derived facet to change the behavior of
00546        *  narrowing.  do_narrow() must always return the same result for the
00547        *  same input.
00548        *
00549        *  Note: this is not what you want for codepage conversions.  See
00550        *  codecvt for that.
00551        *
00552        *  @param __c  The char_type to convert.
00553        *  @param __dfault  Char to return if conversion fails.
00554        *  @return  The converted char.
00555       */
00556       virtual char
00557       do_narrow(char_type __c, char __dfault) const = 0;
00558 
00559       /**
00560        *  @brief  Narrow char_type array to char
00561        *
00562        *  This virtual function converts each char_type in the range
00563        *  [__lo,__hi) to char using the simplest reasonable
00564        *  transformation and writes the results to the destination
00565        *  array.  For any element in the input that cannot be
00566        *  converted, @a __dfault is used instead.
00567        *
00568        *  do_narrow() is a hook for a derived facet to change the behavior of
00569        *  narrowing.  do_narrow() must always return the same result for the
00570        *  same input.
00571        *
00572        *  Note: this is not what you want for codepage conversions.  See
00573        *  codecvt for that.
00574        *
00575        *  @param __lo  Pointer to start of range.
00576        *  @param __hi  Pointer to end of range.
00577        *  @param __dfault  Char to use if conversion fails.
00578        *  @param __to  Pointer to the destination array.
00579        *  @return  @a __hi.
00580       */
00581       virtual const char_type*
00582       do_narrow(const char_type* __lo, const char_type* __hi,
00583         char __dfault, char* __to) const = 0;
00584     };
00585 
00586   /**
00587    *  @brief  Primary class template ctype facet.
00588    *  @ingroup locales
00589    *
00590    *  This template class defines classification and conversion functions for
00591    *  character sets.  It wraps cctype functionality.  Ctype gets used by
00592    *  streams for many I/O operations.
00593    *
00594    *  This template provides the protected virtual functions the developer
00595    *  will have to replace in a derived class or specialization to make a
00596    *  working facet.  The public functions that access them are defined in
00597    *  __ctype_abstract_base, to allow for implementation flexibility.  See
00598    *  ctype<wchar_t> for an example.  The functions are documented in
00599    *  __ctype_abstract_base.
00600    *
00601    *  Note: implementations are provided for all the protected virtual
00602    *  functions, but will likely not be useful.
00603   */
00604   template<typename _CharT>
00605     class ctype : public __ctype_abstract_base<_CharT>
00606     {
00607     public:
00608       // Types:
00609       typedef _CharT            char_type;
00610       typedef typename __ctype_abstract_base<_CharT>::mask mask;
00611 
00612       /// The facet id for ctype<char_type>
00613       static locale::id         id;
00614 
00615       explicit
00616       ctype(size_t __refs = 0) : __ctype_abstract_base<_CharT>(__refs) { }
00617 
00618    protected:
00619       virtual
00620       ~ctype();
00621 
00622       virtual bool
00623       do_is(mask __m, char_type __c) const;
00624 
00625       virtual const char_type*
00626       do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
00627 
00628       virtual const char_type*
00629       do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
00630 
00631       virtual const char_type*
00632       do_scan_not(mask __m, const char_type* __lo,
00633           const char_type* __hi) const;
00634 
00635       virtual char_type
00636       do_toupper(char_type __c) const;
00637 
00638       virtual const char_type*
00639       do_toupper(char_type* __lo, const char_type* __hi) const;
00640 
00641       virtual char_type
00642       do_tolower(char_type __c) const;
00643 
00644       virtual const char_type*
00645       do_tolower(char_type* __lo, const char_type* __hi) const;
00646 
00647       virtual char_type
00648       do_widen(char __c) const;
00649 
00650       virtual const char*
00651       do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
00652 
00653       virtual char
00654       do_narrow(char_type, char __dfault) const;
00655 
00656       virtual const char_type*
00657       do_narrow(const char_type* __lo, const char_type* __hi,
00658         char __dfault, char* __to) const;
00659     };
00660 
00661   template<typename _CharT>
00662     locale::id ctype<_CharT>::id;
00663 
00664   /**
00665    *  @brief  The ctype<char> specialization.
00666    *  @ingroup locales
00667    *
00668    *  This class defines classification and conversion functions for
00669    *  the char type.  It gets used by char streams for many I/O
00670    *  operations.  The char specialization provides a number of
00671    *  optimizations as well.
00672   */
00673   template<>
00674     class ctype<char> : public locale::facet, public ctype_base
00675     {
00676     public:
00677       // Types:
00678       /// Typedef for the template parameter char.
00679       typedef char      char_type;
00680 
00681     protected:
00682       // Data Members:
00683       __c_locale        _M_c_locale_ctype;
00684       bool          _M_del;
00685       __to_type         _M_toupper;
00686       __to_type         _M_tolower;
00687       const mask*       _M_table;
00688       mutable char      _M_widen_ok;
00689       mutable char      _M_widen[1 + static_cast<unsigned char>(-1)];
00690       mutable char      _M_narrow[1 + static_cast<unsigned char>(-1)];
00691       mutable char      _M_narrow_ok;   // 0 uninitialized, 1 init,
00692                         // 2 memcpy can't be used
00693 
00694     public:
00695       /// The facet id for ctype<char>
00696       static locale::id        id;
00697       /// The size of the mask table.  It is SCHAR_MAX + 1.
00698       static const size_t      table_size = 1 + static_cast<unsigned char>(-1);
00699 
00700       /**
00701        *  @brief  Constructor performs initialization.
00702        *
00703        *  This is the constructor provided by the standard.
00704        *
00705        *  @param __table If non-zero, table is used as the per-char mask.
00706        *               Else classic_table() is used.
00707        *  @param __del   If true, passes ownership of table to this facet.
00708        *  @param __refs  Passed to the base facet class.
00709       */
00710       explicit
00711       ctype(const mask* __table = 0, bool __del = false, size_t __refs = 0);
00712 
00713       /**
00714        *  @brief  Constructor performs static initialization.
00715        *
00716        *  This constructor is used to construct the initial C locale facet.
00717        *
00718        *  @param __cloc  Handle to C locale data.
00719        *  @param __table If non-zero, table is used as the per-char mask.
00720        *  @param __del   If true, passes ownership of table to this facet.
00721        *  @param __refs  Passed to the base facet class.
00722       */
00723       explicit
00724       ctype(__c_locale __cloc, const mask* __table = 0, bool __del = false,
00725         size_t __refs = 0);
00726 
00727       /**
00728        *  @brief  Test char classification.
00729        *
00730        *  This function compares the mask table[c] to @a __m.
00731        *
00732        *  @param __c  The char to compare the mask of.
00733        *  @param __m  The mask to compare against.
00734        *  @return  True if __m & table[__c] is true, false otherwise.
00735       */
00736       inline bool
00737       is(mask __m, char __c) const;
00738 
00739       /**
00740        *  @brief  Return a mask array.
00741        *
00742        *  This function finds the mask for each char in the range [lo, hi) and
00743        *  successively writes it to vec.  vec must have as many elements as
00744        *  the char array.
00745        *
00746        *  @param __lo  Pointer to start of range.
00747        *  @param __hi  Pointer to end of range.
00748        *  @param __vec  Pointer to an array of mask storage.
00749        *  @return  @a __hi.
00750       */
00751       inline const char*
00752       is(const char* __lo, const char* __hi, mask* __vec) const;
00753 
00754       /**
00755        *  @brief  Find char matching a mask
00756        *
00757        *  This function searches for and returns the first char in [lo,hi) for
00758        *  which is(m,char) is true.
00759        *
00760        *  @param __m  The mask to compare against.
00761        *  @param __lo  Pointer to start of range.
00762        *  @param __hi  Pointer to end of range.
00763        *  @return  Pointer to a matching char if found, else @a __hi.
00764       */
00765       inline const char*
00766       scan_is(mask __m, const char* __lo, const char* __hi) const;
00767 
00768       /**
00769        *  @brief  Find char not matching a mask
00770        *
00771        *  This function searches for and returns a pointer to the first char
00772        *  in [__lo,__hi) for which is(m,char) is false.
00773        *
00774        *  @param __m  The mask to compare against.
00775        *  @param __lo  Pointer to start of range.
00776        *  @param __hi  Pointer to end of range.
00777        *  @return  Pointer to a non-matching char if found, else @a __hi.
00778       */
00779       inline const char*
00780       scan_not(mask __m, const char* __lo, const char* __hi) const;
00781 
00782       /**
00783        *  @brief  Convert to uppercase.
00784        *
00785        *  This function converts the char argument to uppercase if possible.
00786        *  If not possible (for example, '2'), returns the argument.
00787        *
00788        *  toupper() acts as if it returns ctype<char>::do_toupper(c).
00789        *  do_toupper() must always return the same result for the same input.
00790        *
00791        *  @param __c  The char to convert.
00792        *  @return  The uppercase char if convertible, else @a __c.
00793       */
00794       char_type
00795       toupper(char_type __c) const
00796       { return this->do_toupper(__c); }
00797 
00798       /**
00799        *  @brief  Convert array to uppercase.
00800        *
00801        *  This function converts each char in the range [__lo,__hi) to uppercase
00802        *  if possible.  Other chars remain untouched.
00803        *
00804        *  toupper() acts as if it returns ctype<char>:: do_toupper(__lo, __hi).
00805        *  do_toupper() must always return the same result for the same input.
00806        *
00807        *  @param __lo  Pointer to first char in range.
00808        *  @param __hi  Pointer to end of range.
00809        *  @return  @a __hi.
00810       */
00811       const char_type*
00812       toupper(char_type *__lo, const char_type* __hi) const
00813       { return this->do_toupper(__lo, __hi); }
00814 
00815       /**
00816        *  @brief  Convert to lowercase.
00817        *
00818        *  This function converts the char argument to lowercase if possible.
00819        *  If not possible (for example, '2'), returns the argument.
00820        *
00821        *  tolower() acts as if it returns ctype<char>::do_tolower(__c).
00822        *  do_tolower() must always return the same result for the same input.
00823        *
00824        *  @param __c  The char to convert.
00825        *  @return  The lowercase char if convertible, else @a __c.
00826       */
00827       char_type
00828       tolower(char_type __c) const
00829       { return this->do_tolower(__c); }
00830 
00831       /**
00832        *  @brief  Convert array to lowercase.
00833        *
00834        *  This function converts each char in the range [lo,hi) to lowercase
00835        *  if possible.  Other chars remain untouched.
00836        *
00837        *  tolower() acts as if it returns ctype<char>:: do_tolower(__lo, __hi).
00838        *  do_tolower() must always return the same result for the same input.
00839        *
00840        *  @param __lo  Pointer to first char in range.
00841        *  @param __hi  Pointer to end of range.
00842        *  @return  @a __hi.
00843       */
00844       const char_type*
00845       tolower(char_type* __lo, const char_type* __hi) const
00846       { return this->do_tolower(__lo, __hi); }
00847 
00848       /**
00849        *  @brief  Widen char
00850        *
00851        *  This function converts the char to char_type using the simplest
00852        *  reasonable transformation.  For an underived ctype<char> facet, the
00853        *  argument will be returned unchanged.
00854        *
00855        *  This function works as if it returns ctype<char>::do_widen(c).
00856        *  do_widen() must always return the same result for the same input.
00857        *
00858        *  Note: this is not what you want for codepage conversions.  See
00859        *  codecvt for that.
00860        *
00861        *  @param __c  The char to convert.
00862        *  @return  The converted character.
00863       */
00864       char_type
00865       widen(char __c) const
00866       {
00867     if (_M_widen_ok)
00868       return _M_widen[static_cast<unsigned char>(__c)];
00869     this->_M_widen_init();
00870     return this->do_widen(__c);
00871       }
00872 
00873       /**
00874        *  @brief  Widen char array
00875        *
00876        *  This function converts each char in the input to char using the
00877        *  simplest reasonable transformation.  For an underived ctype<char>
00878        *  facet, the argument will be copied unchanged.
00879        *
00880        *  This function works as if it returns ctype<char>::do_widen(c).
00881        *  do_widen() must always return the same result for the same input.
00882        *
00883        *  Note: this is not what you want for codepage conversions.  See
00884        *  codecvt for that.
00885        *
00886        *  @param __lo  Pointer to first char in range.
00887        *  @param __hi  Pointer to end of range.
00888        *  @param __to  Pointer to the destination array.
00889        *  @return  @a __hi.
00890       */
00891       const char*
00892       widen(const char* __lo, const char* __hi, char_type* __to) const
00893       {
00894     if (_M_widen_ok == 1)
00895       {
00896         __builtin_memcpy(__to, __lo, __hi - __lo);
00897         return __hi;
00898       }
00899     if (!_M_widen_ok)
00900       _M_widen_init();
00901     return this->do_widen(__lo, __hi, __to);
00902       }
00903 
00904       /**
00905        *  @brief  Narrow char
00906        *
00907        *  This function converts the char to char using the simplest
00908        *  reasonable transformation.  If the conversion fails, dfault is
00909        *  returned instead.  For an underived ctype<char> facet, @a c
00910        *  will be returned unchanged.
00911        *
00912        *  This function works as if it returns ctype<char>::do_narrow(c).
00913        *  do_narrow() must always return the same result for the same input.
00914        *
00915        *  Note: this is not what you want for codepage conversions.  See
00916        *  codecvt for that.
00917        *
00918        *  @param __c  The char to convert.
00919        *  @param __dfault  Char to return if conversion fails.
00920        *  @return  The converted character.
00921       */
00922       char
00923       narrow(char_type __c, char __dfault) const
00924       {
00925     if (_M_narrow[static_cast<unsigned char>(__c)])
00926       return _M_narrow[static_cast<unsigned char>(__c)];
00927     const char __t = do_narrow(__c, __dfault);
00928     if (__t != __dfault)
00929       _M_narrow[static_cast<unsigned char>(__c)] = __t;
00930     return __t;
00931       }
00932 
00933       /**
00934        *  @brief  Narrow char array
00935        *
00936        *  This function converts each char in the input to char using the
00937        *  simplest reasonable transformation and writes the results to the
00938        *  destination array.  For any char in the input that cannot be
00939        *  converted, @a dfault is used instead.  For an underived ctype<char>
00940        *  facet, the argument will be copied unchanged.
00941        *
00942        *  This function works as if it returns ctype<char>::do_narrow(lo, hi,
00943        *  dfault, to).  do_narrow() must always return the same result for the
00944        *  same input.
00945        *
00946        *  Note: this is not what you want for codepage conversions.  See
00947        *  codecvt for that.
00948        *
00949        *  @param __lo  Pointer to start of range.
00950        *  @param __hi  Pointer to end of range.
00951        *  @param __dfault  Char to use if conversion fails.
00952        *  @param __to  Pointer to the destination array.
00953        *  @return  @a __hi.
00954       */
00955       const char_type*
00956       narrow(const char_type* __lo, const char_type* __hi,
00957          char __dfault, char* __to) const
00958       {
00959     if (__builtin_expect(_M_narrow_ok == 1, true))
00960       {
00961         __builtin_memcpy(__to, __lo, __hi - __lo);
00962         return __hi;
00963       }
00964     if (!_M_narrow_ok)
00965       _M_narrow_init();
00966     return this->do_narrow(__lo, __hi, __dfault, __to);
00967       }
00968 
00969       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00970       // DR 695. ctype<char>::classic_table() not accessible.
00971       /// Returns a pointer to the mask table provided to the constructor, or
00972       /// the default from classic_table() if none was provided.
00973       const mask*
00974       table() const throw()
00975       { return _M_table; }
00976 
00977       /// Returns a pointer to the C locale mask table.
00978       static const mask*
00979       classic_table() throw();
00980     protected:
00981 
00982       /**
00983        *  @brief  Destructor.
00984        *
00985        *  This function deletes table() if @a del was true in the
00986        *  constructor.
00987       */
00988       virtual
00989       ~ctype();
00990 
00991       /**
00992        *  @brief  Convert to uppercase.
00993        *
00994        *  This virtual function converts the char argument to uppercase if
00995        *  possible.  If not possible (for example, '2'), returns the argument.
00996        *
00997        *  do_toupper() is a hook for a derived facet to change the behavior of
00998        *  uppercasing.  do_toupper() must always return the same result for
00999        *  the same input.
01000        *
01001        *  @param __c  The char to convert.
01002        *  @return  The uppercase char if convertible, else @a __c.
01003       */
01004       virtual char_type
01005       do_toupper(char_type __c) const;
01006 
01007       /**
01008        *  @brief  Convert array to uppercase.
01009        *
01010        *  This virtual function converts each char in the range [lo,hi) to
01011        *  uppercase if possible.  Other chars remain untouched.
01012        *
01013        *  do_toupper() is a hook for a derived facet to change the behavior of
01014        *  uppercasing.  do_toupper() must always return the same result for
01015        *  the same input.
01016        *
01017        *  @param __lo  Pointer to start of range.
01018        *  @param __hi  Pointer to end of range.
01019        *  @return  @a __hi.
01020       */
01021       virtual const char_type*
01022       do_toupper(char_type* __lo, const char_type* __hi) const;
01023 
01024       /**
01025        *  @brief  Convert to lowercase.
01026        *
01027        *  This virtual function converts the char argument to lowercase if
01028        *  possible.  If not possible (for example, '2'), returns the argument.
01029        *
01030        *  do_tolower() is a hook for a derived facet to change the behavior of
01031        *  lowercasing.  do_tolower() must always return the same result for
01032        *  the same input.
01033        *
01034        *  @param __c  The char to convert.
01035        *  @return  The lowercase char if convertible, else @a __c.
01036       */
01037       virtual char_type
01038       do_tolower(char_type __c) const;
01039 
01040       /**
01041        *  @brief  Convert array to lowercase.
01042        *
01043        *  This virtual function converts each char in the range [lo,hi) to
01044        *  lowercase if possible.  Other chars remain untouched.
01045        *
01046        *  do_tolower() is a hook for a derived facet to change the behavior of
01047        *  lowercasing.  do_tolower() must always return the same result for
01048        *  the same input.
01049        *
01050        *  @param __lo  Pointer to first char in range.
01051        *  @param __hi  Pointer to end of range.
01052        *  @return  @a __hi.
01053       */
01054       virtual const char_type*
01055       do_tolower(char_type* __lo, const char_type* __hi) const;
01056 
01057       /**
01058        *  @brief  Widen char
01059        *
01060        *  This virtual function converts the char to char using the simplest
01061        *  reasonable transformation.  For an underived ctype<char> facet, the
01062        *  argument will be returned unchanged.
01063        *
01064        *  do_widen() is a hook for a derived facet to change the behavior of
01065        *  widening.  do_widen() must always return the same result for the
01066        *  same input.
01067        *
01068        *  Note: this is not what you want for codepage conversions.  See
01069        *  codecvt for that.
01070        *
01071        *  @param __c  The char to convert.
01072        *  @return  The converted character.
01073       */
01074       virtual char_type
01075       do_widen(char __c) const
01076       { return __c; }
01077 
01078       /**
01079        *  @brief  Widen char array
01080        *
01081        *  This function converts each char in the range [lo,hi) to char using
01082        *  the simplest reasonable transformation.  For an underived
01083        *  ctype<char> facet, the argument will be copied unchanged.
01084        *
01085        *  do_widen() is a hook for a derived facet to change the behavior of
01086        *  widening.  do_widen() must always return the same result for the
01087        *  same input.
01088        *
01089        *  Note: this is not what you want for codepage conversions.  See
01090        *  codecvt for that.
01091        *
01092        *  @param __lo  Pointer to start of range.
01093        *  @param __hi  Pointer to end of range.
01094        *  @param __to  Pointer to the destination array.
01095        *  @return  @a __hi.
01096       */
01097       virtual const char*
01098       do_widen(const char* __lo, const char* __hi, char_type* __to) const
01099       {
01100     __builtin_memcpy(__to, __lo, __hi - __lo);
01101     return __hi;
01102       }
01103 
01104       /**
01105        *  @brief  Narrow char
01106        *
01107        *  This virtual function converts the char to char using the simplest
01108        *  reasonable transformation.  If the conversion fails, dfault is
01109        *  returned instead.  For an underived ctype<char> facet, @a c will be
01110        *  returned unchanged.
01111        *
01112        *  do_narrow() is a hook for a derived facet to change the behavior of
01113        *  narrowing.  do_narrow() must always return the same result for the
01114        *  same input.
01115        *
01116        *  Note: this is not what you want for codepage conversions.  See
01117        *  codecvt for that.
01118        *
01119        *  @param __c  The char to convert.
01120        *  @param __dfault  Char to return if conversion fails.
01121        *  @return  The converted char.
01122       */
01123       virtual char
01124       do_narrow(char_type __c, char __dfault) const
01125       { return __c; }
01126 
01127       /**
01128        *  @brief  Narrow char array to char array
01129        *
01130        *  This virtual function converts each char in the range [lo,hi) to
01131        *  char using the simplest reasonable transformation and writes the
01132        *  results to the destination array.  For any char in the input that
01133        *  cannot be converted, @a dfault is used instead.  For an underived
01134        *  ctype<char> facet, the argument will be copied unchanged.
01135        *
01136        *  do_narrow() is a hook for a derived facet to change the behavior of
01137        *  narrowing.  do_narrow() must always return the same result for the
01138        *  same input.
01139        *
01140        *  Note: this is not what you want for codepage conversions.  See
01141        *  codecvt for that.
01142        *
01143        *  @param __lo  Pointer to start of range.
01144        *  @param __hi  Pointer to end of range.
01145        *  @param __dfault  Char to use if conversion fails.
01146        *  @param __to  Pointer to the destination array.
01147        *  @return  @a __hi.
01148       */
01149       virtual const char_type*
01150       do_narrow(const char_type* __lo, const char_type* __hi,
01151         char __dfault, char* __to) const
01152       {
01153     __builtin_memcpy(__to, __lo, __hi - __lo);
01154     return __hi;
01155       }
01156 
01157     private:
01158       void _M_narrow_init() const;
01159       void _M_widen_init() const;
01160     };
01161 
01162 #ifdef _GLIBCXX_USE_WCHAR_T
01163   /**
01164    *  @brief  The ctype<wchar_t> specialization.
01165    *  @ingroup locales
01166    *
01167    *  This class defines classification and conversion functions for the
01168    *  wchar_t type.  It gets used by wchar_t streams for many I/O operations.
01169    *  The wchar_t specialization provides a number of optimizations as well.
01170    *
01171    *  ctype<wchar_t> inherits its public methods from
01172    *  __ctype_abstract_base<wchar_t>.
01173   */
01174   template<>
01175     class ctype<wchar_t> : public __ctype_abstract_base<wchar_t>
01176     {
01177     public:
01178       // Types:
01179       /// Typedef for the template parameter wchar_t.
01180       typedef wchar_t       char_type;
01181       typedef wctype_t      __wmask_type;
01182 
01183     protected:
01184       __c_locale        _M_c_locale_ctype;
01185 
01186       // Pre-computed narrowed and widened chars.
01187       bool                      _M_narrow_ok;
01188       char                      _M_narrow[128];
01189       wint_t                    _M_widen[1 + static_cast<unsigned char>(-1)];
01190 
01191       // Pre-computed elements for do_is.
01192       mask                      _M_bit[16];
01193       __wmask_type              _M_wmask[16];
01194 
01195     public:
01196       // Data Members:
01197       /// The facet id for ctype<wchar_t>
01198       static locale::id     id;
01199 
01200       /**
01201        *  @brief  Constructor performs initialization.
01202        *
01203        *  This is the constructor provided by the standard.
01204        *
01205        *  @param __refs  Passed to the base facet class.
01206       */
01207       explicit
01208       ctype(size_t __refs = 0);
01209 
01210       /**
01211        *  @brief  Constructor performs static initialization.
01212        *
01213        *  This constructor is used to construct the initial C locale facet.
01214        *
01215        *  @param __cloc  Handle to C locale data.
01216        *  @param __refs  Passed to the base facet class.
01217       */
01218       explicit
01219       ctype(__c_locale __cloc, size_t __refs = 0);
01220 
01221     protected:
01222       __wmask_type
01223       _M_convert_to_wmask(const mask __m) const throw();
01224 
01225       /// Destructor
01226       virtual
01227       ~ctype();
01228 
01229       /**
01230        *  @brief  Test wchar_t classification.
01231        *
01232        *  This function finds a mask M for @a c and compares it to mask @a m.
01233        *
01234        *  do_is() is a hook for a derived facet to change the behavior of
01235        *  classifying.  do_is() must always return the same result for the
01236        *  same input.
01237        *
01238        *  @param __c  The wchar_t to find the mask of.
01239        *  @param __m  The mask to compare against.
01240        *  @return  (M & __m) != 0.
01241       */
01242       virtual bool
01243       do_is(mask __m, char_type __c) const;
01244 
01245       /**
01246        *  @brief  Return a mask array.
01247        *
01248        *  This function finds the mask for each wchar_t in the range [lo,hi)
01249        *  and successively writes it to vec.  vec must have as many elements
01250        *  as the input.
01251        *
01252        *  do_is() is a hook for a derived facet to change the behavior of
01253        *  classifying.  do_is() must always return the same result for the
01254        *  same input.
01255        *
01256        *  @param __lo  Pointer to start of range.
01257        *  @param __hi  Pointer to end of range.
01258        *  @param __vec  Pointer to an array of mask storage.
01259        *  @return  @a __hi.
01260       */
01261       virtual const char_type*
01262       do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
01263 
01264       /**
01265        *  @brief  Find wchar_t matching mask
01266        *
01267        *  This function searches for and returns the first wchar_t c in
01268        *  [__lo,__hi) for which is(__m,c) is true.
01269        *
01270        *  do_scan_is() is a hook for a derived facet to change the behavior of
01271        *  match searching.  do_is() must always return the same result for the
01272        *  same input.
01273        *
01274        *  @param __m  The mask to compare against.
01275        *  @param __lo  Pointer to start of range.
01276        *  @param __hi  Pointer to end of range.
01277        *  @return  Pointer to a matching wchar_t if found, else @a __hi.
01278       */
01279       virtual const char_type*
01280       do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
01281 
01282       /**
01283        *  @brief  Find wchar_t not matching mask
01284        *
01285        *  This function searches for and returns a pointer to the first
01286        *  wchar_t c of [__lo,__hi) for which is(__m,c) is false.
01287        *
01288        *  do_scan_is() is a hook for a derived facet to change the behavior of
01289        *  match searching.  do_is() must always return the same result for the
01290        *  same input.
01291        *
01292        *  @param __m  The mask to compare against.
01293        *  @param __lo  Pointer to start of range.
01294        *  @param __hi  Pointer to end of range.
01295        *  @return  Pointer to a non-matching wchar_t if found, else @a __hi.
01296       */
01297       virtual const char_type*
01298       do_scan_not(mask __m, const char_type* __lo,
01299           const char_type* __hi) const;
01300 
01301       /**
01302        *  @brief  Convert to uppercase.
01303        *
01304        *  This virtual function converts the wchar_t argument to uppercase if
01305        *  possible.  If not possible (for example, '2'), returns the argument.
01306        *
01307        *  do_toupper() is a hook for a derived facet to change the behavior of
01308        *  uppercasing.  do_toupper() must always return the same result for
01309        *  the same input.
01310        *
01311        *  @param __c  The wchar_t to convert.
01312        *  @return  The uppercase wchar_t if convertible, else @a __c.
01313       */
01314       virtual char_type
01315       do_toupper(char_type __c) const;
01316 
01317       /**
01318        *  @brief  Convert array to uppercase.
01319        *
01320        *  This virtual function converts each wchar_t in the range [lo,hi) to
01321        *  uppercase if possible.  Other elements remain untouched.
01322        *
01323        *  do_toupper() is a hook for a derived facet to change the behavior of
01324        *  uppercasing.  do_toupper() must always return the same result for
01325        *  the same input.
01326        *
01327        *  @param __lo  Pointer to start of range.
01328        *  @param __hi  Pointer to end of range.
01329        *  @return  @a __hi.
01330       */
01331       virtual const char_type*
01332       do_toupper(char_type* __lo, const char_type* __hi) const;
01333 
01334       /**
01335        *  @brief  Convert to lowercase.
01336        *
01337        *  This virtual function converts the argument to lowercase if
01338        *  possible.  If not possible (for example, '2'), returns the argument.
01339        *
01340        *  do_tolower() is a hook for a derived facet to change the behavior of
01341        *  lowercasing.  do_tolower() must always return the same result for
01342        *  the same input.
01343        *
01344        *  @param __c  The wchar_t to convert.
01345        *  @return  The lowercase wchar_t if convertible, else @a __c.
01346       */
01347       virtual char_type
01348       do_tolower(char_type __c) const;
01349 
01350       /**
01351        *  @brief  Convert array to lowercase.
01352        *
01353        *  This virtual function converts each wchar_t in the range [lo,hi) to
01354        *  lowercase if possible.  Other elements remain untouched.
01355        *
01356        *  do_tolower() is a hook for a derived facet to change the behavior of
01357        *  lowercasing.  do_tolower() must always return the same result for
01358        *  the same input.
01359        *
01360        *  @param __lo  Pointer to start of range.
01361        *  @param __hi  Pointer to end of range.
01362        *  @return  @a __hi.
01363       */
01364       virtual const char_type*
01365       do_tolower(char_type* __lo, const char_type* __hi) const;
01366 
01367       /**
01368        *  @brief  Widen char to wchar_t
01369        *
01370        *  This virtual function converts the char to wchar_t using the
01371        *  simplest reasonable transformation.  For an underived ctype<wchar_t>
01372        *  facet, the argument will be cast to wchar_t.
01373        *
01374        *  do_widen() is a hook for a derived facet to change the behavior of
01375        *  widening.  do_widen() must always return the same result for the
01376        *  same input.
01377        *
01378        *  Note: this is not what you want for codepage conversions.  See
01379        *  codecvt for that.
01380        *
01381        *  @param __c  The char to convert.
01382        *  @return  The converted wchar_t.
01383       */
01384       virtual char_type
01385       do_widen(char __c) const;
01386 
01387       /**
01388        *  @brief  Widen char array to wchar_t array
01389        *
01390        *  This function converts each char in the input to wchar_t using the
01391        *  simplest reasonable transformation.  For an underived ctype<wchar_t>
01392        *  facet, the argument will be copied, casting each element to wchar_t.
01393        *
01394        *  do_widen() is a hook for a derived facet to change the behavior of
01395        *  widening.  do_widen() must always return the same result for the
01396        *  same input.
01397        *
01398        *  Note: this is not what you want for codepage conversions.  See
01399        *  codecvt for that.
01400        *
01401        *  @param __lo  Pointer to start range.
01402        *  @param __hi  Pointer to end of range.
01403        *  @param __to  Pointer to the destination array.
01404        *  @return  @a __hi.
01405       */
01406       virtual const char*
01407       do_widen(const char* __lo, const char* __hi, char_type* __to) const;
01408 
01409       /**
01410        *  @brief  Narrow wchar_t to char
01411        *
01412        *  This virtual function converts the argument to char using
01413        *  the simplest reasonable transformation.  If the conversion
01414        *  fails, dfault is returned instead.  For an underived
01415        *  ctype<wchar_t> facet, @a c will be cast to char and
01416        *  returned.
01417        *
01418        *  do_narrow() is a hook for a derived facet to change the
01419        *  behavior of narrowing.  do_narrow() must always return the
01420        *  same result for the same input.
01421        *
01422        *  Note: this is not what you want for codepage conversions.  See
01423        *  codecvt for that.
01424        *
01425        *  @param __c  The wchar_t to convert.
01426        *  @param __dfault  Char to return if conversion fails.
01427        *  @return  The converted char.
01428       */
01429       virtual char
01430       do_narrow(char_type __c, char __dfault) const;
01431 
01432       /**
01433        *  @brief  Narrow wchar_t array to char array
01434        *
01435        *  This virtual function converts each wchar_t in the range [lo,hi) to
01436        *  char using the simplest reasonable transformation and writes the
01437        *  results to the destination array.  For any wchar_t in the input that
01438        *  cannot be converted, @a dfault is used instead.  For an underived
01439        *  ctype<wchar_t> facet, the argument will be copied, casting each
01440        *  element to char.
01441        *
01442        *  do_narrow() is a hook for a derived facet to change the behavior of
01443        *  narrowing.  do_narrow() must always return the same result for the
01444        *  same input.
01445        *
01446        *  Note: this is not what you want for codepage conversions.  See
01447        *  codecvt for that.
01448        *
01449        *  @param __lo  Pointer to start of range.
01450        *  @param __hi  Pointer to end of range.
01451        *  @param __dfault  Char to use if conversion fails.
01452        *  @param __to  Pointer to the destination array.
01453        *  @return  @a __hi.
01454       */
01455       virtual const char_type*
01456       do_narrow(const char_type* __lo, const char_type* __hi,
01457         char __dfault, char* __to) const;
01458 
01459       // For use at construction time only.
01460       void
01461       _M_initialize_ctype() throw();
01462     };
01463 #endif //_GLIBCXX_USE_WCHAR_T
01464 
01465   /// class ctype_byname [22.2.1.2].
01466   template<typename _CharT>
01467     class ctype_byname : public ctype<_CharT>
01468     {
01469     public:
01470       typedef typename ctype<_CharT>::mask  mask;
01471 
01472       explicit
01473       ctype_byname(const char* __s, size_t __refs = 0);
01474 
01475     protected:
01476       virtual
01477       ~ctype_byname() { };
01478     };
01479 
01480   /// 22.2.1.4  Class ctype_byname specializations.
01481   template<>
01482     class ctype_byname<char> : public ctype<char>
01483     {
01484     public:
01485       explicit
01486       ctype_byname(const char* __s, size_t __refs = 0);
01487 
01488     protected:
01489       virtual
01490       ~ctype_byname();
01491     };
01492 
01493 #ifdef _GLIBCXX_USE_WCHAR_T
01494   template<>
01495     class ctype_byname<wchar_t> : public ctype<wchar_t>
01496     {
01497     public:
01498       explicit
01499       ctype_byname(const char* __s, size_t __refs = 0);
01500 
01501     protected:
01502       virtual
01503       ~ctype_byname();
01504     };
01505 #endif
01506 
01507 _GLIBCXX_END_NAMESPACE_VERSION
01508 } // namespace
01509 
01510 // Include host and configuration specific ctype inlines.
01511 #include <bits/ctype_inline.h>
01512 
01513 namespace std _GLIBCXX_VISIBILITY(default)
01514 {
01515 _GLIBCXX_BEGIN_NAMESPACE_VERSION
01516 
01517   // 22.2.2  The numeric category.
01518   class __num_base
01519   {
01520   public:
01521     // NB: Code depends on the order of _S_atoms_out elements.
01522     // Below are the indices into _S_atoms_out.
01523     enum
01524       {
01525     _S_ominus,
01526     _S_oplus,
01527     _S_ox,
01528     _S_oX,
01529     _S_odigits,
01530     _S_odigits_end = _S_odigits + 16,
01531     _S_oudigits = _S_odigits_end,
01532     _S_oudigits_end = _S_oudigits + 16,
01533     _S_oe = _S_odigits + 14,  // For scientific notation, 'e'
01534     _S_oE = _S_oudigits + 14, // For scientific notation, 'E'
01535     _S_oend = _S_oudigits_end
01536       };
01537 
01538     // A list of valid numeric literals for output.  This array
01539     // contains chars that will be passed through the current locale's
01540     // ctype<_CharT>.widen() and then used to render numbers.
01541     // For the standard "C" locale, this is
01542     // "-+xX0123456789abcdef0123456789ABCDEF".
01543     static const char* _S_atoms_out;
01544 
01545     // String literal of acceptable (narrow) input, for num_get.
01546     // "-+xX0123456789abcdefABCDEF"
01547     static const char* _S_atoms_in;
01548 
01549     enum
01550     {
01551       _S_iminus,
01552       _S_iplus,
01553       _S_ix,
01554       _S_iX,
01555       _S_izero,
01556       _S_ie = _S_izero + 14,
01557       _S_iE = _S_izero + 20,
01558       _S_iend = 26
01559     };
01560 
01561     // num_put
01562     // Construct and return valid scanf format for floating point types.
01563     static void
01564     _S_format_float(const ios_base& __io, char* __fptr, char __mod) throw();
01565   };
01566 
01567   template<typename _CharT>
01568     struct __numpunct_cache : public locale::facet
01569     {
01570       const char*           _M_grouping;
01571       size_t                            _M_grouping_size;
01572       bool              _M_use_grouping;
01573       const _CharT*         _M_truename;
01574       size_t                            _M_truename_size;
01575       const _CharT*         _M_falsename;
01576       size_t                            _M_falsename_size;
01577       _CharT                _M_decimal_point;
01578       _CharT                _M_thousands_sep;
01579 
01580       // A list of valid numeric literals for output: in the standard
01581       // "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF".
01582       // This array contains the chars after having been passed
01583       // through the current locale's ctype<_CharT>.widen().
01584       _CharT                _M_atoms_out[__num_base::_S_oend];
01585 
01586       // A list of valid numeric literals for input: in the standard
01587       // "C" locale, this is "-+xX0123456789abcdefABCDEF"
01588       // This array contains the chars after having been passed
01589       // through the current locale's ctype<_CharT>.widen().
01590       _CharT                _M_atoms_in[__num_base::_S_iend];
01591 
01592       bool              _M_allocated;
01593 
01594       __numpunct_cache(size_t __refs = 0)
01595       : facet(__refs), _M_grouping(0), _M_grouping_size(0),
01596     _M_use_grouping(false),
01597     _M_truename(0), _M_truename_size(0), _M_falsename(0),
01598     _M_falsename_size(0), _M_decimal_point(_CharT()),
01599     _M_thousands_sep(_CharT()), _M_allocated(false)
01600     { }
01601 
01602       ~__numpunct_cache();
01603 
01604       void
01605       _M_cache(const locale& __loc);
01606 
01607     private:
01608       __numpunct_cache&
01609       operator=(const __numpunct_cache&);
01610 
01611       explicit
01612       __numpunct_cache(const __numpunct_cache&);
01613     };
01614 
01615   template<typename _CharT>
01616     __numpunct_cache<_CharT>::~__numpunct_cache()
01617     {
01618       if (_M_allocated)
01619     {
01620       delete [] _M_grouping;
01621       delete [] _M_truename;
01622       delete [] _M_falsename;
01623     }
01624     }
01625 
01626   /**
01627    *  @brief  Primary class template numpunct.
01628    *  @ingroup locales
01629    *
01630    *  This facet stores several pieces of information related to printing and
01631    *  scanning numbers, such as the decimal point character.  It takes a
01632    *  template parameter specifying the char type.  The numpunct facet is
01633    *  used by streams for many I/O operations involving numbers.
01634    *
01635    *  The numpunct template uses protected virtual functions to provide the
01636    *  actual results.  The public accessors forward the call to the virtual
01637    *  functions.  These virtual functions are hooks for developers to
01638    *  implement the behavior they require from a numpunct facet.
01639   */
01640   template<typename _CharT>
01641     class numpunct : public locale::facet
01642     {
01643     public:
01644       // Types:
01645       //@{
01646       /// Public typedefs
01647       typedef _CharT            char_type;
01648       typedef basic_string<_CharT>  string_type;
01649       //@}
01650       typedef __numpunct_cache<_CharT>  __cache_type;
01651 
01652     protected:
01653       __cache_type*         _M_data;
01654 
01655     public:
01656       /// Numpunct facet id.
01657       static locale::id         id;
01658 
01659       /**
01660        *  @brief  Numpunct constructor.
01661        *
01662        *  @param  __refs  Refcount to pass to the base class.
01663        */
01664       explicit
01665       numpunct(size_t __refs = 0)
01666       : facet(__refs), _M_data(0)
01667       { _M_initialize_numpunct(); }
01668 
01669       /**
01670        *  @brief  Internal constructor.  Not for general use.
01671        *
01672        *  This is a constructor for use by the library itself to set up the
01673        *  predefined locale facets.
01674        *
01675        *  @param  __cache  __numpunct_cache object.
01676        *  @param  __refs  Refcount to pass to the base class.
01677        */
01678       explicit
01679       numpunct(__cache_type* __cache, size_t __refs = 0)
01680       : facet(__refs), _M_data(__cache)
01681       { _M_initialize_numpunct(); }
01682 
01683       /**
01684        *  @brief  Internal constructor.  Not for general use.
01685        *
01686        *  This is a constructor for use by the library itself to set up new
01687        *  locales.
01688        *
01689        *  @param  __cloc  The C locale.
01690        *  @param  __refs  Refcount to pass to the base class.
01691        */
01692       explicit
01693       numpunct(__c_locale __cloc, size_t __refs = 0)
01694       : facet(__refs), _M_data(0)
01695       { _M_initialize_numpunct(__cloc); }
01696 
01697       /**
01698        *  @brief  Return decimal point character.
01699        *
01700        *  This function returns a char_type to use as a decimal point.  It
01701        *  does so by returning returning
01702        *  numpunct<char_type>::do_decimal_point().
01703        *
01704        *  @return  @a char_type representing a decimal point.
01705       */
01706       char_type
01707       decimal_point() const
01708       { return this->do_decimal_point(); }
01709 
01710       /**
01711        *  @brief  Return thousands separator character.
01712        *
01713        *  This function returns a char_type to use as a thousands
01714        *  separator.  It does so by returning returning
01715        *  numpunct<char_type>::do_thousands_sep().
01716        *
01717        *  @return  char_type representing a thousands separator.
01718       */
01719       char_type
01720       thousands_sep() const
01721       { return this->do_thousands_sep(); }
01722 
01723       /**
01724        *  @brief  Return grouping specification.
01725        *
01726        *  This function returns a string representing groupings for the
01727        *  integer part of a number.  Groupings indicate where thousands
01728        *  separators should be inserted in the integer part of a number.
01729        *
01730        *  Each char in the return string is interpret as an integer
01731        *  rather than a character.  These numbers represent the number
01732        *  of digits in a group.  The first char in the string
01733        *  represents the number of digits in the least significant
01734        *  group.  If a char is negative, it indicates an unlimited
01735        *  number of digits for the group.  If more chars from the
01736        *  string are required to group a number, the last char is used
01737        *  repeatedly.
01738        *
01739        *  For example, if the grouping() returns "\003\002" and is
01740        *  applied to the number 123456789, this corresponds to
01741        *  12,34,56,789.  Note that if the string was "32", this would
01742        *  put more than 50 digits into the least significant group if
01743        *  the character set is ASCII.
01744        *
01745        *  The string is returned by calling
01746        *  numpunct<char_type>::do_grouping().
01747        *
01748        *  @return  string representing grouping specification.
01749       */
01750       string
01751       grouping() const
01752       { return this->do_grouping(); }
01753 
01754       /**
01755        *  @brief  Return string representation of bool true.
01756        *
01757        *  This function returns a string_type containing the text
01758        *  representation for true bool variables.  It does so by calling
01759        *  numpunct<char_type>::do_truename().
01760        *
01761        *  @return  string_type representing printed form of true.
01762       */
01763       string_type
01764       truename() const
01765       { return this->do_truename(); }
01766 
01767       /**
01768        *  @brief  Return string representation of bool false.
01769        *
01770        *  This function returns a string_type containing the text
01771        *  representation for false bool variables.  It does so by calling
01772        *  numpunct<char_type>::do_falsename().
01773        *
01774        *  @return  string_type representing printed form of false.
01775       */
01776       string_type
01777       falsename() const
01778       { return this->do_falsename(); }
01779 
01780     protected:
01781       /// Destructor.
01782       virtual
01783       ~numpunct();
01784 
01785       /**
01786        *  @brief  Return decimal point character.
01787        *
01788        *  Returns a char_type to use as a decimal point.  This function is a
01789        *  hook for derived classes to change the value returned.
01790        *
01791        *  @return  @a char_type representing a decimal point.
01792       */
01793       virtual char_type
01794       do_decimal_point() const
01795       { return _M_data->_M_decimal_point; }
01796 
01797       /**
01798        *  @brief  Return thousands separator character.
01799        *
01800        *  Returns a char_type to use as a thousands separator.  This function
01801        *  is a hook for derived classes to change the value returned.
01802        *
01803        *  @return  @a char_type representing a thousands separator.
01804       */
01805       virtual char_type
01806       do_thousands_sep() const
01807       { return _M_data->_M_thousands_sep; }
01808 
01809       /**
01810        *  @brief  Return grouping specification.
01811        *
01812        *  Returns a string representing groupings for the integer part of a
01813        *  number.  This function is a hook for derived classes to change the
01814        *  value returned.  @see grouping() for details.
01815        *
01816        *  @return  String representing grouping specification.
01817       */
01818       virtual string
01819       do_grouping() const
01820       { return _M_data->_M_grouping; }
01821 
01822       /**
01823        *  @brief  Return string representation of bool true.
01824        *
01825        *  Returns a string_type containing the text representation for true
01826        *  bool variables.  This function is a hook for derived classes to
01827        *  change the value returned.
01828        *
01829        *  @return  string_type representing printed form of true.
01830       */
01831       virtual string_type
01832       do_truename() const
01833       { return _M_data->_M_truename; }
01834 
01835       /**
01836        *  @brief  Return string representation of bool false.
01837        *
01838        *  Returns a string_type containing the text representation for false
01839        *  bool variables.  This function is a hook for derived classes to
01840        *  change the value returned.
01841        *
01842        *  @return  string_type representing printed form of false.
01843       */
01844       virtual string_type
01845       do_falsename() const
01846       { return _M_data->_M_falsename; }
01847 
01848       // For use at construction time only.
01849       void
01850       _M_initialize_numpunct(__c_locale __cloc = 0);
01851     };
01852 
01853   template<typename _CharT>
01854     locale::id numpunct<_CharT>::id;
01855 
01856   template<>
01857     numpunct<char>::~numpunct();
01858 
01859   template<>
01860     void
01861     numpunct<char>::_M_initialize_numpunct(__c_locale __cloc);
01862 
01863 #ifdef _GLIBCXX_USE_WCHAR_T
01864   template<>
01865     numpunct<wchar_t>::~numpunct();
01866 
01867   template<>
01868     void
01869     numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc);
01870 #endif
01871 
01872   /// class numpunct_byname [22.2.3.2].
01873   template<typename _CharT>
01874     class numpunct_byname : public numpunct<_CharT>
01875     {
01876     public:
01877       typedef _CharT            char_type;
01878       typedef basic_string<_CharT>  string_type;
01879 
01880       explicit
01881       numpunct_byname(const char* __s, size_t __refs = 0)
01882       : numpunct<_CharT>(__refs)
01883       {
01884     if (__builtin_strcmp(__s, "C") != 0
01885         && __builtin_strcmp(__s, "POSIX") != 0)
01886       {
01887         __c_locale __tmp;
01888         this->_S_create_c_locale(__tmp, __s);
01889         this->_M_initialize_numpunct(__tmp);
01890         this->_S_destroy_c_locale(__tmp);
01891       }
01892       }
01893 
01894     protected:
01895       virtual
01896       ~numpunct_byname() { }
01897     };
01898 
01899 _GLIBCXX_BEGIN_NAMESPACE_LDBL
01900 
01901   /**
01902    *  @brief  Primary class template num_get.
01903    *  @ingroup locales
01904    *
01905    *  This facet encapsulates the code to parse and return a number
01906    *  from a string.  It is used by the istream numeric extraction
01907    *  operators.
01908    *
01909    *  The num_get template uses protected virtual functions to provide the
01910    *  actual results.  The public accessors forward the call to the virtual
01911    *  functions.  These virtual functions are hooks for developers to
01912    *  implement the behavior they require from the num_get facet.
01913   */
01914   template<typename _CharT, typename _InIter>
01915     class num_get : public locale::facet
01916     {
01917     public:
01918       // Types:
01919       //@{
01920       /// Public typedefs
01921       typedef _CharT            char_type;
01922       typedef _InIter           iter_type;
01923       //@}
01924 
01925       /// Numpunct facet id.
01926       static locale::id         id;
01927 
01928       /**
01929        *  @brief  Constructor performs initialization.
01930        *
01931        *  This is the constructor provided by the standard.
01932        *
01933        *  @param __refs  Passed to the base facet class.
01934       */
01935       explicit
01936       num_get(size_t __refs = 0) : facet(__refs) { }
01937 
01938       /**
01939        *  @brief  Numeric parsing.
01940        *
01941        *  Parses the input stream into the bool @a v.  It does so by calling
01942        *  num_get::do_get().
01943        *
01944        *  If ios_base::boolalpha is set, attempts to read
01945        *  ctype<CharT>::truename() or ctype<CharT>::falsename().  Sets
01946        *  @a v to true or false if successful.  Sets err to
01947        *  ios_base::failbit if reading the string fails.  Sets err to
01948        *  ios_base::eofbit if the stream is emptied.
01949        *
01950        *  If ios_base::boolalpha is not set, proceeds as with reading a long,
01951        *  except if the value is 1, sets @a v to true, if the value is 0, sets
01952        *  @a v to false, and otherwise set err to ios_base::failbit.
01953        *
01954        *  @param  __in  Start of input stream.
01955        *  @param  __end  End of input stream.
01956        *  @param  __io  Source of locale and flags.
01957        *  @param  __err  Error flags to set.
01958        *  @param  __v  Value to format and insert.
01959        *  @return  Iterator after reading.
01960       */
01961       iter_type
01962       get(iter_type __in, iter_type __end, ios_base& __io,
01963       ios_base::iostate& __err, bool& __v) const
01964       { return this->do_get(__in, __end, __io, __err, __v); }
01965 
01966       //@{
01967       /**
01968        *  @brief  Numeric parsing.
01969        *
01970        *  Parses the input stream into the integral variable @a v.  It does so
01971        *  by calling num_get::do_get().
01972        *
01973        *  Parsing is affected by the flag settings in @a io.
01974        *
01975        *  The basic parse is affected by the value of io.flags() &
01976        *  ios_base::basefield.  If equal to ios_base::oct, parses like the
01977        *  scanf %o specifier.  Else if equal to ios_base::hex, parses like %X
01978        *  specifier.  Else if basefield equal to 0, parses like the %i
01979        *  specifier.  Otherwise, parses like %d for signed and %u for unsigned
01980        *  types.  The matching type length modifier is also used.
01981        *
01982        *  Digit grouping is interpreted according to
01983        *  numpunct::grouping() and numpunct::thousands_sep().  If the
01984        *  pattern of digit groups isn't consistent, sets err to
01985        *  ios_base::failbit.
01986        *
01987        *  If parsing the string yields a valid value for @a v, @a v is set.
01988        *  Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
01989        *  Sets err to ios_base::eofbit if the stream is emptied.
01990        *
01991        *  @param  __in  Start of input stream.
01992        *  @param  __end  End of input stream.
01993        *  @param  __io  Source of locale and flags.
01994        *  @param  __err  Error flags to set.
01995        *  @param  __v  Value to format and insert.
01996        *  @return  Iterator after reading.
01997       */
01998       iter_type
01999       get(iter_type __in, iter_type __end, ios_base& __io,
02000       ios_base::iostate& __err, long& __v) const
02001       { return this->do_get(__in, __end, __io, __err, __v); }
02002 
02003       iter_type
02004       get(iter_type __in, iter_type __end, ios_base& __io,
02005       ios_base::iostate& __err, unsigned short& __v) const
02006       { return this->do_get(__in, __end, __io, __err, __v); }
02007 
02008       iter_type
02009       get(iter_type __in, iter_type __end, ios_base& __io,
02010       ios_base::iostate& __err, unsigned int& __v)   const
02011       { return this->do_get(__in, __end, __io, __err, __v); }
02012 
02013       iter_type
02014       get(iter_type __in, iter_type __end, ios_base& __io,
02015       ios_base::iostate& __err, unsigned long& __v)  const
02016       { return this->do_get(__in, __end, __io, __err, __v); }
02017 
02018 #ifdef _GLIBCXX_USE_LONG_LONG
02019       iter_type
02020       get(iter_type __in, iter_type __end, ios_base& __io,
02021       ios_base::iostate& __err, long long& __v) const
02022       { return this->do_get(__in, __end, __io, __err, __v); }
02023 
02024       iter_type
02025       get(iter_type __in, iter_type __end, ios_base& __io,
02026       ios_base::iostate& __err, unsigned long long& __v)  const
02027       { return this->do_get(__in, __end, __io, __err, __v); }
02028 #endif
02029       //@}
02030 
02031       //@{
02032       /**
02033        *  @brief  Numeric parsing.
02034        *
02035        *  Parses the input stream into the integral variable @a v.  It does so
02036        *  by calling num_get::do_get().
02037        *
02038        *  The input characters are parsed like the scanf %g specifier.  The
02039        *  matching type length modifier is also used.
02040        *
02041        *  The decimal point character used is numpunct::decimal_point().
02042        *  Digit grouping is interpreted according to
02043        *  numpunct::grouping() and numpunct::thousands_sep().  If the
02044        *  pattern of digit groups isn't consistent, sets err to
02045        *  ios_base::failbit.
02046        *
02047        *  If parsing the string yields a valid value for @a v, @a v is set.
02048        *  Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
02049        *  Sets err to ios_base::eofbit if the stream is emptied.
02050        *
02051        *  @param  __in  Start of input stream.
02052        *  @param  __end  End of input stream.
02053        *  @param  __io  Source of locale and flags.
02054        *  @param  __err  Error flags to set.
02055        *  @param  __v  Value to format and insert.
02056        *  @return  Iterator after reading.
02057       */
02058       iter_type
02059       get(iter_type __in, iter_type __end, ios_base& __io,
02060       ios_base::iostate& __err, float& __v) const
02061       { return this->do_get(__in, __end, __io, __err, __v); }
02062 
02063       iter_type
02064       get(iter_type __in, iter_type __end, ios_base& __io,
02065       ios_base::iostate& __err, double& __v) const
02066       { return this->do_get(__in, __end, __io, __err, __v); }
02067 
02068       iter_type
02069       get(iter_type __in, iter_type __end, ios_base& __io,
02070       ios_base::iostate& __err, long double& __v) const
02071       { return this->do_get(__in, __end, __io, __err, __v); }
02072       //@}
02073 
02074       /**
02075        *  @brief  Numeric parsing.
02076        *
02077        *  Parses the input stream into the pointer variable @a v.  It does so
02078        *  by calling num_get::do_get().
02079        *
02080        *  The input characters are parsed like the scanf %p specifier.
02081        *
02082        *  Digit grouping is interpreted according to
02083        *  numpunct::grouping() and numpunct::thousands_sep().  If the
02084        *  pattern of digit groups isn't consistent, sets err to
02085        *  ios_base::failbit.
02086        *
02087        *  Note that the digit grouping effect for pointers is a bit ambiguous
02088        *  in the standard and shouldn't be relied on.  See DR 344.
02089        *
02090        *  If parsing the string yields a valid value for @a v, @a v is set.
02091        *  Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
02092        *  Sets err to ios_base::eofbit if the stream is emptied.
02093        *
02094        *  @param  __in  Start of input stream.
02095        *  @param  __end  End of input stream.
02096        *  @param  __io  Source of locale and flags.
02097        *  @param  __err  Error flags to set.
02098        *  @param  __v  Value to format and insert.
02099        *  @return  Iterator after reading.
02100       */
02101       iter_type
02102       get(iter_type __in, iter_type __end, ios_base& __io,
02103       ios_base::iostate& __err, void*& __v) const
02104       { return this->do_get(__in, __end, __io, __err, __v); }
02105 
02106     protected:
02107       /// Destructor.
02108       virtual ~num_get() { }
02109 
02110       iter_type
02111       _M_extract_float(iter_type, iter_type, ios_base&, ios_base::iostate&,
02112                string&) const;
02113 
02114       template<typename _ValueT>
02115     iter_type
02116     _M_extract_int(iter_type, iter_type, ios_base&, ios_base::iostate&,
02117                _ValueT&) const;
02118 
02119       template<typename _CharT2>
02120       typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, int>::__type
02121     _M_find(const _CharT2*, size_t __len, _CharT2 __c) const
02122     {
02123       int __ret = -1;
02124       if (__len <= 10)
02125         {
02126           if (__c >= _CharT2('0') && __c < _CharT2(_CharT2('0') + __len))
02127         __ret = __c - _CharT2('0');
02128         }
02129       else
02130         {
02131           if (__c >= _CharT2('0') && __c <= _CharT2('9'))
02132         __ret = __c - _CharT2('0');
02133           else if (__c >= _CharT2('a') && __c <= _CharT2('f'))
02134         __ret = 10 + (__c - _CharT2('a'));
02135           else if (__c >= _CharT2('A') && __c <= _CharT2('F'))
02136         __ret = 10 + (__c - _CharT2('A'));
02137         }
02138       return __ret;
02139     }
02140 
02141       template<typename _CharT2>
02142       typename __gnu_cxx::__enable_if<!__is_char<_CharT2>::__value,
02143                       int>::__type
02144     _M_find(const _CharT2* __zero, size_t __len, _CharT2 __c) const
02145     {
02146       int __ret = -1;
02147       const char_type* __q = char_traits<_CharT2>::find(__zero, __len, __c);
02148       if (__q)
02149         {
02150           __ret = __q - __zero;
02151           if (__ret > 15)
02152         __ret -= 6;
02153         }
02154       return __ret;
02155     }
02156 
02157       //@{
02158       /**
02159        *  @brief  Numeric parsing.
02160        *
02161        *  Parses the input stream into the variable @a v.  This function is a
02162        *  hook for derived classes to change the value returned.  @see get()
02163        *  for more details.
02164        *
02165        *  @param  __beg  Start of input stream.
02166        *  @param  __end  End of input stream.
02167        *  @param  __io  Source of locale and flags.
02168        *  @param  __err  Error flags to set.
02169        *  @param  __v  Value to format and insert.
02170        *  @return  Iterator after reading.
02171       */
02172       virtual iter_type
02173       do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const;
02174 
02175       virtual iter_type
02176       do_get(iter_type __beg, iter_type __end, ios_base& __io,
02177          ios_base::iostate& __err, long& __v) const
02178       { return _M_extract_int(__beg, __end, __io, __err, __v); }
02179 
02180       virtual iter_type
02181       do_get(iter_type __beg, iter_type __end, ios_base& __io,
02182          ios_base::iostate& __err, unsigned short& __v) const
02183       { return _M_extract_int(__beg, __end, __io, __err, __v); }
02184 
02185       virtual iter_type
02186       do_get(iter_type __beg, iter_type __end, ios_base& __io,
02187          ios_base::iostate& __err, unsigned int& __v) const
02188       { return _M_extract_int(__beg, __end, __io, __err, __v); }
02189 
02190       virtual iter_type
02191       do_get(iter_type __beg, iter_type __end, ios_base& __io,
02192          ios_base::iostate& __err, unsigned long& __v) const
02193       { return _M_extract_int(__beg, __end, __io, __err, __v); }
02194 
02195 #ifdef _GLIBCXX_USE_LONG_LONG
02196       virtual iter_type
02197       do_get(iter_type __beg, iter_type __end, ios_base& __io,
02198          ios_base::iostate& __err, long long& __v) const
02199       { return _M_extract_int(__beg, __end, __io, __err, __v); }
02200 
02201       virtual iter_type
02202       do_get(iter_type __beg, iter_type __end, ios_base& __io,
02203          ios_base::iostate& __err, unsigned long long& __v) const
02204       { return _M_extract_int(__beg, __end, __io, __err, __v); }
02205 #endif
02206 
02207       virtual iter_type
02208       do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, float&) const;
02209 
02210       virtual iter_type
02211       do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
02212          double&) const;
02213 
02214       // XXX GLIBCXX_ABI Deprecated
02215 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
02216       virtual iter_type
02217       __do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
02218            double&) const;
02219 #else
02220       virtual iter_type
02221       do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
02222          long double&) const;
02223 #endif
02224 
02225       virtual iter_type
02226       do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, void*&) const;
02227 
02228       // XXX GLIBCXX_ABI Deprecated
02229 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
02230       virtual iter_type
02231       do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
02232          long double&) const;
02233 #endif
02234       //@}
02235     };
02236 
02237   template<typename _CharT, typename _InIter>
02238     locale::id num_get<_CharT, _InIter>::id;
02239 
02240 
02241   /**
02242    *  @brief  Primary class template num_put.
02243    *  @ingroup locales
02244    *
02245    *  This facet encapsulates the code to convert a number to a string.  It is
02246    *  used by the ostream numeric insertion operators.
02247    *
02248    *  The num_put template uses protected virtual functions to provide the
02249    *  actual results.  The public accessors forward the call to the virtual
02250    *  functions.  These virtual functions are hooks for developers to
02251    *  implement the behavior they require from the num_put facet.
02252   */
02253   template<typename _CharT, typename _OutIter>
02254     class num_put : public locale::facet
02255     {
02256     public:
02257       // Types:
02258       //@{
02259       /// Public typedefs
02260       typedef _CharT        char_type;
02261       typedef _OutIter      iter_type;
02262       //@}
02263 
02264       /// Numpunct facet id.
02265       static locale::id     id;
02266 
02267       /**
02268        *  @brief  Constructor performs initialization.
02269        *
02270        *  This is the constructor provided by the standard.
02271        *
02272        *  @param __refs  Passed to the base facet class.
02273       */
02274       explicit
02275       num_put(size_t __refs = 0) : facet(__refs) { }
02276 
02277       /**
02278        *  @brief  Numeric formatting.
02279        *
02280        *  Formats the boolean @a v and inserts it into a stream.  It does so
02281        *  by calling num_put::do_put().
02282        *
02283        *  If ios_base::boolalpha is set, writes ctype<CharT>::truename() or
02284        *  ctype<CharT>::falsename().  Otherwise formats @a v as an int.
02285        *
02286        *  @param  __s  Stream to write to.
02287        *  @param  __io  Source of locale and flags.
02288        *  @param  __fill  Char_type to use for filling.
02289        *  @param  __v  Value to format and insert.
02290        *  @return  Iterator after writing.
02291       */
02292       iter_type
02293       put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
02294       { return this->do_put(__s, __io, __fill, __v); }
02295 
02296       //@{
02297       /**
02298        *  @brief  Numeric formatting.
02299        *
02300        *  Formats the integral value @a v and inserts it into a
02301        *  stream.  It does so by calling num_put::do_put().
02302        *
02303        *  Formatting is affected by the flag settings in @a io.
02304        *
02305        *  The basic format is affected by the value of io.flags() &
02306        *  ios_base::basefield.  If equal to ios_base::oct, formats like the
02307        *  printf %o specifier.  Else if equal to ios_base::hex, formats like
02308        *  %x or %X with ios_base::uppercase unset or set respectively.
02309        *  Otherwise, formats like %d, %ld, %lld for signed and %u, %lu, %llu
02310        *  for unsigned values.  Note that if both oct and hex are set, neither
02311        *  will take effect.
02312        *
02313        *  If ios_base::showpos is set, '+' is output before positive values.
02314        *  If ios_base::showbase is set, '0' precedes octal values (except 0)
02315        *  and '0[xX]' precedes hex values.
02316        *
02317        *  The decimal point character used is numpunct::decimal_point().
02318        *  Thousands separators are inserted according to
02319        *  numpunct::grouping() and numpunct::thousands_sep().
02320        *
02321        *  If io.width() is non-zero, enough @a fill characters are inserted to
02322        *  make the result at least that wide.  If
02323        *  (io.flags() & ios_base::adjustfield) == ios_base::left, result is
02324        *  padded at the end.  If ios_base::internal, then padding occurs
02325        *  immediately after either a '+' or '-' or after '0x' or '0X'.
02326        *  Otherwise, padding occurs at the beginning.
02327        *
02328        *  @param  __s  Stream to write to.
02329        *  @param  __io  Source of locale and flags.
02330        *  @param  __fill  Char_type to use for filling.
02331        *  @param  __v  Value to format and insert.
02332        *  @return  Iterator after writing.
02333       */
02334       iter_type
02335       put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
02336       { return this->do_put(__s, __io, __fill, __v); }
02337 
02338       iter_type
02339       put(iter_type __s, ios_base& __io, char_type __fill,
02340       unsigned long __v) const
02341       { return this->do_put(__s, __io, __fill, __v); }
02342 
02343 #ifdef _GLIBCXX_USE_LONG_LONG
02344       iter_type
02345       put(iter_type __s, ios_base& __io, char_type __fill, long long __v) const
02346       { return this->do_put(__s, __io, __fill, __v); }
02347 
02348       iter_type
02349       put(iter_type __s, ios_base& __io, char_type __fill,
02350       unsigned long long __v) const
02351       { return this->do_put(__s, __io, __fill, __v); }
02352 #endif
02353       //@}
02354 
02355       //@{
02356       /**
02357        *  @brief  Numeric formatting.
02358        *
02359        *  Formats the floating point value @a v and inserts it into a stream.
02360        *  It does so by calling num_put::do_put().
02361        *
02362        *  Formatting is affected by the flag settings in @a io.
02363        *
02364        *  The basic format is affected by the value of io.flags() &
02365        *  ios_base::floatfield.  If equal to ios_base::fixed, formats like the
02366        *  printf %f specifier.  Else if equal to ios_base::scientific, formats
02367        *  like %e or %E with ios_base::uppercase unset or set respectively.
02368        *  Otherwise, formats like %g or %G depending on uppercase.  Note that
02369        *  if both fixed and scientific are set, the effect will also be like
02370        *  %g or %G.
02371        *
02372        *  The output precision is given by io.precision().  This precision is
02373        *  capped at numeric_limits::digits10 + 2 (different for double and
02374        *  long double).  The default precision is 6.
02375        *
02376        *  If ios_base::showpos is set, '+' is output before positive values.
02377        *  If ios_base::showpoint is set, a decimal point will always be
02378        *  output.
02379        *
02380        *  The decimal point character used is numpunct::decimal_point().
02381        *  Thousands separators are inserted according to
02382        *  numpunct::grouping() and numpunct::thousands_sep().
02383        *
02384        *  If io.width() is non-zero, enough @a fill characters are inserted to
02385        *  make the result at least that wide.  If
02386        *  (io.flags() & ios_base::adjustfield) == ios_base::left, result is
02387        *  padded at the end.  If ios_base::internal, then padding occurs
02388        *  immediately after either a '+' or '-' or after '0x' or '0X'.
02389        *  Otherwise, padding occurs at the beginning.
02390        *
02391        *  @param  __s  Stream to write to.
02392        *  @param  __io  Source of locale and flags.
02393        *  @param  __fill  Char_type to use for filling.
02394        *  @param  __v  Value to format and insert.
02395        *  @return  Iterator after writing.
02396       */
02397       iter_type
02398       put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
02399       { return this->do_put(__s, __io, __fill, __v); }
02400 
02401       iter_type
02402       put(iter_type __s, ios_base& __io, char_type __fill,
02403       long double __v) const
02404       { return this->do_put(__s, __io, __fill, __v); }
02405       //@}
02406 
02407       /**
02408        *  @brief  Numeric formatting.
02409        *
02410        *  Formats the pointer value @a v and inserts it into a stream.  It
02411        *  does so by calling num_put::do_put().
02412        *
02413        *  This function formats @a v as an unsigned long with ios_base::hex
02414        *  and ios_base::showbase set.
02415        *
02416        *  @param  __s  Stream to write to.
02417        *  @param  __io  Source of locale and flags.
02418        *  @param  __fill  Char_type to use for filling.
02419        *  @param  __v  Value to format and insert.
02420        *  @return  Iterator after writing.
02421       */
02422       iter_type
02423       put(iter_type __s, ios_base& __io, char_type __fill,
02424       const void* __v) const
02425       { return this->do_put(__s, __io, __fill, __v); }
02426 
02427     protected:
02428       template<typename _ValueT>
02429     iter_type
02430     _M_insert_float(iter_type, ios_base& __io, char_type __fill,
02431             char __mod, _ValueT __v) const;
02432 
02433       void
02434       _M_group_float(const char* __grouping, size_t __grouping_size,
02435              char_type __sep, const char_type* __p, char_type* __new,
02436              char_type* __cs, int& __len) const;
02437 
02438       template<typename _ValueT>
02439     iter_type
02440     _M_insert_int(iter_type, ios_base& __io, char_type __fill,
02441               _ValueT __v) const;
02442 
02443       void
02444       _M_group_int(const char* __grouping, size_t __grouping_size,
02445            char_type __sep, ios_base& __io, char_type* __new,
02446            char_type* __cs, int& __len) const;
02447 
02448       void
02449       _M_pad(char_type __fill, streamsize __w, ios_base& __io,
02450          char_type* __new, const char_type* __cs, int& __len) const;
02451 
02452       /// Destructor.
02453       virtual
02454       ~num_put() { };
02455 
02456       //@{
02457       /**
02458        *  @brief  Numeric formatting.
02459        *
02460        *  These functions do the work of formatting numeric values and
02461        *  inserting them into a stream. This function is a hook for derived
02462        *  classes to change the value returned.
02463        *
02464        *  @param  __s  Stream to write to.
02465        *  @param  __io  Source of locale and flags.
02466        *  @param  __fill  Char_type to use for filling.
02467        *  @param  __v  Value to format and insert.
02468        *  @return  Iterator after writing.
02469       */
02470       virtual iter_type
02471       do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const;
02472 
02473       virtual iter_type
02474       do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
02475       { return _M_insert_int(__s, __io, __fill, __v); }
02476 
02477       virtual iter_type
02478       do_put(iter_type __s, ios_base& __io, char_type __fill,
02479          unsigned long __v) const
02480       { return _M_insert_int(__s, __io, __fill, __v); }
02481 
02482 #ifdef _GLIBCXX_USE_LONG_LONG
02483       virtual iter_type
02484       do_put(iter_type __s, ios_base& __io, char_type __fill,
02485          long long __v) const
02486       { return _M_insert_int(__s, __io, __fill, __v); }
02487 
02488       virtual iter_type
02489       do_put(iter_type __s, ios_base& __io, char_type __fill,
02490          unsigned long long __v) const
02491       { return _M_insert_int(__s, __io, __fill, __v); }
02492 #endif
02493 
02494       virtual iter_type
02495       do_put(iter_type, ios_base&, char_type, double) const;
02496 
02497       // XXX GLIBCXX_ABI Deprecated
02498 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
02499       virtual iter_type
02500       __do_put(iter_type, ios_base&, char_type, double) const;
02501 #else
02502       virtual iter_type
02503       do_put(iter_type, ios_base&, char_type, long double) const;
02504 #endif
02505 
02506       virtual iter_type
02507       do_put(iter_type, ios_base&, char_type, const void*) const;
02508 
02509       // XXX GLIBCXX_ABI Deprecated
02510 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
02511       virtual iter_type
02512       do_put(iter_type, ios_base&, char_type, long double) const;
02513 #endif
02514       //@}
02515     };
02516 
02517   template <typename _CharT, typename _OutIter>
02518     locale::id num_put<_CharT, _OutIter>::id;
02519 
02520 _GLIBCXX_END_NAMESPACE_LDBL
02521 
02522   // Subclause convenience interfaces, inlines.
02523   // NB: These are inline because, when used in a loop, some compilers
02524   // can hoist the body out of the loop; then it's just as fast as the
02525   // C is*() function.
02526 
02527   /// Convenience interface to ctype.is(ctype_base::space, __c).
02528   template<typename _CharT>
02529     inline bool
02530     isspace(_CharT __c, const locale& __loc)
02531     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); }
02532 
02533   /// Convenience interface to ctype.is(ctype_base::print, __c).
02534   template<typename _CharT>
02535     inline bool
02536     isprint(_CharT __c, const locale& __loc)
02537     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); }
02538 
02539   /// Convenience interface to ctype.is(ctype_base::cntrl, __c).
02540   template<typename _CharT>
02541     inline bool
02542     iscntrl(_CharT __c, const locale& __loc)
02543     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); }
02544 
02545   /// Convenience interface to ctype.is(ctype_base::upper, __c).
02546   template<typename _CharT>
02547     inline bool
02548     isupper(_CharT __c, const locale& __loc)
02549     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); }
02550 
02551   /// Convenience interface to ctype.is(ctype_base::lower, __c).
02552   template<typename _CharT>
02553     inline bool
02554     islower(_CharT __c, const locale& __loc)
02555     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); }
02556 
02557   /// Convenience interface to ctype.is(ctype_base::alpha, __c).
02558   template<typename _CharT>
02559     inline bool
02560     isalpha(_CharT __c, const locale& __loc)
02561     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); }
02562 
02563   /// Convenience interface to ctype.is(ctype_base::digit, __c).
02564   template<typename _CharT>
02565     inline bool
02566     isdigit(_CharT __c, const locale& __loc)
02567     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); }
02568 
02569   /// Convenience interface to ctype.is(ctype_base::punct, __c).
02570   template<typename _CharT>
02571     inline bool
02572     ispunct(_CharT __c, const locale& __loc)
02573     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); }
02574 
02575   /// Convenience interface to ctype.is(ctype_base::xdigit, __c).
02576   template<typename _CharT>
02577     inline bool
02578     isxdigit(_CharT __c, const locale& __loc)
02579     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); }
02580 
02581   /// Convenience interface to ctype.is(ctype_base::alnum, __c).
02582   template<typename _CharT>
02583     inline bool
02584     isalnum(_CharT __c, const locale& __loc)
02585     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); }
02586 
02587   /// Convenience interface to ctype.is(ctype_base::graph, __c).
02588   template<typename _CharT>
02589     inline bool
02590     isgraph(_CharT __c, const locale& __loc)
02591     { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); }
02592 
02593   /// Convenience interface to ctype.toupper(__c).
02594   template<typename _CharT>
02595     inline _CharT
02596     toupper(_CharT __c, const locale& __loc)
02597     { return use_facet<ctype<_CharT> >(__loc).toupper(__c); }
02598 
02599   /// Convenience interface to ctype.tolower(__c).
02600   template<typename _CharT>
02601     inline _CharT
02602     tolower(_CharT __c, const locale& __loc)
02603     { return use_facet<ctype<_CharT> >(__loc).tolower(__c); }
02604 
02605 _GLIBCXX_END_NAMESPACE_VERSION
02606 } // namespace std
02607 
02608 # include <bits/locale_facets.tcc>
02609 
02610 #endif