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