| 1 | // Locale support -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 1997-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | /** @file bits/locale_facets.tcc |
| 26 | * This is an internal header file, included by other library headers. |
| 27 | * Do not attempt to use it directly. @headername{locale} |
| 28 | */ |
| 29 | |
| 30 | #ifndef _LOCALE_FACETS_TCC |
| 31 | #define _LOCALE_FACETS_TCC 1 |
| 32 | |
| 33 | #ifdef _GLIBCXX_SYSHDR |
| 34 | #pragma GCC system_header |
| 35 | #endif |
| 36 | #pragma GCC diagnostic push |
| 37 | #pragma GCC diagnostic ignored "-Wc++11-extensions" // extern template |
| 38 | |
| 39 | namespace std _GLIBCXX_VISIBILITY(default) |
| 40 | { |
| 41 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 42 | |
| 43 | // Routine to access a cache for the facet. If the cache didn't |
| 44 | // exist before, it gets constructed on the fly. |
| 45 | template<typename _Facet> |
| 46 | struct __use_cache |
| 47 | { |
| 48 | const _Facet* |
| 49 | operator() (const locale& __loc) const; |
| 50 | }; |
| 51 | |
| 52 | // Specializations. |
| 53 | template<typename _CharT> |
| 54 | struct __use_cache<__numpunct_cache<_CharT> > |
| 55 | { |
| 56 | const __numpunct_cache<_CharT>* |
| 57 | operator() (const locale& __loc) const |
| 58 | { |
| 59 | const size_t __i = numpunct<_CharT>::id._M_id(); |
| 60 | const locale::facet** __caches = __loc._M_impl->_M_caches; |
| 61 | if (!__caches[__i]) |
| 62 | { |
| 63 | __numpunct_cache<_CharT>* __tmp = 0; |
| 64 | __try |
| 65 | { |
| 66 | __tmp = new __numpunct_cache<_CharT>; |
| 67 | __tmp->_M_cache(__loc); |
| 68 | } |
| 69 | __catch(...) |
| 70 | { |
| 71 | delete __tmp; |
| 72 | __throw_exception_again; |
| 73 | } |
| 74 | __loc._M_impl->_M_install_cache(__tmp, __i); |
| 75 | } |
| 76 | return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | template<typename _CharT> |
| 81 | void |
| 82 | __numpunct_cache<_CharT>::_M_cache(const locale& __loc) |
| 83 | { |
| 84 | const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc); |
| 85 | |
| 86 | char* __grouping = 0; |
| 87 | _CharT* __truename = 0; |
| 88 | _CharT* __falsename = 0; |
| 89 | __try |
| 90 | { |
| 91 | const string& __g = __np.grouping(); |
| 92 | _M_grouping_size = __g.size(); |
| 93 | __grouping = new char[_M_grouping_size]; |
| 94 | __g.copy(s: __grouping, n: _M_grouping_size); |
| 95 | _M_use_grouping = (_M_grouping_size |
| 96 | && static_cast<signed char>(__grouping[0]) > 0 |
| 97 | && (__grouping[0] |
| 98 | != __gnu_cxx::__numeric_traits<char>::__max)); |
| 99 | |
| 100 | const basic_string<_CharT>& __tn = __np.truename(); |
| 101 | _M_truename_size = __tn.size(); |
| 102 | __truename = new _CharT[_M_truename_size]; |
| 103 | __tn.copy(__truename, _M_truename_size); |
| 104 | |
| 105 | const basic_string<_CharT>& __fn = __np.falsename(); |
| 106 | _M_falsename_size = __fn.size(); |
| 107 | __falsename = new _CharT[_M_falsename_size]; |
| 108 | __fn.copy(__falsename, _M_falsename_size); |
| 109 | |
| 110 | _M_decimal_point = __np.decimal_point(); |
| 111 | _M_thousands_sep = __np.thousands_sep(); |
| 112 | |
| 113 | const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc); |
| 114 | __ct.widen(__num_base::_S_atoms_out, |
| 115 | __num_base::_S_atoms_out |
| 116 | + __num_base::_S_oend, _M_atoms_out); |
| 117 | __ct.widen(__num_base::_S_atoms_in, |
| 118 | __num_base::_S_atoms_in |
| 119 | + __num_base::_S_iend, _M_atoms_in); |
| 120 | |
| 121 | _M_grouping = __grouping; |
| 122 | _M_truename = __truename; |
| 123 | _M_falsename = __falsename; |
| 124 | _M_allocated = true; |
| 125 | } |
| 126 | __catch(...) |
| 127 | { |
| 128 | delete [] __grouping; |
| 129 | delete [] __truename; |
| 130 | delete [] __falsename; |
| 131 | __throw_exception_again; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Used by both numeric and monetary facets. |
| 136 | // Check to make sure that the __grouping_tmp string constructed in |
| 137 | // money_get or num_get matches the canonical grouping for a given |
| 138 | // locale. |
| 139 | // __grouping_tmp is parsed L to R |
| 140 | // 1,222,444 == __grouping_tmp of "\1\3\3" |
| 141 | // __grouping is parsed R to L |
| 142 | // 1,222,444 == __grouping of "\3" == "\3\3\3" |
| 143 | _GLIBCXX_PURE bool |
| 144 | __verify_grouping(const char* __grouping, size_t __grouping_size, |
| 145 | const string& __grouping_tmp) throw (); |
| 146 | |
| 147 | _GLIBCXX_BEGIN_NAMESPACE_LDBL |
| 148 | |
| 149 | template<typename _CharT, typename _InIter> |
| 150 | _GLIBCXX_DEFAULT_ABI_TAG |
| 151 | _InIter |
| 152 | num_get<_CharT, _InIter>:: |
| 153 | (_InIter __beg, _InIter __end, ios_base& __io, |
| 154 | ios_base::iostate& __err, string& __xtrc) const |
| 155 | { |
| 156 | typedef char_traits<_CharT> __traits_type; |
| 157 | typedef __numpunct_cache<_CharT> __cache_type; |
| 158 | __use_cache<__cache_type> __uc; |
| 159 | const locale& __loc = __io._M_getloc(); |
| 160 | const __cache_type* __lc = __uc(__loc); |
| 161 | const _CharT* __lit = __lc->_M_atoms_in; |
| 162 | char_type __c = char_type(); |
| 163 | |
| 164 | // True if __beg becomes equal to __end. |
| 165 | bool __testeof = __beg == __end; |
| 166 | |
| 167 | // First check for sign. |
| 168 | if (!__testeof) |
| 169 | { |
| 170 | __c = *__beg; |
| 171 | const bool __plus = __c == __lit[__num_base::_S_iplus]; |
| 172 | if ((__plus || __c == __lit[__num_base::_S_iminus]) |
| 173 | && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep) |
| 174 | && !(__c == __lc->_M_decimal_point)) |
| 175 | { |
| 176 | __xtrc += __plus ? '+' : '-'; |
| 177 | if (++__beg != __end) |
| 178 | __c = *__beg; |
| 179 | else |
| 180 | __testeof = true; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Next, look for leading zeros. |
| 185 | bool __found_mantissa = false; |
| 186 | int __sep_pos = 0; |
| 187 | while (!__testeof) |
| 188 | { |
| 189 | if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep) |
| 190 | || __c == __lc->_M_decimal_point) |
| 191 | break; |
| 192 | else if (__c == __lit[__num_base::_S_izero]) |
| 193 | { |
| 194 | if (!__found_mantissa) |
| 195 | { |
| 196 | __xtrc += '0'; |
| 197 | __found_mantissa = true; |
| 198 | } |
| 199 | ++__sep_pos; |
| 200 | |
| 201 | if (++__beg != __end) |
| 202 | __c = *__beg; |
| 203 | else |
| 204 | __testeof = true; |
| 205 | } |
| 206 | else |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | // Only need acceptable digits for floating point numbers. |
| 211 | bool __found_dec = false; |
| 212 | bool __found_sci = false; |
| 213 | string __found_grouping; |
| 214 | if (__lc->_M_use_grouping) |
| 215 | __found_grouping.reserve(res: 32); |
| 216 | const char_type* __lit_zero = __lit + __num_base::_S_izero; |
| 217 | |
| 218 | if (!__lc->_M_allocated) |
| 219 | // "C" locale |
| 220 | while (!__testeof) |
| 221 | { |
| 222 | const int __digit = _M_find(__lit_zero, 10, __c); |
| 223 | if (__digit != -1) |
| 224 | { |
| 225 | __xtrc += '0' + __digit; |
| 226 | __found_mantissa = true; |
| 227 | } |
| 228 | else if (__c == __lc->_M_decimal_point |
| 229 | && !__found_dec && !__found_sci) |
| 230 | { |
| 231 | __xtrc += '.'; |
| 232 | __found_dec = true; |
| 233 | } |
| 234 | else if ((__c == __lit[__num_base::_S_ie] |
| 235 | || __c == __lit[__num_base::_S_iE]) |
| 236 | && !__found_sci && __found_mantissa) |
| 237 | { |
| 238 | // Scientific notation. |
| 239 | __xtrc += 'e'; |
| 240 | __found_sci = true; |
| 241 | |
| 242 | // Remove optional plus or minus sign, if they exist. |
| 243 | if (++__beg != __end) |
| 244 | { |
| 245 | __c = *__beg; |
| 246 | const bool __plus = __c == __lit[__num_base::_S_iplus]; |
| 247 | if (__plus || __c == __lit[__num_base::_S_iminus]) |
| 248 | __xtrc += __plus ? '+' : '-'; |
| 249 | else |
| 250 | continue; |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | __testeof = true; |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | else |
| 259 | break; |
| 260 | |
| 261 | if (++__beg != __end) |
| 262 | __c = *__beg; |
| 263 | else |
| 264 | __testeof = true; |
| 265 | } |
| 266 | else |
| 267 | while (!__testeof) |
| 268 | { |
| 269 | // According to 22.2.2.1.2, p8-9, first look for thousands_sep |
| 270 | // and decimal_point. |
| 271 | if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep) |
| 272 | { |
| 273 | if (!__found_dec && !__found_sci) |
| 274 | { |
| 275 | // NB: Thousands separator at the beginning of a string |
| 276 | // is a no-no, as is two consecutive thousands separators. |
| 277 | if (__sep_pos) |
| 278 | { |
| 279 | __found_grouping += static_cast<char>(__sep_pos); |
| 280 | __sep_pos = 0; |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | // NB: __convert_to_v will not assign __v and will |
| 285 | // set the failbit. |
| 286 | __xtrc.clear(); |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | else |
| 291 | break; |
| 292 | } |
| 293 | else if (__c == __lc->_M_decimal_point) |
| 294 | { |
| 295 | if (!__found_dec && !__found_sci) |
| 296 | { |
| 297 | // If no grouping chars are seen, no grouping check |
| 298 | // is applied. Therefore __found_grouping is adjusted |
| 299 | // only if decimal_point comes after some thousands_sep. |
| 300 | if (__found_grouping.size()) |
| 301 | __found_grouping += static_cast<char>(__sep_pos); |
| 302 | __xtrc += '.'; |
| 303 | __found_dec = true; |
| 304 | } |
| 305 | else |
| 306 | break; |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | const char_type* __q = |
| 311 | __traits_type::find(__lit_zero, 10, __c); |
| 312 | if (__q) |
| 313 | { |
| 314 | __xtrc += '0' + (__q - __lit_zero); |
| 315 | __found_mantissa = true; |
| 316 | ++__sep_pos; |
| 317 | } |
| 318 | else if ((__c == __lit[__num_base::_S_ie] |
| 319 | || __c == __lit[__num_base::_S_iE]) |
| 320 | && !__found_sci && __found_mantissa) |
| 321 | { |
| 322 | // Scientific notation. |
| 323 | if (__found_grouping.size() && !__found_dec) |
| 324 | __found_grouping += static_cast<char>(__sep_pos); |
| 325 | __xtrc += 'e'; |
| 326 | __found_sci = true; |
| 327 | |
| 328 | // Remove optional plus or minus sign, if they exist. |
| 329 | if (++__beg != __end) |
| 330 | { |
| 331 | __c = *__beg; |
| 332 | const bool __plus = __c == __lit[__num_base::_S_iplus]; |
| 333 | if ((__plus || __c == __lit[__num_base::_S_iminus]) |
| 334 | && !(__lc->_M_use_grouping |
| 335 | && __c == __lc->_M_thousands_sep) |
| 336 | && !(__c == __lc->_M_decimal_point)) |
| 337 | __xtrc += __plus ? '+' : '-'; |
| 338 | else |
| 339 | continue; |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | __testeof = true; |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | else |
| 348 | break; |
| 349 | } |
| 350 | |
| 351 | if (++__beg != __end) |
| 352 | __c = *__beg; |
| 353 | else |
| 354 | __testeof = true; |
| 355 | } |
| 356 | |
| 357 | // Digit grouping is checked. If grouping and found_grouping don't |
| 358 | // match, then get very very upset, and set failbit. |
| 359 | if (__found_grouping.size()) |
| 360 | { |
| 361 | // Add the ending grouping if a decimal or 'e'/'E' wasn't found. |
| 362 | if (!__found_dec && !__found_sci) |
| 363 | __found_grouping += static_cast<char>(__sep_pos); |
| 364 | |
| 365 | if (!std::__verify_grouping(grouping: __lc->_M_grouping, |
| 366 | grouping_size: __lc->_M_grouping_size, |
| 367 | grouping_tmp: __found_grouping)) |
| 368 | __err = ios_base::failbit; |
| 369 | } |
| 370 | |
| 371 | return __beg; |
| 372 | } |
| 373 | |
| 374 | template<typename _CharT, typename _InIter> |
| 375 | template<typename _ValueT> |
| 376 | _GLIBCXX_DEFAULT_ABI_TAG |
| 377 | _InIter |
| 378 | num_get<_CharT, _InIter>:: |
| 379 | (_InIter __beg, _InIter __end, ios_base& __io, |
| 380 | ios_base::iostate& __err, _ValueT& __v) const |
| 381 | { |
| 382 | typedef char_traits<_CharT> __traits_type; |
| 383 | using __gnu_cxx::__add_unsigned; |
| 384 | typedef typename __add_unsigned<_ValueT>::__type __unsigned_type; |
| 385 | typedef __numpunct_cache<_CharT> __cache_type; |
| 386 | __use_cache<__cache_type> __uc; |
| 387 | const locale& __loc = __io._M_getloc(); |
| 388 | const __cache_type* __lc = __uc(__loc); |
| 389 | const _CharT* __lit = __lc->_M_atoms_in; |
| 390 | char_type __c = char_type(); |
| 391 | |
| 392 | // NB: Iff __basefield == 0, __base can change based on contents. |
| 393 | const ios_base::fmtflags __basefield = __io.flags() |
| 394 | & ios_base::basefield; |
| 395 | const bool __oct = __basefield == ios_base::oct; |
| 396 | int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10); |
| 397 | |
| 398 | // True if __beg becomes equal to __end. |
| 399 | bool __testeof = __beg == __end; |
| 400 | |
| 401 | // First check for sign. |
| 402 | bool __negative = false; |
| 403 | if (!__testeof) |
| 404 | { |
| 405 | __c = *__beg; |
| 406 | __negative = __c == __lit[__num_base::_S_iminus]; |
| 407 | if ((__negative || __c == __lit[__num_base::_S_iplus]) |
| 408 | && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep) |
| 409 | && !(__c == __lc->_M_decimal_point)) |
| 410 | { |
| 411 | if (++__beg != __end) |
| 412 | __c = *__beg; |
| 413 | else |
| 414 | __testeof = true; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // Next, look for leading zeros and check required digits |
| 419 | // for base formats. |
| 420 | bool __found_zero = false; |
| 421 | int __sep_pos = 0; |
| 422 | while (!__testeof) |
| 423 | { |
| 424 | if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep) |
| 425 | || __c == __lc->_M_decimal_point) |
| 426 | break; |
| 427 | else if (__c == __lit[__num_base::_S_izero] |
| 428 | && (!__found_zero || __base == 10)) |
| 429 | { |
| 430 | __found_zero = true; |
| 431 | ++__sep_pos; |
| 432 | if (__basefield == 0) |
| 433 | __base = 8; |
| 434 | if (__base == 8) |
| 435 | __sep_pos = 0; |
| 436 | } |
| 437 | else if (__found_zero |
| 438 | && (__c == __lit[__num_base::_S_ix] |
| 439 | || __c == __lit[__num_base::_S_iX])) |
| 440 | { |
| 441 | if (__basefield == 0) |
| 442 | __base = 16; |
| 443 | if (__base == 16) |
| 444 | { |
| 445 | __found_zero = false; |
| 446 | __sep_pos = 0; |
| 447 | } |
| 448 | else |
| 449 | break; |
| 450 | } |
| 451 | else |
| 452 | break; |
| 453 | |
| 454 | if (++__beg != __end) |
| 455 | { |
| 456 | __c = *__beg; |
| 457 | if (!__found_zero) |
| 458 | break; |
| 459 | } |
| 460 | else |
| 461 | __testeof = true; |
| 462 | } |
| 463 | |
| 464 | // At this point, base is determined. If not hex, only allow |
| 465 | // base digits as valid input. |
| 466 | const size_t __len = (__base == 16 ? __num_base::_S_iend |
| 467 | - __num_base::_S_izero : __base); |
| 468 | |
| 469 | // Extract. |
| 470 | typedef __gnu_cxx::__numeric_traits<_ValueT> __num_traits; |
| 471 | string __found_grouping; |
| 472 | if (__lc->_M_use_grouping) |
| 473 | __found_grouping.reserve(res: 32); |
| 474 | bool __testfail = false; |
| 475 | bool __testoverflow = false; |
| 476 | const __unsigned_type __max = |
| 477 | (__negative && __num_traits::__is_signed) |
| 478 | ? -static_cast<__unsigned_type>(__num_traits::__min) |
| 479 | : __num_traits::__max; |
| 480 | const __unsigned_type __smax = __max / __base; |
| 481 | __unsigned_type __result = 0; |
| 482 | int __digit = 0; |
| 483 | const char_type* __lit_zero = __lit + __num_base::_S_izero; |
| 484 | |
| 485 | if (!__lc->_M_allocated) |
| 486 | // "C" locale |
| 487 | while (!__testeof) |
| 488 | { |
| 489 | __digit = _M_find(__lit_zero, __len, __c); |
| 490 | if (__digit == -1) |
| 491 | break; |
| 492 | |
| 493 | if (__result > __smax) |
| 494 | __testoverflow = true; |
| 495 | else |
| 496 | { |
| 497 | __result *= __base; |
| 498 | __testoverflow |= __result > __max - __digit; |
| 499 | __result += __digit; |
| 500 | ++__sep_pos; |
| 501 | } |
| 502 | |
| 503 | if (++__beg != __end) |
| 504 | __c = *__beg; |
| 505 | else |
| 506 | __testeof = true; |
| 507 | } |
| 508 | else |
| 509 | while (!__testeof) |
| 510 | { |
| 511 | // According to 22.2.2.1.2, p8-9, first look for thousands_sep |
| 512 | // and decimal_point. |
| 513 | if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep) |
| 514 | { |
| 515 | // NB: Thousands separator at the beginning of a string |
| 516 | // is a no-no, as is two consecutive thousands separators. |
| 517 | if (__sep_pos) |
| 518 | { |
| 519 | __found_grouping += static_cast<char>(__sep_pos); |
| 520 | __sep_pos = 0; |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | __testfail = true; |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | else if (__c == __lc->_M_decimal_point) |
| 529 | break; |
| 530 | else |
| 531 | { |
| 532 | const char_type* __q = |
| 533 | __traits_type::find(__lit_zero, __len, __c); |
| 534 | if (!__q) |
| 535 | break; |
| 536 | |
| 537 | __digit = __q - __lit_zero; |
| 538 | if (__digit > 15) |
| 539 | __digit -= 6; |
| 540 | if (__result > __smax) |
| 541 | __testoverflow = true; |
| 542 | else |
| 543 | { |
| 544 | __result *= __base; |
| 545 | __testoverflow |= __result > __max - __digit; |
| 546 | __result += __digit; |
| 547 | ++__sep_pos; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (++__beg != __end) |
| 552 | __c = *__beg; |
| 553 | else |
| 554 | __testeof = true; |
| 555 | } |
| 556 | |
| 557 | // Digit grouping is checked. If grouping and found_grouping don't |
| 558 | // match, then get very very upset, and set failbit. |
| 559 | if (__found_grouping.size()) |
| 560 | { |
| 561 | // Add the ending grouping. |
| 562 | __found_grouping += static_cast<char>(__sep_pos); |
| 563 | |
| 564 | if (!std::__verify_grouping(grouping: __lc->_M_grouping, |
| 565 | grouping_size: __lc->_M_grouping_size, |
| 566 | grouping_tmp: __found_grouping)) |
| 567 | __err = ios_base::failbit; |
| 568 | } |
| 569 | |
| 570 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 571 | // 23. Num_get overflow result. |
| 572 | if ((!__sep_pos && !__found_zero && !__found_grouping.size()) |
| 573 | || __testfail) |
| 574 | { |
| 575 | __v = 0; |
| 576 | __err = ios_base::failbit; |
| 577 | } |
| 578 | else if (__testoverflow) |
| 579 | { |
| 580 | if (__negative && __num_traits::__is_signed) |
| 581 | __v = __num_traits::__min; |
| 582 | else |
| 583 | __v = __num_traits::__max; |
| 584 | __err = ios_base::failbit; |
| 585 | } |
| 586 | else |
| 587 | __v = __negative ? -__result : __result; |
| 588 | |
| 589 | if (__testeof) |
| 590 | __err |= ios_base::eofbit; |
| 591 | return __beg; |
| 592 | } |
| 593 | |
| 594 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 595 | // 17. Bad bool parsing |
| 596 | template<typename _CharT, typename _InIter> |
| 597 | _InIter |
| 598 | num_get<_CharT, _InIter>:: |
| 599 | do_get(iter_type __beg, iter_type __end, ios_base& __io, |
| 600 | ios_base::iostate& __err, bool& __v) const |
| 601 | { |
| 602 | if (!(__io.flags() & ios_base::boolalpha)) |
| 603 | { |
| 604 | // Parse bool values as long. |
| 605 | // NB: We can't just call do_get(long) here, as it might |
| 606 | // refer to a derived class. |
| 607 | long __l = -1; |
| 608 | __beg = _M_extract_int(__beg, __end, __io, __err, __l); |
| 609 | if (__l == 0 || __l == 1) |
| 610 | __v = bool(__l); |
| 611 | else |
| 612 | { |
| 613 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 614 | // 23. Num_get overflow result. |
| 615 | __v = true; |
| 616 | __err = ios_base::failbit; |
| 617 | if (__beg == __end) |
| 618 | __err |= ios_base::eofbit; |
| 619 | } |
| 620 | } |
| 621 | else |
| 622 | { |
| 623 | // Parse bool values as alphanumeric. |
| 624 | typedef __numpunct_cache<_CharT> __cache_type; |
| 625 | __use_cache<__cache_type> __uc; |
| 626 | const locale& __loc = __io._M_getloc(); |
| 627 | const __cache_type* __lc = __uc(__loc); |
| 628 | |
| 629 | bool __testf = true; |
| 630 | bool __testt = true; |
| 631 | bool __donef = __lc->_M_falsename_size == 0; |
| 632 | bool __donet = __lc->_M_truename_size == 0; |
| 633 | bool __testeof = false; |
| 634 | size_t __n = 0; |
| 635 | while (!__donef || !__donet) |
| 636 | { |
| 637 | if (__beg == __end) |
| 638 | { |
| 639 | __testeof = true; |
| 640 | break; |
| 641 | } |
| 642 | |
| 643 | const char_type __c = *__beg; |
| 644 | |
| 645 | if (!__donef) |
| 646 | __testf = __c == __lc->_M_falsename[__n]; |
| 647 | |
| 648 | if (!__testf && __donet) |
| 649 | break; |
| 650 | |
| 651 | if (!__donet) |
| 652 | __testt = __c == __lc->_M_truename[__n]; |
| 653 | |
| 654 | if (!__testt && __donef) |
| 655 | break; |
| 656 | |
| 657 | if (!__testt && !__testf) |
| 658 | break; |
| 659 | |
| 660 | ++__n; |
| 661 | ++__beg; |
| 662 | |
| 663 | __donef = !__testf || __n >= __lc->_M_falsename_size; |
| 664 | __donet = !__testt || __n >= __lc->_M_truename_size; |
| 665 | } |
| 666 | if (__testf && __n == __lc->_M_falsename_size && __n) |
| 667 | { |
| 668 | __v = false; |
| 669 | if (__testt && __n == __lc->_M_truename_size) |
| 670 | __err = ios_base::failbit; |
| 671 | else |
| 672 | __err = __testeof ? ios_base::eofbit : ios_base::goodbit; |
| 673 | } |
| 674 | else if (__testt && __n == __lc->_M_truename_size && __n) |
| 675 | { |
| 676 | __v = true; |
| 677 | __err = __testeof ? ios_base::eofbit : ios_base::goodbit; |
| 678 | } |
| 679 | else |
| 680 | { |
| 681 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 682 | // 23. Num_get overflow result. |
| 683 | __v = false; |
| 684 | __err = ios_base::failbit; |
| 685 | if (__testeof) |
| 686 | __err |= ios_base::eofbit; |
| 687 | } |
| 688 | } |
| 689 | return __beg; |
| 690 | } |
| 691 | |
| 692 | template<typename _CharT, typename _InIter> |
| 693 | _InIter |
| 694 | num_get<_CharT, _InIter>:: |
| 695 | do_get(iter_type __beg, iter_type __end, ios_base& __io, |
| 696 | ios_base::iostate& __err, float& __v) const |
| 697 | { |
| 698 | string __xtrc; |
| 699 | __xtrc.reserve(res: 32); |
| 700 | __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc); |
| 701 | std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale()); |
| 702 | if (__beg == __end) |
| 703 | __err |= ios_base::eofbit; |
| 704 | return __beg; |
| 705 | } |
| 706 | |
| 707 | template<typename _CharT, typename _InIter> |
| 708 | _InIter |
| 709 | num_get<_CharT, _InIter>:: |
| 710 | do_get(iter_type __beg, iter_type __end, ios_base& __io, |
| 711 | ios_base::iostate& __err, double& __v) const |
| 712 | { |
| 713 | string __xtrc; |
| 714 | __xtrc.reserve(res: 32); |
| 715 | __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc); |
| 716 | std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale()); |
| 717 | if (__beg == __end) |
| 718 | __err |= ios_base::eofbit; |
| 719 | return __beg; |
| 720 | } |
| 721 | |
| 722 | #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ |
| 723 | template<typename _CharT, typename _InIter> |
| 724 | _InIter |
| 725 | num_get<_CharT, _InIter>:: |
| 726 | __do_get(iter_type __beg, iter_type __end, ios_base& __io, |
| 727 | ios_base::iostate& __err, double& __v) const |
| 728 | { |
| 729 | string __xtrc; |
| 730 | __xtrc.reserve(32); |
| 731 | __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc); |
| 732 | std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale()); |
| 733 | if (__beg == __end) |
| 734 | __err |= ios_base::eofbit; |
| 735 | return __beg; |
| 736 | } |
| 737 | #endif |
| 738 | |
| 739 | template<typename _CharT, typename _InIter> |
| 740 | _InIter |
| 741 | num_get<_CharT, _InIter>:: |
| 742 | do_get(iter_type __beg, iter_type __end, ios_base& __io, |
| 743 | ios_base::iostate& __err, long double& __v) const |
| 744 | { |
| 745 | string __xtrc; |
| 746 | __xtrc.reserve(res: 32); |
| 747 | __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc); |
| 748 | std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale()); |
| 749 | if (__beg == __end) |
| 750 | __err |= ios_base::eofbit; |
| 751 | return __beg; |
| 752 | } |
| 753 | |
| 754 | template<typename _CharT, typename _InIter> |
| 755 | _InIter |
| 756 | num_get<_CharT, _InIter>:: |
| 757 | do_get(iter_type __beg, iter_type __end, ios_base& __io, |
| 758 | ios_base::iostate& __err, void*& __v) const |
| 759 | { |
| 760 | // Prepare for hex formatted input. |
| 761 | typedef ios_base::fmtflags fmtflags; |
| 762 | const fmtflags __fmt = __io.flags(); |
| 763 | __io.flags(fmtfl: (__fmt & ~ios_base::basefield) | ios_base::hex); |
| 764 | |
| 765 | #pragma GCC diagnostic push |
| 766 | #pragma GCC diagnostic ignored "-Wlong-long" |
| 767 | typedef __gnu_cxx::__conditional_type<(sizeof(void*) |
| 768 | <= sizeof(unsigned long)), |
| 769 | unsigned long, unsigned long long>::__type _UIntPtrType; |
| 770 | #pragma GCC diagnostic pop |
| 771 | |
| 772 | _UIntPtrType __ul; |
| 773 | __beg = _M_extract_int(__beg, __end, __io, __err, __ul); |
| 774 | |
| 775 | // Reset from hex formatted input. |
| 776 | __io.flags(fmtfl: __fmt); |
| 777 | |
| 778 | __v = reinterpret_cast<void*>(__ul); |
| 779 | return __beg; |
| 780 | } |
| 781 | |
| 782 | #if defined _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT \ |
| 783 | && defined __LONG_DOUBLE_IEEE128__ |
| 784 | template<typename _CharT, typename _InIter> |
| 785 | _InIter |
| 786 | num_get<_CharT, _InIter>:: |
| 787 | __do_get(iter_type __beg, iter_type __end, ios_base& __io, |
| 788 | ios_base::iostate& __err, __ibm128& __v) const |
| 789 | { |
| 790 | string __xtrc; |
| 791 | __xtrc.reserve(32); |
| 792 | __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc); |
| 793 | std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale()); |
| 794 | if (__beg == __end) |
| 795 | __err |= ios_base::eofbit; |
| 796 | return __beg; |
| 797 | } |
| 798 | #endif |
| 799 | |
| 800 | // For use by integer and floating-point types after they have been |
| 801 | // converted into a char_type string. |
| 802 | template<typename _CharT, typename _OutIter> |
| 803 | void |
| 804 | num_put<_CharT, _OutIter>:: |
| 805 | _M_pad(_CharT __fill, streamsize __w, ios_base& __io, |
| 806 | _CharT* __new, const _CharT* __cs, int& __len) const |
| 807 | { |
| 808 | // [22.2.2.2.2] Stage 3. |
| 809 | // If necessary, pad. |
| 810 | __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new, |
| 811 | __cs, __w, __len); |
| 812 | __len = static_cast<int>(__w); |
| 813 | } |
| 814 | |
| 815 | _GLIBCXX_END_NAMESPACE_LDBL |
| 816 | |
| 817 | template<typename _CharT, typename _ValueT> |
| 818 | int |
| 819 | __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit, |
| 820 | ios_base::fmtflags __flags, bool __dec) |
| 821 | { |
| 822 | _CharT* __buf = __bufend; |
| 823 | if (__builtin_expect(__dec, true)) |
| 824 | { |
| 825 | // Decimal. |
| 826 | do |
| 827 | { |
| 828 | *--__buf = __lit[(__v % 10) + __num_base::_S_odigits]; |
| 829 | __v /= 10; |
| 830 | } |
| 831 | while (__v != 0); |
| 832 | } |
| 833 | else if ((__flags & ios_base::basefield) == ios_base::oct) |
| 834 | { |
| 835 | // Octal. |
| 836 | do |
| 837 | { |
| 838 | *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits]; |
| 839 | __v >>= 3; |
| 840 | } |
| 841 | while (__v != 0); |
| 842 | } |
| 843 | else |
| 844 | { |
| 845 | // Hex. |
| 846 | const bool __uppercase = __flags & ios_base::uppercase; |
| 847 | const int __case_offset = __uppercase ? __num_base::_S_oudigits |
| 848 | : __num_base::_S_odigits; |
| 849 | do |
| 850 | { |
| 851 | *--__buf = __lit[(__v & 0xf) + __case_offset]; |
| 852 | __v >>= 4; |
| 853 | } |
| 854 | while (__v != 0); |
| 855 | } |
| 856 | return __bufend - __buf; |
| 857 | } |
| 858 | |
| 859 | _GLIBCXX_BEGIN_NAMESPACE_LDBL |
| 860 | |
| 861 | template<typename _CharT, typename _OutIter> |
| 862 | void |
| 863 | num_put<_CharT, _OutIter>:: |
| 864 | _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep, |
| 865 | ios_base&, _CharT* __new, _CharT* __cs, int& __len) const |
| 866 | { |
| 867 | _CharT* __p = std::__add_grouping(__new, __sep, __grouping, |
| 868 | __grouping_size, __cs, __cs + __len); |
| 869 | __len = __p - __new; |
| 870 | } |
| 871 | |
| 872 | template<typename _CharT, typename _OutIter> |
| 873 | template<typename _ValueT> |
| 874 | _OutIter |
| 875 | num_put<_CharT, _OutIter>:: |
| 876 | _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill, |
| 877 | _ValueT __v) const |
| 878 | { |
| 879 | using __gnu_cxx::__add_unsigned; |
| 880 | typedef typename __add_unsigned<_ValueT>::__type __unsigned_type; |
| 881 | typedef __numpunct_cache<_CharT> __cache_type; |
| 882 | __use_cache<__cache_type> __uc; |
| 883 | const locale& __loc = __io._M_getloc(); |
| 884 | const __cache_type* __lc = __uc(__loc); |
| 885 | const _CharT* __lit = __lc->_M_atoms_out; |
| 886 | const ios_base::fmtflags __flags = __io.flags(); |
| 887 | |
| 888 | // Long enough to hold hex, dec, and octal representations. |
| 889 | const int __ilen = 5 * sizeof(_ValueT); |
| 890 | _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) |
| 891 | * __ilen)); |
| 892 | |
| 893 | // [22.2.2.2.2] Stage 1, numeric conversion to character. |
| 894 | // Result is returned right-justified in the buffer. |
| 895 | const ios_base::fmtflags __basefield = __flags & ios_base::basefield; |
| 896 | const bool __dec = (__basefield != ios_base::oct |
| 897 | && __basefield != ios_base::hex); |
| 898 | const __unsigned_type __u = ((__v > 0 || !__dec) |
| 899 | ? __unsigned_type(__v) |
| 900 | : -__unsigned_type(__v)); |
| 901 | int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec); |
| 902 | __cs += __ilen - __len; |
| 903 | |
| 904 | // Add grouping, if necessary. |
| 905 | if (__lc->_M_use_grouping) |
| 906 | { |
| 907 | // Grouping can add (almost) as many separators as the number |
| 908 | // of digits + space is reserved for numeric base or sign. |
| 909 | _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) |
| 910 | * (__len + 1) |
| 911 | * 2)); |
| 912 | _M_group_int(grouping: __lc->_M_grouping, grouping_size: __lc->_M_grouping_size, |
| 913 | sep: __lc->_M_thousands_sep, __io, new: __cs2 + 2, __cs, __len); |
| 914 | __cs = __cs2 + 2; |
| 915 | } |
| 916 | |
| 917 | // Complete Stage 1, prepend numeric base or sign. |
| 918 | if (__builtin_expect(__dec, true)) |
| 919 | { |
| 920 | // Decimal. |
| 921 | if (__v >= 0) |
| 922 | { |
| 923 | if (bool(__flags & ios_base::showpos) |
| 924 | && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed) |
| 925 | *--__cs = __lit[__num_base::_S_oplus], ++__len; |
| 926 | } |
| 927 | else |
| 928 | *--__cs = __lit[__num_base::_S_ominus], ++__len; |
| 929 | } |
| 930 | else if (bool(__flags & ios_base::showbase) && __v) |
| 931 | { |
| 932 | if (__basefield == ios_base::oct) |
| 933 | *--__cs = __lit[__num_base::_S_odigits], ++__len; |
| 934 | else |
| 935 | { |
| 936 | // 'x' or 'X' |
| 937 | const bool __uppercase = __flags & ios_base::uppercase; |
| 938 | *--__cs = __lit[__num_base::_S_ox + __uppercase]; |
| 939 | // '0' |
| 940 | *--__cs = __lit[__num_base::_S_odigits]; |
| 941 | __len += 2; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | // Pad. |
| 946 | const streamsize __w = __io.width(); |
| 947 | if (__w > static_cast<streamsize>(__len)) |
| 948 | { |
| 949 | _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) |
| 950 | * __w)); |
| 951 | _M_pad(__fill, __w, __io, new: __cs3, __cs, __len); |
| 952 | __cs = __cs3; |
| 953 | } |
| 954 | __io.width(wide: 0); |
| 955 | |
| 956 | // [22.2.2.2.2] Stage 4. |
| 957 | // Write resulting, fully-formatted string to output iterator. |
| 958 | return std::__write(__s, __cs, __len); |
| 959 | } |
| 960 | |
| 961 | template<typename _CharT, typename _OutIter> |
| 962 | void |
| 963 | num_put<_CharT, _OutIter>:: |
| 964 | _M_group_float(const char* __grouping, size_t __grouping_size, |
| 965 | _CharT __sep, const _CharT* __p, _CharT* __new, |
| 966 | _CharT* __cs, int& __len) const |
| 967 | { |
| 968 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 969 | // 282. What types does numpunct grouping refer to? |
| 970 | // Add grouping, if necessary. |
| 971 | const int __declen = __p ? __p - __cs : __len; |
| 972 | _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping, |
| 973 | __grouping_size, |
| 974 | __cs, __cs + __declen); |
| 975 | |
| 976 | // Tack on decimal part. |
| 977 | int __newlen = __p2 - __new; |
| 978 | if (__p) |
| 979 | { |
| 980 | char_traits<_CharT>::copy(__p2, __p, __len - __declen); |
| 981 | __newlen += __len - __declen; |
| 982 | } |
| 983 | __len = __newlen; |
| 984 | } |
| 985 | |
| 986 | // The following code uses vsnprintf (or vsprintf(), when |
| 987 | // _GLIBCXX_USE_C99_STDIO is not defined) to convert floating point |
| 988 | // values for insertion into a stream. An optimization would be to |
| 989 | // replace them with code that works directly on a wide buffer and |
| 990 | // then use __pad to do the padding. It would be good to replace |
| 991 | // them anyway to gain back the efficiency that C++ provides by |
| 992 | // knowing up front the type of the values to insert. Also, sprintf |
| 993 | // is dangerous since may lead to accidental buffer overruns. This |
| 994 | // implementation follows the C++ standard fairly directly as |
| 995 | // outlined in 22.2.2.2 [lib.locale.num.put] |
| 996 | template<typename _CharT, typename _OutIter> |
| 997 | template<typename _ValueT> |
| 998 | _OutIter |
| 999 | num_put<_CharT, _OutIter>:: |
| 1000 | _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod, |
| 1001 | _ValueT __v) const |
| 1002 | { |
| 1003 | typedef __numpunct_cache<_CharT> __cache_type; |
| 1004 | __use_cache<__cache_type> __uc; |
| 1005 | const locale& __loc = __io._M_getloc(); |
| 1006 | const __cache_type* __lc = __uc(__loc); |
| 1007 | |
| 1008 | // Use default precision if out of range. |
| 1009 | const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision(); |
| 1010 | |
| 1011 | const int __max_digits = |
| 1012 | __gnu_cxx::__numeric_traits<_ValueT>::__digits10; |
| 1013 | |
| 1014 | // [22.2.2.2.2] Stage 1, numeric conversion to character. |
| 1015 | int __len; |
| 1016 | // Long enough for the max format spec. |
| 1017 | char __fbuf[16]; |
| 1018 | __num_base::_S_format_float(__io, fptr: __fbuf, __mod); |
| 1019 | |
| 1020 | #if _GLIBCXX_USE_C99_STDIO && !_GLIBCXX_HAVE_BROKEN_VSNPRINTF |
| 1021 | // Precision is always used except for hexfloat format. |
| 1022 | const bool __use_prec = |
| 1023 | (__io.flags() & ios_base::floatfield) != ios_base::floatfield; |
| 1024 | |
| 1025 | // First try a buffer perhaps big enough (most probably sufficient |
| 1026 | // for non-ios_base::fixed outputs) |
| 1027 | int __cs_size = __max_digits * 3; |
| 1028 | char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); |
| 1029 | if (__use_prec) |
| 1030 | __len = std::__convert_from_v(cloc: _S_get_c_locale(), out: __cs, size: __cs_size, |
| 1031 | fmt: __fbuf, __prec, __v); |
| 1032 | else |
| 1033 | __len = std::__convert_from_v(cloc: _S_get_c_locale(), out: __cs, size: __cs_size, |
| 1034 | fmt: __fbuf, __v); |
| 1035 | |
| 1036 | // If the buffer was not large enough, try again with the correct size. |
| 1037 | if (__len >= __cs_size) |
| 1038 | { |
| 1039 | __cs_size = __len + 1; |
| 1040 | __cs = static_cast<char*>(__builtin_alloca(__cs_size)); |
| 1041 | if (__use_prec) |
| 1042 | __len = std::__convert_from_v(cloc: _S_get_c_locale(), out: __cs, size: __cs_size, |
| 1043 | fmt: __fbuf, __prec, __v); |
| 1044 | else |
| 1045 | __len = std::__convert_from_v(cloc: _S_get_c_locale(), out: __cs, size: __cs_size, |
| 1046 | fmt: __fbuf, __v); |
| 1047 | } |
| 1048 | #else |
| 1049 | // Consider the possibility of long ios_base::fixed outputs |
| 1050 | const bool __fixed = __io.flags() & ios_base::fixed; |
| 1051 | const int __max_exp = |
| 1052 | __gnu_cxx::__numeric_traits<_ValueT>::__max_exponent10; |
| 1053 | |
| 1054 | // The size of the output string is computed as follows. |
| 1055 | // ios_base::fixed outputs may need up to __max_exp + 1 chars |
| 1056 | // for the integer part + __prec chars for the fractional part |
| 1057 | // + 3 chars for sign, decimal point, '\0'. On the other hand, |
| 1058 | // for non-fixed outputs __max_digits * 2 + __prec chars are |
| 1059 | // largely sufficient. |
| 1060 | const int __cs_size = __fixed ? __max_exp + __prec + 4 |
| 1061 | : __max_digits * 2 + __prec; |
| 1062 | char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); |
| 1063 | __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf, |
| 1064 | __prec, __v); |
| 1065 | #endif |
| 1066 | |
| 1067 | // [22.2.2.2.2] Stage 2, convert to char_type, using correct |
| 1068 | // numpunct.decimal_point() values for '.' and adding grouping. |
| 1069 | const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); |
| 1070 | |
| 1071 | _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) |
| 1072 | * __len)); |
| 1073 | __ctype.widen(__cs, __cs + __len, __ws); |
| 1074 | |
| 1075 | // Replace decimal point. |
| 1076 | _CharT* __wp = 0; |
| 1077 | const char* __p = char_traits<char>::find(s: __cs, n: __len, a: '.'); |
| 1078 | if (__p) |
| 1079 | { |
| 1080 | __wp = __ws + (__p - __cs); |
| 1081 | *__wp = __lc->_M_decimal_point; |
| 1082 | } |
| 1083 | |
| 1084 | // Add grouping, if necessary. |
| 1085 | // N.B. Make sure to not group things like 2e20, i.e., no decimal |
| 1086 | // point, scientific notation. |
| 1087 | if (__lc->_M_use_grouping |
| 1088 | && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9' |
| 1089 | && __cs[1] >= '0' && __cs[2] >= '0'))) |
| 1090 | { |
| 1091 | // Grouping can add (almost) as many separators as the |
| 1092 | // number of digits, but no more. |
| 1093 | _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) |
| 1094 | * __len * 2)); |
| 1095 | |
| 1096 | streamsize __off = 0; |
| 1097 | if (__cs[0] == '-' || __cs[0] == '+') |
| 1098 | { |
| 1099 | __off = 1; |
| 1100 | __ws2[0] = __ws[0]; |
| 1101 | __len -= 1; |
| 1102 | } |
| 1103 | |
| 1104 | _M_group_float(grouping: __lc->_M_grouping, grouping_size: __lc->_M_grouping_size, |
| 1105 | sep: __lc->_M_thousands_sep, p: __wp, new: __ws2 + __off, |
| 1106 | cs: __ws + __off, __len); |
| 1107 | __len += __off; |
| 1108 | |
| 1109 | __ws = __ws2; |
| 1110 | } |
| 1111 | |
| 1112 | // Pad. |
| 1113 | const streamsize __w = __io.width(); |
| 1114 | if (__w > static_cast<streamsize>(__len)) |
| 1115 | { |
| 1116 | _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) |
| 1117 | * __w)); |
| 1118 | _M_pad(__fill, __w, __io, new: __ws3, cs: __ws, __len); |
| 1119 | __ws = __ws3; |
| 1120 | } |
| 1121 | __io.width(wide: 0); |
| 1122 | |
| 1123 | // [22.2.2.2.2] Stage 4. |
| 1124 | // Write resulting, fully-formatted string to output iterator. |
| 1125 | return std::__write(__s, __ws, __len); |
| 1126 | } |
| 1127 | |
| 1128 | template<typename _CharT, typename _OutIter> |
| 1129 | _OutIter |
| 1130 | num_put<_CharT, _OutIter>:: |
| 1131 | do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const |
| 1132 | { |
| 1133 | const ios_base::fmtflags __flags = __io.flags(); |
| 1134 | if ((__flags & ios_base::boolalpha) == 0) |
| 1135 | { |
| 1136 | const long __l = __v; |
| 1137 | __s = _M_insert_int(__s, __io, __fill, __l); |
| 1138 | } |
| 1139 | else |
| 1140 | { |
| 1141 | typedef __numpunct_cache<_CharT> __cache_type; |
| 1142 | __use_cache<__cache_type> __uc; |
| 1143 | const locale& __loc = __io._M_getloc(); |
| 1144 | const __cache_type* __lc = __uc(__loc); |
| 1145 | |
| 1146 | const _CharT* __name = __v ? __lc->_M_truename |
| 1147 | : __lc->_M_falsename; |
| 1148 | int __len = __v ? __lc->_M_truename_size |
| 1149 | : __lc->_M_falsename_size; |
| 1150 | |
| 1151 | const streamsize __w = __io.width(); |
| 1152 | if (__w > static_cast<streamsize>(__len)) |
| 1153 | { |
| 1154 | const streamsize __plen = __w - __len; |
| 1155 | _CharT* __ps |
| 1156 | = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) |
| 1157 | * __plen)); |
| 1158 | |
| 1159 | char_traits<_CharT>::assign(__ps, __plen, __fill); |
| 1160 | __io.width(wide: 0); |
| 1161 | |
| 1162 | if ((__flags & ios_base::adjustfield) == ios_base::left) |
| 1163 | { |
| 1164 | __s = std::__write(__s, __name, __len); |
| 1165 | __s = std::__write(__s, __ps, __plen); |
| 1166 | } |
| 1167 | else |
| 1168 | { |
| 1169 | __s = std::__write(__s, __ps, __plen); |
| 1170 | __s = std::__write(__s, __name, __len); |
| 1171 | } |
| 1172 | return __s; |
| 1173 | } |
| 1174 | __io.width(wide: 0); |
| 1175 | __s = std::__write(__s, __name, __len); |
| 1176 | } |
| 1177 | return __s; |
| 1178 | } |
| 1179 | |
| 1180 | template<typename _CharT, typename _OutIter> |
| 1181 | _OutIter |
| 1182 | num_put<_CharT, _OutIter>:: |
| 1183 | do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const |
| 1184 | { return _M_insert_float(__s, __io, __fill, char(), __v); } |
| 1185 | |
| 1186 | #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ |
| 1187 | template<typename _CharT, typename _OutIter> |
| 1188 | _OutIter |
| 1189 | num_put<_CharT, _OutIter>:: |
| 1190 | __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const |
| 1191 | { return _M_insert_float(__s, __io, __fill, char(), __v); } |
| 1192 | #endif |
| 1193 | |
| 1194 | template<typename _CharT, typename _OutIter> |
| 1195 | _OutIter |
| 1196 | num_put<_CharT, _OutIter>:: |
| 1197 | do_put(iter_type __s, ios_base& __io, char_type __fill, |
| 1198 | long double __v) const |
| 1199 | { return _M_insert_float(__s, __io, __fill, 'L', __v); } |
| 1200 | |
| 1201 | template<typename _CharT, typename _OutIter> |
| 1202 | _OutIter |
| 1203 | num_put<_CharT, _OutIter>:: |
| 1204 | do_put(iter_type __s, ios_base& __io, char_type __fill, |
| 1205 | const void* __v) const |
| 1206 | { |
| 1207 | const ios_base::fmtflags __flags = __io.flags(); |
| 1208 | const ios_base::fmtflags __fmt = ~(ios_base::basefield |
| 1209 | | ios_base::uppercase); |
| 1210 | __io.flags(fmtfl: (__flags & __fmt) | (ios_base::hex | ios_base::showbase)); |
| 1211 | |
| 1212 | #pragma GCC diagnostic push |
| 1213 | #pragma GCC diagnostic ignored "-Wlong-long" |
| 1214 | typedef __gnu_cxx::__conditional_type<(sizeof(const void*) |
| 1215 | <= sizeof(unsigned long)), |
| 1216 | unsigned long, unsigned long long>::__type _UIntPtrType; |
| 1217 | #pragma GCC diagnostic pop |
| 1218 | |
| 1219 | __s = _M_insert_int(__s, __io, __fill, |
| 1220 | reinterpret_cast<_UIntPtrType>(__v)); |
| 1221 | __io.flags(fmtfl: __flags); |
| 1222 | return __s; |
| 1223 | } |
| 1224 | |
| 1225 | #if defined _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT \ |
| 1226 | && defined __LONG_DOUBLE_IEEE128__ |
| 1227 | template<typename _CharT, typename _OutIter> |
| 1228 | _OutIter |
| 1229 | num_put<_CharT, _OutIter>:: |
| 1230 | __do_put(iter_type __s, ios_base& __io, char_type __fill, |
| 1231 | __ibm128 __v) const |
| 1232 | { return _M_insert_float(__s, __io, __fill, 'L', __v); } |
| 1233 | #endif |
| 1234 | _GLIBCXX_END_NAMESPACE_LDBL |
| 1235 | |
| 1236 | // Construct correctly padded string, as per 22.2.2.2.2 |
| 1237 | // Assumes |
| 1238 | // __newlen > __oldlen |
| 1239 | // __news is allocated for __newlen size |
| 1240 | |
| 1241 | // NB: Of the two parameters, _CharT can be deduced from the |
| 1242 | // function arguments. The other (_Traits) has to be explicitly specified. |
| 1243 | template<typename _CharT, typename _Traits> |
| 1244 | void |
| 1245 | __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill, |
| 1246 | _CharT* __news, const _CharT* __olds, |
| 1247 | streamsize __newlen, streamsize __oldlen) |
| 1248 | { |
| 1249 | const size_t __plen = static_cast<size_t>(__newlen - __oldlen); |
| 1250 | const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield; |
| 1251 | |
| 1252 | // Padding last. |
| 1253 | if (__adjust == ios_base::left) |
| 1254 | { |
| 1255 | _Traits::copy(__news, __olds, __oldlen); |
| 1256 | _Traits::assign(__news + __oldlen, __plen, __fill); |
| 1257 | return; |
| 1258 | } |
| 1259 | |
| 1260 | size_t __mod = 0; |
| 1261 | if (__adjust == ios_base::internal) |
| 1262 | { |
| 1263 | // Pad after the sign, if there is one. |
| 1264 | // Pad after 0[xX], if there is one. |
| 1265 | // Who came up with these rules, anyway? Jeeze. |
| 1266 | const locale& __loc = __io._M_getloc(); |
| 1267 | const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); |
| 1268 | |
| 1269 | if (__ctype.widen('-') == __olds[0] |
| 1270 | || __ctype.widen('+') == __olds[0]) |
| 1271 | { |
| 1272 | __news[0] = __olds[0]; |
| 1273 | __mod = 1; |
| 1274 | ++__news; |
| 1275 | } |
| 1276 | else if (__ctype.widen('0') == __olds[0] |
| 1277 | && __oldlen > 1 |
| 1278 | && (__ctype.widen('x') == __olds[1] |
| 1279 | || __ctype.widen('X') == __olds[1])) |
| 1280 | { |
| 1281 | __news[0] = __olds[0]; |
| 1282 | __news[1] = __olds[1]; |
| 1283 | __mod = 2; |
| 1284 | __news += 2; |
| 1285 | } |
| 1286 | // else Padding first. |
| 1287 | } |
| 1288 | _Traits::assign(__news, __plen, __fill); |
| 1289 | _Traits::copy(__news + __plen, __olds + __mod, __oldlen - __mod); |
| 1290 | } |
| 1291 | |
| 1292 | template<typename _CharT> |
| 1293 | _CharT* |
| 1294 | __add_grouping(_CharT* __s, _CharT __sep, |
| 1295 | const char* __gbeg, size_t __gsize, |
| 1296 | const _CharT* __first, const _CharT* __last) |
| 1297 | { |
| 1298 | size_t __idx = 0; |
| 1299 | size_t __ctr = 0; |
| 1300 | |
| 1301 | while (__last - __first > __gbeg[__idx] |
| 1302 | && static_cast<signed char>(__gbeg[__idx]) > 0 |
| 1303 | && __gbeg[__idx] != __gnu_cxx::__numeric_traits<char>::__max) |
| 1304 | { |
| 1305 | __last -= __gbeg[__idx]; |
| 1306 | __idx < __gsize - 1 ? ++__idx : ++__ctr; |
| 1307 | } |
| 1308 | |
| 1309 | while (__first != __last) |
| 1310 | *__s++ = *__first++; |
| 1311 | |
| 1312 | while (__ctr--) |
| 1313 | { |
| 1314 | *__s++ = __sep; |
| 1315 | for (char __i = __gbeg[__idx]; __i > 0; --__i) |
| 1316 | *__s++ = *__first++; |
| 1317 | } |
| 1318 | |
| 1319 | while (__idx--) |
| 1320 | { |
| 1321 | *__s++ = __sep; |
| 1322 | for (char __i = __gbeg[__idx]; __i > 0; --__i) |
| 1323 | *__s++ = *__first++; |
| 1324 | } |
| 1325 | |
| 1326 | return __s; |
| 1327 | } |
| 1328 | |
| 1329 | // Inhibit implicit instantiations for required instantiations, |
| 1330 | // which are defined via explicit instantiations elsewhere. |
| 1331 | #if _GLIBCXX_EXTERN_TEMPLATE |
| 1332 | extern template class _GLIBCXX_NAMESPACE_CXX11 numpunct<char>; |
| 1333 | extern template class _GLIBCXX_NAMESPACE_CXX11 numpunct_byname<char>; |
| 1334 | extern template class _GLIBCXX_NAMESPACE_LDBL num_get<char>; |
| 1335 | extern template class _GLIBCXX_NAMESPACE_LDBL num_put<char>; |
| 1336 | extern template class ctype_byname<char>; |
| 1337 | |
| 1338 | extern template |
| 1339 | const ctype<char>* |
| 1340 | __try_use_facet<ctype<char> >(const locale&) _GLIBCXX_NOTHROW; |
| 1341 | |
| 1342 | extern template |
| 1343 | const numpunct<char>* |
| 1344 | __try_use_facet<numpunct<char> >(const locale&) _GLIBCXX_NOTHROW; |
| 1345 | |
| 1346 | extern template |
| 1347 | const num_put<char>* |
| 1348 | __try_use_facet<num_put<char> >(const locale&) _GLIBCXX_NOTHROW; |
| 1349 | |
| 1350 | extern template |
| 1351 | const num_get<char>* |
| 1352 | __try_use_facet<num_get<char> >(const locale&) _GLIBCXX_NOTHROW; |
| 1353 | |
| 1354 | extern template |
| 1355 | const ctype<char>& |
| 1356 | use_facet<ctype<char> >(const locale&); |
| 1357 | |
| 1358 | extern template |
| 1359 | const numpunct<char>& |
| 1360 | use_facet<numpunct<char> >(const locale&); |
| 1361 | |
| 1362 | extern template |
| 1363 | const num_put<char>& |
| 1364 | use_facet<num_put<char> >(const locale&); |
| 1365 | |
| 1366 | extern template |
| 1367 | const num_get<char>& |
| 1368 | use_facet<num_get<char> >(const locale&); |
| 1369 | |
| 1370 | extern template |
| 1371 | bool |
| 1372 | has_facet<ctype<char> >(const locale&); |
| 1373 | |
| 1374 | extern template |
| 1375 | bool |
| 1376 | has_facet<numpunct<char> >(const locale&); |
| 1377 | |
| 1378 | extern template |
| 1379 | bool |
| 1380 | has_facet<num_put<char> >(const locale&); |
| 1381 | |
| 1382 | extern template |
| 1383 | bool |
| 1384 | has_facet<num_get<char> >(const locale&); |
| 1385 | |
| 1386 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 1387 | extern template class _GLIBCXX_NAMESPACE_CXX11 numpunct<wchar_t>; |
| 1388 | extern template class _GLIBCXX_NAMESPACE_CXX11 numpunct_byname<wchar_t>; |
| 1389 | extern template class _GLIBCXX_NAMESPACE_LDBL num_get<wchar_t>; |
| 1390 | extern template class _GLIBCXX_NAMESPACE_LDBL num_put<wchar_t>; |
| 1391 | extern template class ctype_byname<wchar_t>; |
| 1392 | |
| 1393 | extern template |
| 1394 | const ctype<wchar_t>* |
| 1395 | __try_use_facet<ctype<wchar_t> >(const locale&) _GLIBCXX_NOTHROW; |
| 1396 | |
| 1397 | extern template |
| 1398 | const numpunct<wchar_t>* |
| 1399 | __try_use_facet<numpunct<wchar_t> >(const locale&) _GLIBCXX_NOTHROW; |
| 1400 | |
| 1401 | extern template |
| 1402 | const num_put<wchar_t>* |
| 1403 | __try_use_facet<num_put<wchar_t> >(const locale&) _GLIBCXX_NOTHROW; |
| 1404 | |
| 1405 | extern template |
| 1406 | const num_get<wchar_t>* |
| 1407 | __try_use_facet<num_get<wchar_t> >(const locale&) _GLIBCXX_NOTHROW; |
| 1408 | |
| 1409 | extern template |
| 1410 | const ctype<wchar_t>& |
| 1411 | use_facet<ctype<wchar_t> >(const locale&); |
| 1412 | |
| 1413 | extern template |
| 1414 | const numpunct<wchar_t>& |
| 1415 | use_facet<numpunct<wchar_t> >(const locale&); |
| 1416 | |
| 1417 | extern template |
| 1418 | const num_put<wchar_t>& |
| 1419 | use_facet<num_put<wchar_t> >(const locale&); |
| 1420 | |
| 1421 | extern template |
| 1422 | const num_get<wchar_t>& |
| 1423 | use_facet<num_get<wchar_t> >(const locale&); |
| 1424 | |
| 1425 | extern template |
| 1426 | bool |
| 1427 | has_facet<ctype<wchar_t> >(const locale&); |
| 1428 | |
| 1429 | extern template |
| 1430 | bool |
| 1431 | has_facet<numpunct<wchar_t> >(const locale&); |
| 1432 | |
| 1433 | extern template |
| 1434 | bool |
| 1435 | has_facet<num_put<wchar_t> >(const locale&); |
| 1436 | |
| 1437 | extern template |
| 1438 | bool |
| 1439 | has_facet<num_get<wchar_t> >(const locale&); |
| 1440 | #endif |
| 1441 | #endif |
| 1442 | |
| 1443 | _GLIBCXX_END_NAMESPACE_VERSION |
| 1444 | } // namespace |
| 1445 | |
| 1446 | #pragma GCC diagnostic pop |
| 1447 | #endif |
| 1448 | |