libstdc++
|
00001 // Locale support -*- C++ -*- 00002 00003 // Copyright (C) 1997-2019 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_CHAR8_T 00063 # define _GLIBCXX_NUM_UNICODE_FACETS 4 00064 #else 00065 # define _GLIBCXX_NUM_UNICODE_FACETS 2 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 if (__builtin_expect(__hi != __lo, true)) 00904 __builtin_memcpy(__to, __lo, __hi - __lo); 00905 return __hi; 00906 } 00907 if (!_M_widen_ok) 00908 _M_widen_init(); 00909 return this->do_widen(__lo, __hi, __to); 00910 } 00911 00912 /** 00913 * @brief Narrow char 00914 * 00915 * This function converts the char to char using the simplest 00916 * reasonable transformation. If the conversion fails, dfault is 00917 * returned instead. For an underived ctype<char> facet, @a c 00918 * will be returned unchanged. 00919 * 00920 * This function works as if it returns ctype<char>::do_narrow(c). 00921 * do_narrow() must always return the same result for the same input. 00922 * 00923 * Note: this is not what you want for codepage conversions. See 00924 * codecvt for that. 00925 * 00926 * @param __c The char to convert. 00927 * @param __dfault Char to return if conversion fails. 00928 * @return The converted character. 00929 */ 00930 char 00931 narrow(char_type __c, char __dfault) const 00932 { 00933 if (_M_narrow[static_cast<unsigned char>(__c)]) 00934 return _M_narrow[static_cast<unsigned char>(__c)]; 00935 const char __t = do_narrow(__c, __dfault); 00936 if (__t != __dfault) 00937 _M_narrow[static_cast<unsigned char>(__c)] = __t; 00938 return __t; 00939 } 00940 00941 /** 00942 * @brief Narrow char array 00943 * 00944 * This function converts each char in the input to char using the 00945 * simplest reasonable transformation and writes the results to the 00946 * destination array. For any char in the input that cannot be 00947 * converted, @a dfault is used instead. For an underived ctype<char> 00948 * facet, the argument will be copied unchanged. 00949 * 00950 * This function works as if it returns ctype<char>::do_narrow(lo, hi, 00951 * dfault, to). do_narrow() must always return the same result for the 00952 * same input. 00953 * 00954 * Note: this is not what you want for codepage conversions. See 00955 * codecvt for that. 00956 * 00957 * @param __lo Pointer to start of range. 00958 * @param __hi Pointer to end of range. 00959 * @param __dfault Char to use if conversion fails. 00960 * @param __to Pointer to the destination array. 00961 * @return @a __hi. 00962 */ 00963 const char_type* 00964 narrow(const char_type* __lo, const char_type* __hi, 00965 char __dfault, char* __to) const 00966 { 00967 if (__builtin_expect(_M_narrow_ok == 1, true)) 00968 { 00969 if (__builtin_expect(__hi != __lo, true)) 00970 __builtin_memcpy(__to, __lo, __hi - __lo); 00971 return __hi; 00972 } 00973 if (!_M_narrow_ok) 00974 _M_narrow_init(); 00975 return this->do_narrow(__lo, __hi, __dfault, __to); 00976 } 00977 00978 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00979 // DR 695. ctype<char>::classic_table() not accessible. 00980 /// Returns a pointer to the mask table provided to the constructor, or 00981 /// the default from classic_table() if none was provided. 00982 const mask* 00983 table() const throw() 00984 { return _M_table; } 00985 00986 /// Returns a pointer to the C locale mask table. 00987 static const mask* 00988 classic_table() throw(); 00989 protected: 00990 00991 /** 00992 * @brief Destructor. 00993 * 00994 * This function deletes table() if @a del was true in the 00995 * constructor. 00996 */ 00997 virtual 00998 ~ctype(); 00999 01000 /** 01001 * @brief Convert to uppercase. 01002 * 01003 * This virtual function converts the char argument to uppercase if 01004 * possible. If not possible (for example, '2'), returns the argument. 01005 * 01006 * do_toupper() is a hook for a derived facet to change the behavior of 01007 * uppercasing. do_toupper() must always return the same result for 01008 * the same input. 01009 * 01010 * @param __c The char to convert. 01011 * @return The uppercase char if convertible, else @a __c. 01012 */ 01013 virtual char_type 01014 do_toupper(char_type __c) const; 01015 01016 /** 01017 * @brief Convert array to uppercase. 01018 * 01019 * This virtual function converts each char in the range [lo,hi) to 01020 * uppercase if possible. Other chars remain untouched. 01021 * 01022 * do_toupper() is a hook for a derived facet to change the behavior of 01023 * uppercasing. do_toupper() must always return the same result for 01024 * the same input. 01025 * 01026 * @param __lo Pointer to start of range. 01027 * @param __hi Pointer to end of range. 01028 * @return @a __hi. 01029 */ 01030 virtual const char_type* 01031 do_toupper(char_type* __lo, const char_type* __hi) const; 01032 01033 /** 01034 * @brief Convert to lowercase. 01035 * 01036 * This virtual function converts the char argument to lowercase if 01037 * possible. If not possible (for example, '2'), returns the argument. 01038 * 01039 * do_tolower() is a hook for a derived facet to change the behavior of 01040 * lowercasing. do_tolower() must always return the same result for 01041 * the same input. 01042 * 01043 * @param __c The char to convert. 01044 * @return The lowercase char if convertible, else @a __c. 01045 */ 01046 virtual char_type 01047 do_tolower(char_type __c) const; 01048 01049 /** 01050 * @brief Convert array to lowercase. 01051 * 01052 * This virtual function converts each char in the range [lo,hi) to 01053 * lowercase if possible. Other chars remain untouched. 01054 * 01055 * do_tolower() is a hook for a derived facet to change the behavior of 01056 * lowercasing. do_tolower() must always return the same result for 01057 * the same input. 01058 * 01059 * @param __lo Pointer to first char in range. 01060 * @param __hi Pointer to end of range. 01061 * @return @a __hi. 01062 */ 01063 virtual const char_type* 01064 do_tolower(char_type* __lo, const char_type* __hi) const; 01065 01066 /** 01067 * @brief Widen char 01068 * 01069 * This virtual function converts the char to char using the simplest 01070 * reasonable transformation. For an underived ctype<char> facet, the 01071 * argument will be returned unchanged. 01072 * 01073 * do_widen() is a hook for a derived facet to change the behavior of 01074 * widening. do_widen() must always return the same result for the 01075 * same input. 01076 * 01077 * Note: this is not what you want for codepage conversions. See 01078 * codecvt for that. 01079 * 01080 * @param __c The char to convert. 01081 * @return The converted character. 01082 */ 01083 virtual char_type 01084 do_widen(char __c) const 01085 { return __c; } 01086 01087 /** 01088 * @brief Widen char array 01089 * 01090 * This function converts each char in the range [lo,hi) to char using 01091 * the simplest reasonable transformation. For an underived 01092 * ctype<char> facet, the argument will be copied unchanged. 01093 * 01094 * do_widen() is a hook for a derived facet to change the behavior of 01095 * widening. do_widen() must always return the same result for the 01096 * same input. 01097 * 01098 * Note: this is not what you want for codepage conversions. See 01099 * codecvt for that. 01100 * 01101 * @param __lo Pointer to start of range. 01102 * @param __hi Pointer to end of range. 01103 * @param __to Pointer to the destination array. 01104 * @return @a __hi. 01105 */ 01106 virtual const char* 01107 do_widen(const char* __lo, const char* __hi, char_type* __to) const 01108 { 01109 if (__builtin_expect(__hi != __lo, true)) 01110 __builtin_memcpy(__to, __lo, __hi - __lo); 01111 return __hi; 01112 } 01113 01114 /** 01115 * @brief Narrow char 01116 * 01117 * This virtual function converts the char to char using the simplest 01118 * reasonable transformation. If the conversion fails, dfault is 01119 * returned instead. For an underived ctype<char> facet, @a c will be 01120 * returned unchanged. 01121 * 01122 * do_narrow() is a hook for a derived facet to change the behavior of 01123 * narrowing. do_narrow() must always return the same result for the 01124 * same input. 01125 * 01126 * Note: this is not what you want for codepage conversions. See 01127 * codecvt for that. 01128 * 01129 * @param __c The char to convert. 01130 * @param __dfault Char to return if conversion fails. 01131 * @return The converted char. 01132 */ 01133 virtual char 01134 do_narrow(char_type __c, char __dfault __attribute__((__unused__))) const 01135 { return __c; } 01136 01137 /** 01138 * @brief Narrow char array to char array 01139 * 01140 * This virtual function converts each char in the range [lo,hi) to 01141 * char using the simplest reasonable transformation and writes the 01142 * results to the destination array. For any char in the input that 01143 * cannot be converted, @a dfault is used instead. For an underived 01144 * ctype<char> facet, the argument will be copied unchanged. 01145 * 01146 * do_narrow() is a hook for a derived facet to change the behavior of 01147 * narrowing. do_narrow() must always return the same result for the 01148 * same input. 01149 * 01150 * Note: this is not what you want for codepage conversions. See 01151 * codecvt for that. 01152 * 01153 * @param __lo Pointer to start of range. 01154 * @param __hi Pointer to end of range. 01155 * @param __dfault Char to use if conversion fails. 01156 * @param __to Pointer to the destination array. 01157 * @return @a __hi. 01158 */ 01159 virtual const char_type* 01160 do_narrow(const char_type* __lo, const char_type* __hi, 01161 char __dfault __attribute__((__unused__)), char* __to) const 01162 { 01163 if (__builtin_expect(__hi != __lo, true)) 01164 __builtin_memcpy(__to, __lo, __hi - __lo); 01165 return __hi; 01166 } 01167 01168 private: 01169 void _M_narrow_init() const; 01170 void _M_widen_init() const; 01171 }; 01172 01173 #ifdef _GLIBCXX_USE_WCHAR_T 01174 /** 01175 * @brief The ctype<wchar_t> specialization. 01176 * @ingroup locales 01177 * 01178 * This class defines classification and conversion functions for the 01179 * wchar_t type. It gets used by wchar_t streams for many I/O operations. 01180 * The wchar_t specialization provides a number of optimizations as well. 01181 * 01182 * ctype<wchar_t> inherits its public methods from 01183 * __ctype_abstract_base<wchar_t>. 01184 */ 01185 template<> 01186 class ctype<wchar_t> : public __ctype_abstract_base<wchar_t> 01187 { 01188 public: 01189 // Types: 01190 /// Typedef for the template parameter wchar_t. 01191 typedef wchar_t char_type; 01192 typedef wctype_t __wmask_type; 01193 01194 protected: 01195 __c_locale _M_c_locale_ctype; 01196 01197 // Pre-computed narrowed and widened chars. 01198 bool _M_narrow_ok; 01199 char _M_narrow[128]; 01200 wint_t _M_widen[1 + static_cast<unsigned char>(-1)]; 01201 01202 // Pre-computed elements for do_is. 01203 mask _M_bit[16]; 01204 __wmask_type _M_wmask[16]; 01205 01206 public: 01207 // Data Members: 01208 /// The facet id for ctype<wchar_t> 01209 static locale::id id; 01210 01211 /** 01212 * @brief Constructor performs initialization. 01213 * 01214 * This is the constructor provided by the standard. 01215 * 01216 * @param __refs Passed to the base facet class. 01217 */ 01218 explicit 01219 ctype(size_t __refs = 0); 01220 01221 /** 01222 * @brief Constructor performs static initialization. 01223 * 01224 * This constructor is used to construct the initial C locale facet. 01225 * 01226 * @param __cloc Handle to C locale data. 01227 * @param __refs Passed to the base facet class. 01228 */ 01229 explicit 01230 ctype(__c_locale __cloc, size_t __refs = 0); 01231 01232 protected: 01233 __wmask_type 01234 _M_convert_to_wmask(const mask __m) const throw(); 01235 01236 /// Destructor 01237 virtual 01238 ~ctype(); 01239 01240 /** 01241 * @brief Test wchar_t classification. 01242 * 01243 * This function finds a mask M for @a c and compares it to mask @a m. 01244 * 01245 * do_is() is a hook for a derived facet to change the behavior of 01246 * classifying. do_is() must always return the same result for the 01247 * same input. 01248 * 01249 * @param __c The wchar_t to find the mask of. 01250 * @param __m The mask to compare against. 01251 * @return (M & __m) != 0. 01252 */ 01253 virtual bool 01254 do_is(mask __m, char_type __c) const; 01255 01256 /** 01257 * @brief Return a mask array. 01258 * 01259 * This function finds the mask for each wchar_t in the range [lo,hi) 01260 * and successively writes it to vec. vec must have as many elements 01261 * as the input. 01262 * 01263 * do_is() is a hook for a derived facet to change the behavior of 01264 * classifying. do_is() must always return the same result for the 01265 * same input. 01266 * 01267 * @param __lo Pointer to start of range. 01268 * @param __hi Pointer to end of range. 01269 * @param __vec Pointer to an array of mask storage. 01270 * @return @a __hi. 01271 */ 01272 virtual const char_type* 01273 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const; 01274 01275 /** 01276 * @brief Find wchar_t matching mask 01277 * 01278 * This function searches for and returns the first wchar_t c in 01279 * [__lo,__hi) for which is(__m,c) is true. 01280 * 01281 * do_scan_is() is a hook for a derived facet to change the behavior of 01282 * match searching. do_is() must always return the same result for the 01283 * same input. 01284 * 01285 * @param __m The mask to compare against. 01286 * @param __lo Pointer to start of range. 01287 * @param __hi Pointer to end of range. 01288 * @return Pointer to a matching wchar_t if found, else @a __hi. 01289 */ 01290 virtual const char_type* 01291 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const; 01292 01293 /** 01294 * @brief Find wchar_t not matching mask 01295 * 01296 * This function searches for and returns a pointer to the first 01297 * wchar_t c of [__lo,__hi) for which is(__m,c) is false. 01298 * 01299 * do_scan_is() is a hook for a derived facet to change the behavior of 01300 * match searching. do_is() must always return the same result for the 01301 * same input. 01302 * 01303 * @param __m The mask to compare against. 01304 * @param __lo Pointer to start of range. 01305 * @param __hi Pointer to end of range. 01306 * @return Pointer to a non-matching wchar_t if found, else @a __hi. 01307 */ 01308 virtual const char_type* 01309 do_scan_not(mask __m, const char_type* __lo, 01310 const char_type* __hi) const; 01311 01312 /** 01313 * @brief Convert to uppercase. 01314 * 01315 * This virtual function converts the wchar_t argument to uppercase if 01316 * possible. If not possible (for example, '2'), returns the argument. 01317 * 01318 * do_toupper() is a hook for a derived facet to change the behavior of 01319 * uppercasing. do_toupper() must always return the same result for 01320 * the same input. 01321 * 01322 * @param __c The wchar_t to convert. 01323 * @return The uppercase wchar_t if convertible, else @a __c. 01324 */ 01325 virtual char_type 01326 do_toupper(char_type __c) const; 01327 01328 /** 01329 * @brief Convert array to uppercase. 01330 * 01331 * This virtual function converts each wchar_t in the range [lo,hi) to 01332 * uppercase if possible. Other elements remain untouched. 01333 * 01334 * do_toupper() is a hook for a derived facet to change the behavior of 01335 * uppercasing. do_toupper() must always return the same result for 01336 * the same input. 01337 * 01338 * @param __lo Pointer to start of range. 01339 * @param __hi Pointer to end of range. 01340 * @return @a __hi. 01341 */ 01342 virtual const char_type* 01343 do_toupper(char_type* __lo, const char_type* __hi) const; 01344 01345 /** 01346 * @brief Convert to lowercase. 01347 * 01348 * This virtual function converts the argument to lowercase if 01349 * possible. If not possible (for example, '2'), returns the argument. 01350 * 01351 * do_tolower() is a hook for a derived facet to change the behavior of 01352 * lowercasing. do_tolower() must always return the same result for 01353 * the same input. 01354 * 01355 * @param __c The wchar_t to convert. 01356 * @return The lowercase wchar_t if convertible, else @a __c. 01357 */ 01358 virtual char_type 01359 do_tolower(char_type __c) const; 01360 01361 /** 01362 * @brief Convert array to lowercase. 01363 * 01364 * This virtual function converts each wchar_t in the range [lo,hi) to 01365 * lowercase if possible. Other elements remain untouched. 01366 * 01367 * do_tolower() is a hook for a derived facet to change the behavior of 01368 * lowercasing. do_tolower() must always return the same result for 01369 * the same input. 01370 * 01371 * @param __lo Pointer to start of range. 01372 * @param __hi Pointer to end of range. 01373 * @return @a __hi. 01374 */ 01375 virtual const char_type* 01376 do_tolower(char_type* __lo, const char_type* __hi) const; 01377 01378 /** 01379 * @brief Widen char to wchar_t 01380 * 01381 * This virtual function converts the char to wchar_t using the 01382 * simplest reasonable transformation. For an underived ctype<wchar_t> 01383 * facet, the argument will be cast to wchar_t. 01384 * 01385 * do_widen() is a hook for a derived facet to change the behavior of 01386 * widening. do_widen() must always return the same result for the 01387 * same input. 01388 * 01389 * Note: this is not what you want for codepage conversions. See 01390 * codecvt for that. 01391 * 01392 * @param __c The char to convert. 01393 * @return The converted wchar_t. 01394 */ 01395 virtual char_type 01396 do_widen(char __c) const; 01397 01398 /** 01399 * @brief Widen char array to wchar_t array 01400 * 01401 * This function converts each char in the input to wchar_t using the 01402 * simplest reasonable transformation. For an underived ctype<wchar_t> 01403 * facet, the argument will be copied, casting each element to wchar_t. 01404 * 01405 * do_widen() is a hook for a derived facet to change the behavior of 01406 * widening. do_widen() must always return the same result for the 01407 * same input. 01408 * 01409 * Note: this is not what you want for codepage conversions. See 01410 * codecvt for that. 01411 * 01412 * @param __lo Pointer to start range. 01413 * @param __hi Pointer to end of range. 01414 * @param __to Pointer to the destination array. 01415 * @return @a __hi. 01416 */ 01417 virtual const char* 01418 do_widen(const char* __lo, const char* __hi, char_type* __to) const; 01419 01420 /** 01421 * @brief Narrow wchar_t to char 01422 * 01423 * This virtual function converts the argument to char using 01424 * the simplest reasonable transformation. If the conversion 01425 * fails, dfault is returned instead. For an underived 01426 * ctype<wchar_t> facet, @a c will be cast to char and 01427 * returned. 01428 * 01429 * do_narrow() is a hook for a derived facet to change the 01430 * behavior of narrowing. do_narrow() must always return the 01431 * same result for the same input. 01432 * 01433 * Note: this is not what you want for codepage conversions. See 01434 * codecvt for that. 01435 * 01436 * @param __c The wchar_t to convert. 01437 * @param __dfault Char to return if conversion fails. 01438 * @return The converted char. 01439 */ 01440 virtual char 01441 do_narrow(char_type __c, char __dfault) const; 01442 01443 /** 01444 * @brief Narrow wchar_t array to char array 01445 * 01446 * This virtual function converts each wchar_t in the range [lo,hi) to 01447 * char using the simplest reasonable transformation and writes the 01448 * results to the destination array. For any wchar_t in the input that 01449 * cannot be converted, @a dfault is used instead. For an underived 01450 * ctype<wchar_t> facet, the argument will be copied, casting each 01451 * element to char. 01452 * 01453 * do_narrow() is a hook for a derived facet to change the behavior of 01454 * narrowing. do_narrow() must always return the same result for the 01455 * same input. 01456 * 01457 * Note: this is not what you want for codepage conversions. See 01458 * codecvt for that. 01459 * 01460 * @param __lo Pointer to start of range. 01461 * @param __hi Pointer to end of range. 01462 * @param __dfault Char to use if conversion fails. 01463 * @param __to Pointer to the destination array. 01464 * @return @a __hi. 01465 */ 01466 virtual const char_type* 01467 do_narrow(const char_type* __lo, const char_type* __hi, 01468 char __dfault, char* __to) const; 01469 01470 // For use at construction time only. 01471 void 01472 _M_initialize_ctype() throw(); 01473 }; 01474 #endif //_GLIBCXX_USE_WCHAR_T 01475 01476 /// class ctype_byname [22.2.1.2]. 01477 template<typename _CharT> 01478 class ctype_byname : public ctype<_CharT> 01479 { 01480 public: 01481 typedef typename ctype<_CharT>::mask mask; 01482 01483 explicit 01484 ctype_byname(const char* __s, size_t __refs = 0); 01485 01486 #if __cplusplus >= 201103L 01487 explicit 01488 ctype_byname(const string& __s, size_t __refs = 0) 01489 : ctype_byname(__s.c_str(), __refs) { } 01490 #endif 01491 01492 protected: 01493 virtual 01494 ~ctype_byname() { } 01495 }; 01496 01497 /// 22.2.1.4 Class ctype_byname specializations. 01498 template<> 01499 class ctype_byname<char> : public ctype<char> 01500 { 01501 public: 01502 explicit 01503 ctype_byname(const char* __s, size_t __refs = 0); 01504 01505 #if __cplusplus >= 201103L 01506 explicit 01507 ctype_byname(const string& __s, size_t __refs = 0); 01508 #endif 01509 01510 protected: 01511 virtual 01512 ~ctype_byname(); 01513 }; 01514 01515 #ifdef _GLIBCXX_USE_WCHAR_T 01516 template<> 01517 class ctype_byname<wchar_t> : public ctype<wchar_t> 01518 { 01519 public: 01520 explicit 01521 ctype_byname(const char* __s, size_t __refs = 0); 01522 01523 #if __cplusplus >= 201103L 01524 explicit 01525 ctype_byname(const string& __s, size_t __refs = 0); 01526 #endif 01527 01528 protected: 01529 virtual 01530 ~ctype_byname(); 01531 }; 01532 #endif 01533 01534 _GLIBCXX_END_NAMESPACE_VERSION 01535 } // namespace 01536 01537 // Include host and configuration specific ctype inlines. 01538 #include <bits/ctype_inline.h> 01539 01540 namespace std _GLIBCXX_VISIBILITY(default) 01541 { 01542 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01543 01544 // 22.2.2 The numeric category. 01545 class __num_base 01546 { 01547 public: 01548 // NB: Code depends on the order of _S_atoms_out elements. 01549 // Below are the indices into _S_atoms_out. 01550 enum 01551 { 01552 _S_ominus, 01553 _S_oplus, 01554 _S_ox, 01555 _S_oX, 01556 _S_odigits, 01557 _S_odigits_end = _S_odigits + 16, 01558 _S_oudigits = _S_odigits_end, 01559 _S_oudigits_end = _S_oudigits + 16, 01560 _S_oe = _S_odigits + 14, // For scientific notation, 'e' 01561 _S_oE = _S_oudigits + 14, // For scientific notation, 'E' 01562 _S_oend = _S_oudigits_end 01563 }; 01564 01565 // A list of valid numeric literals for output. This array 01566 // contains chars that will be passed through the current locale's 01567 // ctype<_CharT>.widen() and then used to render numbers. 01568 // For the standard "C" locale, this is 01569 // "-+xX0123456789abcdef0123456789ABCDEF". 01570 static const char* _S_atoms_out; 01571 01572 // String literal of acceptable (narrow) input, for num_get. 01573 // "-+xX0123456789abcdefABCDEF" 01574 static const char* _S_atoms_in; 01575 01576 enum 01577 { 01578 _S_iminus, 01579 _S_iplus, 01580 _S_ix, 01581 _S_iX, 01582 _S_izero, 01583 _S_ie = _S_izero + 14, 01584 _S_iE = _S_izero + 20, 01585 _S_iend = 26 01586 }; 01587 01588 // num_put 01589 // Construct and return valid scanf format for floating point types. 01590 static void 01591 _S_format_float(const ios_base& __io, char* __fptr, char __mod) throw(); 01592 }; 01593 01594 template<typename _CharT> 01595 struct __numpunct_cache : public locale::facet 01596 { 01597 const char* _M_grouping; 01598 size_t _M_grouping_size; 01599 bool _M_use_grouping; 01600 const _CharT* _M_truename; 01601 size_t _M_truename_size; 01602 const _CharT* _M_falsename; 01603 size_t _M_falsename_size; 01604 _CharT _M_decimal_point; 01605 _CharT _M_thousands_sep; 01606 01607 // A list of valid numeric literals for output: in the standard 01608 // "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF". 01609 // This array contains the chars after having been passed 01610 // through the current locale's ctype<_CharT>.widen(). 01611 _CharT _M_atoms_out[__num_base::_S_oend]; 01612 01613 // A list of valid numeric literals for input: in the standard 01614 // "C" locale, this is "-+xX0123456789abcdefABCDEF" 01615 // This array contains the chars after having been passed 01616 // through the current locale's ctype<_CharT>.widen(). 01617 _CharT _M_atoms_in[__num_base::_S_iend]; 01618 01619 bool _M_allocated; 01620 01621 __numpunct_cache(size_t __refs = 0) 01622 : facet(__refs), _M_grouping(0), _M_grouping_size(0), 01623 _M_use_grouping(false), 01624 _M_truename(0), _M_truename_size(0), _M_falsename(0), 01625 _M_falsename_size(0), _M_decimal_point(_CharT()), 01626 _M_thousands_sep(_CharT()), _M_allocated(false) 01627 { } 01628 01629 ~__numpunct_cache(); 01630 01631 void 01632 _M_cache(const locale& __loc); 01633 01634 private: 01635 __numpunct_cache& 01636 operator=(const __numpunct_cache&); 01637 01638 explicit 01639 __numpunct_cache(const __numpunct_cache&); 01640 }; 01641 01642 template<typename _CharT> 01643 __numpunct_cache<_CharT>::~__numpunct_cache() 01644 { 01645 if (_M_allocated) 01646 { 01647 delete [] _M_grouping; 01648 delete [] _M_truename; 01649 delete [] _M_falsename; 01650 } 01651 } 01652 01653 _GLIBCXX_BEGIN_NAMESPACE_CXX11 01654 01655 /** 01656 * @brief Primary class template numpunct. 01657 * @ingroup locales 01658 * 01659 * This facet stores several pieces of information related to printing and 01660 * scanning numbers, such as the decimal point character. It takes a 01661 * template parameter specifying the char type. The numpunct facet is 01662 * used by streams for many I/O operations involving numbers. 01663 * 01664 * The numpunct template uses protected virtual functions to provide the 01665 * actual results. The public accessors forward the call to the virtual 01666 * functions. These virtual functions are hooks for developers to 01667 * implement the behavior they require from a numpunct facet. 01668 */ 01669 template<typename _CharT> 01670 class numpunct : public locale::facet 01671 { 01672 public: 01673 // Types: 01674 //@{ 01675 /// Public typedefs 01676 typedef _CharT char_type; 01677 typedef basic_string<_CharT> string_type; 01678 //@} 01679 typedef __numpunct_cache<_CharT> __cache_type; 01680 01681 protected: 01682 __cache_type* _M_data; 01683 01684 public: 01685 /// Numpunct facet id. 01686 static locale::id id; 01687 01688 /** 01689 * @brief Numpunct constructor. 01690 * 01691 * @param __refs Refcount to pass to the base class. 01692 */ 01693 explicit 01694 numpunct(size_t __refs = 0) 01695 : facet(__refs), _M_data(0) 01696 { _M_initialize_numpunct(); } 01697 01698 /** 01699 * @brief Internal constructor. Not for general use. 01700 * 01701 * This is a constructor for use by the library itself to set up the 01702 * predefined locale facets. 01703 * 01704 * @param __cache __numpunct_cache object. 01705 * @param __refs Refcount to pass to the base class. 01706 */ 01707 explicit 01708 numpunct(__cache_type* __cache, size_t __refs = 0) 01709 : facet(__refs), _M_data(__cache) 01710 { _M_initialize_numpunct(); } 01711 01712 /** 01713 * @brief Internal constructor. Not for general use. 01714 * 01715 * This is a constructor for use by the library itself to set up new 01716 * locales. 01717 * 01718 * @param __cloc The C locale. 01719 * @param __refs Refcount to pass to the base class. 01720 */ 01721 explicit 01722 numpunct(__c_locale __cloc, size_t __refs = 0) 01723 : facet(__refs), _M_data(0) 01724 { _M_initialize_numpunct(__cloc); } 01725 01726 /** 01727 * @brief Return decimal point character. 01728 * 01729 * This function returns a char_type to use as a decimal point. It 01730 * does so by returning returning 01731 * numpunct<char_type>::do_decimal_point(). 01732 * 01733 * @return @a char_type representing a decimal point. 01734 */ 01735 char_type 01736 decimal_point() const 01737 { return this->do_decimal_point(); } 01738 01739 /** 01740 * @brief Return thousands separator character. 01741 * 01742 * This function returns a char_type to use as a thousands 01743 * separator. It does so by returning returning 01744 * numpunct<char_type>::do_thousands_sep(). 01745 * 01746 * @return char_type representing a thousands separator. 01747 */ 01748 char_type 01749 thousands_sep() const 01750 { return this->do_thousands_sep(); } 01751 01752 /** 01753 * @brief Return grouping specification. 01754 * 01755 * This function returns a string representing groupings for the 01756 * integer part of a number. Groupings indicate where thousands 01757 * separators should be inserted in the integer part of a number. 01758 * 01759 * Each char in the return string is interpret as an integer 01760 * rather than a character. These numbers represent the number 01761 * of digits in a group. The first char in the string 01762 * represents the number of digits in the least significant 01763 * group. If a char is negative, it indicates an unlimited 01764 * number of digits for the group. If more chars from the 01765 * string are required to group a number, the last char is used 01766 * repeatedly. 01767 * 01768 * For example, if the grouping() returns "\003\002" and is 01769 * applied to the number 123456789, this corresponds to 01770 * 12,34,56,789. Note that if the string was "32", this would 01771 * put more than 50 digits into the least significant group if 01772 * the character set is ASCII. 01773 * 01774 * The string is returned by calling 01775 * numpunct<char_type>::do_grouping(). 01776 * 01777 * @return string representing grouping specification. 01778 */ 01779 string 01780 grouping() const 01781 { return this->do_grouping(); } 01782 01783 /** 01784 * @brief Return string representation of bool true. 01785 * 01786 * This function returns a string_type containing the text 01787 * representation for true bool variables. It does so by calling 01788 * numpunct<char_type>::do_truename(). 01789 * 01790 * @return string_type representing printed form of true. 01791 */ 01792 string_type 01793 truename() const 01794 { return this->do_truename(); } 01795 01796 /** 01797 * @brief Return string representation of bool false. 01798 * 01799 * This function returns a string_type containing the text 01800 * representation for false bool variables. It does so by calling 01801 * numpunct<char_type>::do_falsename(). 01802 * 01803 * @return string_type representing printed form of false. 01804 */ 01805 string_type 01806 falsename() const 01807 { return this->do_falsename(); } 01808 01809 protected: 01810 /// Destructor. 01811 virtual 01812 ~numpunct(); 01813 01814 /** 01815 * @brief Return decimal point character. 01816 * 01817 * Returns a char_type to use as a decimal point. This function is a 01818 * hook for derived classes to change the value returned. 01819 * 01820 * @return @a char_type representing a decimal point. 01821 */ 01822 virtual char_type 01823 do_decimal_point() const 01824 { return _M_data->_M_decimal_point; } 01825 01826 /** 01827 * @brief Return thousands separator character. 01828 * 01829 * Returns a char_type to use as a thousands separator. This function 01830 * is a hook for derived classes to change the value returned. 01831 * 01832 * @return @a char_type representing a thousands separator. 01833 */ 01834 virtual char_type 01835 do_thousands_sep() const 01836 { return _M_data->_M_thousands_sep; } 01837 01838 /** 01839 * @brief Return grouping specification. 01840 * 01841 * Returns a string representing groupings for the integer part of a 01842 * number. This function is a hook for derived classes to change the 01843 * value returned. @see grouping() for details. 01844 * 01845 * @return String representing grouping specification. 01846 */ 01847 virtual string 01848 do_grouping() const 01849 { return _M_data->_M_grouping; } 01850 01851 /** 01852 * @brief Return string representation of bool true. 01853 * 01854 * Returns a string_type containing the text representation for true 01855 * bool variables. This function is a hook for derived classes to 01856 * change the value returned. 01857 * 01858 * @return string_type representing printed form of true. 01859 */ 01860 virtual string_type 01861 do_truename() const 01862 { return _M_data->_M_truename; } 01863 01864 /** 01865 * @brief Return string representation of bool false. 01866 * 01867 * Returns a string_type containing the text representation for false 01868 * bool variables. This function is a hook for derived classes to 01869 * change the value returned. 01870 * 01871 * @return string_type representing printed form of false. 01872 */ 01873 virtual string_type 01874 do_falsename() const 01875 { return _M_data->_M_falsename; } 01876 01877 // For use at construction time only. 01878 void 01879 _M_initialize_numpunct(__c_locale __cloc = 0); 01880 }; 01881 01882 template<typename _CharT> 01883 locale::id numpunct<_CharT>::id; 01884 01885 template<> 01886 numpunct<char>::~numpunct(); 01887 01888 template<> 01889 void 01890 numpunct<char>::_M_initialize_numpunct(__c_locale __cloc); 01891 01892 #ifdef _GLIBCXX_USE_WCHAR_T 01893 template<> 01894 numpunct<wchar_t>::~numpunct(); 01895 01896 template<> 01897 void 01898 numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc); 01899 #endif 01900 01901 /// class numpunct_byname [22.2.3.2]. 01902 template<typename _CharT> 01903 class numpunct_byname : public numpunct<_CharT> 01904 { 01905 public: 01906 typedef _CharT char_type; 01907 typedef basic_string<_CharT> string_type; 01908 01909 explicit 01910 numpunct_byname(const char* __s, size_t __refs = 0) 01911 : numpunct<_CharT>(__refs) 01912 { 01913 if (__builtin_strcmp(__s, "C") != 0 01914 && __builtin_strcmp(__s, "POSIX") != 0) 01915 { 01916 __c_locale __tmp; 01917 this->_S_create_c_locale(__tmp, __s); 01918 this->_M_initialize_numpunct(__tmp); 01919 this->_S_destroy_c_locale(__tmp); 01920 } 01921 } 01922 01923 #if __cplusplus >= 201103L 01924 explicit 01925 numpunct_byname(const string& __s, size_t __refs = 0) 01926 : numpunct_byname(__s.c_str(), __refs) { } 01927 #endif 01928 01929 protected: 01930 virtual 01931 ~numpunct_byname() { } 01932 }; 01933 01934 _GLIBCXX_END_NAMESPACE_CXX11 01935 01936 _GLIBCXX_BEGIN_NAMESPACE_LDBL 01937 01938 /** 01939 * @brief Primary class template num_get. 01940 * @ingroup locales 01941 * 01942 * This facet encapsulates the code to parse and return a number 01943 * from a string. It is used by the istream numeric extraction 01944 * operators. 01945 * 01946 * The num_get template uses protected virtual functions to provide the 01947 * actual results. The public accessors forward the call to the virtual 01948 * functions. These virtual functions are hooks for developers to 01949 * implement the behavior they require from the num_get facet. 01950 */ 01951 template<typename _CharT, typename _InIter> 01952 class num_get : public locale::facet 01953 { 01954 public: 01955 // Types: 01956 //@{ 01957 /// Public typedefs 01958 typedef _CharT char_type; 01959 typedef _InIter iter_type; 01960 //@} 01961 01962 /// Numpunct facet id. 01963 static locale::id id; 01964 01965 /** 01966 * @brief Constructor performs initialization. 01967 * 01968 * This is the constructor provided by the standard. 01969 * 01970 * @param __refs Passed to the base facet class. 01971 */ 01972 explicit 01973 num_get(size_t __refs = 0) : facet(__refs) { } 01974 01975 /** 01976 * @brief Numeric parsing. 01977 * 01978 * Parses the input stream into the bool @a v. It does so by calling 01979 * num_get::do_get(). 01980 * 01981 * If ios_base::boolalpha is set, attempts to read 01982 * ctype<CharT>::truename() or ctype<CharT>::falsename(). Sets 01983 * @a v to true or false if successful. Sets err to 01984 * ios_base::failbit if reading the string fails. Sets err to 01985 * ios_base::eofbit if the stream is emptied. 01986 * 01987 * If ios_base::boolalpha is not set, proceeds as with reading a long, 01988 * except if the value is 1, sets @a v to true, if the value is 0, sets 01989 * @a v to false, and otherwise set err to ios_base::failbit. 01990 * 01991 * @param __in Start of input stream. 01992 * @param __end End of input stream. 01993 * @param __io Source of locale and flags. 01994 * @param __err Error flags to set. 01995 * @param __v Value to format and insert. 01996 * @return Iterator after reading. 01997 */ 01998 iter_type 01999 get(iter_type __in, iter_type __end, ios_base& __io, 02000 ios_base::iostate& __err, bool& __v) const 02001 { return this->do_get(__in, __end, __io, __err, __v); } 02002 02003 //@{ 02004 /** 02005 * @brief Numeric parsing. 02006 * 02007 * Parses the input stream into the integral variable @a v. It does so 02008 * by calling num_get::do_get(). 02009 * 02010 * Parsing is affected by the flag settings in @a io. 02011 * 02012 * The basic parse is affected by the value of io.flags() & 02013 * ios_base::basefield. If equal to ios_base::oct, parses like the 02014 * scanf %o specifier. Else if equal to ios_base::hex, parses like %X 02015 * specifier. Else if basefield equal to 0, parses like the %i 02016 * specifier. Otherwise, parses like %d for signed and %u for unsigned 02017 * types. The matching type length modifier is also used. 02018 * 02019 * Digit grouping is interpreted according to 02020 * numpunct::grouping() and numpunct::thousands_sep(). If the 02021 * pattern of digit groups isn't consistent, sets err to 02022 * ios_base::failbit. 02023 * 02024 * If parsing the string yields a valid value for @a v, @a v is set. 02025 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered. 02026 * Sets err to ios_base::eofbit if the stream is emptied. 02027 * 02028 * @param __in Start of input stream. 02029 * @param __end End of input stream. 02030 * @param __io Source of locale and flags. 02031 * @param __err Error flags to set. 02032 * @param __v Value to format and insert. 02033 * @return Iterator after reading. 02034 */ 02035 iter_type 02036 get(iter_type __in, iter_type __end, ios_base& __io, 02037 ios_base::iostate& __err, long& __v) const 02038 { return this->do_get(__in, __end, __io, __err, __v); } 02039 02040 iter_type 02041 get(iter_type __in, iter_type __end, ios_base& __io, 02042 ios_base::iostate& __err, unsigned short& __v) const 02043 { return this->do_get(__in, __end, __io, __err, __v); } 02044 02045 iter_type 02046 get(iter_type __in, iter_type __end, ios_base& __io, 02047 ios_base::iostate& __err, unsigned int& __v) const 02048 { return this->do_get(__in, __end, __io, __err, __v); } 02049 02050 iter_type 02051 get(iter_type __in, iter_type __end, ios_base& __io, 02052 ios_base::iostate& __err, unsigned long& __v) const 02053 { return this->do_get(__in, __end, __io, __err, __v); } 02054 02055 #ifdef _GLIBCXX_USE_LONG_LONG 02056 iter_type 02057 get(iter_type __in, iter_type __end, ios_base& __io, 02058 ios_base::iostate& __err, long long& __v) const 02059 { return this->do_get(__in, __end, __io, __err, __v); } 02060 02061 iter_type 02062 get(iter_type __in, iter_type __end, ios_base& __io, 02063 ios_base::iostate& __err, unsigned long long& __v) const 02064 { return this->do_get(__in, __end, __io, __err, __v); } 02065 #endif 02066 //@} 02067 02068 //@{ 02069 /** 02070 * @brief Numeric parsing. 02071 * 02072 * Parses the input stream into the integral variable @a v. It does so 02073 * by calling num_get::do_get(). 02074 * 02075 * The input characters are parsed like the scanf %g specifier. The 02076 * matching type length modifier is also used. 02077 * 02078 * The decimal point character used is numpunct::decimal_point(). 02079 * Digit grouping is interpreted according to 02080 * numpunct::grouping() and numpunct::thousands_sep(). If the 02081 * pattern of digit groups isn't consistent, sets err to 02082 * ios_base::failbit. 02083 * 02084 * If parsing the string yields a valid value for @a v, @a v is set. 02085 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered. 02086 * Sets err to ios_base::eofbit if the stream is emptied. 02087 * 02088 * @param __in Start of input stream. 02089 * @param __end End of input stream. 02090 * @param __io Source of locale and flags. 02091 * @param __err Error flags to set. 02092 * @param __v Value to format and insert. 02093 * @return Iterator after reading. 02094 */ 02095 iter_type 02096 get(iter_type __in, iter_type __end, ios_base& __io, 02097 ios_base::iostate& __err, float& __v) const 02098 { return this->do_get(__in, __end, __io, __err, __v); } 02099 02100 iter_type 02101 get(iter_type __in, iter_type __end, ios_base& __io, 02102 ios_base::iostate& __err, double& __v) const 02103 { return this->do_get(__in, __end, __io, __err, __v); } 02104 02105 iter_type 02106 get(iter_type __in, iter_type __end, ios_base& __io, 02107 ios_base::iostate& __err, long double& __v) const 02108 { return this->do_get(__in, __end, __io, __err, __v); } 02109 //@} 02110 02111 /** 02112 * @brief Numeric parsing. 02113 * 02114 * Parses the input stream into the pointer variable @a v. It does so 02115 * by calling num_get::do_get(). 02116 * 02117 * The input characters are parsed like the scanf %p specifier. 02118 * 02119 * Digit grouping is interpreted according to 02120 * numpunct::grouping() and numpunct::thousands_sep(). If the 02121 * pattern of digit groups isn't consistent, sets err to 02122 * ios_base::failbit. 02123 * 02124 * Note that the digit grouping effect for pointers is a bit ambiguous 02125 * in the standard and shouldn't be relied on. See DR 344. 02126 * 02127 * If parsing the string yields a valid value for @a v, @a v is set. 02128 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered. 02129 * Sets err to ios_base::eofbit if the stream is emptied. 02130 * 02131 * @param __in Start of input stream. 02132 * @param __end End of input stream. 02133 * @param __io Source of locale and flags. 02134 * @param __err Error flags to set. 02135 * @param __v Value to format and insert. 02136 * @return Iterator after reading. 02137 */ 02138 iter_type 02139 get(iter_type __in, iter_type __end, ios_base& __io, 02140 ios_base::iostate& __err, void*& __v) const 02141 { return this->do_get(__in, __end, __io, __err, __v); } 02142 02143 protected: 02144 /// Destructor. 02145 virtual ~num_get() { } 02146 02147 _GLIBCXX_DEFAULT_ABI_TAG 02148 iter_type 02149 _M_extract_float(iter_type, iter_type, ios_base&, ios_base::iostate&, 02150 string&) const; 02151 02152 template<typename _ValueT> 02153 _GLIBCXX_DEFAULT_ABI_TAG 02154 iter_type 02155 _M_extract_int(iter_type, iter_type, ios_base&, ios_base::iostate&, 02156 _ValueT&) const; 02157 02158 template<typename _CharT2> 02159 typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, int>::__type 02160 _M_find(const _CharT2*, size_t __len, _CharT2 __c) const 02161 { 02162 int __ret = -1; 02163 if (__len <= 10) 02164 { 02165 if (__c >= _CharT2('0') && __c < _CharT2(_CharT2('0') + __len)) 02166 __ret = __c - _CharT2('0'); 02167 } 02168 else 02169 { 02170 if (__c >= _CharT2('0') && __c <= _CharT2('9')) 02171 __ret = __c - _CharT2('0'); 02172 else if (__c >= _CharT2('a') && __c <= _CharT2('f')) 02173 __ret = 10 + (__c - _CharT2('a')); 02174 else if (__c >= _CharT2('A') && __c <= _CharT2('F')) 02175 __ret = 10 + (__c - _CharT2('A')); 02176 } 02177 return __ret; 02178 } 02179 02180 template<typename _CharT2> 02181 typename __gnu_cxx::__enable_if<!__is_char<_CharT2>::__value, 02182 int>::__type 02183 _M_find(const _CharT2* __zero, size_t __len, _CharT2 __c) const 02184 { 02185 int __ret = -1; 02186 const char_type* __q = char_traits<_CharT2>::find(__zero, __len, __c); 02187 if (__q) 02188 { 02189 __ret = __q - __zero; 02190 if (__ret > 15) 02191 __ret -= 6; 02192 } 02193 return __ret; 02194 } 02195 02196 //@{ 02197 /** 02198 * @brief Numeric parsing. 02199 * 02200 * Parses the input stream into the variable @a v. This function is a 02201 * hook for derived classes to change the value returned. @see get() 02202 * for more details. 02203 * 02204 * @param __beg Start of input stream. 02205 * @param __end End of input stream. 02206 * @param __io Source of locale and flags. 02207 * @param __err Error flags to set. 02208 * @param __v Value to format and insert. 02209 * @return Iterator after reading. 02210 */ 02211 virtual iter_type 02212 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const; 02213 02214 virtual iter_type 02215 do_get(iter_type __beg, iter_type __end, ios_base& __io, 02216 ios_base::iostate& __err, long& __v) const 02217 { return _M_extract_int(__beg, __end, __io, __err, __v); } 02218 02219 virtual iter_type 02220 do_get(iter_type __beg, iter_type __end, ios_base& __io, 02221 ios_base::iostate& __err, unsigned short& __v) const 02222 { return _M_extract_int(__beg, __end, __io, __err, __v); } 02223 02224 virtual iter_type 02225 do_get(iter_type __beg, iter_type __end, ios_base& __io, 02226 ios_base::iostate& __err, unsigned int& __v) const 02227 { return _M_extract_int(__beg, __end, __io, __err, __v); } 02228 02229 virtual iter_type 02230 do_get(iter_type __beg, iter_type __end, ios_base& __io, 02231 ios_base::iostate& __err, unsigned long& __v) const 02232 { return _M_extract_int(__beg, __end, __io, __err, __v); } 02233 02234 #ifdef _GLIBCXX_USE_LONG_LONG 02235 virtual iter_type 02236 do_get(iter_type __beg, iter_type __end, ios_base& __io, 02237 ios_base::iostate& __err, long long& __v) const 02238 { return _M_extract_int(__beg, __end, __io, __err, __v); } 02239 02240 virtual iter_type 02241 do_get(iter_type __beg, iter_type __end, ios_base& __io, 02242 ios_base::iostate& __err, unsigned long long& __v) const 02243 { return _M_extract_int(__beg, __end, __io, __err, __v); } 02244 #endif 02245 02246 virtual iter_type 02247 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, float&) const; 02248 02249 virtual iter_type 02250 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, 02251 double&) const; 02252 02253 // XXX GLIBCXX_ABI Deprecated 02254 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 02255 virtual iter_type 02256 __do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, 02257 double&) const; 02258 #else 02259 virtual iter_type 02260 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, 02261 long double&) const; 02262 #endif 02263 02264 virtual iter_type 02265 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, void*&) const; 02266 02267 // XXX GLIBCXX_ABI Deprecated 02268 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 02269 virtual iter_type 02270 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, 02271 long double&) const; 02272 #endif 02273 //@} 02274 }; 02275 02276 template<typename _CharT, typename _InIter> 02277 locale::id num_get<_CharT, _InIter>::id; 02278 02279 02280 /** 02281 * @brief Primary class template num_put. 02282 * @ingroup locales 02283 * 02284 * This facet encapsulates the code to convert a number to a string. It is 02285 * used by the ostream numeric insertion operators. 02286 * 02287 * The num_put template uses protected virtual functions to provide the 02288 * actual results. The public accessors forward the call to the virtual 02289 * functions. These virtual functions are hooks for developers to 02290 * implement the behavior they require from the num_put facet. 02291 */ 02292 template<typename _CharT, typename _OutIter> 02293 class num_put : public locale::facet 02294 { 02295 public: 02296 // Types: 02297 //@{ 02298 /// Public typedefs 02299 typedef _CharT char_type; 02300 typedef _OutIter iter_type; 02301 //@} 02302 02303 /// Numpunct facet id. 02304 static locale::id id; 02305 02306 /** 02307 * @brief Constructor performs initialization. 02308 * 02309 * This is the constructor provided by the standard. 02310 * 02311 * @param __refs Passed to the base facet class. 02312 */ 02313 explicit 02314 num_put(size_t __refs = 0) : facet(__refs) { } 02315 02316 /** 02317 * @brief Numeric formatting. 02318 * 02319 * Formats the boolean @a v and inserts it into a stream. It does so 02320 * by calling num_put::do_put(). 02321 * 02322 * If ios_base::boolalpha is set, writes ctype<CharT>::truename() or 02323 * ctype<CharT>::falsename(). Otherwise formats @a v as an int. 02324 * 02325 * @param __s Stream to write to. 02326 * @param __io Source of locale and flags. 02327 * @param __fill Char_type to use for filling. 02328 * @param __v Value to format and insert. 02329 * @return Iterator after writing. 02330 */ 02331 iter_type 02332 put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const 02333 { return this->do_put(__s, __io, __fill, __v); } 02334 02335 //@{ 02336 /** 02337 * @brief Numeric formatting. 02338 * 02339 * Formats the integral value @a v and inserts it into a 02340 * stream. It does so by calling num_put::do_put(). 02341 * 02342 * Formatting is affected by the flag settings in @a io. 02343 * 02344 * The basic format is affected by the value of io.flags() & 02345 * ios_base::basefield. If equal to ios_base::oct, formats like the 02346 * printf %o specifier. Else if equal to ios_base::hex, formats like 02347 * %x or %X with ios_base::uppercase unset or set respectively. 02348 * Otherwise, formats like %d, %ld, %lld for signed and %u, %lu, %llu 02349 * for unsigned values. Note that if both oct and hex are set, neither 02350 * will take effect. 02351 * 02352 * If ios_base::showpos is set, '+' is output before positive values. 02353 * If ios_base::showbase is set, '0' precedes octal values (except 0) 02354 * and '0[xX]' precedes hex values. 02355 * 02356 * The decimal point character used is numpunct::decimal_point(). 02357 * Thousands separators are inserted according to 02358 * numpunct::grouping() and numpunct::thousands_sep(). 02359 * 02360 * If io.width() is non-zero, enough @a fill characters are inserted to 02361 * make the result at least that wide. If 02362 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is 02363 * padded at the end. If ios_base::internal, then padding occurs 02364 * immediately after either a '+' or '-' or after '0x' or '0X'. 02365 * Otherwise, padding occurs at the beginning. 02366 * 02367 * @param __s Stream to write to. 02368 * @param __io Source of locale and flags. 02369 * @param __fill Char_type to use for filling. 02370 * @param __v Value to format and insert. 02371 * @return Iterator after writing. 02372 */ 02373 iter_type 02374 put(iter_type __s, ios_base& __io, char_type __fill, long __v) const 02375 { return this->do_put(__s, __io, __fill, __v); } 02376 02377 iter_type 02378 put(iter_type __s, ios_base& __io, char_type __fill, 02379 unsigned long __v) const 02380 { return this->do_put(__s, __io, __fill, __v); } 02381 02382 #ifdef _GLIBCXX_USE_LONG_LONG 02383 iter_type 02384 put(iter_type __s, ios_base& __io, char_type __fill, long long __v) const 02385 { return this->do_put(__s, __io, __fill, __v); } 02386 02387 iter_type 02388 put(iter_type __s, ios_base& __io, char_type __fill, 02389 unsigned long long __v) const 02390 { return this->do_put(__s, __io, __fill, __v); } 02391 #endif 02392 //@} 02393 02394 //@{ 02395 /** 02396 * @brief Numeric formatting. 02397 * 02398 * Formats the floating point value @a v and inserts it into a stream. 02399 * It does so by calling num_put::do_put(). 02400 * 02401 * Formatting is affected by the flag settings in @a io. 02402 * 02403 * The basic format is affected by the value of io.flags() & 02404 * ios_base::floatfield. If equal to ios_base::fixed, formats like the 02405 * printf %f specifier. Else if equal to ios_base::scientific, formats 02406 * like %e or %E with ios_base::uppercase unset or set respectively. 02407 * Otherwise, formats like %g or %G depending on uppercase. Note that 02408 * if both fixed and scientific are set, the effect will also be like 02409 * %g or %G. 02410 * 02411 * The output precision is given by io.precision(). This precision is 02412 * capped at numeric_limits::digits10 + 2 (different for double and 02413 * long double). The default precision is 6. 02414 * 02415 * If ios_base::showpos is set, '+' is output before positive values. 02416 * If ios_base::showpoint is set, a decimal point will always be 02417 * output. 02418 * 02419 * The decimal point character used is numpunct::decimal_point(). 02420 * Thousands separators are inserted according to 02421 * numpunct::grouping() and numpunct::thousands_sep(). 02422 * 02423 * If io.width() is non-zero, enough @a fill characters are inserted to 02424 * make the result at least that wide. If 02425 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is 02426 * padded at the end. If ios_base::internal, then padding occurs 02427 * immediately after either a '+' or '-' or after '0x' or '0X'. 02428 * Otherwise, padding occurs at the beginning. 02429 * 02430 * @param __s Stream to write to. 02431 * @param __io Source of locale and flags. 02432 * @param __fill Char_type to use for filling. 02433 * @param __v Value to format and insert. 02434 * @return Iterator after writing. 02435 */ 02436 iter_type 02437 put(iter_type __s, ios_base& __io, char_type __fill, double __v) const 02438 { return this->do_put(__s, __io, __fill, __v); } 02439 02440 iter_type 02441 put(iter_type __s, ios_base& __io, char_type __fill, 02442 long double __v) const 02443 { return this->do_put(__s, __io, __fill, __v); } 02444 //@} 02445 02446 /** 02447 * @brief Numeric formatting. 02448 * 02449 * Formats the pointer value @a v and inserts it into a stream. It 02450 * does so by calling num_put::do_put(). 02451 * 02452 * This function formats @a v as an unsigned long with ios_base::hex 02453 * and ios_base::showbase set. 02454 * 02455 * @param __s Stream to write to. 02456 * @param __io Source of locale and flags. 02457 * @param __fill Char_type to use for filling. 02458 * @param __v Value to format and insert. 02459 * @return Iterator after writing. 02460 */ 02461 iter_type 02462 put(iter_type __s, ios_base& __io, char_type __fill, 02463 const void* __v) const 02464 { return this->do_put(__s, __io, __fill, __v); } 02465 02466 protected: 02467 template<typename _ValueT> 02468 iter_type 02469 _M_insert_float(iter_type, ios_base& __io, char_type __fill, 02470 char __mod, _ValueT __v) const; 02471 02472 void 02473 _M_group_float(const char* __grouping, size_t __grouping_size, 02474 char_type __sep, const char_type* __p, char_type* __new, 02475 char_type* __cs, int& __len) const; 02476 02477 template<typename _ValueT> 02478 iter_type 02479 _M_insert_int(iter_type, ios_base& __io, char_type __fill, 02480 _ValueT __v) const; 02481 02482 void 02483 _M_group_int(const char* __grouping, size_t __grouping_size, 02484 char_type __sep, ios_base& __io, char_type* __new, 02485 char_type* __cs, int& __len) const; 02486 02487 void 02488 _M_pad(char_type __fill, streamsize __w, ios_base& __io, 02489 char_type* __new, const char_type* __cs, int& __len) const; 02490 02491 /// Destructor. 02492 virtual 02493 ~num_put() { } 02494 02495 //@{ 02496 /** 02497 * @brief Numeric formatting. 02498 * 02499 * These functions do the work of formatting numeric values and 02500 * inserting them into a stream. This function is a hook for derived 02501 * classes to change the value returned. 02502 * 02503 * @param __s Stream to write to. 02504 * @param __io Source of locale and flags. 02505 * @param __fill Char_type to use for filling. 02506 * @param __v Value to format and insert. 02507 * @return Iterator after writing. 02508 */ 02509 virtual iter_type 02510 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const; 02511 02512 virtual iter_type 02513 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const 02514 { return _M_insert_int(__s, __io, __fill, __v); } 02515 02516 virtual iter_type 02517 do_put(iter_type __s, ios_base& __io, char_type __fill, 02518 unsigned long __v) const 02519 { return _M_insert_int(__s, __io, __fill, __v); } 02520 02521 #ifdef _GLIBCXX_USE_LONG_LONG 02522 virtual iter_type 02523 do_put(iter_type __s, ios_base& __io, char_type __fill, 02524 long long __v) const 02525 { return _M_insert_int(__s, __io, __fill, __v); } 02526 02527 virtual iter_type 02528 do_put(iter_type __s, ios_base& __io, char_type __fill, 02529 unsigned long long __v) const 02530 { return _M_insert_int(__s, __io, __fill, __v); } 02531 #endif 02532 02533 virtual iter_type 02534 do_put(iter_type, ios_base&, char_type, double) const; 02535 02536 // XXX GLIBCXX_ABI Deprecated 02537 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 02538 virtual iter_type 02539 __do_put(iter_type, ios_base&, char_type, double) const; 02540 #else 02541 virtual iter_type 02542 do_put(iter_type, ios_base&, char_type, long double) const; 02543 #endif 02544 02545 virtual iter_type 02546 do_put(iter_type, ios_base&, char_type, const void*) const; 02547 02548 // XXX GLIBCXX_ABI Deprecated 02549 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 02550 virtual iter_type 02551 do_put(iter_type, ios_base&, char_type, long double) const; 02552 #endif 02553 //@} 02554 }; 02555 02556 template <typename _CharT, typename _OutIter> 02557 locale::id num_put<_CharT, _OutIter>::id; 02558 02559 _GLIBCXX_END_NAMESPACE_LDBL 02560 02561 // Subclause convenience interfaces, inlines. 02562 // NB: These are inline because, when used in a loop, some compilers 02563 // can hoist the body out of the loop; then it's just as fast as the 02564 // C is*() function. 02565 02566 /// Convenience interface to ctype.is(ctype_base::space, __c). 02567 template<typename _CharT> 02568 inline bool 02569 isspace(_CharT __c, const locale& __loc) 02570 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); } 02571 02572 /// Convenience interface to ctype.is(ctype_base::print, __c). 02573 template<typename _CharT> 02574 inline bool 02575 isprint(_CharT __c, const locale& __loc) 02576 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); } 02577 02578 /// Convenience interface to ctype.is(ctype_base::cntrl, __c). 02579 template<typename _CharT> 02580 inline bool 02581 iscntrl(_CharT __c, const locale& __loc) 02582 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); } 02583 02584 /// Convenience interface to ctype.is(ctype_base::upper, __c). 02585 template<typename _CharT> 02586 inline bool 02587 isupper(_CharT __c, const locale& __loc) 02588 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); } 02589 02590 /// Convenience interface to ctype.is(ctype_base::lower, __c). 02591 template<typename _CharT> 02592 inline bool 02593 islower(_CharT __c, const locale& __loc) 02594 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); } 02595 02596 /// Convenience interface to ctype.is(ctype_base::alpha, __c). 02597 template<typename _CharT> 02598 inline bool 02599 isalpha(_CharT __c, const locale& __loc) 02600 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); } 02601 02602 /// Convenience interface to ctype.is(ctype_base::digit, __c). 02603 template<typename _CharT> 02604 inline bool 02605 isdigit(_CharT __c, const locale& __loc) 02606 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); } 02607 02608 /// Convenience interface to ctype.is(ctype_base::punct, __c). 02609 template<typename _CharT> 02610 inline bool 02611 ispunct(_CharT __c, const locale& __loc) 02612 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); } 02613 02614 /// Convenience interface to ctype.is(ctype_base::xdigit, __c). 02615 template<typename _CharT> 02616 inline bool 02617 isxdigit(_CharT __c, const locale& __loc) 02618 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); } 02619 02620 /// Convenience interface to ctype.is(ctype_base::alnum, __c). 02621 template<typename _CharT> 02622 inline bool 02623 isalnum(_CharT __c, const locale& __loc) 02624 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); } 02625 02626 /// Convenience interface to ctype.is(ctype_base::graph, __c). 02627 template<typename _CharT> 02628 inline bool 02629 isgraph(_CharT __c, const locale& __loc) 02630 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); } 02631 02632 #if __cplusplus >= 201103L 02633 /// Convenience interface to ctype.is(ctype_base::blank, __c). 02634 template<typename _CharT> 02635 inline bool 02636 isblank(_CharT __c, const locale& __loc) 02637 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::blank, __c); } 02638 #endif 02639 02640 /// Convenience interface to ctype.toupper(__c). 02641 template<typename _CharT> 02642 inline _CharT 02643 toupper(_CharT __c, const locale& __loc) 02644 { return use_facet<ctype<_CharT> >(__loc).toupper(__c); } 02645 02646 /// Convenience interface to ctype.tolower(__c). 02647 template<typename _CharT> 02648 inline _CharT 02649 tolower(_CharT __c, const locale& __loc) 02650 { return use_facet<ctype<_CharT> >(__loc).tolower(__c); } 02651 02652 _GLIBCXX_END_NAMESPACE_VERSION 02653 } // namespace std 02654 02655 # include <bits/locale_facets.tcc> 02656 02657 #endif