1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_CHARCONV 11#define _LIBCPP_CHARCONV 12 13/* 14 charconv synopsis 15 16namespace std { 17 18 // floating-point format for primitive numerical conversion 19 enum class chars_format { 20 scientific = unspecified, 21 fixed = unspecified, 22 hex = unspecified, 23 general = fixed | scientific 24 }; 25 26 // 23.20.2, primitive numerical output conversion 27 struct to_chars_result { 28 char* ptr; 29 errc ec; 30 friend bool operator==(const to_chars_result&, const to_chars_result&) = default; // since C++20 31 }; 32 33 to_chars_result to_chars(char* first, char* last, see below value, 34 int base = 10); 35 to_chars_result to_chars(char* first, char* last, bool value, 36 int base = 10) = delete; 37 38 to_chars_result to_chars(char* first, char* last, float value); 39 to_chars_result to_chars(char* first, char* last, double value); 40 to_chars_result to_chars(char* first, char* last, long double value); 41 42 to_chars_result to_chars(char* first, char* last, float value, 43 chars_format fmt); 44 to_chars_result to_chars(char* first, char* last, double value, 45 chars_format fmt); 46 to_chars_result to_chars(char* first, char* last, long double value, 47 chars_format fmt); 48 49 to_chars_result to_chars(char* first, char* last, float value, 50 chars_format fmt, int precision); 51 to_chars_result to_chars(char* first, char* last, double value, 52 chars_format fmt, int precision); 53 to_chars_result to_chars(char* first, char* last, long double value, 54 chars_format fmt, int precision); 55 56 // 23.20.3, primitive numerical input conversion 57 struct from_chars_result { 58 const char* ptr; 59 errc ec; 60 friend bool operator==(const from_chars_result&, const from_chars_result&) = default; // since C++20 61 }; 62 63 from_chars_result from_chars(const char* first, const char* last, 64 see below& value, int base = 10); 65 66 from_chars_result from_chars(const char* first, const char* last, 67 float& value, 68 chars_format fmt = chars_format::general); 69 from_chars_result from_chars(const char* first, const char* last, 70 double& value, 71 chars_format fmt = chars_format::general); 72 from_chars_result from_chars(const char* first, const char* last, 73 long double& value, 74 chars_format fmt = chars_format::general); 75 76} // namespace std 77 78*/ 79 80#include <__assert> // all public C++ headers provide the assertion handler 81#include <__availability> 82#include <__bits> 83#include <__charconv/chars_format.h> 84#include <__charconv/from_chars_result.h> 85#include <__charconv/tables.h> 86#include <__charconv/to_chars_base_10.h> 87#include <__charconv/to_chars_result.h> 88#include <__config> 89#include <__debug> 90#include <__errc> 91#include <__type_traits/make_32_64_or_128_bit.h> 92#include <__utility/unreachable.h> 93#include <cmath> // for log2f 94#include <cstdint> 95#include <cstdlib> 96#include <cstring> 97#include <limits> 98#include <type_traits> 99 100#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES 101# include <iosfwd> 102#endif 103 104#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 105# pragma GCC system_header 106#endif 107 108_LIBCPP_PUSH_MACROS 109#include <__undef_macros> 110 111_LIBCPP_BEGIN_NAMESPACE_STD 112 113#ifndef _LIBCPP_CXX03_LANG 114 115to_chars_result to_chars(char*, char*, bool, int = 10) = delete; 116from_chars_result from_chars(const char*, const char*, bool, int = 10) = delete; 117 118namespace __itoa 119{ 120 121template <typename _Tp, typename = void> 122struct _LIBCPP_HIDDEN __traits_base; 123 124template <typename _Tp> 125struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) <= sizeof(uint32_t)>> 126{ 127 using type = uint32_t; 128 129 /// The width estimation using a log10 algorithm. 130 /// 131 /// The algorithm is based on 132 /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 133 /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that 134 /// function requires its input to have at least one bit set the value of 135 /// zero is set to one. This means the first element of the lookup table is 136 /// zero. 137 static _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) 138 { 139 auto __t = (32 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12; 140 return __t - (__v < __table<>::__pow10_32[__t]) + 1; 141 } 142 143 static _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) 144 { 145 return __itoa::__base_10_u32(__p, __v); 146 } 147 148 static _LIBCPP_HIDE_FROM_ABI decltype(__table<>::__pow10_32)& __pow() { return __table<>::__pow10_32; } 149}; 150 151template <typename _Tp> 152struct _LIBCPP_HIDDEN 153 __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(uint64_t)>> { 154 using type = uint64_t; 155 156 /// The width estimation using a log10 algorithm. 157 /// 158 /// The algorithm is based on 159 /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 160 /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that 161 /// function requires its input to have at least one bit set the value of 162 /// zero is set to one. This means the first element of the lookup table is 163 /// zero. 164 static _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) { 165 auto __t = (64 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12; 166 return __t - (__v < __table<>::__pow10_64[__t]) + 1; 167 } 168 169 static _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u64(__p, __v); } 170 171 static _LIBCPP_HIDE_FROM_ABI decltype(__table<>::__pow10_64)& __pow() { return __table<>::__pow10_64; } 172}; 173 174 175# ifndef _LIBCPP_HAS_NO_INT128 176template <typename _Tp> 177struct _LIBCPP_HIDDEN 178 __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(__uint128_t)> > { 179 using type = __uint128_t; 180 181 /// The width estimation using a log10 algorithm. 182 /// 183 /// The algorithm is based on 184 /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 185 /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that 186 /// function requires its input to have at least one bit set the value of 187 /// zero is set to one. This means the first element of the lookup table is 188 /// zero. 189 static _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) { 190 _LIBCPP_ASSERT(__v > numeric_limits<uint64_t>::max(), "The optimizations for this algorithm fail when this isn't true."); 191 // There's always a bit set in the upper 64-bits. 192 auto __t = (128 - std::__libcpp_clz(static_cast<uint64_t>(__v >> 64))) * 1233 >> 12; 193 _LIBCPP_ASSERT(__t >= __table<>::__pow10_128_offset, "Index out of bounds"); 194 // __t is adjusted since the lookup table misses the lower entries. 195 return __t - (__v < __table<>::__pow10_128[__t - __table<>::__pow10_128_offset]) + 1; 196 } 197 198 static _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u128(__p, __v); } 199 200 // TODO FMT This pow function should get an index. 201 // By moving this to its own header it can be reused by the pow function in to_chars_base_10. 202 static _LIBCPP_HIDE_FROM_ABI decltype(__table<>::__pow10_128)& __pow() { return __table<>::__pow10_128; } 203}; 204#endif 205 206template <typename _Tp> 207inline _LIBCPP_HIDE_FROM_ABI bool 208__mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r) 209{ 210 auto __c = __a * __b; 211 __r = __c; 212 return __c > numeric_limits<unsigned char>::max(); 213} 214 215template <typename _Tp> 216inline _LIBCPP_HIDE_FROM_ABI bool 217__mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r) 218{ 219 auto __c = __a * __b; 220 __r = __c; 221 return __c > numeric_limits<unsigned short>::max(); 222} 223 224template <typename _Tp> 225inline _LIBCPP_HIDE_FROM_ABI bool 226__mul_overflowed(_Tp __a, _Tp __b, _Tp& __r) 227{ 228 static_assert(is_unsigned<_Tp>::value, ""); 229#if !defined(_LIBCPP_COMPILER_MSVC) 230 return __builtin_mul_overflow(__a, __b, &__r); 231#else 232 bool __did = __b && (numeric_limits<_Tp>::max() / __b) < __a; 233 __r = __a * __b; 234 return __did; 235#endif 236} 237 238template <typename _Tp, typename _Up> 239inline _LIBCPP_HIDE_FROM_ABI bool 240__mul_overflowed(_Tp __a, _Up __b, _Tp& __r) 241{ 242 return __mul_overflowed(__a, static_cast<_Tp>(__b), __r); 243} 244 245template <typename _Tp> 246struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp> 247{ 248 static constexpr int digits = numeric_limits<_Tp>::digits10 + 1; 249 using __traits_base<_Tp>::__pow; 250 using typename __traits_base<_Tp>::type; 251 252 // precondition: at least one non-zero character available 253 static _LIBCPP_HIDE_FROM_ABI char const* 254 __read(char const* __p, char const* __ep, type& __a, type& __b) 255 { 256 type __cprod[digits]; 257 int __j = digits - 1; 258 int __i = digits; 259 do 260 { 261 if (!('0' <= *__p && *__p <= '9')) 262 break; 263 __cprod[--__i] = *__p++ - '0'; 264 } while (__p != __ep && __i != 0); 265 266 __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1, 267 __cprod[__i]); 268 if (__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b)) 269 --__p; 270 return __p; 271 } 272 273 template <typename _It1, typename _It2, class _Up> 274 static _LIBCPP_HIDE_FROM_ABI _Up 275 __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init) 276 { 277 for (; __first1 < __last1; ++__first1, ++__first2) 278 __init = __init + *__first1 * *__first2; 279 return __init; 280 } 281}; 282 283} // namespace __itoa 284 285template <typename _Tp> 286inline _LIBCPP_HIDE_FROM_ABI _Tp 287__complement(_Tp __x) 288{ 289 static_assert(is_unsigned<_Tp>::value, "cast to unsigned first"); 290 return _Tp(~__x + 1); 291} 292 293template <typename _Tp> 294inline _LIBCPP_HIDE_FROM_ABI to_chars_result 295__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type) 296{ 297 auto __x = __to_unsigned_like(__value); 298 if (__value < 0 && __first != __last) 299 { 300 *__first++ = '-'; 301 __x = __complement(__x); 302 } 303 304 return __to_chars_itoa(__first, __last, __x, false_type()); 305} 306 307template <typename _Tp> 308inline _LIBCPP_HIDE_FROM_ABI to_chars_result 309__to_chars_itoa(char* __first, char* __last, _Tp __value, false_type) 310{ 311 using __tx = __itoa::__traits<_Tp>; 312 auto __diff = __last - __first; 313 314 if (__tx::digits <= __diff || __tx::__width(__value) <= __diff) 315 return {__tx::__convert(__first, __value), errc(0)}; 316 else 317 return {__last, errc::value_too_large}; 318} 319 320# ifndef _LIBCPP_HAS_NO_INT128 321template <> 322inline _LIBCPP_HIDE_FROM_ABI to_chars_result 323__to_chars_itoa(char* __first, char* __last, __uint128_t __value, false_type) 324{ 325 // When the value fits in 64-bits use the 64-bit code path. This reduces 326 // the number of expensive calculations on 128-bit values. 327 // 328 // NOTE the 128-bit code path requires this optimization. 329 if(__value <= numeric_limits<uint64_t>::max()) 330 return __to_chars_itoa(__first, __last, static_cast<uint64_t>(__value), false_type()); 331 332 using __tx = __itoa::__traits<__uint128_t>; 333 auto __diff = __last - __first; 334 335 if (__tx::digits <= __diff || __tx::__width(__value) <= __diff) 336 return {__tx::__convert(__first, __value), errc(0)}; 337 else 338 return {__last, errc::value_too_large}; 339} 340#endif 341 342template <typename _Tp> 343inline _LIBCPP_HIDE_FROM_ABI to_chars_result 344__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, 345 true_type) 346{ 347 auto __x = __to_unsigned_like(__value); 348 if (__value < 0 && __first != __last) 349 { 350 *__first++ = '-'; 351 __x = __complement(__x); 352 } 353 354 return __to_chars_integral(__first, __last, __x, __base, false_type()); 355} 356 357namespace __itoa { 358 359template <unsigned _Base> 360struct _LIBCPP_HIDDEN __integral; 361 362template <> 363struct _LIBCPP_HIDDEN __integral<2> { 364 template <typename _Tp> 365 _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept { 366 // If value == 0 still need one digit. If the value != this has no 367 // effect since the code scans for the most significant bit set. (Note 368 // that __libcpp_clz doesn't work for 0.) 369 return numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1); 370 } 371 372 template <typename _Tp> 373 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) { 374 ptrdiff_t __cap = __last - __first; 375 int __n = __width(__value); 376 if (__n > __cap) 377 return {__last, errc::value_too_large}; 378 379 __last = __first + __n; 380 char* __p = __last; 381 const unsigned __divisor = 16; 382 while (__value > __divisor) { 383 unsigned __c = __value % __divisor; 384 __value /= __divisor; 385 __p -= 4; 386 std::memcpy(__p, &__table<>::__base_2_lut[4 * __c], 4); 387 } 388 do { 389 unsigned __c = __value % 2; 390 __value /= 2; 391 *--__p = "01"[__c]; 392 } while (__value != 0); 393 return {__last, errc(0)}; 394 } 395}; 396 397template <> 398struct _LIBCPP_HIDDEN __integral<8> { 399 template <typename _Tp> 400 _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept { 401 // If value == 0 still need one digit. If the value != this has no 402 // effect since the code scans for the most significat bit set. (Note 403 // that __libcpp_clz doesn't work for 0.) 404 return ((numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1)) + 2) / 3; 405 } 406 407 template <typename _Tp> 408 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) { 409 ptrdiff_t __cap = __last - __first; 410 int __n = __width(__value); 411 if (__n > __cap) 412 return {__last, errc::value_too_large}; 413 414 __last = __first + __n; 415 char* __p = __last; 416 unsigned __divisor = 64; 417 while (__value > __divisor) { 418 unsigned __c = __value % __divisor; 419 __value /= __divisor; 420 __p -= 2; 421 std::memcpy(__p, &__table<>::__base_8_lut[2 * __c], 2); 422 } 423 do { 424 unsigned __c = __value % 8; 425 __value /= 8; 426 *--__p = "01234567"[__c]; 427 } while (__value != 0); 428 return {__last, errc(0)}; 429 } 430 431}; 432 433template <> 434struct _LIBCPP_HIDDEN __integral<16> { 435 template <typename _Tp> 436 _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept { 437 // If value == 0 still need one digit. If the value != this has no 438 // effect since the code scans for the most significat bit set. (Note 439 // that __libcpp_clz doesn't work for 0.) 440 return (numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1) + 3) / 4; 441 } 442 443 template <typename _Tp> 444 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) { 445 ptrdiff_t __cap = __last - __first; 446 int __n = __width(__value); 447 if (__n > __cap) 448 return {__last, errc::value_too_large}; 449 450 __last = __first + __n; 451 char* __p = __last; 452 unsigned __divisor = 256; 453 while (__value > __divisor) { 454 unsigned __c = __value % __divisor; 455 __value /= __divisor; 456 __p -= 2; 457 std::memcpy(__p, &__table<>::__base_16_lut[2 * __c], 2); 458 } 459 if (__first != __last) 460 do { 461 unsigned __c = __value % 16; 462 __value /= 16; 463 *--__p = "0123456789abcdef"[__c]; 464 } while (__value != 0); 465 return {__last, errc(0)}; 466 } 467}; 468 469} // namespace __itoa 470 471template <unsigned _Base, typename _Tp, 472 typename enable_if<(sizeof(_Tp) >= sizeof(unsigned)), int>::type = 0> 473_LIBCPP_HIDE_FROM_ABI int 474__to_chars_integral_width(_Tp __value) { 475 return __itoa::__integral<_Base>::__width(__value); 476} 477 478template <unsigned _Base, typename _Tp, 479 typename enable_if<(sizeof(_Tp) < sizeof(unsigned)), int>::type = 0> 480_LIBCPP_HIDE_FROM_ABI int 481__to_chars_integral_width(_Tp __value) { 482 return std::__to_chars_integral_width<_Base>(static_cast<unsigned>(__value)); 483} 484 485template <unsigned _Base, typename _Tp, 486 typename enable_if<(sizeof(_Tp) >= sizeof(unsigned)), int>::type = 0> 487_LIBCPP_HIDE_FROM_ABI to_chars_result 488__to_chars_integral(char* __first, char* __last, _Tp __value) { 489 return __itoa::__integral<_Base>::__to_chars(__first, __last, __value); 490} 491 492template <unsigned _Base, typename _Tp, 493 typename enable_if<(sizeof(_Tp) < sizeof(unsigned)), int>::type = 0> 494_LIBCPP_HIDE_FROM_ABI to_chars_result 495__to_chars_integral(char* __first, char* __last, _Tp __value) { 496 return std::__to_chars_integral<_Base>(__first, __last, static_cast<unsigned>(__value)); 497} 498 499template <typename _Tp> 500_LIBCPP_HIDE_FROM_ABI int 501__to_chars_integral_width(_Tp __value, unsigned __base) { 502 _LIBCPP_ASSERT(__value >= 0, "The function requires a non-negative value."); 503 504 unsigned __base_2 = __base * __base; 505 unsigned __base_3 = __base_2 * __base; 506 unsigned __base_4 = __base_2 * __base_2; 507 508 int __r = 0; 509 while (true) { 510 if (__value < __base) 511 return __r + 1; 512 if (__value < __base_2) 513 return __r + 2; 514 if (__value < __base_3) 515 return __r + 3; 516 if (__value < __base_4) 517 return __r + 4; 518 519 __value /= __base_4; 520 __r += 4; 521 } 522 523 __libcpp_unreachable(); 524} 525 526template <typename _Tp> 527inline _LIBCPP_HIDE_FROM_ABI to_chars_result 528__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, 529 false_type) 530{ 531 if (__base == 10) [[likely]] 532 return __to_chars_itoa(__first, __last, __value, false_type()); 533 534 switch (__base) { 535 case 2: 536 return __to_chars_integral<2>(__first, __last, __value); 537 case 8: 538 return __to_chars_integral<8>(__first, __last, __value); 539 case 16: 540 return __to_chars_integral<16>(__first, __last, __value); 541 } 542 543 ptrdiff_t __cap = __last - __first; 544 int __n = __to_chars_integral_width(__value, __base); 545 if (__n > __cap) 546 return {__last, errc::value_too_large}; 547 548 __last = __first + __n; 549 char* __p = __last; 550 do { 551 unsigned __c = __value % __base; 552 __value /= __base; 553 *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c]; 554 } while (__value != 0); 555 return {__last, errc(0)}; 556} 557 558template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> 559inline _LIBCPP_HIDE_FROM_ABI to_chars_result 560to_chars(char* __first, char* __last, _Tp __value) 561{ 562 using _Type = __make_32_64_or_128_bit_t<_Tp>; 563 static_assert(!is_same<_Type, void>::value, "unsupported integral type used in to_chars"); 564 return std::__to_chars_itoa(__first, __last, static_cast<_Type>(__value), is_signed<_Tp>()); 565} 566 567template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> 568inline _LIBCPP_HIDE_FROM_ABI to_chars_result 569to_chars(char* __first, char* __last, _Tp __value, int __base) 570{ 571 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); 572 573 using _Type = __make_32_64_or_128_bit_t<_Tp>; 574 return std::__to_chars_integral(__first, __last, static_cast<_Type>(__value), __base, is_signed<_Tp>()); 575} 576 577template <typename _It, typename _Tp, typename _Fn, typename... _Ts> 578inline _LIBCPP_HIDE_FROM_ABI from_chars_result 579__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) 580{ 581 using __tl = numeric_limits<_Tp>; 582 decltype(__to_unsigned_like(__value)) __x; 583 584 bool __neg = (__first != __last && *__first == '-'); 585 auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...); 586 switch (__r.ec) 587 { 588 case errc::invalid_argument: 589 return {__first, __r.ec}; 590 case errc::result_out_of_range: 591 return __r; 592 default: 593 break; 594 } 595 596 if (__neg) 597 { 598 if (__x <= __complement(__to_unsigned_like(__tl::min()))) 599 { 600 __x = __complement(__x); 601 std::memcpy(&__value, &__x, sizeof(__x)); 602 return __r; 603 } 604 } 605 else 606 { 607 if (__x <= __to_unsigned_like(__tl::max())) 608 { 609 __value = __x; 610 return __r; 611 } 612 } 613 614 return {__r.ptr, errc::result_out_of_range}; 615} 616 617template <typename _Tp> 618inline _LIBCPP_HIDE_FROM_ABI bool 619__in_pattern(_Tp __c) 620{ 621 return '0' <= __c && __c <= '9'; 622} 623 624struct _LIBCPP_HIDDEN __in_pattern_result 625{ 626 bool __ok; 627 int __val; 628 629 explicit _LIBCPP_HIDE_FROM_ABI operator bool() const { return __ok; } 630}; 631 632template <typename _Tp> 633inline _LIBCPP_HIDE_FROM_ABI __in_pattern_result 634__in_pattern(_Tp __c, int __base) 635{ 636 if (__base <= 10) 637 return {'0' <= __c && __c < '0' + __base, __c - '0'}; 638 else if (__in_pattern(__c)) 639 return {true, __c - '0'}; 640 else if ('a' <= __c && __c < 'a' + __base - 10) 641 return {true, __c - 'a' + 10}; 642 else 643 return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10}; 644} 645 646template <typename _It, typename _Tp, typename _Fn, typename... _Ts> 647inline _LIBCPP_HIDE_FROM_ABI from_chars_result 648__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, 649 _Ts... __args) 650{ 651 auto __find_non_zero = [](_It __firstit, _It __lastit) { 652 for (; __firstit != __lastit; ++__firstit) 653 if (*__firstit != '0') 654 break; 655 return __firstit; 656 }; 657 658 auto __p = __find_non_zero(__first, __last); 659 if (__p == __last || !__in_pattern(*__p, __args...)) 660 { 661 if (__p == __first) 662 return {__first, errc::invalid_argument}; 663 else 664 { 665 __value = 0; 666 return {__p, {}}; 667 } 668 } 669 670 auto __r = __f(__p, __last, __value, __args...); 671 if (__r.ec == errc::result_out_of_range) 672 { 673 for (; __r.ptr != __last; ++__r.ptr) 674 { 675 if (!__in_pattern(*__r.ptr, __args...)) 676 break; 677 } 678 } 679 680 return __r; 681} 682 683template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0> 684inline _LIBCPP_HIDE_FROM_ABI from_chars_result 685__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) 686{ 687 using __tx = __itoa::__traits<_Tp>; 688 using __output_type = typename __tx::type; 689 690 return __subject_seq_combinator( 691 __first, __last, __value, 692 [](const char* __f, const char* __l, 693 _Tp& __val) -> from_chars_result { 694 __output_type __a, __b; 695 auto __p = __tx::__read(__f, __l, __a, __b); 696 if (__p == __l || !__in_pattern(*__p)) 697 { 698 __output_type __m = numeric_limits<_Tp>::max(); 699 if (__m >= __a && __m - __a >= __b) 700 { 701 __val = __a + __b; 702 return {__p, {}}; 703 } 704 } 705 return {__p, errc::result_out_of_range}; 706 }); 707} 708 709template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0> 710inline _LIBCPP_HIDE_FROM_ABI from_chars_result 711__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) 712{ 713 using __t = decltype(__to_unsigned_like(__value)); 714 return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>); 715} 716 717template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0> 718inline _LIBCPP_HIDE_FROM_ABI from_chars_result 719__from_chars_integral(const char* __first, const char* __last, _Tp& __value, 720 int __base) 721{ 722 if (__base == 10) 723 return __from_chars_atoi(__first, __last, __value); 724 725 return __subject_seq_combinator( 726 __first, __last, __value, 727 [](const char* __p, const char* __lastp, _Tp& __val, 728 int __b) -> from_chars_result { 729 using __tl = numeric_limits<_Tp>; 730 auto __digits = __tl::digits / log2f(float(__b)); 731 _Tp __x = __in_pattern(*__p++, __b).__val, __y = 0; 732 733 for (int __i = 1; __p != __lastp; ++__i, ++__p) 734 { 735 if (auto __c = __in_pattern(*__p, __b)) 736 { 737 if (__i < __digits - 1) 738 __x = __x * __b + __c.__val; 739 else 740 { 741 if (!__itoa::__mul_overflowed(__x, __b, __x)) 742 ++__p; 743 __y = __c.__val; 744 break; 745 } 746 } 747 else 748 break; 749 } 750 751 if (__p == __lastp || !__in_pattern(*__p, __b)) 752 { 753 if (__tl::max() - __x >= __y) 754 { 755 __val = __x + __y; 756 return {__p, {}}; 757 } 758 } 759 return {__p, errc::result_out_of_range}; 760 }, 761 __base); 762} 763 764template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0> 765inline _LIBCPP_HIDE_FROM_ABI from_chars_result 766__from_chars_integral(const char* __first, const char* __last, _Tp& __value, 767 int __base) 768{ 769 using __t = decltype(__to_unsigned_like(__value)); 770 return __sign_combinator(__first, __last, __value, 771 __from_chars_integral<__t>, __base); 772} 773 774template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> 775inline _LIBCPP_HIDE_FROM_ABI from_chars_result 776from_chars(const char* __first, const char* __last, _Tp& __value) 777{ 778 return __from_chars_atoi(__first, __last, __value); 779} 780 781template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> 782inline _LIBCPP_HIDE_FROM_ABI from_chars_result 783from_chars(const char* __first, const char* __last, _Tp& __value, int __base) 784{ 785 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); 786 return __from_chars_integral(__first, __last, __value, __base); 787} 788 789// Floating-point implementation starts here. 790// Unlike the other parts of charconv this is only available in C++17 and newer. 791#if _LIBCPP_STD_VER > 14 792 793_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS 794to_chars_result to_chars(char* __first, char* __last, float __value); 795 796_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS 797to_chars_result to_chars(char* __first, char* __last, double __value); 798 799_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS 800to_chars_result to_chars(char* __first, char* __last, long double __value); 801 802_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS 803to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt); 804 805_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS 806to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt); 807 808_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS 809to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt); 810 811_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS 812to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt, int __precision); 813 814_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS 815to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt, int __precision); 816 817_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS 818to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt, int __precision); 819 820# endif // _LIBCPP_STD_VER > 14 821#endif // _LIBCPP_CXX03_LANG 822 823_LIBCPP_END_NAMESPACE_STD 824 825_LIBCPP_POP_MACROS 826 827#endif // _LIBCPP_CHARCONV 828