1 // 2 // corecrt_internal_strtox.h 3 // 4 // Copyright (c) Microsoft Corporation. All rights reserved. 5 // 6 // This file defines the core implementations of the numeric parsers that parse 7 // integer or floating point numbers stored as strings. These general parsers 8 // operate on abstract character sources, which allow the functions to be used 9 // to parse both random access and nonseekable ranges of characters (to support 10 // both strtod-style functions and fscanf-style functions). 11 // 12 #pragma once 13 14 #include <corecrt_internal.h> 15 #include <corecrt_internal_big_integer.h> 16 #include <corecrt_internal_fltintrn.h> 17 #include <corecrt_internal_ptd_propagation.h> 18 #include <corecrt_internal_stdio.h> 19 #include <ctype.h> 20 #include <fenv.h> 21 #include <locale.h> 22 #include <stdint.h> 23 24 // This header is temporarily mixed PTD-propagation and direct errno usage. 25 #pragma push_macro("_VALIDATE_RETURN_VOID") 26 #pragma push_macro("_VALIDATE_RETURN") 27 #pragma push_macro("_INVALID_PARAMETER") 28 #undef _VALIDATE_RETURN_VOID 29 #undef _VALIDATE_RETURN 30 #undef _INVALID_PARAMETER 31 32 #ifdef _DEBUG 33 #define _INVALID_PARAMETER(expr) _invalid_parameter(expr, __FUNCTIONW__, __FILEW__, __LINE__, 0) 34 #else 35 #define _INVALID_PARAMETER(expr) _invalid_parameter_noinfo() 36 #endif 37 38 #define _VALIDATE_RETURN(expr, errorcode, retexpr) \ 39 { \ 40 int _Expr_val = !!(expr); \ 41 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \ 42 if (!(_Expr_val)) \ 43 { \ 44 *_errno() = (errorcode); \ 45 _INVALID_PARAMETER(_CRT_WIDE(#expr)); \ 46 return (retexpr); \ 47 } \ 48 } 49 50 #define _VALIDATE_RETURN_VOID(expr, errorcode) \ 51 { \ 52 int _Expr_val = !!(expr); \ 53 _ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \ 54 if (!(_Expr_val)) \ 55 { \ 56 *_errno() = (errorcode); \ 57 _INVALID_PARAMETER(_CRT_WIDE(#expr)); \ 58 return; \ 59 } \ 60 } 61 62 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 63 // 64 // String-to-Integer Conversion 65 // 66 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 67 namespace __crt_strtox { 68 69 template <typename T> struct is_signed; 70 template <> struct is_signed<long > { enum { value = true }; }; 71 template <> struct is_signed<long long > { enum { value = true }; }; 72 template <> struct is_signed<unsigned long > { enum { value = false }; }; 73 template <> struct is_signed<unsigned long long> { enum { value = false }; }; 74 75 template <typename T> struct make_signed; 76 template <> struct make_signed<long > { using type = long; }; 77 template <> struct make_signed<long long > { using type = long long; }; 78 template <> struct make_signed<unsigned long > { using type = long; }; 79 template <> struct make_signed<unsigned long long> { using type = long long; }; 80 81 template <typename T> struct make_unsigned; 82 template <> struct make_unsigned<long > { using type = unsigned long; }; 83 template <> struct make_unsigned<long long > { using type = unsigned long long; }; 84 template <> struct make_unsigned<unsigned long > { using type = unsigned long; }; 85 template <> struct make_unsigned<unsigned long long> { using type = unsigned long long; }; 86 87 // Converts a wide character to its corresponding digit. Returns -1 on failure. 88 __forceinline int __cdecl wide_character_to_digit(wchar_t const c) throw() 89 { 90 #define DIGIT_RANGE_TEST(zero) \ 91 if (c < zero) \ 92 return -1; \ 93 \ 94 if (c < zero + 10) \ 95 return c - zero; 96 97 DIGIT_RANGE_TEST(0x0030) // 0030;DIGIT ZERO 98 if (c < 0xFF10) // FF10;FULLWIDTH DIGIT ZERO 99 { 100 DIGIT_RANGE_TEST(0x0660) // 0660;ARABIC-INDIC DIGIT ZERO 101 DIGIT_RANGE_TEST(0x06F0) // 06F0;EXTENDED ARABIC-INDIC DIGIT ZERO 102 DIGIT_RANGE_TEST(0x0966) // 0966;DEVANAGARI DIGIT ZERO 103 DIGIT_RANGE_TEST(0x09E6) // 09E6;BENGALI DIGIT ZERO 104 DIGIT_RANGE_TEST(0x0A66) // 0A66;GURMUKHI DIGIT ZERO 105 DIGIT_RANGE_TEST(0x0AE6) // 0AE6;GUJARATI DIGIT ZERO 106 DIGIT_RANGE_TEST(0x0B66) // 0B66;ORIYA DIGIT ZERO 107 DIGIT_RANGE_TEST(0x0C66) // 0C66;TELUGU DIGIT ZERO 108 DIGIT_RANGE_TEST(0x0CE6) // 0CE6;KANNADA DIGIT ZERO 109 DIGIT_RANGE_TEST(0x0D66) // 0D66;MALAYALAM DIGIT ZERO 110 DIGIT_RANGE_TEST(0x0E50) // 0E50;THAI DIGIT ZERO 111 DIGIT_RANGE_TEST(0x0ED0) // 0ED0;LAO DIGIT ZERO 112 DIGIT_RANGE_TEST(0x0F20) // 0F20;TIBETAN DIGIT ZERO 113 DIGIT_RANGE_TEST(0x1040) // 1040;MYANMAR DIGIT ZERO 114 DIGIT_RANGE_TEST(0x17E0) // 17E0;KHMER DIGIT ZERO 115 DIGIT_RANGE_TEST(0x1810) // 1810;MONGOLIAN DIGIT ZERO 116 117 return -1; 118 } 119 120 #undef DIGIT_RANGE_TEST 121 122 if (c < 0xFF10 + 10) // FF10;FULLWIDTH DIGIT ZERO 123 return c - 0xFF10; 124 125 return -1; 126 } 127 128 __forceinline unsigned __cdecl parse_digit(char const c) throw() 129 { 130 if (c >= '0' && c <= '9') 131 { 132 return static_cast<unsigned>(c - '0'); 133 } 134 135 if (c >= 'a' && c <= 'z') 136 { 137 return static_cast<unsigned>(c - 'a' + 10); 138 } 139 140 if (c >= 'A' && c <= 'Z') 141 { 142 return static_cast<unsigned>(c - 'A' + 10); 143 } 144 145 return static_cast<unsigned>(-1); 146 } 147 148 __forceinline unsigned __cdecl parse_digit(wchar_t const c) throw() 149 { 150 int const value = wide_character_to_digit(c); 151 if (value != -1) 152 return static_cast<unsigned>(value); 153 154 if (__ascii_iswalpha(c)) 155 return static_cast<unsigned>(__ascii_towupper(c) - 'A' + 10); 156 157 return static_cast<unsigned>(-1); 158 } 159 160 // The digit and nondigit categories include 0-9, a-z, A-Z, and _. They are not 161 // locale-dependent, so we do not call isalnum (which uses the current locale to 162 // test for alphabetic characters). 163 __forceinline bool __cdecl is_digit_or_nondigit(int const c) throw() 164 { 165 if (c >= '0' && c <= '9') 166 return true; 167 168 if (c >= 'a' && c <= 'z') 169 return true; 170 171 if (c >= 'A' && c <= 'Z') 172 return true; 173 174 if (c == '_') 175 return true; 176 177 return false; 178 } 179 180 __forceinline bool __cdecl is_space(char const c, _locale_t const locale) throw() 181 { 182 return _isspace_l(static_cast<int>(static_cast<unsigned char>(c)), locale) != 0; 183 } 184 185 __forceinline bool __cdecl is_space(wchar_t const c, _locale_t) throw() 186 { 187 return iswspace(c) != 0; 188 } 189 190 inline long minimum_signed_value(unsigned long) throw() { return LONG_MIN; } 191 inline long maximum_signed_value(unsigned long) throw() { return LONG_MAX; } 192 193 inline __int64 minimum_signed_value(unsigned __int64) throw() { return _I64_MIN; } 194 inline __int64 maximum_signed_value(unsigned __int64) throw() { return _I64_MAX; } 195 196 enum : unsigned 197 { 198 FL_SIGNED = 0x01, 199 FL_NEGATIVE = 0x02, 200 FL_OVERFLOW = 0x04, 201 FL_READ_DIGIT = 0x08 202 }; 203 204 template <typename UnsignedInteger> 205 bool is_overflow_condition(unsigned const flags, UnsignedInteger const number) throw() 206 { 207 if (flags & FL_OVERFLOW) 208 return true; 209 210 if (flags & FL_SIGNED) 211 { 212 if ((flags & FL_NEGATIVE) != 0 && number > static_cast<UnsignedInteger>(-minimum_signed_value(UnsignedInteger()))) 213 return true; 214 215 if ((flags & FL_NEGATIVE) == 0 && number > static_cast<UnsignedInteger>(maximum_signed_value(UnsignedInteger()))) 216 return true; 217 } 218 219 return false; 220 } 221 222 template <typename UnsignedInteger, typename CharacterSource, bool TrimWhitespace = true> 223 UnsignedInteger __cdecl parse_integer( 224 __crt_cached_ptd_host& ptd, 225 CharacterSource source, 226 int base, 227 bool const is_result_signed 228 ) throw() 229 { 230 static_assert(!__crt_strtox::is_signed<UnsignedInteger>::value, "UnsignedInteger must be unsigned"); 231 232 using char_type = typename CharacterSource::char_type; 233 234 if (!source.validate()) 235 return 0; 236 237 _UCRT_VALIDATE_RETURN(ptd, base == 0 || (2 <= base && base <= 36), EINVAL, 0); 238 UnsignedInteger number{0}; // number is the accumulator 239 240 auto const initial_state = source.save_state(); 241 242 243 char_type c{source.get()}; 244 245 if constexpr (TrimWhitespace) 246 { 247 const _locale_t loc = ptd.get_locale(); 248 while (is_space(c, loc)) 249 { 250 c = source.get(); 251 } 252 } 253 254 unsigned flags{is_result_signed ? FL_SIGNED : 0}; 255 256 // Optional sign (+ or -): 257 if (c == '-') 258 { 259 flags |= FL_NEGATIVE; 260 } 261 262 if (c == '-' || c == '+') 263 { 264 c = source.get(); 265 } 266 267 // If the base is zero, we try to detect the base from the string prefix: 268 if (base == 0 || base == 16) 269 { 270 if (parse_digit(c) != 0) 271 { 272 if (base == 0) 273 { 274 base = 10; 275 } 276 } 277 else 278 { 279 char_type const next_c = source.get(); 280 if (next_c == 'x' || next_c == 'X') 281 { 282 if (base == 0) 283 { 284 base = 16; 285 } 286 c = source.get(); 287 } 288 else 289 { 290 if (base == 0) 291 { 292 base = 8; 293 } 294 source.unget(next_c); 295 } 296 } 297 } 298 299 UnsignedInteger const max_pre_multiply_value = static_cast<UnsignedInteger>(-1) / base; 300 301 for (;;) 302 { 303 unsigned const digit{parse_digit(c)}; 304 if (digit >= static_cast<unsigned>(base)) 305 { 306 // This also handles the case where the digit could not 307 // be parsed and parse_digit returns -1. 308 break; 309 } 310 311 flags |= FL_READ_DIGIT; 312 313 UnsignedInteger const number_after_multiply = number * base; 314 UnsignedInteger const number_after_add = number_after_multiply + digit; 315 316 // Avoid branching when setting overflow flag. 317 flags |= FL_OVERFLOW * ((number > max_pre_multiply_value) | (number_after_add < number_after_multiply)); 318 319 number = number_after_add; 320 321 c = source.get(); 322 } 323 324 source.unget(c); // Return the pointer to the character that ended the scan 325 326 // If we failed to read any digits, there's no number to be read: 327 if ((flags & FL_READ_DIGIT) == 0) 328 { 329 source.restore_state(initial_state); 330 return 0; 331 } 332 333 if (is_overflow_condition(flags, number)) 334 { 335 ptd.get_errno().set(ERANGE); 336 337 if ((flags & FL_SIGNED) == 0) 338 { 339 number = static_cast<UnsignedInteger>(-1); 340 } 341 else if (flags & FL_NEGATIVE) 342 { 343 return minimum_signed_value(UnsignedInteger()); 344 } 345 else 346 { 347 return maximum_signed_value(UnsignedInteger()); 348 } 349 } 350 else if (flags & FL_NEGATIVE) 351 { 352 number = static_cast<UnsignedInteger>(-static_cast<typename make_signed<UnsignedInteger>::type>(number)); 353 } 354 355 return number; 356 } 357 358 template <typename UnsignedInteger, typename CharacterSource> 359 UnsignedInteger __cdecl parse_integer( 360 _locale_t const locale, 361 CharacterSource source, 362 int base, 363 bool const is_result_signed 364 ) throw() 365 { 366 __crt_cached_ptd_host ptd{locale}; 367 return parse_integer<UnsignedInteger>(ptd, static_cast<CharacterSource&&>(source), base, is_result_signed); 368 } 369 370 } // namespace __crt_strtox 371 372 373 374 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 375 // 376 // String-to-Floating-Point Conversion 377 // 378 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 379 typedef enum 380 { 381 SLD_OK, 382 383 SLD_NODIGITS, 384 385 SLD_UNDERFLOW, 386 SLD_OVERFLOW 387 } SLD_STATUS; 388 389 namespace __crt_strtox { 390 391 // This is the internal result type of an attempt to parse a floating point value 392 // from a string. The SLD_STATUS type (above) is the type returned to callers of 393 // the top-level parse_floating_point. 394 enum class floating_point_parse_result 395 { 396 decimal_digits, 397 hexadecimal_digits, 398 399 zero, 400 infinity, 401 qnan, 402 snan, 403 indeterminate, 404 405 no_digits, 406 underflow, 407 overflow 408 }; 409 410 enum 411 { 412 maximum_temporary_decimal_exponent = 5200, 413 minimum_temporary_decimal_exponent = -5200 414 }; 415 416 // This type is used to hold a partially-parsed string representation of a 417 // floating point number. The number is stored in the following form: 418 // 419 // [sign] 0._mantissa * B^_exponent 420 // 421 // The _mantissa buffer stores the mantissa digits in big endian, binary coded 422 // decimal form. The _mantissa_count stores the number of digits present in the 423 // _mantissa buffer. The base B is not stored; it must be tracked separately. 424 // Note that the base of the mantissa digits may not be the same as B (e.g., for 425 // hexadecimal floating point hexadecimal, the mantissa digits are in base 16 426 // but the exponent is a base 2 exponent). 427 // 428 // We consider up to 768 decimal digits during conversion. In most cases, we 429 // require nowhere near this many bits of precision to compute the correctly 430 // rounded binary floating point value for the input string. 768 bits gives us 431 // room to handle the exact decimal representation of the smallest denormal 432 // value, 2^-1074 (752 decimal digits after trimming zeroes) with a bit of slack 433 // space. 434 // 435 // NOTE: The mantissa buffer count here must be kept in sync with the precision 436 // of the big_integer type, defined in <corecrt_internal_big_integer.h>. See that file 437 // for details. 438 struct floating_point_string 439 { 440 int32_t _exponent; 441 uint32_t _mantissa_count; 442 uint8_t _mantissa[768]; 443 bool _is_negative; 444 }; 445 446 // This type wraps a float or double. It serves as a crude form of type erasure 447 // to allow us to avoid instantiating most of the parsing logic twice (once for 448 // float and once for double). 449 class floating_point_value 450 { 451 public: 452 453 template <typename T> 454 using traits = __acrt_floating_type_traits<T>; 455 456 explicit floating_point_value(double* const value) throw() 457 : _value(value), _is_double(true) 458 { 459 _ASSERTE(value != nullptr); 460 } 461 462 explicit floating_point_value(float* const value) throw() 463 : _value(value), _is_double(false) 464 { 465 _ASSERTE(value != nullptr); 466 } 467 468 bool is_double() const throw() 469 { 470 return _is_double; 471 } 472 473 double& as_double() const throw() 474 { 475 _ASSERT_AND_INVOKE_WATSON(_is_double); 476 return *static_cast<double*>(_value); 477 } 478 479 float& as_float() const throw() 480 { 481 _ASSERT_AND_INVOKE_WATSON(!_is_double); 482 return *static_cast<float*>(_value); 483 } 484 485 int32_t mantissa_bits () const throw() { return _is_double ? traits<double>::mantissa_bits : traits<float>::mantissa_bits; } 486 int32_t exponent_bits () const throw() { return _is_double ? traits<double>::exponent_bits : traits<float>::exponent_bits; } 487 int32_t maximum_binary_exponent() const throw() { return _is_double ? traits<double>::maximum_binary_exponent : traits<float>::maximum_binary_exponent; } 488 int32_t minimum_binary_exponent() const throw() { return _is_double ? traits<double>::minimum_binary_exponent : traits<float>::minimum_binary_exponent; } 489 int32_t exponent_bias () const throw() { return _is_double ? traits<double>::exponent_bias : traits<float>::exponent_bias; } 490 491 uint64_t exponent_mask () const throw() { return _is_double ? traits<double>::exponent_mask : traits<float>::exponent_mask; } 492 uint64_t normal_mantissa_mask () const throw() { return _is_double ? traits<double>::normal_mantissa_mask : traits<float>::normal_mantissa_mask; } 493 uint64_t denormal_mantissa_mask () const throw() { return _is_double ? traits<double>::denormal_mantissa_mask : traits<float>::denormal_mantissa_mask; } 494 uint64_t special_nan_mantissa_mask() const throw() { return _is_double ? traits<double>::special_nan_mantissa_mask : traits<float>::special_nan_mantissa_mask; } 495 496 private: 497 498 void* _value; 499 bool _is_double; 500 }; 501 502 // Stores a positive or negative zero into the result object 503 template <typename FloatingType> 504 void __cdecl assemble_floating_point_zero(bool const is_negative, FloatingType& result) throw() 505 { 506 using floating_traits = __acrt_floating_type_traits<FloatingType>; 507 using components_type = typename floating_traits::components_type; 508 509 components_type& result_components = reinterpret_cast<components_type&>(result); 510 result_components._sign = is_negative ? 1 : 0; 511 result_components._exponent = 0; 512 result_components._mantissa = 0; 513 } 514 515 inline void __cdecl assemble_floating_point_zero(bool const is_negative, floating_point_value const& result) throw() 516 { 517 if (result.is_double()) 518 { 519 assemble_floating_point_zero(is_negative, result.as_double()); 520 } 521 else 522 { 523 assemble_floating_point_zero(is_negative, result.as_float()); 524 } 525 } 526 527 // Stores a positive or negative infinity into the result object 528 template <typename FloatingType> 529 void __cdecl assemble_floating_point_infinity(bool const is_negative, FloatingType& result) throw() 530 { 531 using floating_traits = __acrt_floating_type_traits<FloatingType>; 532 using components_type = typename floating_traits::components_type; 533 534 components_type& result_components = reinterpret_cast<components_type&>(result); 535 result_components._sign = is_negative ? 1 : 0; 536 result_components._exponent = floating_traits::exponent_mask; 537 result_components._mantissa = 0; 538 } 539 540 inline void __cdecl assemble_floating_point_infinity(bool const is_negative, floating_point_value const& result) throw() 541 { 542 if (result.is_double()) 543 { 544 assemble_floating_point_infinity(is_negative, result.as_double()); 545 } 546 else 547 { 548 assemble_floating_point_infinity(is_negative, result.as_float()); 549 } 550 } 551 552 // Stores a positive or negative quiet NaN into the result object 553 template <typename FloatingType> 554 void __cdecl assemble_floating_point_qnan(bool const is_negative, FloatingType& result) throw() 555 { 556 using floating_traits = __acrt_floating_type_traits<FloatingType>; 557 using components_type = typename floating_traits::components_type; 558 559 components_type& result_components = reinterpret_cast<components_type&>(result); 560 result_components._sign = is_negative ? 1 : 0; 561 result_components._exponent = floating_traits::exponent_mask; 562 result_components._mantissa = floating_traits::denormal_mantissa_mask; 563 } 564 565 inline void __cdecl assemble_floating_point_qnan(bool const is_negative, floating_point_value const& result) throw() 566 { 567 if (result.is_double()) 568 { 569 assemble_floating_point_qnan(is_negative, result.as_double()); 570 } 571 else 572 { 573 assemble_floating_point_qnan(is_negative, result.as_float()); 574 } 575 } 576 577 // Stores a positive or negative signaling NaN into the result object 578 template <typename FloatingType> 579 void __cdecl assemble_floating_point_snan(bool const is_negative, FloatingType& result) throw() 580 { 581 using floating_traits = __acrt_floating_type_traits<FloatingType>; 582 using components_type = typename floating_traits::components_type; 583 584 components_type& result_components = reinterpret_cast<components_type&>(result); 585 result_components._sign = is_negative ? 1 : 0; 586 result_components._exponent = floating_traits::exponent_mask; 587 result_components._mantissa = 1; 588 } 589 590 inline void __cdecl assemble_floating_point_snan(bool const is_negative, floating_point_value const& result) throw() 591 { 592 if (result.is_double()) 593 { 594 assemble_floating_point_snan(is_negative, result.as_double()); 595 } 596 else 597 { 598 assemble_floating_point_snan(is_negative, result.as_float()); 599 } 600 } 601 602 // Stores an indeterminate into the result object (the indeterminate is "negative") 603 template <typename FloatingType> 604 void __cdecl assemble_floating_point_ind(FloatingType& result) throw() 605 { 606 using floating_traits = __acrt_floating_type_traits<FloatingType>; 607 using components_type = typename floating_traits::components_type; 608 609 components_type& result_components = reinterpret_cast<components_type&>(result); 610 result_components._sign = 1; 611 result_components._exponent = floating_traits::exponent_mask; 612 result_components._mantissa = floating_traits::special_nan_mantissa_mask; 613 } 614 615 inline void __cdecl assemble_floating_point_ind(floating_point_value const& result) throw() 616 { 617 if (result.is_double()) 618 { 619 assemble_floating_point_ind(result.as_double()); 620 } 621 else 622 { 623 assemble_floating_point_ind(result.as_float()); 624 } 625 } 626 627 // Determines whether a mantissa should be rounded up in the current rounding 628 // mode given [1] the value of the least significant bit of the mantissa, [2] 629 // the value of the next bit after the least significant bit (the "round" bit) 630 // and [3] whether any trailing bits after the round bit are set. 631 // 632 // The mantissa is treated as an unsigned integer magnitude; the sign is used 633 // only to compute the correct rounding direction for directional rounding modes. 634 // 635 // For this function, "round up" is defined as "increase the magnitude" of the 636 // mantissa. (Note that this means that if we need to round a negative value to 637 // the next largest representable value, we return false, because the next 638 // largest representable value has a smaller magnitude.) 639 __forceinline bool __cdecl should_round_up( 640 bool const is_negative, 641 bool const lsb_bit, 642 bool const round_bit, 643 bool const has_tail_bits 644 ) throw() 645 { 646 // If there are no insignificant set bits, the value is exactly representable 647 // and should not be rounded in any rounding mode: 648 bool const is_exactly_representable = !round_bit && !has_tail_bits; 649 if (is_exactly_representable) 650 { 651 return false; 652 } 653 654 // If there are insignificant set bits, we need to round according to the 655 // current rounding mode. For FE_TONEAREST, we need to handle two cases: 656 // we round up if either [1] the value is slightly greater than the midpoint 657 // between two exactly representable values or [2] the value is exactly the 658 // midpoint between two exactly representable values and the greater of the 659 // two is even (this is "round-to-even"). 660 switch (fegetround()) 661 { 662 case FE_TONEAREST: return round_bit && (has_tail_bits || lsb_bit); 663 case FE_UPWARD: return !is_negative; 664 case FE_DOWNWARD: return is_negative; 665 case FE_TOWARDZERO: return false; 666 } 667 668 _ASSERTE(("unexpected rounding mode", false)); 669 return false; 670 } 671 672 // Computes value / 2^shift, then rounds the result according to the current 673 // rounding mode. By the time we call this function, we will already have 674 // discarded most digits. The caller must pass true for has_zero_tail if 675 // all discarded bits were zeroes. 676 __forceinline uint64_t __cdecl right_shift_with_rounding( 677 bool const is_negative, 678 uint64_t const value, 679 uint32_t const shift, 680 bool const has_zero_tail 681 ) throw() 682 { 683 // If we'd need to shift further than it is possible to shift, the answer 684 // is always zero: 685 if (shift >= sizeof(uint64_t) * CHAR_BIT) 686 { 687 return 0; 688 } 689 690 uint64_t const extra_bits_mask = (1ull << (shift - 1)) - 1; 691 uint64_t const round_bit_mask = (1ull << (shift - 1)); 692 uint64_t const lsb_bit_mask = 1ull << shift; 693 694 bool const lsb_bit = (value & lsb_bit_mask) != 0; 695 bool const round_bit = (value & round_bit_mask) != 0; 696 bool const tail_bits = !has_zero_tail || (value & extra_bits_mask) != 0; 697 698 return (value >> shift) + should_round_up(is_negative, lsb_bit, round_bit, tail_bits); 699 } 700 701 // Converts the floating point value [sign] 0.mantissa * 2^exponent into the 702 // correct form for FloatingType and stores the result into the result object. 703 // The caller must ensure that the mantissa and exponent are correctly computed 704 // such that either [1] the most significant bit of the mantissa is in the 705 // correct position for the FloatingType, or [2] the exponent has been correctly 706 // adjusted to account for the shift of the mantissa that will be required. 707 // 708 // This function correctly handles range errors and stores a zero or infinity in 709 // the result object on underflow and overflow errors, respectively. This 710 // function correctly forms denormal numbers when required. 711 // 712 // If the provided mantissa has more bits of precision than can be stored in the 713 // result object, the mantissa is rounded to the available precision. Thus, if 714 // possible, the caller should provide a mantissa with at least one more bit of 715 // precision than is required, to ensure that the mantissa is correctly rounded. 716 // (The caller should not round the mantissa before calling this function.) 717 template <typename FloatingType> 718 SLD_STATUS __cdecl assemble_floating_point_value_t( 719 bool const is_negative, 720 int32_t const exponent, 721 uint64_t const mantissa, 722 FloatingType & result 723 ) throw() 724 { 725 using floating_traits = __acrt_floating_type_traits<FloatingType>; 726 using components_type = typename floating_traits::components_type; 727 728 components_type& result_components = reinterpret_cast<components_type&>(result); 729 result_components._sign = is_negative; 730 result_components._exponent = exponent + floating_traits::exponent_bias; 731 result_components._mantissa = mantissa; 732 return SLD_OK; 733 } 734 735 inline SLD_STATUS __cdecl assemble_floating_point_value( 736 uint64_t const initial_mantissa, 737 int32_t const initial_exponent, 738 bool const is_negative, 739 bool const has_zero_tail, 740 floating_point_value const& result 741 ) throw() 742 { 743 // Assume that the number is representable as a normal value. Compute the 744 // number of bits by which we must adjust the mantissa to shift it into the 745 // correct position, and compute the resulting base two exponent for the 746 // normalized mantissa: 747 uint32_t const initial_mantissa_bits = bit_scan_reverse(initial_mantissa); 748 int32_t const normal_mantissa_shift = static_cast<int32_t>(result.mantissa_bits() - initial_mantissa_bits); 749 int32_t const normal_exponent = initial_exponent - normal_mantissa_shift; 750 751 uint64_t mantissa = initial_mantissa; 752 int32_t exponent = normal_exponent; 753 754 if (normal_exponent > result.maximum_binary_exponent()) 755 { 756 // The exponent is too large to be represented by the floating point 757 // type; report the overflow condition: 758 assemble_floating_point_infinity(is_negative, result); 759 return SLD_OVERFLOW; 760 } 761 else if (normal_exponent < result.minimum_binary_exponent()) 762 { 763 // The exponent is too small to be represented by the floating point 764 // type as a normal value, but it may be representable as a denormal 765 // value. Compute the number of bits by which we need to shift the 766 // mantissa in order to form a denormal number. (The subtraction of 767 // an extra 1 is to account for the hidden bit of the mantissa that 768 // is not available for use when representing a denormal.) 769 int32_t const denormal_mantissa_shift = 770 normal_mantissa_shift + 771 normal_exponent + 772 result.exponent_bias() - 773 1; 774 775 // Denormal values have an exponent of zero, so the debiased exponent is 776 // the negation of the exponent bias: 777 exponent = -result.exponent_bias(); 778 779 if (denormal_mantissa_shift < 0) 780 { 781 // Use two steps for right shifts: for a shift of N bits, we first 782 // shift by N-1 bits, then shift the last bit and use its value to 783 // round the mantissa. 784 mantissa = right_shift_with_rounding(is_negative, mantissa, -denormal_mantissa_shift, has_zero_tail); 785 786 // If the mantissa is now zero, we have underflowed: 787 if (mantissa == 0) 788 { 789 assemble_floating_point_zero(is_negative, result); 790 return SLD_UNDERFLOW; 791 } 792 793 // When we round the mantissa, the result may be so large that the 794 // number becomes a normal value. For example, consider the single 795 // precision case where the mantissa is 0x01ffffff and a right shift 796 // of 2 is required to shift the value into position. We perform the 797 // shift in two steps: we shift by one bit, then we shift again and 798 // round using the dropped bit. The initial shift yields 0x00ffffff. 799 // The rounding shift then yields 0x007fffff and because the least 800 // significant bit was 1, we add 1 to this number to round it. The 801 // final result is 0x00800000. 802 // 803 // 0x00800000 is 24 bits, which is more than the 23 bits available 804 // in the mantissa. Thus, we have rounded our denormal number into 805 // a normal number. 806 // 807 // We detect this case here and re-adjust the mantissa and exponent 808 // appropriately, to form a normal number: 809 if (mantissa > result.denormal_mantissa_mask()) 810 { 811 // We add one to the denormal_mantissa_shift to account for the 812 // hidden mantissa bit (we subtracted one to account for this bit 813 // when we computed the denormal_mantissa_shift above). 814 exponent = 815 initial_exponent - 816 (denormal_mantissa_shift + 1) - 817 normal_mantissa_shift; 818 } 819 } 820 else 821 { 822 mantissa <<= denormal_mantissa_shift; 823 } 824 } 825 else 826 { 827 if (normal_mantissa_shift < 0) 828 { 829 // Use two steps for right shifts: for a shift of N bits, we first 830 // shift by N-1 bits, then shift the last bit and use its value to 831 // round the mantissa. 832 mantissa = right_shift_with_rounding(is_negative, mantissa, -normal_mantissa_shift, has_zero_tail); 833 834 // When we round the mantissa, it may produce a result that is too 835 // large. In this case, we divide the mantissa by two and increment 836 // the exponent (this does not change the value). 837 if (mantissa > result.normal_mantissa_mask()) 838 { 839 mantissa >>= 1; 840 ++exponent; 841 842 // The increment of the exponent may have generated a value too 843 // large to be represented. In this case, report the overflow: 844 if (exponent > result.maximum_binary_exponent()) 845 { 846 assemble_floating_point_infinity(is_negative, result); 847 return SLD_OVERFLOW; 848 } 849 } 850 } 851 else if (normal_mantissa_shift > 0) 852 { 853 mantissa <<= normal_mantissa_shift; 854 } 855 } 856 857 // Unset the hidden bit in the mantissa and assemble the floating point value 858 // from the computed components: 859 mantissa &= result.denormal_mantissa_mask(); 860 861 return result.is_double() 862 ? assemble_floating_point_value_t(is_negative, exponent, mantissa, result.as_double()) 863 : assemble_floating_point_value_t(is_negative, exponent, mantissa, result.as_float()); 864 } 865 866 // This function is part of the fast track for integer floating point strings. 867 // It takes an integer and a sign and converts the value into its FloatingType 868 // representation, storing the result in the result object. If the value is not 869 // representable, +/-infinity is stored and overflow is reported (since this 870 // function only deals with integers, underflow is impossible). 871 inline SLD_STATUS __cdecl assemble_floating_point_value_from_big_integer( 872 big_integer const& integer_value, 873 uint32_t const integer_bits_of_precision, 874 bool const is_negative, 875 bool const has_nonzero_fractional_part, 876 floating_point_value const& result 877 ) throw() 878 { 879 int32_t const base_exponent = result.mantissa_bits() - 1; 880 881 // Very fast case: If we have fewer than 64 bits of precision, we can just 882 // take the two low order elements from the big_integer: 883 if (integer_bits_of_precision <= 64) 884 { 885 int32_t const exponent = base_exponent; 886 887 uint32_t const mantissa_low = integer_value._used > 0 ? integer_value._data[0] : 0; 888 uint32_t const mantissa_high = integer_value._used > 1 ? integer_value._data[1] : 0; 889 uint64_t const mantissa = 890 mantissa_low + 891 (static_cast<uint64_t>(mantissa_high) << 32); 892 893 return assemble_floating_point_value(mantissa, exponent, is_negative, !has_nonzero_fractional_part, result); 894 } 895 896 uint32_t const top_element_bits = integer_bits_of_precision % 32; 897 uint32_t const top_element_index = integer_bits_of_precision / 32; 898 899 uint32_t const middle_element_index = top_element_index - 1; 900 uint32_t const bottom_element_index = top_element_index - 2; 901 902 // Pretty fast case: If the top 64 bits occupy only two elements, we can 903 // just combine those two elements: 904 if (top_element_bits == 0) 905 { 906 int32_t const exponent = base_exponent + bottom_element_index * 32; 907 908 uint64_t const mantissa = 909 integer_value._data[bottom_element_index] + 910 (static_cast<uint64_t>(integer_value._data[middle_element_index]) << 32); 911 912 bool has_zero_tail = !has_nonzero_fractional_part; 913 for (uint32_t i = 0; i != bottom_element_index; ++i) 914 { 915 has_zero_tail &= integer_value._data[i] == 0; 916 } 917 918 return assemble_floating_point_value(mantissa, exponent, is_negative, has_zero_tail, result); 919 } 920 921 // Not quite so fast case: The top 64 bits span three elements in the big 922 // integer. Assemble the three pieces: 923 uint32_t const top_element_mask = (1u << top_element_bits) - 1; 924 uint32_t const top_element_shift = 64 - top_element_bits; // Left 925 926 uint32_t const middle_element_shift = top_element_shift - 32; // Left 927 928 uint32_t const bottom_element_bits = 32 - top_element_bits; 929 uint32_t const bottom_element_mask = ~top_element_mask; 930 uint32_t const bottom_element_shift = 32 - bottom_element_bits; // Right 931 932 int32_t const exponent = base_exponent + bottom_element_index * 32 + top_element_bits; 933 934 uint64_t const mantissa = 935 (static_cast<uint64_t>(integer_value._data[top_element_index] & top_element_mask) << top_element_shift) + 936 (static_cast<uint64_t>(integer_value._data[middle_element_index]) << middle_element_shift) + 937 (static_cast<uint64_t>(integer_value._data[bottom_element_index] & bottom_element_mask) >> bottom_element_shift); 938 939 bool has_zero_tail = 940 !has_nonzero_fractional_part && 941 (integer_value._data[bottom_element_index] & top_element_mask) == 0; 942 943 for (uint32_t i = 0; i != bottom_element_index; ++i) 944 { 945 has_zero_tail &= integer_value._data[i] == 0; 946 } 947 948 return assemble_floating_point_value(mantissa, exponent, is_negative, has_zero_tail, result); 949 } 950 951 // Accumulates the decimal digits in [first_digit, last_digit) into the result 952 // high precision integer. This function assumes that no overflow will occur. 953 __forceinline void __cdecl accumulate_decimal_digits_into_big_integer( 954 uint8_t const* const first_digit, 955 uint8_t const* const last_digit, 956 big_integer& result 957 ) throw() 958 { 959 // We accumulate nine digit chunks, transforming the base ten string into 960 // base one billion on the fly, allowing us to reduce the number of high 961 // precision multiplication and addition operations by 8/9. 962 uint32_t accumulator{}; 963 uint32_t accumulator_count{}; 964 for (uint8_t const* it = first_digit; it != last_digit; ++it) 965 { 966 if (accumulator_count == 9) 967 { 968 multiply(result, 1000 * 1000 * 1000); 969 add(result, accumulator); 970 971 accumulator = 0; 972 accumulator_count = 0; 973 } 974 975 accumulator *= 10; 976 accumulator += *it; 977 ++accumulator_count; 978 } 979 980 if (accumulator_count != 0) 981 { 982 multiply_by_power_of_ten(result, accumulator_count); 983 add(result, accumulator); 984 } 985 } 986 987 // The core floating point string parser for decimal strings. After a subject 988 // string is parsed and converted into a floating_point_string object, if the 989 // subject string was determined to be a decimal string, the object is passed to 990 // this function. This function converts the decimal real value to floating 991 // point. 992 inline SLD_STATUS __cdecl convert_decimal_string_to_floating_type_common( 993 floating_point_string const& data, 994 floating_point_value const& result 995 ) throw() 996 { 997 // To generate an N bit mantissa we require N + 1 bits of precision. The 998 // extra bit is used to correctly round the mantissa (if there are fewer bits 999 // than this available, then that's totally okay; in that case we use what we 1000 // have and we don't need to round). 1001 uint32_t const required_bits_of_precision = result.mantissa_bits() + 1; 1002 1003 // The input is of the form 0.mantissa x 10^exponent, where 'mantissa' are 1004 // the decimal digits of the mantissa and 'exponent' is the decimal exponent. 1005 // We decompose the mantissa into two parts: an integer part and a fractional 1006 // part. If the exponent is positive, then the integer part consists of the 1007 // first 'exponent' digits, or all present digits if there are fewer digits. 1008 // If the exponent is zero or negative, then the integer part is empty. In 1009 // either case, the remaining digits form the fractional part of the mantissa. 1010 uint32_t const positive_exponent = static_cast<uint32_t>(__max(0, data._exponent)); 1011 uint32_t const integer_digits_present = __min(positive_exponent, data._mantissa_count); 1012 uint32_t const integer_digits_missing = positive_exponent - integer_digits_present; 1013 uint8_t const* const integer_first = data._mantissa; 1014 uint8_t const* const integer_last = data._mantissa + integer_digits_present; 1015 1016 uint8_t const* const fractional_first = integer_last; 1017 uint8_t const* const fractional_last = data._mantissa + data._mantissa_count; 1018 uint32_t const fractional_digits_present = static_cast<uint32_t>(fractional_last - fractional_first); 1019 1020 // First, we accumulate the integer part of the mantissa into a big_integer: 1021 big_integer integer_value{}; 1022 accumulate_decimal_digits_into_big_integer(integer_first, integer_last, integer_value); 1023 1024 if (integer_digits_missing > 0) 1025 { 1026 if (!multiply_by_power_of_ten(integer_value, integer_digits_missing)) 1027 { 1028 assemble_floating_point_infinity(data._is_negative, result); 1029 return SLD_OVERFLOW; 1030 } 1031 } 1032 1033 // At this point, the integer_value contains the value of the integer part 1034 // of the mantissa. If either [1] this number has more than the required 1035 // number of bits of precision or [2] the mantissa has no fractional part, 1036 // then we can assemble the result immediately: 1037 uint32_t const integer_bits_of_precision = bit_scan_reverse(integer_value); 1038 if (integer_bits_of_precision >= required_bits_of_precision || 1039 fractional_digits_present == 0) 1040 { 1041 return assemble_floating_point_value_from_big_integer( 1042 integer_value, 1043 integer_bits_of_precision, 1044 data._is_negative, 1045 fractional_digits_present != 0, 1046 result); 1047 } 1048 1049 // Otherwise, we did not get enough bits of precision from the integer part, 1050 // and the mantissa has a fractional part. We parse the fractional part of 1051 // the mantsisa to obtain more bits of precision. To do this, we convert 1052 // the fractional part into an actual fraction N/M, where the numerator N is 1053 // computed from the digits of the fractional part, and the denominator M is 1054 // computed as the power of 10 such that N/M is equal to the value of the 1055 // fractional part of the mantissa. 1056 big_integer fractional_numerator{}; 1057 accumulate_decimal_digits_into_big_integer(fractional_first, fractional_last, fractional_numerator); 1058 1059 uint32_t const fractional_denominator_exponent = data._exponent < 0 1060 ? fractional_digits_present + static_cast<uint32_t>(-data._exponent) 1061 : fractional_digits_present; 1062 1063 big_integer fractional_denominator = make_big_integer(1); 1064 if (!multiply_by_power_of_ten(fractional_denominator, fractional_denominator_exponent)) 1065 { 1066 // If there were any digits in the integer part, it is impossible to 1067 // underflow (because the exponent cannot possibly be small enough), 1068 // so if we underflow here it is a true underflow and we return zero. 1069 assemble_floating_point_zero(data._is_negative, result); 1070 return SLD_UNDERFLOW; 1071 } 1072 1073 // Because we are using only the fractional part of the mantissa here, the 1074 // numerator is guaranteed to be smaller than the denominator. We normalize 1075 // the fraction such that the most significant bit of the numerator is in 1076 // the same position as the most significant bit in the denominator. This 1077 // ensures that when we later shift the numerator N bits to the left, we 1078 // will produce N bits of precision. 1079 uint32_t const fractional_numerator_bits = bit_scan_reverse(fractional_numerator); 1080 uint32_t const fractional_denominator_bits = bit_scan_reverse(fractional_denominator); 1081 1082 uint32_t const fractional_shift = fractional_denominator_bits > fractional_numerator_bits 1083 ? fractional_denominator_bits - fractional_numerator_bits 1084 : 0; 1085 1086 if (fractional_shift > 0) 1087 { 1088 shift_left(fractional_numerator, fractional_shift); 1089 } 1090 1091 uint32_t const required_fractional_bits_of_precision = 1092 required_bits_of_precision - 1093 integer_bits_of_precision; 1094 1095 uint32_t remaining_bits_of_precision_required = required_fractional_bits_of_precision; 1096 if (integer_bits_of_precision > 0) 1097 { 1098 // If the fractional part of the mantissa provides no bits of precision 1099 // and cannot affect rounding, we can just take whatever bits we got from 1100 // the integer part of the mantissa. This is the case for numbers like 1101 // 5.0000000000000000000001, where the significant digits of the fractional 1102 // part start so far to the right that they do not affect the floating 1103 // point representation. 1104 // 1105 // If the fractional shift is exactly equal to the number of bits of 1106 // precision that we require, then no fractional bits will be part of the 1107 // result, but the result may affect rounding. This is e.g. the case for 1108 // large, odd integers with a fractional part greater than or equal to .5. 1109 // Thus, we need to do the division to correctl round the result. 1110 if (fractional_shift > remaining_bits_of_precision_required) 1111 { 1112 return assemble_floating_point_value_from_big_integer( 1113 integer_value, 1114 integer_bits_of_precision, 1115 data._is_negative, 1116 fractional_digits_present != 0, 1117 result); 1118 } 1119 1120 remaining_bits_of_precision_required -= fractional_shift; 1121 } 1122 1123 // If there was no integer part of the mantissa, we will need to compute the 1124 // exponent from the fractional part. The fractional exponent is the power 1125 // of two by which we must multiply the fractional part to move it into the 1126 // range [1.0, 2.0). This will either be the same as the shift we computed 1127 // earlier, or one greater than that shift: 1128 uint32_t const fractional_exponent = fractional_numerator < fractional_denominator 1129 ? fractional_shift + 1 1130 : fractional_shift; 1131 1132 shift_left(fractional_numerator, remaining_bits_of_precision_required); 1133 uint64_t fractional_mantissa = divide(fractional_numerator, fractional_denominator); 1134 1135 bool has_zero_tail = fractional_numerator._used == 0; 1136 1137 // We may have produced more bits of precision than were required. Check, 1138 // and remove any "extra" bits: 1139 uint32_t const fractional_mantissa_bits = bit_scan_reverse(fractional_mantissa); 1140 if (fractional_mantissa_bits > required_fractional_bits_of_precision) 1141 { 1142 uint32_t const shift = fractional_mantissa_bits - required_fractional_bits_of_precision; 1143 has_zero_tail = has_zero_tail && (fractional_mantissa & ((1ull << shift) - 1)) == 0; 1144 fractional_mantissa >>= shift; 1145 } 1146 1147 // Compose the mantissa from the integer and fractional parts: 1148 uint32_t const integer_mantissa_low = integer_value._used > 0 ? integer_value._data[0] : 0; 1149 uint32_t const integer_mantissa_high = integer_value._used > 1 ? integer_value._data[1] : 0; 1150 uint64_t const integer_mantissa = 1151 integer_mantissa_low + 1152 (static_cast<uint64_t>(integer_mantissa_high) << 32); 1153 1154 uint64_t const complete_mantissa = 1155 (integer_mantissa << required_fractional_bits_of_precision) + 1156 fractional_mantissa; 1157 1158 // Compute the final exponent: 1159 // * If the mantissa had an integer part, then the exponent is one less than 1160 // the number of bits we obtained from the integer part. (It's one less 1161 // because we are converting to the form 1.11111, with one 1 to the left 1162 // of the decimal point.) 1163 // * If the mantissa had no integer part, then the exponent is the fractional 1164 // exponent that we computed. 1165 // Then, in both cases, we subtract an additional one from the exponent, to 1166 // account for the fact that we've generated an extra bit of precision, for 1167 // use in rounding. 1168 int32_t const final_exponent = integer_bits_of_precision > 0 1169 ? integer_bits_of_precision - 2 1170 : -static_cast<int32_t>(fractional_exponent) - 1; 1171 1172 return assemble_floating_point_value(complete_mantissa, final_exponent, data._is_negative, has_zero_tail, result); 1173 } 1174 1175 template <typename FloatingType> 1176 SLD_STATUS __cdecl convert_decimal_string_to_floating_type( 1177 floating_point_string const& data, 1178 FloatingType & result 1179 ) throw() 1180 { 1181 return convert_decimal_string_to_floating_type_common(data, floating_point_value(&result)); 1182 } 1183 1184 inline SLD_STATUS __cdecl convert_hexadecimal_string_to_floating_type_common( 1185 floating_point_string const& data, 1186 floating_point_value const& result 1187 ) throw() 1188 { 1189 uint64_t mantissa = 0; 1190 int32_t exponent = data._exponent + result.mantissa_bits() - 1; 1191 1192 // Accumulate bits into the mantissa buffer 1193 uint8_t const* const mantissa_last = data._mantissa + data._mantissa_count; 1194 uint8_t const* mantissa_it = data._mantissa; 1195 while (mantissa_it != mantissa_last && mantissa <= result.normal_mantissa_mask()) 1196 { 1197 mantissa *= 16; 1198 mantissa += *mantissa_it++; 1199 exponent -= 4; // The exponent is in binary; log2(16) == 4 1200 } 1201 1202 bool has_zero_tail = true; 1203 while (mantissa_it != mantissa_last && has_zero_tail) 1204 { 1205 has_zero_tail = has_zero_tail && *mantissa_it++ == 0; 1206 } 1207 1208 return assemble_floating_point_value(mantissa, exponent, data._is_negative, has_zero_tail, result); 1209 } 1210 1211 template <typename FloatingType> 1212 SLD_STATUS __cdecl convert_hexadecimal_string_to_floating_type( 1213 floating_point_string const& data, 1214 FloatingType & result 1215 ) throw() 1216 { 1217 return convert_hexadecimal_string_to_floating_type_common(data, floating_point_value(&result)); 1218 } 1219 1220 // The high precision conversion algorithm defined above supports only float and 1221 // double. It does not support the 10- and 12-byte extended precision types. 1222 // These types are supported only for legacy reasons, so we use the old, low- 1223 // precision algorithm for these types, and do so by overloading the main 1224 // conversion and assembly functions for _LDBL12. 1225 void __cdecl assemble_floating_point_zero (bool is_negative, _LDBL12& result) throw(); 1226 void __cdecl assemble_floating_point_infinity(bool is_negative, _LDBL12& result) throw(); 1227 void __cdecl assemble_floating_point_qnan (bool is_negative, _LDBL12& result) throw(); 1228 void __cdecl assemble_floating_point_snan (bool is_negative, _LDBL12& result) throw(); 1229 void __cdecl assemble_floating_point_ind (_LDBL12& result) throw(); 1230 1231 SLD_STATUS __cdecl convert_decimal_string_to_floating_type( 1232 floating_point_string const& data, 1233 _LDBL12 & result 1234 ) throw(); 1235 1236 SLD_STATUS __cdecl convert_hexadecimal_string_to_floating_type( 1237 floating_point_string const& data, 1238 _LDBL12 & result 1239 ) throw(); 1240 1241 template <typename Character, typename CharacterSource> 1242 bool __cdecl parse_next_characters_from_source( 1243 Character const* const uppercase, 1244 Character const* const lowercase, 1245 size_t const count, 1246 Character& c, 1247 CharacterSource& source 1248 ) throw() 1249 { 1250 for (size_t i = 0; i != count; ++i) 1251 { 1252 if (c != uppercase[i] && c != lowercase[i]) 1253 { 1254 return false; 1255 } 1256 1257 c = source.get(); 1258 } 1259 1260 return true; 1261 } 1262 1263 template <typename Character, typename CharacterSource> 1264 bool __cdecl parse_floating_point_possible_nan_is_snan( 1265 Character& c, 1266 CharacterSource& source 1267 ) throw() 1268 { 1269 static Character const uppercase[] = { 'S', 'N', 'A', 'N', ')' }; 1270 static Character const lowercase[] = { 's', 'n', 'a', 'n', ')' }; 1271 return parse_next_characters_from_source(uppercase, lowercase, _countof(uppercase), c, source); 1272 } 1273 1274 template <typename Character, typename CharacterSource> 1275 bool __cdecl parse_floating_point_possible_nan_is_ind( 1276 Character& c, 1277 CharacterSource& source 1278 ) throw() 1279 { 1280 static Character const uppercase[] = { 'I', 'N', 'D', ')' }; 1281 static Character const lowercase[] = { 'i', 'n', 'd', ')' }; 1282 return parse_next_characters_from_source(uppercase, lowercase, _countof(uppercase), c, source); 1283 } 1284 1285 template <typename Character, typename CharacterSource, typename StoredState> 1286 floating_point_parse_result __cdecl parse_floating_point_possible_infinity( 1287 Character& c, 1288 CharacterSource& source, 1289 StoredState stored_state 1290 ) throw() 1291 { 1292 using char_type = typename CharacterSource::char_type; 1293 1294 auto restore_state = [&]() 1295 { 1296 source.unget(c); 1297 c = '\0'; 1298 return source.restore_state(stored_state); 1299 }; 1300 1301 static char_type const inf_uppercase[] = { 'I', 'N', 'F' }; 1302 static char_type const inf_lowercase[] = { 'i', 'n', 'f' }; 1303 if (!parse_next_characters_from_source(inf_uppercase, inf_lowercase, _countof(inf_uppercase), c, source)) 1304 { 1305 return restore_state(), floating_point_parse_result::no_digits; 1306 } 1307 1308 source.unget(c); 1309 stored_state = source.save_state(); 1310 c = source.get(); 1311 1312 static char_type const inity_uppercase[] = { 'I', 'N', 'I', 'T', 'Y' }; 1313 static char_type const inity_lowercase[] = { 'i', 'n', 'i', 't', 'y' }; 1314 if (!parse_next_characters_from_source(inity_uppercase, inity_lowercase, _countof(inity_uppercase), c, source)) 1315 { 1316 return restore_state() 1317 ? floating_point_parse_result::infinity 1318 : floating_point_parse_result::no_digits; 1319 } 1320 1321 source.unget(c); 1322 return floating_point_parse_result::infinity; 1323 } 1324 1325 template <typename Character, typename CharacterSource, typename StoredState> 1326 floating_point_parse_result __cdecl parse_floating_point_possible_nan( 1327 Character& c, 1328 CharacterSource& source, 1329 StoredState stored_state 1330 ) throw() 1331 { 1332 using char_type = typename CharacterSource::char_type; 1333 1334 auto restore_state = [&]() 1335 { 1336 source.unget(c); 1337 c = '\0'; 1338 return source.restore_state(stored_state); 1339 }; 1340 1341 static char_type const uppercase[] = { 'N', 'A', 'N' }; 1342 static char_type const lowercase[] = { 'n', 'a', 'n' }; 1343 if (!parse_next_characters_from_source(uppercase, lowercase, _countof(uppercase), c, source)) 1344 { 1345 return restore_state(), floating_point_parse_result::no_digits; 1346 } 1347 1348 source.unget(c); 1349 stored_state = source.save_state(); 1350 c = source.get(); 1351 1352 if (c != '(') 1353 { 1354 return restore_state() 1355 ? floating_point_parse_result::qnan 1356 : floating_point_parse_result::no_digits; 1357 } 1358 1359 c = source.get(); // Advance past the left parenthesis 1360 1361 // After we've parsed a left parenthesis, test to see whether the parenthesized 1362 // string represents a signaling NaN "(SNAN)" or an indeterminate "(IND)". If 1363 // so, we return the corresponding kind of NaN: 1364 if (parse_floating_point_possible_nan_is_snan(c, source)) 1365 { 1366 source.unget(c); 1367 return floating_point_parse_result::snan; 1368 } 1369 1370 if (parse_floating_point_possible_nan_is_ind(c, source)) 1371 { 1372 source.unget(c); 1373 return floating_point_parse_result::indeterminate; 1374 } 1375 1376 // Otherwise, we didn't match one of the two special parenthesized strings. 1377 // Keep eating chracters until we come across the right parenthesis or the 1378 // end of the character sequence: 1379 while (c != ')' && c != '\0') 1380 { 1381 if (!is_digit_or_nondigit(c)) 1382 { 1383 return restore_state() 1384 ? floating_point_parse_result::qnan 1385 : floating_point_parse_result::no_digits; 1386 } 1387 1388 c = source.get(); 1389 } 1390 1391 if (c != ')') 1392 { 1393 return restore_state() 1394 ? floating_point_parse_result::qnan 1395 : floating_point_parse_result::no_digits; 1396 } 1397 1398 return floating_point_parse_result::qnan; 1399 } 1400 1401 template <typename CharacterSource> 1402 floating_point_parse_result __cdecl parse_floating_point_from_source( 1403 _locale_t const locale, 1404 CharacterSource & source, 1405 floating_point_string & fp_string 1406 ) throw() 1407 { 1408 using char_type = typename CharacterSource::char_type; 1409 1410 if (!source.validate()) 1411 { 1412 return floating_point_parse_result::no_digits; 1413 } 1414 1415 auto stored_state = source.save_state(); 1416 char_type c{source.get()}; 1417 1418 auto restore_state = [&]() 1419 { 1420 source.unget(c); 1421 c = '\0'; 1422 return source.restore_state(stored_state); 1423 }; 1424 1425 // Skip past any leading whitespace: 1426 while (is_space(c, locale)) 1427 { 1428 c = source.get(); 1429 } 1430 1431 // Check for the optional plus or minus sign: 1432 fp_string._is_negative = c == '-'; 1433 if (c == '-' || c == '+') 1434 { 1435 c = source.get(); 1436 } 1437 1438 // Handle special cases "INF" and "INFINITY" (these are the only accepted 1439 // character sequences that start with 'I'): 1440 if (c == 'I' || c == 'i') 1441 { 1442 return parse_floating_point_possible_infinity(c, source, stored_state); 1443 } 1444 1445 // Handle special cases "NAN" and "NAN(...)" (these are the only accepted 1446 // character sequences that start with 'N'): 1447 if (c == 'N' || c == 'n') 1448 { 1449 return parse_floating_point_possible_nan(c, source, stored_state); 1450 } 1451 1452 // Check for optional "0x" or "0X" hexadecimal base prefix: 1453 bool is_hexadecimal{false}; 1454 if (c == '0') 1455 { 1456 auto const next_stored_state = source.save_state(); 1457 1458 char_type const next_c{source.get()}; 1459 if (next_c == 'x' || next_c == 'X') 1460 { 1461 is_hexadecimal = true; 1462 c = source.get(); 1463 1464 // If we match the hexadecimal base prefix we update the state to 1465 // reflect that we consumed the leading zero to handle the case 1466 // where a valid mantissa does not follow the base prefix. In this 1467 // case, the "0x" string is treated as a decimal zero subject ("0") 1468 // followed by a final string starting with the "x". 1469 stored_state = next_stored_state; 1470 } 1471 else 1472 { 1473 source.unget(next_c); 1474 } 1475 } 1476 1477 uint8_t* mantissa_first{fp_string._mantissa}; 1478 uint8_t* const mantissa_last {fp_string._mantissa + _countof(fp_string._mantissa)}; 1479 uint8_t* mantissa_it {fp_string._mantissa}; 1480 1481 // The exponent adjustment holds the number of digits in the mantissa buffer 1482 // that appeared before the radix point. It is positive for number strings 1483 // with an integer component and negative for number strings without. 1484 int exponent_adjustment{0}; 1485 1486 // Track whether we've seen any digits, so we know whether we've successfully 1487 // parsed something: 1488 bool found_digits{false}; 1489 1490 // Skip past any leading zeroes in the mantissa: 1491 while (c == '0') 1492 { 1493 found_digits = true; 1494 c = source.get(); 1495 } 1496 1497 // Scan the integer part of the mantissa: 1498 for (; ; c = source.get()) 1499 { 1500 unsigned const max_digit_value{is_hexadecimal ? 0xfu : 9u}; 1501 1502 unsigned const digit_value{parse_digit(c)}; 1503 if (digit_value > max_digit_value) 1504 { 1505 break; 1506 } 1507 1508 found_digits = true; 1509 if (mantissa_it != mantissa_last) 1510 { 1511 *mantissa_it++ = static_cast<uint8_t>(digit_value); 1512 } 1513 1514 ++exponent_adjustment; 1515 } 1516 1517 // If a radix point is present, scan the fractional part of the mantissa: 1518 char const radix_point{*locale->locinfo->lconv->decimal_point}; 1519 if (c == radix_point) 1520 { 1521 c = source.get(); 1522 1523 // If we haven't yet scanned any nonzero digits, continue skipping over 1524 // zeroes, updating the exponent adjustment to account for the zeroes 1525 // we are skipping: 1526 if (mantissa_it == mantissa_first) 1527 { 1528 while (c == '0') 1529 { 1530 found_digits = true; 1531 --exponent_adjustment; 1532 c = source.get(); 1533 } 1534 } 1535 1536 for (; ; c = source.get()) 1537 { 1538 unsigned const max_digit_value{is_hexadecimal ? 0xfu : 9u}; 1539 1540 unsigned const digit_value{parse_digit(c)}; 1541 if (digit_value > max_digit_value) 1542 break; 1543 1544 found_digits = true; 1545 if (mantissa_it != mantissa_last) 1546 { 1547 *mantissa_it++ = static_cast<uint8_t>(digit_value); 1548 } 1549 } 1550 } 1551 1552 if (!found_digits) 1553 { 1554 // We failed to parse any digits, so attempt to restore back to the last 1555 // good terminal state. This may fail if we are reading from a stream, 1556 // we read a hexadecimal base prefix ("0x"), but we did not find any digits 1557 // following the base prefix. 1558 if (!restore_state()) 1559 { 1560 return floating_point_parse_result::no_digits; 1561 } 1562 1563 // If a hexadecimal base prefix was present ("0x"), then the string is a 1564 // valid input: the "0" is the subject sequence and the "x" is the first 1565 // character of the final string. Otherwise, the string is not a valid 1566 // input. 1567 if (is_hexadecimal) 1568 { 1569 return floating_point_parse_result::zero; 1570 } 1571 else 1572 { 1573 return floating_point_parse_result::no_digits; 1574 } 1575 } 1576 1577 source.unget(c); 1578 stored_state = source.save_state(); 1579 c = source.get(); 1580 1581 // Check for the optional 'e' or 'p' exponent introducer: 1582 bool has_exponent{false}; 1583 switch (c) 1584 { 1585 case 'e': 1586 case 'E': 1587 has_exponent = !is_hexadecimal; 1588 break; 1589 1590 case 'p': 1591 case 'P': 1592 has_exponent = is_hexadecimal; 1593 break; 1594 } 1595 1596 // If there was an exponent introducer, scan the exponent: 1597 int exponent{0}; 1598 if (has_exponent) 1599 { 1600 c = source.get(); // Skip past exponent introducer character 1601 1602 // Check for the optional plus or minus sign: 1603 bool const exponent_is_negative{c == '-'}; 1604 if (c == '+' || c == '-') 1605 { 1606 c = source.get(); 1607 } 1608 1609 bool has_exponent_digits{false}; 1610 1611 while (c == '0') 1612 { 1613 has_exponent_digits = true; 1614 c = source.get(); 1615 } 1616 1617 for (; ; c = source.get()) 1618 { 1619 unsigned const digit_value{parse_digit(c)}; 1620 if (digit_value >= 10) 1621 { 1622 break; 1623 } 1624 1625 has_exponent_digits = true; 1626 exponent = exponent * 10 + digit_value; 1627 if (exponent > maximum_temporary_decimal_exponent) 1628 { 1629 exponent = maximum_temporary_decimal_exponent + 1; 1630 break; 1631 } 1632 } 1633 1634 // If the exponent is too large, skip over the remaining exponent digits 1635 // so we can correctly update the end pointer: 1636 while (parse_digit(c) < 10) 1637 { 1638 c = source.get(); 1639 } 1640 1641 if (exponent_is_negative) 1642 { 1643 exponent = -exponent; 1644 } 1645 1646 // If no exponent digits were present, attempt to restore the last good 1647 // terminal state. 1648 if (!has_exponent_digits) 1649 { 1650 if (restore_state()) 1651 { 1652 // The call to restore_state will have ungotten the exponent 1653 // introducer. Re-get this character, to restore us to the 1654 // state we had before we entered the exponent parsing block. 1655 c = source.get(); 1656 } 1657 else 1658 { 1659 return floating_point_parse_result::no_digits; 1660 } 1661 } 1662 } 1663 1664 // Unget the last character that we read that terminated input. After this 1665 // point, we must not use the source, c, or stored_state. 1666 source.unget(c); 1667 1668 // Remove trailing zeroes from mantissa: 1669 while (mantissa_it != mantissa_first && *(mantissa_it - 1) == 0) 1670 { 1671 --mantissa_it; 1672 } 1673 1674 // If the mantissa buffer is empty, the mantissa was composed of all zeroes 1675 // (so the mantissa is 0). All such strings have the value zero, regardless 1676 // what the exponent is (because 0 x b^n == 0 for all b and n). We can return 1677 // now. Note that we defer this check until after we scan the exponent, so 1678 // that we can correctly update end_ptr to point past the end of the exponent. 1679 if (mantissa_it == mantissa_first) 1680 { 1681 return floating_point_parse_result::zero; 1682 } 1683 1684 // Before we adjust the exponent, handle the case where we detected a wildly 1685 // out of range exponent during parsing and clamped the value: 1686 if (exponent > maximum_temporary_decimal_exponent) 1687 { 1688 return floating_point_parse_result::overflow; 1689 } 1690 1691 if (exponent < minimum_temporary_decimal_exponent) 1692 { 1693 return floating_point_parse_result::underflow; 1694 } 1695 1696 // In hexadecimal floating constants, the exponent is a base 2 exponent. The 1697 // exponent adjustment computed during parsing has the same base as the 1698 // mantissa (so, 16 for hexadecimal floating constants). We therefore need to 1699 // scale the base 16 multiplier to base 2 by multiplying by log2(16): 1700 int const exponent_adjustment_multiplier{is_hexadecimal ? 4 : 1}; 1701 1702 exponent += exponent_adjustment * exponent_adjustment_multiplier; 1703 1704 // Verify that after adjustment the exponent isn't wildly out of range (if 1705 // it is, it isn't representable in any supported floating point format). 1706 if (exponent > maximum_temporary_decimal_exponent) 1707 { 1708 return floating_point_parse_result::overflow; 1709 } 1710 1711 if (exponent < minimum_temporary_decimal_exponent) 1712 { 1713 return floating_point_parse_result::underflow; 1714 } 1715 1716 fp_string._exponent = exponent; 1717 fp_string._mantissa_count = static_cast<uint32_t>(mantissa_it - mantissa_first); 1718 1719 return is_hexadecimal 1720 ? floating_point_parse_result::hexadecimal_digits 1721 : floating_point_parse_result::decimal_digits; 1722 } 1723 1724 template <typename FloatingType> 1725 SLD_STATUS __cdecl parse_floating_point_write_result( 1726 floating_point_parse_result const parse_result, 1727 floating_point_string const& fp_string, 1728 FloatingType* const result 1729 ) throw() 1730 { 1731 switch (parse_result) 1732 { 1733 case floating_point_parse_result::decimal_digits: return convert_decimal_string_to_floating_type (fp_string, *result); 1734 case floating_point_parse_result::hexadecimal_digits: return convert_hexadecimal_string_to_floating_type(fp_string, *result); 1735 1736 case floating_point_parse_result::zero: assemble_floating_point_zero (fp_string._is_negative, *result); return SLD_OK; 1737 case floating_point_parse_result::infinity: assemble_floating_point_infinity(fp_string._is_negative, *result); return SLD_OK; 1738 case floating_point_parse_result::qnan: assemble_floating_point_qnan (fp_string._is_negative, *result); return SLD_OK; 1739 case floating_point_parse_result::snan: assemble_floating_point_snan (fp_string._is_negative, *result); return SLD_OK; 1740 case floating_point_parse_result::indeterminate: assemble_floating_point_ind ( *result); return SLD_OK; 1741 1742 case floating_point_parse_result::no_digits: assemble_floating_point_zero (false, *result); return SLD_NODIGITS; 1743 case floating_point_parse_result::underflow: assemble_floating_point_zero (fp_string._is_negative, *result); return SLD_UNDERFLOW; 1744 case floating_point_parse_result::overflow: assemble_floating_point_infinity(fp_string._is_negative, *result); return SLD_OVERFLOW; 1745 1746 default: 1747 // Unreachable 1748 _ASSERTE(false); 1749 return SLD_NODIGITS; 1750 } 1751 } 1752 1753 template <typename CharacterSource, typename FloatingType> 1754 SLD_STATUS __cdecl parse_floating_point( 1755 _locale_t const locale, 1756 CharacterSource source, 1757 FloatingType* const result 1758 ) throw() 1759 { 1760 using char_type = typename CharacterSource::char_type; 1761 1762 _VALIDATE_RETURN(result != nullptr, EINVAL, SLD_NODIGITS); 1763 _VALIDATE_RETURN(locale != nullptr, EINVAL, SLD_NODIGITS); 1764 1765 // PERFORMANCE NOTE: fp_string is intentionally left uninitialized. Zero- 1766 // initialization is quite expensive and is unnecessary. The benefit of not 1767 // zero-initializing is greatest for short inputs. 1768 floating_point_string fp_string; 1769 1770 floating_point_parse_result const parse_result = parse_floating_point_from_source(locale, source, fp_string); 1771 1772 return parse_floating_point_write_result(parse_result, fp_string, result); 1773 } 1774 1775 } // namespace __crt_strtox 1776 1777 1778 1779 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 1780 // 1781 // Character Sources 1782 // 1783 //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 1784 namespace __crt_strtox { 1785 1786 template <typename Character> 1787 class c_string_character_source 1788 { 1789 public: 1790 1791 typedef Character char_type; 1792 1793 c_string_character_source( 1794 Character const* const string, 1795 Character const** const end 1796 ) throw() 1797 : _p{string}, _end{end} 1798 { 1799 if (end) 1800 { 1801 *end = string; 1802 } 1803 } 1804 1805 c_string_character_source(c_string_character_source&& other) throw() 1806 : _p{other._p}, _end{other._end} 1807 { 1808 other._p = nullptr; 1809 other._end = nullptr; 1810 } 1811 1812 c_string_character_source& operator=(c_string_character_source&& other) throw() 1813 { 1814 _p = other._p; 1815 _end = other._end; 1816 1817 other._p = nullptr; 1818 other._end = nullptr; 1819 return *this; 1820 } 1821 1822 ~c_string_character_source() 1823 { 1824 if (_end) 1825 { 1826 *_end = _p; 1827 } 1828 } 1829 1830 bool validate() const throw() 1831 { 1832 _VALIDATE_RETURN(_p != nullptr, EINVAL, false); 1833 return true; 1834 } 1835 1836 Character get() throw() 1837 { 1838 return *_p++; 1839 } 1840 1841 void unget(Character const c) throw() 1842 { 1843 --_p; 1844 _VALIDATE_RETURN_VOID(c == '\0' || *_p == c, EINVAL); 1845 } 1846 1847 Character const* save_state() const throw() 1848 { 1849 return _p; 1850 } 1851 1852 bool restore_state(Character const* const state) throw() 1853 { 1854 _p = state; 1855 return true; 1856 } 1857 1858 private: 1859 1860 c_string_character_source(c_string_character_source const&) = delete; 1861 c_string_character_source& operator=(c_string_character_source const&) = delete; 1862 1863 Character const* _p; 1864 Character const** _end; 1865 }; 1866 1867 template <typename Character, typename EndPointer> 1868 c_string_character_source<Character> __cdecl make_c_string_character_source( 1869 Character const* const string, 1870 EndPointer const end 1871 ) throw() 1872 { 1873 return c_string_character_source<Character>(string, (Character const**)(end)); 1874 } 1875 1876 template <typename Integer, typename Character, typename EndPointer> 1877 __forceinline Integer __cdecl parse_integer_from_string( 1878 Character const* const string, 1879 EndPointer const end, 1880 int const base, 1881 _locale_t const locale 1882 ) throw() 1883 { 1884 __crt_cached_ptd_host ptd{locale}; 1885 1886 return static_cast<Integer>(parse_integer<typename make_unsigned<Integer>::type>( 1887 ptd, 1888 make_c_string_character_source(string, end), 1889 base, 1890 is_signed<Integer>::value)); 1891 } 1892 1893 template <typename InputAdapter> 1894 class input_adapter_character_source 1895 { 1896 public: 1897 1898 typedef typename InputAdapter::char_type char_type; 1899 typedef __acrt_stdio_char_traits<char_type> traits; 1900 1901 input_adapter_character_source( 1902 InputAdapter* const input_adapter, 1903 uint64_t const width, 1904 bool* const succeeded 1905 ) throw() 1906 : _input_adapter{input_adapter}, 1907 _max_get_count{width }, 1908 _get_count {0 }, 1909 _succeeded {succeeded } 1910 { 1911 if (succeeded) 1912 *succeeded = true; 1913 } 1914 1915 input_adapter_character_source(input_adapter_character_source&& other) throw() 1916 : _input_adapter{other._input_adapter}, 1917 _max_get_count{other._max_get_count}, 1918 _get_count {other._get_count }, 1919 _succeeded {other._succeeded } 1920 { 1921 other._input_adapter = nullptr; 1922 other._succeeded = nullptr; 1923 } 1924 1925 input_adapter_character_source& operator=(input_adapter_character_source&& other) throw() 1926 { 1927 _input_adapter = other._input_adapter; 1928 _max_get_count = other._max_get_count; 1929 _get_count = other._get_count; 1930 _succeeded = other._succeeded; 1931 1932 other._input_adapter = nullptr; 1933 other._succeeded = nullptr; 1934 return *this; 1935 } 1936 1937 ~input_adapter_character_source() 1938 { 1939 // If no characters were consumed, report as a failure. This occurs if 1940 // a matching failure occurs on the first character (if it occurs on a 1941 // subsequent character, then the attempt to restore state will have 1942 // failed and will set the failure flag). 1943 if (_succeeded != nullptr && _get_count == 0) 1944 { 1945 *_succeeded = false; 1946 } 1947 } 1948 1949 bool validate() const throw() 1950 { 1951 _VALIDATE_RETURN(_input_adapter != nullptr, EINVAL, false); 1952 _VALIDATE_RETURN(_succeeded != nullptr, EINVAL, false); 1953 return true; 1954 } 1955 1956 char_type get() throw() 1957 { 1958 ++_get_count; 1959 1960 if (_max_get_count != 0 && _get_count > _max_get_count) 1961 return '\0'; 1962 1963 auto c = _input_adapter->get(); 1964 if (c == traits::eof) 1965 return '\0'; 1966 1967 return static_cast<char_type>(c); 1968 } 1969 1970 void unget(char_type const c) throw() 1971 { 1972 --_get_count; 1973 1974 if (_max_get_count != 0 && _get_count > _max_get_count) 1975 return; 1976 1977 if (c == '\0' || c == traits::eof) 1978 return; 1979 1980 _input_adapter->unget(c); 1981 } 1982 1983 uint64_t save_state() const throw() 1984 { 1985 return _get_count; 1986 } 1987 1988 bool restore_state(uint64_t const get_count) throw() 1989 { 1990 if (get_count != _get_count) 1991 { 1992 *_succeeded = false; 1993 return false; 1994 } 1995 1996 return true; 1997 } 1998 1999 private: 2000 2001 input_adapter_character_source(input_adapter_character_source const&) = delete; 2002 input_adapter_character_source& operator=(input_adapter_character_source const&) = delete; 2003 2004 InputAdapter* _input_adapter; 2005 uint64_t _max_get_count; 2006 uint64_t _get_count; 2007 bool* _succeeded; 2008 }; 2009 2010 template <typename InputAdapter> 2011 input_adapter_character_source<InputAdapter> __cdecl make_input_adapter_character_source( 2012 InputAdapter* const input_adapter, 2013 uint64_t const width, 2014 bool* const succeeded 2015 ) throw() 2016 { 2017 return input_adapter_character_source<InputAdapter>{input_adapter, width, succeeded}; 2018 } 2019 2020 } // namespace __crt_strtox 2021 2022 // Internal-only variations of the above functions 2023 2024 // Note this is different from a usual tcstol call in that whitespace is not 2025 // trimmed in order to avoid acquiring the global locale for code paths that 2026 // do not require that functionality. 2027 template <typename Character, typename EndPointer, bool TrimWhitespace = false> 2028 __forceinline long __cdecl _tcstol_internal( 2029 __crt_cached_ptd_host& ptd, 2030 Character const* const string, 2031 EndPointer const end, 2032 int const base 2033 ) throw() 2034 { 2035 return static_cast<long>(__crt_strtox::parse_integer<unsigned long, __crt_strtox::c_string_character_source<Character>, TrimWhitespace>( 2036 ptd, 2037 __crt_strtox::make_c_string_character_source(string, end), 2038 base, 2039 true // long is signed 2040 )); 2041 } 2042 2043 #pragma pop_macro("_INVALID_PARAMETER") 2044 #pragma pop_macro("_VALIDATE_RETURN") 2045 #pragma pop_macro("_VALIDATE_RETURN_VOID") 2046 2047