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