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