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