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