1 // Locale support -*- C++ -*- 2 3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 4 // 2006, 2007, 2008, 2009, 2010, 2011 5 // Free Software Foundation, Inc. 6 // 7 // This file is part of the GNU ISO C++ Library. This library is free 8 // software; you can redistribute it and/or modify it under the 9 // terms of the GNU General Public License as published by the 10 // Free Software Foundation; either version 3, or (at your option) 11 // any later version. 12 13 // This library is distributed in the hope that it will be useful, 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 // GNU General Public License for more details. 17 18 // Under Section 7 of GPL version 3, you are granted additional 19 // permissions described in the GCC Runtime Library Exception, version 20 // 3.1, as published by the Free Software Foundation. 21 22 // You should have received a copy of the GNU General Public License and 23 // a copy of the GCC Runtime Library Exception along with this program; 24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 25 // <http://www.gnu.org/licenses/>. 26 27 /** @file bits/locale_facets.h 28 * This is an internal header file, included by other library headers. 29 * Do not attempt to use it directly. @headername{locale} 30 */ 31 32 // 33 // ISO C++ 14882: 22.1 Locales 34 // 35 36 #ifndef _LOCALE_FACETS_H 37 #define _LOCALE_FACETS_H 1 38 39 #pragma GCC system_header 40 41 #include <cwctype> // For wctype_t 42 #include <cctype> 43 #include <bits/ctype_base.h> 44 #include <iosfwd> 45 #include <bits/ios_base.h> // For ios_base, ios_base::iostate 46 #include <streambuf> 47 #include <bits/cpp_type_traits.h> 48 #include <ext/type_traits.h> 49 #include <ext/numeric_traits.h> 50 #include <bits/streambuf_iterator.h> 51 52 namespace std _GLIBCXX_VISIBILITY(default) 53 { 54 _GLIBCXX_BEGIN_NAMESPACE_VERSION 55 56 // NB: Don't instantiate required wchar_t facets if no wchar_t support. 57 #ifdef _GLIBCXX_USE_WCHAR_T 58 # define _GLIBCXX_NUM_FACETS 28 59 #else 60 # define _GLIBCXX_NUM_FACETS 14 61 #endif 62 63 // Convert string to numeric value of type _Tp and store results. 64 // NB: This is specialized for all required types, there is no 65 // generic definition. 66 template<typename _Tp> 67 void 68 __convert_to_v(const char*, _Tp&, ios_base::iostate&, 69 const __c_locale&) throw(); 70 71 // Explicit specializations for required types. 72 template<> 73 void 74 __convert_to_v(const char*, float&, ios_base::iostate&, 75 const __c_locale&) throw(); 76 77 template<> 78 void 79 __convert_to_v(const char*, double&, ios_base::iostate&, 80 const __c_locale&) throw(); 81 82 template<> 83 void 84 __convert_to_v(const char*, long double&, ios_base::iostate&, 85 const __c_locale&) throw(); 86 87 // NB: __pad is a struct, rather than a function, so it can be 88 // partially-specialized. 89 template<typename _CharT, typename _Traits> 90 struct __pad 91 { 92 static void 93 _S_pad(ios_base& __io, _CharT __fill, _CharT* __news, 94 const _CharT* __olds, streamsize __newlen, streamsize __oldlen); 95 }; 96 97 // Used by both numeric and monetary facets. 98 // Inserts "group separator" characters into an array of characters. 99 // It's recursive, one iteration per group. It moves the characters 100 // in the buffer this way: "xxxx12345" -> "12,345xxx". Call this 101 // only with __gsize != 0. 102 template<typename _CharT> 103 _CharT* 104 __add_grouping(_CharT* __s, _CharT __sep, 105 const char* __gbeg, size_t __gsize, 106 const _CharT* __first, const _CharT* __last); 107 108 // This template permits specializing facet output code for 109 // ostreambuf_iterator. For ostreambuf_iterator, sputn is 110 // significantly more efficient than incrementing iterators. 111 template<typename _CharT> 112 inline 113 ostreambuf_iterator<_CharT> 114 __write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len) 115 { 116 __s._M_put(__ws, __len); 117 return __s; 118 } 119 120 // This is the unspecialized form of the template. 121 template<typename _CharT, typename _OutIter> 122 inline 123 _OutIter 124 __write(_OutIter __s, const _CharT* __ws, int __len) 125 { 126 for (int __j = 0; __j < __len; __j++, ++__s) 127 *__s = __ws[__j]; 128 return __s; 129 } 130 131 132 // 22.2.1.1 Template class ctype 133 // Include host and configuration specific ctype enums for ctype_base. 134 135 /** 136 * @brief Common base for ctype facet 137 * 138 * This template class provides implementations of the public functions 139 * that forward to the protected virtual functions. 140 * 141 * This template also provides abstract stubs for the protected virtual 142 * functions. 143 */ 144 template<typename _CharT> 145 class __ctype_abstract_base : public locale::facet, public ctype_base 146 { 147 public: 148 // Types: 149 /// Typedef for the template parameter 150 typedef _CharT char_type; 151 152 /** 153 * @brief Test char_type classification. 154 * 155 * This function finds a mask M for @a c and compares it to mask @a m. 156 * It does so by returning the value of ctype<char_type>::do_is(). 157 * 158 * @param c The char_type to compare the mask of. 159 * @param m The mask to compare against. 160 * @return (M & m) != 0. 161 */ 162 bool 163 is(mask __m, char_type __c) const 164 { return this->do_is(__m, __c); } 165 166 /** 167 * @brief Return a mask array. 168 * 169 * This function finds the mask for each char_type in the range [lo,hi) 170 * and successively writes it to vec. vec must have as many elements 171 * as the char array. It does so by returning the value of 172 * ctype<char_type>::do_is(). 173 * 174 * @param lo Pointer to start of range. 175 * @param hi Pointer to end of range. 176 * @param vec Pointer to an array of mask storage. 177 * @return @a hi. 178 */ 179 const char_type* 180 is(const char_type *__lo, const char_type *__hi, mask *__vec) const 181 { return this->do_is(__lo, __hi, __vec); } 182 183 /** 184 * @brief Find char_type matching a mask 185 * 186 * This function searches for and returns the first char_type c in 187 * [lo,hi) for which is(m,c) is true. It does so by returning 188 * ctype<char_type>::do_scan_is(). 189 * 190 * @param m The mask to compare against. 191 * @param lo Pointer to start of range. 192 * @param hi Pointer to end of range. 193 * @return Pointer to matching char_type if found, else @a hi. 194 */ 195 const char_type* 196 scan_is(mask __m, const char_type* __lo, const char_type* __hi) const 197 { return this->do_scan_is(__m, __lo, __hi); } 198 199 /** 200 * @brief Find char_type not matching a mask 201 * 202 * This function searches for and returns the first char_type c in 203 * [lo,hi) for which is(m,c) is false. It does so by returning 204 * ctype<char_type>::do_scan_not(). 205 * 206 * @param m The mask to compare against. 207 * @param lo Pointer to first char in range. 208 * @param hi Pointer to end of range. 209 * @return Pointer to non-matching char if found, else @a hi. 210 */ 211 const char_type* 212 scan_not(mask __m, const char_type* __lo, const char_type* __hi) const 213 { return this->do_scan_not(__m, __lo, __hi); } 214 215 /** 216 * @brief Convert to uppercase. 217 * 218 * This function converts the argument to uppercase if possible. 219 * If not possible (for example, '2'), returns the argument. It does 220 * so by returning ctype<char_type>::do_toupper(). 221 * 222 * @param c The char_type to convert. 223 * @return The uppercase char_type if convertible, else @a c. 224 */ 225 char_type 226 toupper(char_type __c) const 227 { return this->do_toupper(__c); } 228 229 /** 230 * @brief Convert array to uppercase. 231 * 232 * This function converts each char_type in the range [lo,hi) to 233 * uppercase if possible. Other elements remain untouched. It does so 234 * by returning ctype<char_type>:: do_toupper(lo, hi). 235 * 236 * @param lo Pointer to start of range. 237 * @param hi Pointer to end of range. 238 * @return @a hi. 239 */ 240 const char_type* 241 toupper(char_type *__lo, const char_type* __hi) const 242 { return this->do_toupper(__lo, __hi); } 243 244 /** 245 * @brief Convert to lowercase. 246 * 247 * This function converts the argument to lowercase if possible. If 248 * not possible (for example, '2'), returns the argument. It does so 249 * by returning ctype<char_type>::do_tolower(c). 250 * 251 * @param c The char_type to convert. 252 * @return The lowercase char_type if convertible, else @a c. 253 */ 254 char_type 255 tolower(char_type __c) const 256 { return this->do_tolower(__c); } 257 258 /** 259 * @brief Convert array to lowercase. 260 * 261 * This function converts each char_type in the range [lo,hi) to 262 * lowercase if possible. Other elements remain untouched. It does so 263 * by returning ctype<char_type>:: do_tolower(lo, hi). 264 * 265 * @param lo Pointer to start of range. 266 * @param hi Pointer to end of range. 267 * @return @a hi. 268 */ 269 const char_type* 270 tolower(char_type* __lo, const char_type* __hi) const 271 { return this->do_tolower(__lo, __hi); } 272 273 /** 274 * @brief Widen char to char_type 275 * 276 * This function converts the char argument to char_type using the 277 * simplest reasonable transformation. It does so by returning 278 * ctype<char_type>::do_widen(c). 279 * 280 * Note: this is not what you want for codepage conversions. See 281 * codecvt for that. 282 * 283 * @param c The char to convert. 284 * @return The converted char_type. 285 */ 286 char_type 287 widen(char __c) const 288 { return this->do_widen(__c); } 289 290 /** 291 * @brief Widen array to char_type 292 * 293 * This function converts each char in the input to char_type using the 294 * simplest reasonable transformation. It does so by returning 295 * ctype<char_type>::do_widen(c). 296 * 297 * Note: this is not what you want for codepage conversions. See 298 * codecvt for that. 299 * 300 * @param lo Pointer to start of range. 301 * @param hi Pointer to end of range. 302 * @param to Pointer to the destination array. 303 * @return @a hi. 304 */ 305 const char* 306 widen(const char* __lo, const char* __hi, char_type* __to) const 307 { return this->do_widen(__lo, __hi, __to); } 308 309 /** 310 * @brief Narrow char_type to char 311 * 312 * This function converts the char_type to char using the simplest 313 * reasonable transformation. If the conversion fails, dfault is 314 * returned instead. It does so by returning 315 * ctype<char_type>::do_narrow(c). 316 * 317 * Note: this is not what you want for codepage conversions. See 318 * codecvt for that. 319 * 320 * @param c The char_type to convert. 321 * @param dfault Char to return if conversion fails. 322 * @return The converted char. 323 */ 324 char 325 narrow(char_type __c, char __dfault) const 326 { return this->do_narrow(__c, __dfault); } 327 328 /** 329 * @brief Narrow array to char array 330 * 331 * This function converts each char_type in the input to char using the 332 * simplest reasonable transformation and writes the results to the 333 * destination array. For any char_type in the input that cannot be 334 * converted, @a dfault is used instead. It does so by returning 335 * ctype<char_type>::do_narrow(lo, hi, dfault, to). 336 * 337 * Note: this is not what you want for codepage conversions. See 338 * codecvt for that. 339 * 340 * @param lo Pointer to start of range. 341 * @param hi Pointer to end of range. 342 * @param dfault Char to use if conversion fails. 343 * @param to Pointer to the destination array. 344 * @return @a hi. 345 */ 346 const char_type* 347 narrow(const char_type* __lo, const char_type* __hi, 348 char __dfault, char *__to) const 349 { return this->do_narrow(__lo, __hi, __dfault, __to); } 350 351 protected: 352 explicit 353 __ctype_abstract_base(size_t __refs = 0): facet(__refs) { } 354 355 virtual 356 ~__ctype_abstract_base() { } 357 358 /** 359 * @brief Test char_type classification. 360 * 361 * This function finds a mask M for @a c and compares it to mask @a m. 362 * 363 * do_is() is a hook for a derived facet to change the behavior of 364 * classifying. do_is() must always return the same result for the 365 * same input. 366 * 367 * @param c The char_type to find the mask of. 368 * @param m The mask to compare against. 369 * @return (M & m) != 0. 370 */ 371 virtual bool 372 do_is(mask __m, char_type __c) const = 0; 373 374 /** 375 * @brief Return a mask array. 376 * 377 * This function finds the mask for each char_type in the range [lo,hi) 378 * and successively writes it to vec. vec must have as many elements 379 * as the input. 380 * 381 * do_is() is a hook for a derived facet to change the behavior of 382 * classifying. do_is() must always return the same result for the 383 * same input. 384 * 385 * @param lo Pointer to start of range. 386 * @param hi Pointer to end of range. 387 * @param vec Pointer to an array of mask storage. 388 * @return @a hi. 389 */ 390 virtual const char_type* 391 do_is(const char_type* __lo, const char_type* __hi, 392 mask* __vec) const = 0; 393 394 /** 395 * @brief Find char_type matching mask 396 * 397 * This function searches for and returns the first char_type c in 398 * [lo,hi) for which is(m,c) is true. 399 * 400 * do_scan_is() is a hook for a derived facet to change the behavior of 401 * match searching. do_is() must always return the same result for the 402 * same input. 403 * 404 * @param m The mask to compare against. 405 * @param lo Pointer to start of range. 406 * @param hi Pointer to end of range. 407 * @return Pointer to a matching char_type if found, else @a hi. 408 */ 409 virtual const char_type* 410 do_scan_is(mask __m, const char_type* __lo, 411 const char_type* __hi) const = 0; 412 413 /** 414 * @brief Find char_type not matching mask 415 * 416 * This function searches for and returns a pointer to the first 417 * char_type c of [lo,hi) for which is(m,c) is false. 418 * 419 * do_scan_is() is a hook for a derived facet to change the behavior of 420 * match searching. do_is() must always return the same result for the 421 * same input. 422 * 423 * @param m The mask to compare against. 424 * @param lo Pointer to start of range. 425 * @param hi Pointer to end of range. 426 * @return Pointer to a non-matching char_type if found, else @a hi. 427 */ 428 virtual const char_type* 429 do_scan_not(mask __m, const char_type* __lo, 430 const char_type* __hi) const = 0; 431 432 /** 433 * @brief Convert to uppercase. 434 * 435 * This virtual function converts the char_type argument to uppercase 436 * if possible. If not possible (for example, '2'), returns the 437 * argument. 438 * 439 * do_toupper() is a hook for a derived facet to change the behavior of 440 * uppercasing. do_toupper() must always return the same result for 441 * the same input. 442 * 443 * @param c The char_type to convert. 444 * @return The uppercase char_type if convertible, else @a c. 445 */ 446 virtual char_type 447 do_toupper(char_type) const = 0; 448 449 /** 450 * @brief Convert array to uppercase. 451 * 452 * This virtual function converts each char_type in the range [lo,hi) 453 * to uppercase if possible. Other elements remain untouched. 454 * 455 * do_toupper() is a hook for a derived facet to change the behavior of 456 * uppercasing. do_toupper() must always return the same result for 457 * the same input. 458 * 459 * @param lo Pointer to start of range. 460 * @param hi Pointer to end of range. 461 * @return @a hi. 462 */ 463 virtual const char_type* 464 do_toupper(char_type* __lo, const char_type* __hi) const = 0; 465 466 /** 467 * @brief Convert to lowercase. 468 * 469 * This virtual function converts the argument to lowercase if 470 * possible. If not possible (for example, '2'), returns the argument. 471 * 472 * do_tolower() is a hook for a derived facet to change the behavior of 473 * lowercasing. do_tolower() must always return the same result for 474 * the same input. 475 * 476 * @param c The char_type to convert. 477 * @return The lowercase char_type if convertible, else @a c. 478 */ 479 virtual char_type 480 do_tolower(char_type) const = 0; 481 482 /** 483 * @brief Convert array to lowercase. 484 * 485 * This virtual function converts each char_type in the range [lo,hi) 486 * to lowercase if possible. Other elements remain untouched. 487 * 488 * do_tolower() is a hook for a derived facet to change the behavior of 489 * lowercasing. do_tolower() must always return the same result for 490 * the same input. 491 * 492 * @param lo Pointer to start of range. 493 * @param hi Pointer to end of range. 494 * @return @a hi. 495 */ 496 virtual const char_type* 497 do_tolower(char_type* __lo, const char_type* __hi) const = 0; 498 499 /** 500 * @brief Widen char 501 * 502 * This virtual function converts the char to char_type using the 503 * simplest reasonable transformation. 504 * 505 * do_widen() is a hook for a derived facet to change the behavior of 506 * widening. do_widen() must always return the same result for the 507 * same input. 508 * 509 * Note: this is not what you want for codepage conversions. See 510 * codecvt for that. 511 * 512 * @param c The char to convert. 513 * @return The converted char_type 514 */ 515 virtual char_type 516 do_widen(char) const = 0; 517 518 /** 519 * @brief Widen char array 520 * 521 * This function converts each char in the input to char_type using the 522 * simplest reasonable transformation. 523 * 524 * do_widen() is a hook for a derived facet to change the behavior of 525 * widening. do_widen() must always return the same result for the 526 * same input. 527 * 528 * Note: this is not what you want for codepage conversions. See 529 * codecvt for that. 530 * 531 * @param lo Pointer to start range. 532 * @param hi Pointer to end of range. 533 * @param to Pointer to the destination array. 534 * @return @a hi. 535 */ 536 virtual const char* 537 do_widen(const char* __lo, const char* __hi, 538 char_type* __dest) const = 0; 539 540 /** 541 * @brief Narrow char_type to char 542 * 543 * This virtual function converts the argument to char using the 544 * simplest reasonable transformation. If the conversion fails, dfault 545 * is returned instead. 546 * 547 * do_narrow() is a hook for a derived facet to change the behavior of 548 * narrowing. do_narrow() must always return the same result for the 549 * same input. 550 * 551 * Note: this is not what you want for codepage conversions. See 552 * codecvt for that. 553 * 554 * @param c The char_type to convert. 555 * @param dfault Char to return if conversion fails. 556 * @return The converted char. 557 */ 558 virtual char 559 do_narrow(char_type, char __dfault) const = 0; 560 561 /** 562 * @brief Narrow char_type array to char 563 * 564 * This virtual function converts each char_type in the range [lo,hi) to 565 * char using the simplest reasonable transformation and writes the 566 * results to the destination array. For any element in the input that 567 * cannot be converted, @a dfault is used instead. 568 * 569 * do_narrow() is a hook for a derived facet to change the behavior of 570 * narrowing. do_narrow() must always return the same result for the 571 * same input. 572 * 573 * Note: this is not what you want for codepage conversions. See 574 * codecvt for that. 575 * 576 * @param lo Pointer to start of range. 577 * @param hi Pointer to end of range. 578 * @param dfault Char to use if conversion fails. 579 * @param to Pointer to the destination array. 580 * @return @a hi. 581 */ 582 virtual const char_type* 583 do_narrow(const char_type* __lo, const char_type* __hi, 584 char __dfault, char* __dest) const = 0; 585 }; 586 587 /** 588 * @brief Primary class template ctype facet. 589 * @ingroup locales 590 * 591 * This template class defines classification and conversion functions for 592 * character sets. It wraps cctype functionality. Ctype gets used by 593 * streams for many I/O operations. 594 * 595 * This template provides the protected virtual functions the developer 596 * will have to replace in a derived class or specialization to make a 597 * working facet. The public functions that access them are defined in 598 * __ctype_abstract_base, to allow for implementation flexibility. See 599 * ctype<wchar_t> for an example. The functions are documented in 600 * __ctype_abstract_base. 601 * 602 * Note: implementations are provided for all the protected virtual 603 * functions, but will likely not be useful. 604 */ 605 template<typename _CharT> 606 class ctype : public __ctype_abstract_base<_CharT> 607 { 608 public: 609 // Types: 610 typedef _CharT char_type; 611 typedef typename __ctype_abstract_base<_CharT>::mask mask; 612 613 /// The facet id for ctype<char_type> 614 static locale::id id; 615 616 explicit 617 ctype(size_t __refs = 0) : __ctype_abstract_base<_CharT>(__refs) { } 618 619 protected: 620 virtual 621 ~ctype(); 622 623 virtual bool 624 do_is(mask __m, char_type __c) const; 625 626 virtual const char_type* 627 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const; 628 629 virtual const char_type* 630 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const; 631 632 virtual const char_type* 633 do_scan_not(mask __m, const char_type* __lo, 634 const char_type* __hi) const; 635 636 virtual char_type 637 do_toupper(char_type __c) const; 638 639 virtual const char_type* 640 do_toupper(char_type* __lo, const char_type* __hi) const; 641 642 virtual char_type 643 do_tolower(char_type __c) const; 644 645 virtual const char_type* 646 do_tolower(char_type* __lo, const char_type* __hi) const; 647 648 virtual char_type 649 do_widen(char __c) const; 650 651 virtual const char* 652 do_widen(const char* __lo, const char* __hi, char_type* __dest) const; 653 654 virtual char 655 do_narrow(char_type, char __dfault) const; 656 657 virtual const char_type* 658 do_narrow(const char_type* __lo, const char_type* __hi, 659 char __dfault, char* __dest) const; 660 }; 661 662 template<typename _CharT> 663 locale::id ctype<_CharT>::id; 664 665 /** 666 * @brief The ctype<char> specialization. 667 * @ingroup locales 668 * 669 * This class defines classification and conversion functions for 670 * the char type. It gets used by char streams for many I/O 671 * operations. The char specialization provides a number of 672 * optimizations as well. 673 */ 674 template<> 675 class ctype<char> : public locale::facet, public ctype_base 676 { 677 public: 678 // Types: 679 /// Typedef for the template parameter char. 680 typedef char char_type; 681 682 protected: 683 // Data Members: 684 __c_locale _M_c_locale_ctype; 685 bool _M_del; 686 __to_type _M_toupper; 687 __to_type _M_tolower; 688 const mask* _M_table; 689 mutable char _M_widen_ok; 690 mutable char _M_widen[1 + static_cast<unsigned char>(-1)]; 691 mutable char _M_narrow[1 + static_cast<unsigned char>(-1)]; 692 mutable char _M_narrow_ok; // 0 uninitialized, 1 init, 693 // 2 memcpy can't be used 694 695 public: 696 /// The facet id for ctype<char> 697 static locale::id id; 698 /// The size of the mask table. It is SCHAR_MAX + 1. 699 static const size_t table_size = 1 + static_cast<unsigned char>(-1); 700 701 /** 702 * @brief Constructor performs initialization. 703 * 704 * This is the constructor provided by the standard. 705 * 706 * @param table If non-zero, table is used as the per-char mask. 707 * Else classic_table() is used. 708 * @param del If true, passes ownership of table to this facet. 709 * @param refs Passed to the base facet class. 710 */ 711 explicit 712 ctype(const mask* __table = 0, bool __del = false, size_t __refs = 0); 713 714 /** 715 * @brief Constructor performs static initialization. 716 * 717 * This constructor is used to construct the initial C locale facet. 718 * 719 * @param cloc Handle to C locale data. 720 * @param table If non-zero, table is used as the per-char mask. 721 * @param del If true, passes ownership of table to this facet. 722 * @param refs Passed to the base facet class. 723 */ 724 explicit 725 ctype(__c_locale __cloc, const mask* __table = 0, bool __del = false, 726 size_t __refs = 0); 727 728 /** 729 * @brief Test char classification. 730 * 731 * This function compares the mask table[c] to @a m. 732 * 733 * @param c The char to compare the mask of. 734 * @param m The mask to compare against. 735 * @return True if m & table[c] is true, false otherwise. 736 */ 737 inline bool 738 is(mask __m, char __c) const; 739 740 /** 741 * @brief Return a mask array. 742 * 743 * This function finds the mask for each char in the range [lo, hi) and 744 * successively writes it to vec. vec must have as many elements as 745 * the char array. 746 * 747 * @param lo Pointer to start of range. 748 * @param hi Pointer to end of range. 749 * @param vec Pointer to an array of mask storage. 750 * @return @a hi. 751 */ 752 inline const char* 753 is(const char* __lo, const char* __hi, mask* __vec) const; 754 755 /** 756 * @brief Find char matching a mask 757 * 758 * This function searches for and returns the first char in [lo,hi) for 759 * which is(m,char) is true. 760 * 761 * @param m The mask to compare against. 762 * @param lo Pointer to start of range. 763 * @param hi Pointer to end of range. 764 * @return Pointer to a matching char if found, else @a hi. 765 */ 766 inline const char* 767 scan_is(mask __m, const char* __lo, const char* __hi) const; 768 769 /** 770 * @brief Find char not matching a mask 771 * 772 * This function searches for and returns a pointer to the first char 773 * in [lo,hi) for which is(m,char) is false. 774 * 775 * @param m The mask to compare against. 776 * @param lo Pointer to start of range. 777 * @param hi Pointer to end of range. 778 * @return Pointer to a non-matching char if found, else @a hi. 779 */ 780 inline const char* 781 scan_not(mask __m, const char* __lo, const char* __hi) const; 782 783 /** 784 * @brief Convert to uppercase. 785 * 786 * This function converts the char argument to uppercase if possible. 787 * If not possible (for example, '2'), returns the argument. 788 * 789 * toupper() acts as if it returns ctype<char>::do_toupper(c). 790 * do_toupper() must always return the same result for the same input. 791 * 792 * @param c The char to convert. 793 * @return The uppercase char if convertible, else @a c. 794 */ 795 char_type 796 toupper(char_type __c) const 797 { return this->do_toupper(__c); } 798 799 /** 800 * @brief Convert array to uppercase. 801 * 802 * This function converts each char in the range [lo,hi) to uppercase 803 * if possible. Other chars remain untouched. 804 * 805 * toupper() acts as if it returns ctype<char>:: do_toupper(lo, hi). 806 * do_toupper() must always return the same result for the same input. 807 * 808 * @param lo Pointer to first char in range. 809 * @param hi Pointer to end of range. 810 * @return @a hi. 811 */ 812 const char_type* 813 toupper(char_type *__lo, const char_type* __hi) const 814 { return this->do_toupper(__lo, __hi); } 815 816 /** 817 * @brief Convert to lowercase. 818 * 819 * This function converts the char argument to lowercase if possible. 820 * If not possible (for example, '2'), returns the argument. 821 * 822 * tolower() acts as if it returns ctype<char>::do_tolower(c). 823 * do_tolower() must always return the same result for the same input. 824 * 825 * @param c The char to convert. 826 * @return The lowercase char if convertible, else @a c. 827 */ 828 char_type 829 tolower(char_type __c) const 830 { return this->do_tolower(__c); } 831 832 /** 833 * @brief Convert array to lowercase. 834 * 835 * This function converts each char in the range [lo,hi) to lowercase 836 * if possible. Other chars remain untouched. 837 * 838 * tolower() acts as if it returns ctype<char>:: do_tolower(lo, hi). 839 * do_tolower() must always return the same result for the same input. 840 * 841 * @param lo Pointer to first char in range. 842 * @param hi Pointer to end of range. 843 * @return @a hi. 844 */ 845 const char_type* 846 tolower(char_type* __lo, const char_type* __hi) const 847 { return this->do_tolower(__lo, __hi); } 848 849 /** 850 * @brief Widen char 851 * 852 * This function converts the char to char_type using the simplest 853 * reasonable transformation. For an underived ctype<char> facet, the 854 * argument will be returned unchanged. 855 * 856 * This function works as if it returns ctype<char>::do_widen(c). 857 * do_widen() must always return the same result for the same input. 858 * 859 * Note: this is not what you want for codepage conversions. See 860 * codecvt for that. 861 * 862 * @param c The char to convert. 863 * @return The converted character. 864 */ 865 char_type 866 widen(char __c) const 867 { 868 if (_M_widen_ok) 869 return _M_widen[static_cast<unsigned char>(__c)]; 870 this->_M_widen_init(); 871 return this->do_widen(__c); 872 } 873 874 /** 875 * @brief Widen char array 876 * 877 * This function converts each char in the input to char using the 878 * simplest reasonable transformation. For an underived ctype<char> 879 * facet, the argument will be copied unchanged. 880 * 881 * This function works as if it returns ctype<char>::do_widen(c). 882 * do_widen() must always return the same result for the same input. 883 * 884 * Note: this is not what you want for codepage conversions. See 885 * codecvt for that. 886 * 887 * @param lo Pointer to first char in range. 888 * @param hi Pointer to end of range. 889 * @param to Pointer to the destination array. 890 * @return @a hi. 891 */ 892 const char* 893 widen(const char* __lo, const char* __hi, char_type* __to) const 894 { 895 if (_M_widen_ok == 1) 896 { 897 __builtin_memcpy(__to, __lo, __hi - __lo); 898 return __hi; 899 } 900 if (!_M_widen_ok) 901 _M_widen_init(); 902 return this->do_widen(__lo, __hi, __to); 903 } 904 905 /** 906 * @brief Narrow char 907 * 908 * This function converts the char to char using the simplest 909 * reasonable transformation. If the conversion fails, dfault is 910 * returned instead. For an underived ctype<char> facet, @a c 911 * will be returned unchanged. 912 * 913 * This function works as if it returns ctype<char>::do_narrow(c). 914 * do_narrow() must always return the same result for the same input. 915 * 916 * Note: this is not what you want for codepage conversions. See 917 * codecvt for that. 918 * 919 * @param c The char to convert. 920 * @param dfault Char to return if conversion fails. 921 * @return The converted character. 922 */ 923 char 924 narrow(char_type __c, char __dfault) const 925 { 926 if (_M_narrow[static_cast<unsigned char>(__c)]) 927 return _M_narrow[static_cast<unsigned char>(__c)]; 928 const char __t = do_narrow(__c, __dfault); 929 if (__t != __dfault) 930 _M_narrow[static_cast<unsigned char>(__c)] = __t; 931 return __t; 932 } 933 934 /** 935 * @brief Narrow char array 936 * 937 * This function converts each char in the input to char using the 938 * simplest reasonable transformation and writes the results to the 939 * destination array. For any char in the input that cannot be 940 * converted, @a dfault is used instead. For an underived ctype<char> 941 * facet, the argument will be copied unchanged. 942 * 943 * This function works as if it returns ctype<char>::do_narrow(lo, hi, 944 * dfault, to). do_narrow() must always return the same result for the 945 * same input. 946 * 947 * Note: this is not what you want for codepage conversions. See 948 * codecvt for that. 949 * 950 * @param lo Pointer to start of range. 951 * @param hi Pointer to end of range. 952 * @param dfault Char to use if conversion fails. 953 * @param to Pointer to the destination array. 954 * @return @a hi. 955 */ 956 const char_type* 957 narrow(const char_type* __lo, const char_type* __hi, 958 char __dfault, char *__to) const 959 { 960 if (__builtin_expect(_M_narrow_ok == 1, true)) 961 { 962 __builtin_memcpy(__to, __lo, __hi - __lo); 963 return __hi; 964 } 965 if (!_M_narrow_ok) 966 _M_narrow_init(); 967 return this->do_narrow(__lo, __hi, __dfault, __to); 968 } 969 970 // _GLIBCXX_RESOLVE_LIB_DEFECTS 971 // DR 695. ctype<char>::classic_table() not accessible. 972 /// Returns a pointer to the mask table provided to the constructor, or 973 /// the default from classic_table() if none was provided. 974 const mask* 975 table() const throw() 976 { return _M_table; } 977 978 /// Returns a pointer to the C locale mask table. 979 static const mask* 980 classic_table() throw(); 981 protected: 982 983 /** 984 * @brief Destructor. 985 * 986 * This function deletes table() if @a del was true in the 987 * constructor. 988 */ 989 virtual 990 ~ctype(); 991 992 /** 993 * @brief Convert to uppercase. 994 * 995 * This virtual function converts the char argument to uppercase if 996 * possible. If not possible (for example, '2'), returns the argument. 997 * 998 * do_toupper() is a hook for a derived facet to change the behavior of 999 * uppercasing. do_toupper() must always return the same result for 1000 * the same input. 1001 * 1002 * @param c The char to convert. 1003 * @return The uppercase char if convertible, else @a c. 1004 */ 1005 virtual char_type 1006 do_toupper(char_type) const; 1007 1008 /** 1009 * @brief Convert array to uppercase. 1010 * 1011 * This virtual function converts each char in the range [lo,hi) to 1012 * uppercase if possible. Other chars remain untouched. 1013 * 1014 * do_toupper() is a hook for a derived facet to change the behavior of 1015 * uppercasing. do_toupper() must always return the same result for 1016 * the same input. 1017 * 1018 * @param lo Pointer to start of range. 1019 * @param hi Pointer to end of range. 1020 * @return @a hi. 1021 */ 1022 virtual const char_type* 1023 do_toupper(char_type* __lo, const char_type* __hi) const; 1024 1025 /** 1026 * @brief Convert to lowercase. 1027 * 1028 * This virtual function converts the char argument to lowercase if 1029 * possible. If not possible (for example, '2'), returns the argument. 1030 * 1031 * do_tolower() is a hook for a derived facet to change the behavior of 1032 * lowercasing. do_tolower() must always return the same result for 1033 * the same input. 1034 * 1035 * @param c The char to convert. 1036 * @return The lowercase char if convertible, else @a c. 1037 */ 1038 virtual char_type 1039 do_tolower(char_type) const; 1040 1041 /** 1042 * @brief Convert array to lowercase. 1043 * 1044 * This virtual function converts each char in the range [lo,hi) to 1045 * lowercase if possible. Other chars remain untouched. 1046 * 1047 * do_tolower() is a hook for a derived facet to change the behavior of 1048 * lowercasing. do_tolower() must always return the same result for 1049 * the same input. 1050 * 1051 * @param lo Pointer to first char in range. 1052 * @param hi Pointer to end of range. 1053 * @return @a hi. 1054 */ 1055 virtual const char_type* 1056 do_tolower(char_type* __lo, const char_type* __hi) const; 1057 1058 /** 1059 * @brief Widen char 1060 * 1061 * This virtual function converts the char to char using the simplest 1062 * reasonable transformation. For an underived ctype<char> facet, the 1063 * argument will be returned unchanged. 1064 * 1065 * do_widen() is a hook for a derived facet to change the behavior of 1066 * widening. do_widen() must always return the same result for the 1067 * same input. 1068 * 1069 * Note: this is not what you want for codepage conversions. See 1070 * codecvt for that. 1071 * 1072 * @param c The char to convert. 1073 * @return The converted character. 1074 */ 1075 virtual char_type 1076 do_widen(char __c) const 1077 { return __c; } 1078 1079 /** 1080 * @brief Widen char array 1081 * 1082 * This function converts each char in the range [lo,hi) to char using 1083 * the simplest reasonable transformation. For an underived 1084 * ctype<char> facet, the argument will be copied unchanged. 1085 * 1086 * do_widen() is a hook for a derived facet to change the behavior of 1087 * widening. do_widen() must always return the same result for the 1088 * same input. 1089 * 1090 * Note: this is not what you want for codepage conversions. See 1091 * codecvt for that. 1092 * 1093 * @param lo Pointer to start of range. 1094 * @param hi Pointer to end of range. 1095 * @param to Pointer to the destination array. 1096 * @return @a hi. 1097 */ 1098 virtual const char* 1099 do_widen(const char* __lo, const char* __hi, char_type* __dest) const 1100 { 1101 __builtin_memcpy(__dest, __lo, __hi - __lo); 1102 return __hi; 1103 } 1104 1105 /** 1106 * @brief Narrow char 1107 * 1108 * This virtual function converts the char to char using the simplest 1109 * reasonable transformation. If the conversion fails, dfault is 1110 * returned instead. For an underived ctype<char> facet, @a c will be 1111 * returned unchanged. 1112 * 1113 * do_narrow() is a hook for a derived facet to change the behavior of 1114 * narrowing. do_narrow() must always return the same result for the 1115 * same input. 1116 * 1117 * Note: this is not what you want for codepage conversions. See 1118 * codecvt for that. 1119 * 1120 * @param c The char to convert. 1121 * @param dfault Char to return if conversion fails. 1122 * @return The converted char. 1123 */ 1124 virtual char 1125 do_narrow(char_type __c, char) const 1126 { return __c; } 1127 1128 /** 1129 * @brief Narrow char array to char array 1130 * 1131 * This virtual function converts each char in the range [lo,hi) to 1132 * char using the simplest reasonable transformation and writes the 1133 * results to the destination array. For any char in the input that 1134 * cannot be converted, @a dfault is used instead. For an underived 1135 * ctype<char> facet, the argument will be copied unchanged. 1136 * 1137 * do_narrow() is a hook for a derived facet to change the behavior of 1138 * narrowing. do_narrow() must always return the same result for the 1139 * same input. 1140 * 1141 * Note: this is not what you want for codepage conversions. See 1142 * codecvt for that. 1143 * 1144 * @param lo Pointer to start of range. 1145 * @param hi Pointer to end of range. 1146 * @param dfault Char to use if conversion fails. 1147 * @param to Pointer to the destination array. 1148 * @return @a hi. 1149 */ 1150 virtual const char_type* 1151 do_narrow(const char_type* __lo, const char_type* __hi, 1152 char, char* __dest) const 1153 { 1154 __builtin_memcpy(__dest, __lo, __hi - __lo); 1155 return __hi; 1156 } 1157 1158 private: 1159 void _M_narrow_init() const; 1160 /* The following member function is moved from src/ctype.cc. 1161 See comment there for reason why */ 1162 void _M_widen_init() const 1163 { 1164 char __tmp[sizeof(_M_widen)]; 1165 for (size_t __i = 0; __i < sizeof(_M_widen); ++__i) 1166 __tmp[__i] = __i; 1167 do_widen(__tmp, __tmp + sizeof(__tmp), _M_widen); 1168 1169 _M_widen_ok = 1; 1170 // Set _M_widen_ok to 2 if memcpy can't be used. 1171 if (__builtin_memcmp(__tmp, _M_widen, sizeof(_M_widen))) 1172 _M_widen_ok = 2; 1173 } 1174 }; 1175 1176 #ifdef _GLIBCXX_USE_WCHAR_T 1177 /** 1178 * @brief The ctype<wchar_t> specialization. 1179 * @ingroup locales 1180 * 1181 * This class defines classification and conversion functions for the 1182 * wchar_t type. It gets used by wchar_t streams for many I/O operations. 1183 * The wchar_t specialization provides a number of optimizations as well. 1184 * 1185 * ctype<wchar_t> inherits its public methods from 1186 * __ctype_abstract_base<wchar_t>. 1187 */ 1188 template<> 1189 class ctype<wchar_t> : public __ctype_abstract_base<wchar_t> 1190 { 1191 public: 1192 // Types: 1193 /// Typedef for the template parameter wchar_t. 1194 typedef wchar_t char_type; 1195 typedef wctype_t __wmask_type; 1196 1197 protected: 1198 __c_locale _M_c_locale_ctype; 1199 1200 // Pre-computed narrowed and widened chars. 1201 bool _M_narrow_ok; 1202 char _M_narrow[128]; 1203 wint_t _M_widen[1 + static_cast<unsigned char>(-1)]; 1204 1205 // Pre-computed elements for do_is. 1206 mask _M_bit[16]; 1207 __wmask_type _M_wmask[16]; 1208 1209 public: 1210 // Data Members: 1211 /// The facet id for ctype<wchar_t> 1212 static locale::id id; 1213 1214 /** 1215 * @brief Constructor performs initialization. 1216 * 1217 * This is the constructor provided by the standard. 1218 * 1219 * @param refs Passed to the base facet class. 1220 */ 1221 explicit 1222 ctype(size_t __refs = 0); 1223 1224 /** 1225 * @brief Constructor performs static initialization. 1226 * 1227 * This constructor is used to construct the initial C locale facet. 1228 * 1229 * @param cloc Handle to C locale data. 1230 * @param refs Passed to the base facet class. 1231 */ 1232 explicit 1233 ctype(__c_locale __cloc, size_t __refs = 0); 1234 1235 protected: 1236 __wmask_type 1237 _M_convert_to_wmask(const mask __m) const throw(); 1238 1239 /// Destructor 1240 virtual 1241 ~ctype(); 1242 1243 /** 1244 * @brief Test wchar_t classification. 1245 * 1246 * This function finds a mask M for @a c and compares it to mask @a m. 1247 * 1248 * do_is() is a hook for a derived facet to change the behavior of 1249 * classifying. do_is() must always return the same result for the 1250 * same input. 1251 * 1252 * @param c The wchar_t to find the mask of. 1253 * @param m The mask to compare against. 1254 * @return (M & m) != 0. 1255 */ 1256 virtual bool 1257 do_is(mask __m, char_type __c) const; 1258 1259 /** 1260 * @brief Return a mask array. 1261 * 1262 * This function finds the mask for each wchar_t in the range [lo,hi) 1263 * and successively writes it to vec. vec must have as many elements 1264 * as the input. 1265 * 1266 * do_is() is a hook for a derived facet to change the behavior of 1267 * classifying. do_is() must always return the same result for the 1268 * same input. 1269 * 1270 * @param lo Pointer to start of range. 1271 * @param hi Pointer to end of range. 1272 * @param vec Pointer to an array of mask storage. 1273 * @return @a hi. 1274 */ 1275 virtual const char_type* 1276 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const; 1277 1278 /** 1279 * @brief Find wchar_t matching mask 1280 * 1281 * This function searches for and returns the first wchar_t c in 1282 * [lo,hi) for which is(m,c) is true. 1283 * 1284 * do_scan_is() is a hook for a derived facet to change the behavior of 1285 * match searching. do_is() must always return the same result for the 1286 * same input. 1287 * 1288 * @param m The mask to compare against. 1289 * @param lo Pointer to start of range. 1290 * @param hi Pointer to end of range. 1291 * @return Pointer to a matching wchar_t if found, else @a hi. 1292 */ 1293 virtual const char_type* 1294 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const; 1295 1296 /** 1297 * @brief Find wchar_t not matching mask 1298 * 1299 * This function searches for and returns a pointer to the first 1300 * wchar_t c of [lo,hi) for which is(m,c) is false. 1301 * 1302 * do_scan_is() is a hook for a derived facet to change the behavior of 1303 * match searching. do_is() must always return the same result for the 1304 * same input. 1305 * 1306 * @param m The mask to compare against. 1307 * @param lo Pointer to start of range. 1308 * @param hi Pointer to end of range. 1309 * @return Pointer to a non-matching wchar_t if found, else @a hi. 1310 */ 1311 virtual const char_type* 1312 do_scan_not(mask __m, const char_type* __lo, 1313 const char_type* __hi) const; 1314 1315 /** 1316 * @brief Convert to uppercase. 1317 * 1318 * This virtual function converts the wchar_t argument to uppercase if 1319 * possible. If not possible (for example, '2'), returns the argument. 1320 * 1321 * do_toupper() is a hook for a derived facet to change the behavior of 1322 * uppercasing. do_toupper() must always return the same result for 1323 * the same input. 1324 * 1325 * @param c The wchar_t to convert. 1326 * @return The uppercase wchar_t if convertible, else @a c. 1327 */ 1328 virtual char_type 1329 do_toupper(char_type) const; 1330 1331 /** 1332 * @brief Convert array to uppercase. 1333 * 1334 * This virtual function converts each wchar_t in the range [lo,hi) to 1335 * uppercase if possible. Other elements remain untouched. 1336 * 1337 * do_toupper() is a hook for a derived facet to change the behavior of 1338 * uppercasing. do_toupper() must always return the same result for 1339 * the same input. 1340 * 1341 * @param lo Pointer to start of range. 1342 * @param hi Pointer to end of range. 1343 * @return @a hi. 1344 */ 1345 virtual const char_type* 1346 do_toupper(char_type* __lo, const char_type* __hi) const; 1347 1348 /** 1349 * @brief Convert to lowercase. 1350 * 1351 * This virtual function converts the argument to lowercase if 1352 * possible. If not possible (for example, '2'), returns the argument. 1353 * 1354 * do_tolower() is a hook for a derived facet to change the behavior of 1355 * lowercasing. do_tolower() must always return the same result for 1356 * the same input. 1357 * 1358 * @param c The wchar_t to convert. 1359 * @return The lowercase wchar_t if convertible, else @a c. 1360 */ 1361 virtual char_type 1362 do_tolower(char_type) const; 1363 1364 /** 1365 * @brief Convert array to lowercase. 1366 * 1367 * This virtual function converts each wchar_t in the range [lo,hi) to 1368 * lowercase if possible. Other elements remain untouched. 1369 * 1370 * do_tolower() is a hook for a derived facet to change the behavior of 1371 * lowercasing. do_tolower() must always return the same result for 1372 * the same input. 1373 * 1374 * @param lo Pointer to start of range. 1375 * @param hi Pointer to end of range. 1376 * @return @a hi. 1377 */ 1378 virtual const char_type* 1379 do_tolower(char_type* __lo, const char_type* __hi) const; 1380 1381 /** 1382 * @brief Widen char to wchar_t 1383 * 1384 * This virtual function converts the char to wchar_t using the 1385 * simplest reasonable transformation. For an underived ctype<wchar_t> 1386 * facet, the argument will be cast to wchar_t. 1387 * 1388 * do_widen() is a hook for a derived facet to change the behavior of 1389 * widening. do_widen() must always return the same result for the 1390 * same input. 1391 * 1392 * Note: this is not what you want for codepage conversions. See 1393 * codecvt for that. 1394 * 1395 * @param c The char to convert. 1396 * @return The converted wchar_t. 1397 */ 1398 virtual char_type 1399 do_widen(char) const; 1400 1401 /** 1402 * @brief Widen char array to wchar_t array 1403 * 1404 * This function converts each char in the input to wchar_t using the 1405 * simplest reasonable transformation. For an underived ctype<wchar_t> 1406 * facet, the argument will be copied, casting each element to wchar_t. 1407 * 1408 * do_widen() is a hook for a derived facet to change the behavior of 1409 * widening. do_widen() must always return the same result for the 1410 * same input. 1411 * 1412 * Note: this is not what you want for codepage conversions. See 1413 * codecvt for that. 1414 * 1415 * @param lo Pointer to start range. 1416 * @param hi Pointer to end of range. 1417 * @param to Pointer to the destination array. 1418 * @return @a hi. 1419 */ 1420 virtual const char* 1421 do_widen(const char* __lo, const char* __hi, char_type* __dest) const; 1422 1423 /** 1424 * @brief Narrow wchar_t to char 1425 * 1426 * This virtual function converts the argument to char using 1427 * the simplest reasonable transformation. If the conversion 1428 * fails, dfault is returned instead. For an underived 1429 * ctype<wchar_t> facet, @a c will be cast to char and 1430 * returned. 1431 * 1432 * do_narrow() is a hook for a derived facet to change the 1433 * behavior of narrowing. do_narrow() must always return the 1434 * same result for the same input. 1435 * 1436 * Note: this is not what you want for codepage conversions. See 1437 * codecvt for that. 1438 * 1439 * @param c The wchar_t to convert. 1440 * @param dfault Char to return if conversion fails. 1441 * @return The converted char. 1442 */ 1443 virtual char 1444 do_narrow(char_type, char __dfault) const; 1445 1446 /** 1447 * @brief Narrow wchar_t array to char array 1448 * 1449 * This virtual function converts each wchar_t in the range [lo,hi) to 1450 * char using the simplest reasonable transformation and writes the 1451 * results to the destination array. For any wchar_t in the input that 1452 * cannot be converted, @a dfault is used instead. For an underived 1453 * ctype<wchar_t> facet, the argument will be copied, casting each 1454 * element to char. 1455 * 1456 * do_narrow() is a hook for a derived facet to change the behavior of 1457 * narrowing. do_narrow() must always return the same result for the 1458 * same input. 1459 * 1460 * Note: this is not what you want for codepage conversions. See 1461 * codecvt for that. 1462 * 1463 * @param lo Pointer to start of range. 1464 * @param hi Pointer to end of range. 1465 * @param dfault Char to use if conversion fails. 1466 * @param to Pointer to the destination array. 1467 * @return @a hi. 1468 */ 1469 virtual const char_type* 1470 do_narrow(const char_type* __lo, const char_type* __hi, 1471 char __dfault, char* __dest) const; 1472 1473 // For use at construction time only. 1474 void 1475 _M_initialize_ctype() throw(); 1476 }; 1477 #endif //_GLIBCXX_USE_WCHAR_T 1478 1479 /// class ctype_byname [22.2.1.2]. 1480 template<typename _CharT> 1481 class ctype_byname : public ctype<_CharT> 1482 { 1483 public: 1484 typedef typename ctype<_CharT>::mask mask; 1485 1486 explicit 1487 ctype_byname(const char* __s, size_t __refs = 0); 1488 1489 protected: 1490 virtual 1491 ~ctype_byname() { }; 1492 }; 1493 1494 /// 22.2.1.4 Class ctype_byname specializations. 1495 template<> 1496 class ctype_byname<char> : public ctype<char> 1497 { 1498 public: 1499 explicit 1500 ctype_byname(const char* __s, size_t __refs = 0); 1501 1502 protected: 1503 virtual 1504 ~ctype_byname(); 1505 }; 1506 1507 #ifdef _GLIBCXX_USE_WCHAR_T 1508 template<> 1509 class ctype_byname<wchar_t> : public ctype<wchar_t> 1510 { 1511 public: 1512 explicit 1513 ctype_byname(const char* __s, size_t __refs = 0); 1514 1515 protected: 1516 virtual 1517 ~ctype_byname(); 1518 }; 1519 #endif 1520 1521 _GLIBCXX_END_NAMESPACE_VERSION 1522 } // namespace 1523 1524 // Include host and configuration specific ctype inlines. 1525 #include <bits/ctype_inline.h> 1526 1527 namespace std _GLIBCXX_VISIBILITY(default) 1528 { 1529 _GLIBCXX_BEGIN_NAMESPACE_VERSION 1530 1531 // 22.2.2 The numeric category. 1532 class __num_base 1533 { 1534 public: 1535 // NB: Code depends on the order of _S_atoms_out elements. 1536 // Below are the indices into _S_atoms_out. 1537 enum 1538 { 1539 _S_ominus, 1540 _S_oplus, 1541 _S_ox, 1542 _S_oX, 1543 _S_odigits, 1544 _S_odigits_end = _S_odigits + 16, 1545 _S_oudigits = _S_odigits_end, 1546 _S_oudigits_end = _S_oudigits + 16, 1547 _S_oe = _S_odigits + 14, // For scientific notation, 'e' 1548 _S_oE = _S_oudigits + 14, // For scientific notation, 'E' 1549 _S_oend = _S_oudigits_end 1550 }; 1551 1552 // A list of valid numeric literals for output. This array 1553 // contains chars that will be passed through the current locale's 1554 // ctype<_CharT>.widen() and then used to render numbers. 1555 // For the standard "C" locale, this is 1556 // "-+xX0123456789abcdef0123456789ABCDEF". 1557 static const char* _S_atoms_out; 1558 1559 // String literal of acceptable (narrow) input, for num_get. 1560 // "-+xX0123456789abcdefABCDEF" 1561 static const char* _S_atoms_in; 1562 1563 enum 1564 { 1565 _S_iminus, 1566 _S_iplus, 1567 _S_ix, 1568 _S_iX, 1569 _S_izero, 1570 _S_ie = _S_izero + 14, 1571 _S_iE = _S_izero + 20, 1572 _S_iend = 26 1573 }; 1574 1575 // num_put 1576 // Construct and return valid scanf format for floating point types. 1577 static void 1578 _S_format_float(const ios_base& __io, char* __fptr, char __mod) throw(); 1579 }; 1580 1581 template<typename _CharT> 1582 struct __numpunct_cache : public locale::facet 1583 { 1584 const char* _M_grouping; 1585 size_t _M_grouping_size; 1586 bool _M_use_grouping; 1587 const _CharT* _M_truename; 1588 size_t _M_truename_size; 1589 const _CharT* _M_falsename; 1590 size_t _M_falsename_size; 1591 _CharT _M_decimal_point; 1592 _CharT _M_thousands_sep; 1593 1594 // A list of valid numeric literals for output: in the standard 1595 // "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF". 1596 // This array contains the chars after having been passed 1597 // through the current locale's ctype<_CharT>.widen(). 1598 _CharT _M_atoms_out[__num_base::_S_oend]; 1599 1600 // A list of valid numeric literals for input: in the standard 1601 // "C" locale, this is "-+xX0123456789abcdefABCDEF" 1602 // This array contains the chars after having been passed 1603 // through the current locale's ctype<_CharT>.widen(). 1604 _CharT _M_atoms_in[__num_base::_S_iend]; 1605 1606 bool _M_allocated; 1607 1608 __numpunct_cache(size_t __refs = 0) 1609 : facet(__refs), _M_grouping(0), _M_grouping_size(0), 1610 _M_use_grouping(false), 1611 _M_truename(0), _M_truename_size(0), _M_falsename(0), 1612 _M_falsename_size(0), _M_decimal_point(_CharT()), 1613 _M_thousands_sep(_CharT()), _M_allocated(false) 1614 { } 1615 1616 ~__numpunct_cache(); 1617 1618 void 1619 _M_cache(const locale& __loc); 1620 1621 private: 1622 __numpunct_cache& 1623 operator=(const __numpunct_cache&); 1624 1625 explicit 1626 __numpunct_cache(const __numpunct_cache&); 1627 }; 1628 1629 template<typename _CharT> 1630 __numpunct_cache<_CharT>::~__numpunct_cache() 1631 { 1632 if (_M_allocated) 1633 { 1634 delete [] _M_grouping; 1635 delete [] _M_truename; 1636 delete [] _M_falsename; 1637 } 1638 } 1639 1640 /** 1641 * @brief Primary class template numpunct. 1642 * @ingroup locales 1643 * 1644 * This facet stores several pieces of information related to printing and 1645 * scanning numbers, such as the decimal point character. It takes a 1646 * template parameter specifying the char type. The numpunct facet is 1647 * used by streams for many I/O operations involving numbers. 1648 * 1649 * The numpunct template uses protected virtual functions to provide the 1650 * actual results. The public accessors forward the call to the virtual 1651 * functions. These virtual functions are hooks for developers to 1652 * implement the behavior they require from a numpunct facet. 1653 */ 1654 template<typename _CharT> 1655 class numpunct : public locale::facet 1656 { 1657 public: 1658 // Types: 1659 //@{ 1660 /// Public typedefs 1661 typedef _CharT char_type; 1662 typedef basic_string<_CharT> string_type; 1663 //@} 1664 typedef __numpunct_cache<_CharT> __cache_type; 1665 1666 protected: 1667 __cache_type* _M_data; 1668 1669 public: 1670 /// Numpunct facet id. 1671 static locale::id id; 1672 1673 /** 1674 * @brief Numpunct constructor. 1675 * 1676 * @param refs Refcount to pass to the base class. 1677 */ 1678 explicit 1679 numpunct(size_t __refs = 0) 1680 : facet(__refs), _M_data(0) 1681 { _M_initialize_numpunct(); } 1682 1683 /** 1684 * @brief Internal constructor. Not for general use. 1685 * 1686 * This is a constructor for use by the library itself to set up the 1687 * predefined locale facets. 1688 * 1689 * @param cache __numpunct_cache object. 1690 * @param refs Refcount to pass to the base class. 1691 */ 1692 explicit 1693 numpunct(__cache_type* __cache, size_t __refs = 0) 1694 : facet(__refs), _M_data(__cache) 1695 { _M_initialize_numpunct(); } 1696 1697 /** 1698 * @brief Internal constructor. Not for general use. 1699 * 1700 * This is a constructor for use by the library itself to set up new 1701 * locales. 1702 * 1703 * @param cloc The C locale. 1704 * @param refs Refcount to pass to the base class. 1705 */ 1706 explicit 1707 numpunct(__c_locale __cloc, size_t __refs = 0) 1708 : facet(__refs), _M_data(0) 1709 { _M_initialize_numpunct(__cloc); } 1710 1711 /** 1712 * @brief Return decimal point character. 1713 * 1714 * This function returns a char_type to use as a decimal point. It 1715 * does so by returning returning 1716 * numpunct<char_type>::do_decimal_point(). 1717 * 1718 * @return @a char_type representing a decimal point. 1719 */ 1720 char_type 1721 decimal_point() const 1722 { return this->do_decimal_point(); } 1723 1724 /** 1725 * @brief Return thousands separator character. 1726 * 1727 * This function returns a char_type to use as a thousands 1728 * separator. It does so by returning returning 1729 * numpunct<char_type>::do_thousands_sep(). 1730 * 1731 * @return char_type representing a thousands separator. 1732 */ 1733 char_type 1734 thousands_sep() const 1735 { return this->do_thousands_sep(); } 1736 1737 /** 1738 * @brief Return grouping specification. 1739 * 1740 * This function returns a string representing groupings for the 1741 * integer part of a number. Groupings indicate where thousands 1742 * separators should be inserted in the integer part of a number. 1743 * 1744 * Each char in the return string is interpret as an integer 1745 * rather than a character. These numbers represent the number 1746 * of digits in a group. The first char in the string 1747 * represents the number of digits in the least significant 1748 * group. If a char is negative, it indicates an unlimited 1749 * number of digits for the group. If more chars from the 1750 * string are required to group a number, the last char is used 1751 * repeatedly. 1752 * 1753 * For example, if the grouping() returns "\003\002" and is 1754 * applied to the number 123456789, this corresponds to 1755 * 12,34,56,789. Note that if the string was "32", this would 1756 * put more than 50 digits into the least significant group if 1757 * the character set is ASCII. 1758 * 1759 * The string is returned by calling 1760 * numpunct<char_type>::do_grouping(). 1761 * 1762 * @return string representing grouping specification. 1763 */ 1764 string 1765 grouping() const 1766 { return this->do_grouping(); } 1767 1768 /** 1769 * @brief Return string representation of bool true. 1770 * 1771 * This function returns a string_type containing the text 1772 * representation for true bool variables. It does so by calling 1773 * numpunct<char_type>::do_truename(). 1774 * 1775 * @return string_type representing printed form of true. 1776 */ 1777 string_type 1778 truename() const 1779 { return this->do_truename(); } 1780 1781 /** 1782 * @brief Return string representation of bool false. 1783 * 1784 * This function returns a string_type containing the text 1785 * representation for false bool variables. It does so by calling 1786 * numpunct<char_type>::do_falsename(). 1787 * 1788 * @return string_type representing printed form of false. 1789 */ 1790 string_type 1791 falsename() const 1792 { return this->do_falsename(); } 1793 1794 protected: 1795 /// Destructor. 1796 virtual 1797 ~numpunct(); 1798 1799 /** 1800 * @brief Return decimal point character. 1801 * 1802 * Returns a char_type to use as a decimal point. This function is a 1803 * hook for derived classes to change the value returned. 1804 * 1805 * @return @a char_type representing a decimal point. 1806 */ 1807 virtual char_type 1808 do_decimal_point() const 1809 { return _M_data->_M_decimal_point; } 1810 1811 /** 1812 * @brief Return thousands separator character. 1813 * 1814 * Returns a char_type to use as a thousands separator. This function 1815 * is a hook for derived classes to change the value returned. 1816 * 1817 * @return @a char_type representing a thousands separator. 1818 */ 1819 virtual char_type 1820 do_thousands_sep() const 1821 { return _M_data->_M_thousands_sep; } 1822 1823 /** 1824 * @brief Return grouping specification. 1825 * 1826 * Returns a string representing groupings for the integer part of a 1827 * number. This function is a hook for derived classes to change the 1828 * value returned. @see grouping() for details. 1829 * 1830 * @return String representing grouping specification. 1831 */ 1832 virtual string 1833 do_grouping() const 1834 { return _M_data->_M_grouping; } 1835 1836 /** 1837 * @brief Return string representation of bool true. 1838 * 1839 * Returns a string_type containing the text representation for true 1840 * bool variables. This function is a hook for derived classes to 1841 * change the value returned. 1842 * 1843 * @return string_type representing printed form of true. 1844 */ 1845 virtual string_type 1846 do_truename() const 1847 { return _M_data->_M_truename; } 1848 1849 /** 1850 * @brief Return string representation of bool false. 1851 * 1852 * Returns a string_type containing the text representation for false 1853 * bool variables. This function is a hook for derived classes to 1854 * change the value returned. 1855 * 1856 * @return string_type representing printed form of false. 1857 */ 1858 virtual string_type 1859 do_falsename() const 1860 { return _M_data->_M_falsename; } 1861 1862 // For use at construction time only. 1863 void 1864 _M_initialize_numpunct(__c_locale __cloc = 0); 1865 }; 1866 1867 template<typename _CharT> 1868 locale::id numpunct<_CharT>::id; 1869 1870 template<> 1871 numpunct<char>::~numpunct(); 1872 1873 template<> 1874 void 1875 numpunct<char>::_M_initialize_numpunct(__c_locale __cloc); 1876 1877 #ifdef _GLIBCXX_USE_WCHAR_T 1878 template<> 1879 numpunct<wchar_t>::~numpunct(); 1880 1881 template<> 1882 void 1883 numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc); 1884 #endif 1885 1886 /// class numpunct_byname [22.2.3.2]. 1887 template<typename _CharT> 1888 class numpunct_byname : public numpunct<_CharT> 1889 { 1890 public: 1891 typedef _CharT char_type; 1892 typedef basic_string<_CharT> string_type; 1893 1894 explicit 1895 numpunct_byname(const char* __s, size_t __refs = 0) 1896 : numpunct<_CharT>(__refs) 1897 { 1898 if (__builtin_strcmp(__s, "C") != 0 1899 && __builtin_strcmp(__s, "POSIX") != 0) 1900 { 1901 __c_locale __tmp; 1902 this->_S_create_c_locale(__tmp, __s); 1903 this->_M_initialize_numpunct(__tmp); 1904 this->_S_destroy_c_locale(__tmp); 1905 } 1906 } 1907 1908 protected: 1909 virtual 1910 ~numpunct_byname() { } 1911 }; 1912 1913 _GLIBCXX_BEGIN_NAMESPACE_LDBL 1914 1915 /** 1916 * @brief Primary class template num_get. 1917 * @ingroup locales 1918 * 1919 * This facet encapsulates the code to parse and return a number 1920 * from a string. It is used by the istream numeric extraction 1921 * operators. 1922 * 1923 * The num_get template uses protected virtual functions to provide the 1924 * actual results. The public accessors forward the call to the virtual 1925 * functions. These virtual functions are hooks for developers to 1926 * implement the behavior they require from the num_get facet. 1927 */ 1928 template<typename _CharT, typename _InIter> 1929 class num_get : public locale::facet 1930 { 1931 public: 1932 // Types: 1933 //@{ 1934 /// Public typedefs 1935 typedef _CharT char_type; 1936 typedef _InIter iter_type; 1937 //@} 1938 1939 /// Numpunct facet id. 1940 static locale::id id; 1941 1942 /** 1943 * @brief Constructor performs initialization. 1944 * 1945 * This is the constructor provided by the standard. 1946 * 1947 * @param refs Passed to the base facet class. 1948 */ 1949 explicit 1950 num_get(size_t __refs = 0) : facet(__refs) { } 1951 1952 /** 1953 * @brief Numeric parsing. 1954 * 1955 * Parses the input stream into the bool @a v. It does so by calling 1956 * num_get::do_get(). 1957 * 1958 * If ios_base::boolalpha is set, attempts to read 1959 * ctype<CharT>::truename() or ctype<CharT>::falsename(). Sets 1960 * @a v to true or false if successful. Sets err to 1961 * ios_base::failbit if reading the string fails. Sets err to 1962 * ios_base::eofbit if the stream is emptied. 1963 * 1964 * If ios_base::boolalpha is not set, proceeds as with reading a long, 1965 * except if the value is 1, sets @a v to true, if the value is 0, sets 1966 * @a v to false, and otherwise set err to ios_base::failbit. 1967 * 1968 * @param in Start of input stream. 1969 * @param end End of input stream. 1970 * @param io Source of locale and flags. 1971 * @param err Error flags to set. 1972 * @param v Value to format and insert. 1973 * @return Iterator after reading. 1974 */ 1975 iter_type 1976 get(iter_type __in, iter_type __end, ios_base& __io, 1977 ios_base::iostate& __err, bool& __v) const 1978 { return this->do_get(__in, __end, __io, __err, __v); } 1979 1980 //@{ 1981 /** 1982 * @brief Numeric parsing. 1983 * 1984 * Parses the input stream into the integral variable @a v. It does so 1985 * by calling num_get::do_get(). 1986 * 1987 * Parsing is affected by the flag settings in @a io. 1988 * 1989 * The basic parse is affected by the value of io.flags() & 1990 * ios_base::basefield. If equal to ios_base::oct, parses like the 1991 * scanf %o specifier. Else if equal to ios_base::hex, parses like %X 1992 * specifier. Else if basefield equal to 0, parses like the %i 1993 * specifier. Otherwise, parses like %d for signed and %u for unsigned 1994 * types. The matching type length modifier is also used. 1995 * 1996 * Digit grouping is interpreted according to numpunct::grouping() and 1997 * numpunct::thousands_sep(). If the pattern of digit groups isn't 1998 * consistent, sets err to ios_base::failbit. 1999 * 2000 * If parsing the string yields a valid value for @a v, @a v is set. 2001 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered. 2002 * Sets err to ios_base::eofbit if the stream is emptied. 2003 * 2004 * @param in Start of input stream. 2005 * @param end End of input stream. 2006 * @param io Source of locale and flags. 2007 * @param err Error flags to set. 2008 * @param v Value to format and insert. 2009 * @return Iterator after reading. 2010 */ 2011 iter_type 2012 get(iter_type __in, iter_type __end, ios_base& __io, 2013 ios_base::iostate& __err, long& __v) const 2014 { return this->do_get(__in, __end, __io, __err, __v); } 2015 2016 iter_type 2017 get(iter_type __in, iter_type __end, ios_base& __io, 2018 ios_base::iostate& __err, unsigned short& __v) const 2019 { return this->do_get(__in, __end, __io, __err, __v); } 2020 2021 iter_type 2022 get(iter_type __in, iter_type __end, ios_base& __io, 2023 ios_base::iostate& __err, unsigned int& __v) const 2024 { return this->do_get(__in, __end, __io, __err, __v); } 2025 2026 iter_type 2027 get(iter_type __in, iter_type __end, ios_base& __io, 2028 ios_base::iostate& __err, unsigned long& __v) const 2029 { return this->do_get(__in, __end, __io, __err, __v); } 2030 2031 #ifdef _GLIBCXX_USE_LONG_LONG 2032 iter_type 2033 get(iter_type __in, iter_type __end, ios_base& __io, 2034 ios_base::iostate& __err, long long& __v) const 2035 { return this->do_get(__in, __end, __io, __err, __v); } 2036 2037 iter_type 2038 get(iter_type __in, iter_type __end, ios_base& __io, 2039 ios_base::iostate& __err, unsigned long long& __v) const 2040 { return this->do_get(__in, __end, __io, __err, __v); } 2041 #endif 2042 //@} 2043 2044 //@{ 2045 /** 2046 * @brief Numeric parsing. 2047 * 2048 * Parses the input stream into the integral variable @a v. It does so 2049 * by calling num_get::do_get(). 2050 * 2051 * The input characters are parsed like the scanf %g specifier. The 2052 * matching type length modifier is also used. 2053 * 2054 * The decimal point character used is numpunct::decimal_point(). 2055 * Digit grouping is interpreted according to numpunct::grouping() and 2056 * numpunct::thousands_sep(). If the pattern of digit groups isn't 2057 * consistent, sets err to ios_base::failbit. 2058 * 2059 * If parsing the string yields a valid value for @a v, @a v is set. 2060 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered. 2061 * Sets err to ios_base::eofbit if the stream is emptied. 2062 * 2063 * @param in Start of input stream. 2064 * @param end End of input stream. 2065 * @param io Source of locale and flags. 2066 * @param err Error flags to set. 2067 * @param v Value to format and insert. 2068 * @return Iterator after reading. 2069 */ 2070 iter_type 2071 get(iter_type __in, iter_type __end, ios_base& __io, 2072 ios_base::iostate& __err, float& __v) const 2073 { return this->do_get(__in, __end, __io, __err, __v); } 2074 2075 iter_type 2076 get(iter_type __in, iter_type __end, ios_base& __io, 2077 ios_base::iostate& __err, double& __v) const 2078 { return this->do_get(__in, __end, __io, __err, __v); } 2079 2080 iter_type 2081 get(iter_type __in, iter_type __end, ios_base& __io, 2082 ios_base::iostate& __err, long double& __v) const 2083 { return this->do_get(__in, __end, __io, __err, __v); } 2084 //@} 2085 2086 /** 2087 * @brief Numeric parsing. 2088 * 2089 * Parses the input stream into the pointer variable @a v. It does so 2090 * by calling num_get::do_get(). 2091 * 2092 * The input characters are parsed like the scanf %p specifier. 2093 * 2094 * Digit grouping is interpreted according to numpunct::grouping() and 2095 * numpunct::thousands_sep(). If the pattern of digit groups isn't 2096 * consistent, sets err to ios_base::failbit. 2097 * 2098 * Note that the digit grouping effect for pointers is a bit ambiguous 2099 * in the standard and shouldn't be relied on. See DR 344. 2100 * 2101 * If parsing the string yields a valid value for @a v, @a v is set. 2102 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered. 2103 * Sets err to ios_base::eofbit if the stream is emptied. 2104 * 2105 * @param in Start of input stream. 2106 * @param end End of input stream. 2107 * @param io Source of locale and flags. 2108 * @param err Error flags to set. 2109 * @param v Value to format and insert. 2110 * @return Iterator after reading. 2111 */ 2112 iter_type 2113 get(iter_type __in, iter_type __end, ios_base& __io, 2114 ios_base::iostate& __err, void*& __v) const 2115 { return this->do_get(__in, __end, __io, __err, __v); } 2116 2117 protected: 2118 /// Destructor. 2119 virtual ~num_get() { } 2120 2121 iter_type 2122 _M_extract_float(iter_type, iter_type, ios_base&, ios_base::iostate&, 2123 string&) const; 2124 2125 template<typename _ValueT> 2126 iter_type 2127 _M_extract_int(iter_type, iter_type, ios_base&, ios_base::iostate&, 2128 _ValueT&) const; 2129 2130 template<typename _CharT2> 2131 typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, int>::__type 2132 _M_find(const _CharT2*, size_t __len, _CharT2 __c) const 2133 { 2134 int __ret = -1; 2135 if (__len <= 10) 2136 { 2137 if (__c >= _CharT2('0') && __c < _CharT2(_CharT2('0') + __len)) 2138 __ret = __c - _CharT2('0'); 2139 } 2140 else 2141 { 2142 if (__c >= _CharT2('0') && __c <= _CharT2('9')) 2143 __ret = __c - _CharT2('0'); 2144 else if (__c >= _CharT2('a') && __c <= _CharT2('f')) 2145 __ret = 10 + (__c - _CharT2('a')); 2146 else if (__c >= _CharT2('A') && __c <= _CharT2('F')) 2147 __ret = 10 + (__c - _CharT2('A')); 2148 } 2149 return __ret; 2150 } 2151 2152 template<typename _CharT2> 2153 typename __gnu_cxx::__enable_if<!__is_char<_CharT2>::__value, 2154 int>::__type 2155 _M_find(const _CharT2* __zero, size_t __len, _CharT2 __c) const 2156 { 2157 int __ret = -1; 2158 const char_type* __q = char_traits<_CharT2>::find(__zero, __len, __c); 2159 if (__q) 2160 { 2161 __ret = __q - __zero; 2162 if (__ret > 15) 2163 __ret -= 6; 2164 } 2165 return __ret; 2166 } 2167 2168 //@{ 2169 /** 2170 * @brief Numeric parsing. 2171 * 2172 * Parses the input stream into the variable @a v. This function is a 2173 * hook for derived classes to change the value returned. @see get() 2174 * for more details. 2175 * 2176 * @param in Start of input stream. 2177 * @param end End of input stream. 2178 * @param io Source of locale and flags. 2179 * @param err Error flags to set. 2180 * @param v Value to format and insert. 2181 * @return Iterator after reading. 2182 */ 2183 virtual iter_type 2184 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const; 2185 2186 virtual iter_type 2187 do_get(iter_type __beg, iter_type __end, ios_base& __io, 2188 ios_base::iostate& __err, long& __v) const 2189 { return _M_extract_int(__beg, __end, __io, __err, __v); } 2190 2191 virtual iter_type 2192 do_get(iter_type __beg, iter_type __end, ios_base& __io, 2193 ios_base::iostate& __err, unsigned short& __v) const 2194 { return _M_extract_int(__beg, __end, __io, __err, __v); } 2195 2196 virtual iter_type 2197 do_get(iter_type __beg, iter_type __end, ios_base& __io, 2198 ios_base::iostate& __err, unsigned int& __v) const 2199 { return _M_extract_int(__beg, __end, __io, __err, __v); } 2200 2201 virtual iter_type 2202 do_get(iter_type __beg, iter_type __end, ios_base& __io, 2203 ios_base::iostate& __err, unsigned long& __v) const 2204 { return _M_extract_int(__beg, __end, __io, __err, __v); } 2205 2206 #ifdef _GLIBCXX_USE_LONG_LONG 2207 virtual iter_type 2208 do_get(iter_type __beg, iter_type __end, ios_base& __io, 2209 ios_base::iostate& __err, long long& __v) const 2210 { return _M_extract_int(__beg, __end, __io, __err, __v); } 2211 2212 virtual iter_type 2213 do_get(iter_type __beg, iter_type __end, ios_base& __io, 2214 ios_base::iostate& __err, unsigned long long& __v) const 2215 { return _M_extract_int(__beg, __end, __io, __err, __v); } 2216 #endif 2217 2218 virtual iter_type 2219 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err, 2220 float&) const; 2221 2222 virtual iter_type 2223 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err, 2224 double&) const; 2225 2226 // XXX GLIBCXX_ABI Deprecated 2227 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 2228 virtual iter_type 2229 __do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err, 2230 double&) const; 2231 #else 2232 virtual iter_type 2233 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err, 2234 long double&) const; 2235 #endif 2236 2237 virtual iter_type 2238 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err, 2239 void*&) const; 2240 2241 // XXX GLIBCXX_ABI Deprecated 2242 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 2243 virtual iter_type 2244 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err, 2245 long double&) const; 2246 #endif 2247 //@} 2248 }; 2249 2250 template<typename _CharT, typename _InIter> 2251 locale::id num_get<_CharT, _InIter>::id; 2252 2253 2254 /** 2255 * @brief Primary class template num_put. 2256 * @ingroup locales 2257 * 2258 * This facet encapsulates the code to convert a number to a string. It is 2259 * used by the ostream numeric insertion operators. 2260 * 2261 * The num_put template uses protected virtual functions to provide the 2262 * actual results. The public accessors forward the call to the virtual 2263 * functions. These virtual functions are hooks for developers to 2264 * implement the behavior they require from the num_put facet. 2265 */ 2266 template<typename _CharT, typename _OutIter> 2267 class num_put : public locale::facet 2268 { 2269 public: 2270 // Types: 2271 //@{ 2272 /// Public typedefs 2273 typedef _CharT char_type; 2274 typedef _OutIter iter_type; 2275 //@} 2276 2277 /// Numpunct facet id. 2278 static locale::id id; 2279 2280 /** 2281 * @brief Constructor performs initialization. 2282 * 2283 * This is the constructor provided by the standard. 2284 * 2285 * @param refs Passed to the base facet class. 2286 */ 2287 explicit 2288 num_put(size_t __refs = 0) : facet(__refs) { } 2289 2290 /** 2291 * @brief Numeric formatting. 2292 * 2293 * Formats the boolean @a v and inserts it into a stream. It does so 2294 * by calling num_put::do_put(). 2295 * 2296 * If ios_base::boolalpha is set, writes ctype<CharT>::truename() or 2297 * ctype<CharT>::falsename(). Otherwise formats @a v as an int. 2298 * 2299 * @param s Stream to write to. 2300 * @param io Source of locale and flags. 2301 * @param fill Char_type to use for filling. 2302 * @param v Value to format and insert. 2303 * @return Iterator after writing. 2304 */ 2305 iter_type 2306 put(iter_type __s, ios_base& __f, char_type __fill, bool __v) const 2307 { return this->do_put(__s, __f, __fill, __v); } 2308 2309 //@{ 2310 /** 2311 * @brief Numeric formatting. 2312 * 2313 * Formats the integral value @a v and inserts it into a 2314 * stream. It does so by calling num_put::do_put(). 2315 * 2316 * Formatting is affected by the flag settings in @a io. 2317 * 2318 * The basic format is affected by the value of io.flags() & 2319 * ios_base::basefield. If equal to ios_base::oct, formats like the 2320 * printf %o specifier. Else if equal to ios_base::hex, formats like 2321 * %x or %X with ios_base::uppercase unset or set respectively. 2322 * Otherwise, formats like %d, %ld, %lld for signed and %u, %lu, %llu 2323 * for unsigned values. Note that if both oct and hex are set, neither 2324 * will take effect. 2325 * 2326 * If ios_base::showpos is set, '+' is output before positive values. 2327 * If ios_base::showbase is set, '0' precedes octal values (except 0) 2328 * and '0[xX]' precedes hex values. 2329 * 2330 * Thousands separators are inserted according to numpunct::grouping() 2331 * and numpunct::thousands_sep(). The decimal point character used is 2332 * numpunct::decimal_point(). 2333 * 2334 * If io.width() is non-zero, enough @a fill characters are inserted to 2335 * make the result at least that wide. If 2336 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is 2337 * padded at the end. If ios_base::internal, then padding occurs 2338 * immediately after either a '+' or '-' or after '0x' or '0X'. 2339 * Otherwise, padding occurs at the beginning. 2340 * 2341 * @param s Stream to write to. 2342 * @param io Source of locale and flags. 2343 * @param fill Char_type to use for filling. 2344 * @param v Value to format and insert. 2345 * @return Iterator after writing. 2346 */ 2347 iter_type 2348 put(iter_type __s, ios_base& __f, char_type __fill, long __v) const 2349 { return this->do_put(__s, __f, __fill, __v); } 2350 2351 iter_type 2352 put(iter_type __s, ios_base& __f, char_type __fill, 2353 unsigned long __v) const 2354 { return this->do_put(__s, __f, __fill, __v); } 2355 2356 #ifdef _GLIBCXX_USE_LONG_LONG 2357 iter_type 2358 put(iter_type __s, ios_base& __f, char_type __fill, long long __v) const 2359 { return this->do_put(__s, __f, __fill, __v); } 2360 2361 iter_type 2362 put(iter_type __s, ios_base& __f, char_type __fill, 2363 unsigned long long __v) const 2364 { return this->do_put(__s, __f, __fill, __v); } 2365 #endif 2366 //@} 2367 2368 //@{ 2369 /** 2370 * @brief Numeric formatting. 2371 * 2372 * Formats the floating point value @a v and inserts it into a stream. 2373 * It does so by calling num_put::do_put(). 2374 * 2375 * Formatting is affected by the flag settings in @a io. 2376 * 2377 * The basic format is affected by the value of io.flags() & 2378 * ios_base::floatfield. If equal to ios_base::fixed, formats like the 2379 * printf %f specifier. Else if equal to ios_base::scientific, formats 2380 * like %e or %E with ios_base::uppercase unset or set respectively. 2381 * Otherwise, formats like %g or %G depending on uppercase. Note that 2382 * if both fixed and scientific are set, the effect will also be like 2383 * %g or %G. 2384 * 2385 * The output precision is given by io.precision(). This precision is 2386 * capped at numeric_limits::digits10 + 2 (different for double and 2387 * long double). The default precision is 6. 2388 * 2389 * If ios_base::showpos is set, '+' is output before positive values. 2390 * If ios_base::showpoint is set, a decimal point will always be 2391 * output. 2392 * 2393 * Thousands separators are inserted according to numpunct::grouping() 2394 * and numpunct::thousands_sep(). The decimal point character used is 2395 * numpunct::decimal_point(). 2396 * 2397 * If io.width() is non-zero, enough @a fill characters are inserted to 2398 * make the result at least that wide. If 2399 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is 2400 * padded at the end. If ios_base::internal, then padding occurs 2401 * immediately after either a '+' or '-' or after '0x' or '0X'. 2402 * Otherwise, padding occurs at the beginning. 2403 * 2404 * @param s Stream to write to. 2405 * @param io Source of locale and flags. 2406 * @param fill Char_type to use for filling. 2407 * @param v Value to format and insert. 2408 * @return Iterator after writing. 2409 */ 2410 iter_type 2411 put(iter_type __s, ios_base& __f, char_type __fill, double __v) const 2412 { return this->do_put(__s, __f, __fill, __v); } 2413 2414 iter_type 2415 put(iter_type __s, ios_base& __f, char_type __fill, 2416 long double __v) const 2417 { return this->do_put(__s, __f, __fill, __v); } 2418 //@} 2419 2420 /** 2421 * @brief Numeric formatting. 2422 * 2423 * Formats the pointer value @a v and inserts it into a stream. It 2424 * does so by calling num_put::do_put(). 2425 * 2426 * This function formats @a v as an unsigned long with ios_base::hex 2427 * and ios_base::showbase set. 2428 * 2429 * @param s Stream to write to. 2430 * @param io Source of locale and flags. 2431 * @param fill Char_type to use for filling. 2432 * @param v Value to format and insert. 2433 * @return Iterator after writing. 2434 */ 2435 iter_type 2436 put(iter_type __s, ios_base& __f, char_type __fill, 2437 const void* __v) const 2438 { return this->do_put(__s, __f, __fill, __v); } 2439 2440 protected: 2441 template<typename _ValueT> 2442 iter_type 2443 _M_insert_float(iter_type, ios_base& __io, char_type __fill, 2444 char __mod, _ValueT __v) const; 2445 2446 void 2447 _M_group_float(const char* __grouping, size_t __grouping_size, 2448 char_type __sep, const char_type* __p, char_type* __new, 2449 char_type* __cs, int& __len) const; 2450 2451 template<typename _ValueT> 2452 iter_type 2453 _M_insert_int(iter_type, ios_base& __io, char_type __fill, 2454 _ValueT __v) const; 2455 2456 void 2457 _M_group_int(const char* __grouping, size_t __grouping_size, 2458 char_type __sep, ios_base& __io, char_type* __new, 2459 char_type* __cs, int& __len) const; 2460 2461 void 2462 _M_pad(char_type __fill, streamsize __w, ios_base& __io, 2463 char_type* __new, const char_type* __cs, int& __len) const; 2464 2465 /// Destructor. 2466 virtual 2467 ~num_put() { }; 2468 2469 //@{ 2470 /** 2471 * @brief Numeric formatting. 2472 * 2473 * These functions do the work of formatting numeric values and 2474 * inserting them into a stream. This function is a hook for derived 2475 * classes to change the value returned. 2476 * 2477 * @param s Stream to write to. 2478 * @param io Source of locale and flags. 2479 * @param fill Char_type to use for filling. 2480 * @param v Value to format and insert. 2481 * @return Iterator after writing. 2482 */ 2483 virtual iter_type 2484 do_put(iter_type, ios_base&, char_type __fill, bool __v) const; 2485 2486 virtual iter_type 2487 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const 2488 { return _M_insert_int(__s, __io, __fill, __v); } 2489 2490 virtual iter_type 2491 do_put(iter_type __s, ios_base& __io, char_type __fill, 2492 unsigned long __v) const 2493 { return _M_insert_int(__s, __io, __fill, __v); } 2494 2495 #ifdef _GLIBCXX_USE_LONG_LONG 2496 virtual iter_type 2497 do_put(iter_type __s, ios_base& __io, char_type __fill, 2498 long long __v) const 2499 { return _M_insert_int(__s, __io, __fill, __v); } 2500 2501 virtual iter_type 2502 do_put(iter_type __s, ios_base& __io, char_type __fill, 2503 unsigned long long __v) const 2504 { return _M_insert_int(__s, __io, __fill, __v); } 2505 #endif 2506 2507 virtual iter_type 2508 do_put(iter_type, ios_base&, char_type __fill, double __v) const; 2509 2510 // XXX GLIBCXX_ABI Deprecated 2511 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 2512 virtual iter_type 2513 __do_put(iter_type, ios_base&, char_type __fill, double __v) const; 2514 #else 2515 virtual iter_type 2516 do_put(iter_type, ios_base&, char_type __fill, long double __v) const; 2517 #endif 2518 2519 virtual iter_type 2520 do_put(iter_type, ios_base&, char_type __fill, const void* __v) const; 2521 2522 // XXX GLIBCXX_ABI Deprecated 2523 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 2524 virtual iter_type 2525 do_put(iter_type, ios_base&, char_type __fill, long double __v) const; 2526 #endif 2527 //@} 2528 }; 2529 2530 template <typename _CharT, typename _OutIter> 2531 locale::id num_put<_CharT, _OutIter>::id; 2532 2533 _GLIBCXX_END_NAMESPACE_LDBL 2534 2535 // Subclause convenience interfaces, inlines. 2536 // NB: These are inline because, when used in a loop, some compilers 2537 // can hoist the body out of the loop; then it's just as fast as the 2538 // C is*() function. 2539 2540 /// Convenience interface to ctype.is(ctype_base::space, __c). 2541 template<typename _CharT> 2542 inline bool 2543 isspace(_CharT __c, const locale& __loc) 2544 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); } 2545 2546 /// Convenience interface to ctype.is(ctype_base::print, __c). 2547 template<typename _CharT> 2548 inline bool 2549 isprint(_CharT __c, const locale& __loc) 2550 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); } 2551 2552 /// Convenience interface to ctype.is(ctype_base::cntrl, __c). 2553 template<typename _CharT> 2554 inline bool 2555 iscntrl(_CharT __c, const locale& __loc) 2556 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); } 2557 2558 /// Convenience interface to ctype.is(ctype_base::upper, __c). 2559 template<typename _CharT> 2560 inline bool 2561 isupper(_CharT __c, const locale& __loc) 2562 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); } 2563 2564 /// Convenience interface to ctype.is(ctype_base::lower, __c). 2565 template<typename _CharT> 2566 inline bool 2567 islower(_CharT __c, const locale& __loc) 2568 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); } 2569 2570 /// Convenience interface to ctype.is(ctype_base::alpha, __c). 2571 template<typename _CharT> 2572 inline bool 2573 isalpha(_CharT __c, const locale& __loc) 2574 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); } 2575 2576 /// Convenience interface to ctype.is(ctype_base::digit, __c). 2577 template<typename _CharT> 2578 inline bool 2579 isdigit(_CharT __c, const locale& __loc) 2580 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); } 2581 2582 /// Convenience interface to ctype.is(ctype_base::punct, __c). 2583 template<typename _CharT> 2584 inline bool 2585 ispunct(_CharT __c, const locale& __loc) 2586 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); } 2587 2588 /// Convenience interface to ctype.is(ctype_base::xdigit, __c). 2589 template<typename _CharT> 2590 inline bool 2591 isxdigit(_CharT __c, const locale& __loc) 2592 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); } 2593 2594 /// Convenience interface to ctype.is(ctype_base::alnum, __c). 2595 template<typename _CharT> 2596 inline bool 2597 isalnum(_CharT __c, const locale& __loc) 2598 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); } 2599 2600 /// Convenience interface to ctype.is(ctype_base::graph, __c). 2601 template<typename _CharT> 2602 inline bool 2603 isgraph(_CharT __c, const locale& __loc) 2604 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); } 2605 2606 /// Convenience interface to ctype.toupper(__c). 2607 template<typename _CharT> 2608 inline _CharT 2609 toupper(_CharT __c, const locale& __loc) 2610 { return use_facet<ctype<_CharT> >(__loc).toupper(__c); } 2611 2612 /// Convenience interface to ctype.tolower(__c). 2613 template<typename _CharT> 2614 inline _CharT 2615 tolower(_CharT __c, const locale& __loc) 2616 { return use_facet<ctype<_CharT> >(__loc).tolower(__c); } 2617 2618 _GLIBCXX_END_NAMESPACE_VERSION 2619 } // namespace 2620 2621 # include <bits/locale_facets.tcc> 2622 2623 #endif 2624