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