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