1// -*- C++ -*- 2//===------------------------------ charconv ------------------------------===// 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 }; 31 32 to_chars_result to_chars(char* first, char* last, see below value, 33 int base = 10); 34 35 to_chars_result to_chars(char* first, char* last, float value); 36 to_chars_result to_chars(char* first, char* last, double value); 37 to_chars_result to_chars(char* first, char* last, long double value); 38 39 to_chars_result to_chars(char* first, char* last, float value, 40 chars_format fmt); 41 to_chars_result to_chars(char* first, char* last, double value, 42 chars_format fmt); 43 to_chars_result to_chars(char* first, char* last, long double value, 44 chars_format fmt); 45 46 to_chars_result to_chars(char* first, char* last, float value, 47 chars_format fmt, int precision); 48 to_chars_result to_chars(char* first, char* last, double value, 49 chars_format fmt, int precision); 50 to_chars_result to_chars(char* first, char* last, long double value, 51 chars_format fmt, int precision); 52 53 // 23.20.3, primitive numerical input conversion 54 struct from_chars_result { 55 const char* ptr; 56 errc ec; 57 }; 58 59 from_chars_result from_chars(const char* first, const char* last, 60 see below& value, int base = 10); 61 62 from_chars_result from_chars(const char* first, const char* last, 63 float& value, 64 chars_format fmt = chars_format::general); 65 from_chars_result from_chars(const char* first, const char* last, 66 double& value, 67 chars_format fmt = chars_format::general); 68 from_chars_result from_chars(const char* first, const char* last, 69 long double& value, 70 chars_format fmt = chars_format::general); 71 72} // namespace std 73 74*/ 75 76#include <__availability> 77#include <__config> 78#include <__errc> 79#include <__utility/to_underlying.h> 80#include <cmath> // for log2f 81#include <cstdint> 82#include <cstdlib> // for _LIBCPP_UNREACHABLE 83#include <cstring> 84#include <limits> 85#include <type_traits> 86 87#include <__debug> 88 89#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 90#pragma GCC system_header 91#endif 92 93_LIBCPP_PUSH_MACROS 94#include <__undef_macros> 95 96_LIBCPP_BEGIN_NAMESPACE_STD 97 98namespace __itoa { 99_LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_FUNC_VIS char* __u64toa(uint64_t __value, char* __buffer) _NOEXCEPT; 100_LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_FUNC_VIS char* __u32toa(uint32_t __value, char* __buffer) _NOEXCEPT; 101} 102 103#ifndef _LIBCPP_CXX03_LANG 104 105enum class _LIBCPP_ENUM_VIS chars_format 106{ 107 scientific = 0x1, 108 fixed = 0x2, 109 hex = 0x4, 110 general = fixed | scientific 111}; 112 113inline _LIBCPP_INLINE_VISIBILITY constexpr chars_format 114operator~(chars_format __x) { 115 return chars_format(~_VSTD::__to_underlying(__x)); 116} 117 118inline _LIBCPP_INLINE_VISIBILITY constexpr chars_format 119operator&(chars_format __x, chars_format __y) { 120 return chars_format(_VSTD::__to_underlying(__x) & 121 _VSTD::__to_underlying(__y)); 122} 123 124inline _LIBCPP_INLINE_VISIBILITY constexpr chars_format 125operator|(chars_format __x, chars_format __y) { 126 return chars_format(_VSTD::__to_underlying(__x) | 127 _VSTD::__to_underlying(__y)); 128} 129 130inline _LIBCPP_INLINE_VISIBILITY constexpr chars_format 131operator^(chars_format __x, chars_format __y) { 132 return chars_format(_VSTD::__to_underlying(__x) ^ 133 _VSTD::__to_underlying(__y)); 134} 135 136inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 chars_format& 137operator&=(chars_format& __x, chars_format __y) { 138 __x = __x & __y; 139 return __x; 140} 141 142inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 chars_format& 143operator|=(chars_format& __x, chars_format __y) { 144 __x = __x | __y; 145 return __x; 146} 147 148inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 chars_format& 149operator^=(chars_format& __x, chars_format __y) { 150 __x = __x ^ __y; 151 return __x; 152} 153 154struct _LIBCPP_TYPE_VIS to_chars_result 155{ 156 char* ptr; 157 errc ec; 158}; 159 160struct _LIBCPP_TYPE_VIS from_chars_result 161{ 162 const char* ptr; 163 errc ec; 164}; 165 166void to_chars(char*, char*, bool, int = 10) = delete; 167void from_chars(const char*, const char*, bool, int = 10) = delete; 168 169namespace __itoa 170{ 171 172static _LIBCPP_CONSTEXPR uint64_t __pow10_64[] = { 173 UINT64_C(0), 174 UINT64_C(10), 175 UINT64_C(100), 176 UINT64_C(1000), 177 UINT64_C(10000), 178 UINT64_C(100000), 179 UINT64_C(1000000), 180 UINT64_C(10000000), 181 UINT64_C(100000000), 182 UINT64_C(1000000000), 183 UINT64_C(10000000000), 184 UINT64_C(100000000000), 185 UINT64_C(1000000000000), 186 UINT64_C(10000000000000), 187 UINT64_C(100000000000000), 188 UINT64_C(1000000000000000), 189 UINT64_C(10000000000000000), 190 UINT64_C(100000000000000000), 191 UINT64_C(1000000000000000000), 192 UINT64_C(10000000000000000000), 193}; 194 195static _LIBCPP_CONSTEXPR uint32_t __pow10_32[] = { 196 UINT32_C(0), UINT32_C(10), UINT32_C(100), 197 UINT32_C(1000), UINT32_C(10000), UINT32_C(100000), 198 UINT32_C(1000000), UINT32_C(10000000), UINT32_C(100000000), 199 UINT32_C(1000000000), 200}; 201 202template <typename _Tp, typename = void> 203struct _LIBCPP_HIDDEN __traits_base 204{ 205 using type = uint64_t; 206 207#if !defined(_LIBCPP_COMPILER_MSVC) 208 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v) 209 { 210 auto __t = (64 - __builtin_clzll(__v | 1)) * 1233 >> 12; 211 return __t - (__v < __pow10_64[__t]) + 1; 212 } 213#endif 214 215 _LIBCPP_AVAILABILITY_TO_CHARS 216 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p) 217 { 218 return __u64toa(__v, __p); 219 } 220 221 static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_64)& __pow() { return __pow10_64; } 222}; 223 224template <typename _Tp> 225struct _LIBCPP_HIDDEN 226 __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))> 227{ 228 using type = uint32_t; 229 230#if !defined(_LIBCPP_COMPILER_MSVC) 231 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v) 232 { 233 auto __t = (32 - __builtin_clz(__v | 1)) * 1233 >> 12; 234 return __t - (__v < __pow10_32[__t]) + 1; 235 } 236#endif 237 238 _LIBCPP_AVAILABILITY_TO_CHARS 239 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p) 240 { 241 return __u32toa(__v, __p); 242 } 243 244 static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_32)& __pow() { return __pow10_32; } 245}; 246 247template <typename _Tp> 248inline _LIBCPP_INLINE_VISIBILITY bool 249__mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r) 250{ 251 auto __c = __a * __b; 252 __r = __c; 253 return __c > numeric_limits<unsigned char>::max(); 254} 255 256template <typename _Tp> 257inline _LIBCPP_INLINE_VISIBILITY bool 258__mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r) 259{ 260 auto __c = __a * __b; 261 __r = __c; 262 return __c > numeric_limits<unsigned short>::max(); 263} 264 265template <typename _Tp> 266inline _LIBCPP_INLINE_VISIBILITY bool 267__mul_overflowed(_Tp __a, _Tp __b, _Tp& __r) 268{ 269 static_assert(is_unsigned<_Tp>::value, ""); 270#if !defined(_LIBCPP_COMPILER_MSVC) 271 return __builtin_mul_overflow(__a, __b, &__r); 272#else 273 bool __did = __b && (numeric_limits<_Tp>::max() / __b) < __a; 274 __r = __a * __b; 275 return __did; 276#endif 277} 278 279template <typename _Tp, typename _Up> 280inline _LIBCPP_INLINE_VISIBILITY bool 281__mul_overflowed(_Tp __a, _Up __b, _Tp& __r) 282{ 283 return __mul_overflowed(__a, static_cast<_Tp>(__b), __r); 284} 285 286template <typename _Tp> 287struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp> 288{ 289 static _LIBCPP_CONSTEXPR int digits = numeric_limits<_Tp>::digits10 + 1; 290 using __traits_base<_Tp>::__pow; 291 using typename __traits_base<_Tp>::type; 292 293 // precondition: at least one non-zero character available 294 static _LIBCPP_INLINE_VISIBILITY char const* 295 __read(char const* __p, char const* __ep, type& __a, type& __b) 296 { 297 type __cprod[digits]; 298 int __j = digits - 1; 299 int __i = digits; 300 do 301 { 302 if (!('0' <= *__p && *__p <= '9')) 303 break; 304 __cprod[--__i] = *__p++ - '0'; 305 } while (__p != __ep && __i != 0); 306 307 __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1, 308 __cprod[__i]); 309 if (__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b)) 310 --__p; 311 return __p; 312 } 313 314 template <typename _It1, typename _It2, class _Up> 315 static _LIBCPP_INLINE_VISIBILITY _Up 316 __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init) 317 { 318 for (; __first1 < __last1; ++__first1, ++__first2) 319 __init = __init + *__first1 * *__first2; 320 return __init; 321 } 322}; 323 324} // namespace __itoa 325 326template <typename _Tp> 327inline _LIBCPP_INLINE_VISIBILITY _Tp 328__complement(_Tp __x) 329{ 330 static_assert(is_unsigned<_Tp>::value, "cast to unsigned first"); 331 return _Tp(~__x + 1); 332} 333 334template <typename _Tp> 335_LIBCPP_AVAILABILITY_TO_CHARS 336inline _LIBCPP_INLINE_VISIBILITY to_chars_result 337__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type) 338{ 339 auto __x = __to_unsigned_like(__value); 340 if (__value < 0 && __first != __last) 341 { 342 *__first++ = '-'; 343 __x = __complement(__x); 344 } 345 346 return __to_chars_itoa(__first, __last, __x, false_type()); 347} 348 349template <typename _Tp> 350_LIBCPP_AVAILABILITY_TO_CHARS 351inline _LIBCPP_INLINE_VISIBILITY to_chars_result 352__to_chars_itoa(char* __first, char* __last, _Tp __value, false_type) 353{ 354 using __tx = __itoa::__traits<_Tp>; 355 auto __diff = __last - __first; 356 357#if !defined(_LIBCPP_COMPILER_MSVC) 358 if (__tx::digits <= __diff || __tx::__width(__value) <= __diff) 359 return {__tx::__convert(__value, __first), errc(0)}; 360 else 361 return {__last, errc::value_too_large}; 362#else 363 if (__tx::digits <= __diff) 364 return {__tx::__convert(__value, __first), {}}; 365 else 366 { 367 char __buf[__tx::digits]; 368 auto __p = __tx::__convert(__value, __buf); 369 auto __len = __p - __buf; 370 if (__len <= __diff) 371 { 372 _VSTD::memcpy(__first, __buf, __len); 373 return {__first + __len, {}}; 374 } 375 else 376 return {__last, errc::value_too_large}; 377 } 378#endif 379} 380 381template <typename _Tp> 382_LIBCPP_AVAILABILITY_TO_CHARS 383inline _LIBCPP_INLINE_VISIBILITY to_chars_result 384__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, 385 true_type) 386{ 387 auto __x = __to_unsigned_like(__value); 388 if (__value < 0 && __first != __last) 389 { 390 *__first++ = '-'; 391 __x = __complement(__x); 392 } 393 394 return __to_chars_integral(__first, __last, __x, __base, false_type()); 395} 396 397template <typename _Tp> 398_LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_INLINE_VISIBILITY int __to_chars_integral_width(_Tp __value, unsigned __base) { 399 _LIBCPP_ASSERT(__value >= 0, "The function requires a non-negative value."); 400 401 unsigned __base_2 = __base * __base; 402 unsigned __base_3 = __base_2 * __base; 403 unsigned __base_4 = __base_2 * __base_2; 404 405 int __r = 0; 406 while (true) { 407 if (__value < __base) 408 return __r + 1; 409 if (__value < __base_2) 410 return __r + 2; 411 if (__value < __base_3) 412 return __r + 3; 413 if (__value < __base_4) 414 return __r + 4; 415 416 __value /= __base_4; 417 __r += 4; 418 } 419 420 _LIBCPP_UNREACHABLE(); 421} 422 423template <typename _Tp> 424_LIBCPP_AVAILABILITY_TO_CHARS 425inline _LIBCPP_INLINE_VISIBILITY to_chars_result 426__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, 427 false_type) 428{ 429 if (__base == 10) 430 return __to_chars_itoa(__first, __last, __value, false_type()); 431 432 ptrdiff_t __cap = __last - __first; 433 int __n = __to_chars_integral_width(__value, __base); 434 if (__n > __cap) 435 return {__last, errc::value_too_large}; 436 437 __last = __first + __n; 438 char* __p = __last; 439 do { 440 unsigned __c = __value % __base; 441 __value /= __base; 442 *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c]; 443 } while (__value != 0); 444 return {__last, errc(0)}; 445} 446 447template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> 448_LIBCPP_AVAILABILITY_TO_CHARS 449inline _LIBCPP_INLINE_VISIBILITY to_chars_result 450to_chars(char* __first, char* __last, _Tp __value) 451{ 452 return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>()); 453} 454 455template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> 456_LIBCPP_AVAILABILITY_TO_CHARS 457inline _LIBCPP_INLINE_VISIBILITY to_chars_result 458to_chars(char* __first, char* __last, _Tp __value, int __base) 459{ 460 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); 461 return __to_chars_integral(__first, __last, __value, __base, 462 is_signed<_Tp>()); 463} 464 465template <typename _It, typename _Tp, typename _Fn, typename... _Ts> 466inline _LIBCPP_INLINE_VISIBILITY from_chars_result 467__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) 468{ 469 using __tl = numeric_limits<_Tp>; 470 decltype(__to_unsigned_like(__value)) __x; 471 472 bool __neg = (__first != __last && *__first == '-'); 473 auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...); 474 switch (__r.ec) 475 { 476 case errc::invalid_argument: 477 return {__first, __r.ec}; 478 case errc::result_out_of_range: 479 return __r; 480 default: 481 break; 482 } 483 484 if (__neg) 485 { 486 if (__x <= __complement(__to_unsigned_like(__tl::min()))) 487 { 488 __x = __complement(__x); 489 _VSTD::memcpy(&__value, &__x, sizeof(__x)); 490 return __r; 491 } 492 } 493 else 494 { 495 if (__x <= __tl::max()) 496 { 497 __value = __x; 498 return __r; 499 } 500 } 501 502 return {__r.ptr, errc::result_out_of_range}; 503} 504 505template <typename _Tp> 506inline _LIBCPP_INLINE_VISIBILITY bool 507__in_pattern(_Tp __c) 508{ 509 return '0' <= __c && __c <= '9'; 510} 511 512struct _LIBCPP_HIDDEN __in_pattern_result 513{ 514 bool __ok; 515 int __val; 516 517 explicit _LIBCPP_INLINE_VISIBILITY operator bool() const { return __ok; } 518}; 519 520template <typename _Tp> 521inline _LIBCPP_INLINE_VISIBILITY __in_pattern_result 522__in_pattern(_Tp __c, int __base) 523{ 524 if (__base <= 10) 525 return {'0' <= __c && __c < '0' + __base, __c - '0'}; 526 else if (__in_pattern(__c)) 527 return {true, __c - '0'}; 528 else if ('a' <= __c && __c < 'a' + __base - 10) 529 return {true, __c - 'a' + 10}; 530 else 531 return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10}; 532} 533 534template <typename _It, typename _Tp, typename _Fn, typename... _Ts> 535inline _LIBCPP_INLINE_VISIBILITY from_chars_result 536__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, 537 _Ts... __args) 538{ 539 auto __find_non_zero = [](_It __first, _It __last) { 540 for (; __first != __last; ++__first) 541 if (*__first != '0') 542 break; 543 return __first; 544 }; 545 546 auto __p = __find_non_zero(__first, __last); 547 if (__p == __last || !__in_pattern(*__p, __args...)) 548 { 549 if (__p == __first) 550 return {__first, errc::invalid_argument}; 551 else 552 { 553 __value = 0; 554 return {__p, {}}; 555 } 556 } 557 558 auto __r = __f(__p, __last, __value, __args...); 559 if (__r.ec == errc::result_out_of_range) 560 { 561 for (; __r.ptr != __last; ++__r.ptr) 562 { 563 if (!__in_pattern(*__r.ptr, __args...)) 564 break; 565 } 566 } 567 568 return __r; 569} 570 571template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0> 572inline _LIBCPP_INLINE_VISIBILITY from_chars_result 573__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) 574{ 575 using __tx = __itoa::__traits<_Tp>; 576 using __output_type = typename __tx::type; 577 578 return __subject_seq_combinator( 579 __first, __last, __value, 580 [](const char* __first, const char* __last, 581 _Tp& __value) -> from_chars_result { 582 __output_type __a, __b; 583 auto __p = __tx::__read(__first, __last, __a, __b); 584 if (__p == __last || !__in_pattern(*__p)) 585 { 586 __output_type __m = numeric_limits<_Tp>::max(); 587 if (__m >= __a && __m - __a >= __b) 588 { 589 __value = __a + __b; 590 return {__p, {}}; 591 } 592 } 593 return {__p, errc::result_out_of_range}; 594 }); 595} 596 597template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0> 598inline _LIBCPP_INLINE_VISIBILITY from_chars_result 599__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) 600{ 601 using __t = decltype(__to_unsigned_like(__value)); 602 return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>); 603} 604 605template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0> 606inline _LIBCPP_INLINE_VISIBILITY from_chars_result 607__from_chars_integral(const char* __first, const char* __last, _Tp& __value, 608 int __base) 609{ 610 if (__base == 10) 611 return __from_chars_atoi(__first, __last, __value); 612 613 return __subject_seq_combinator( 614 __first, __last, __value, 615 [](const char* __p, const char* __lastx, _Tp& __value, 616 int __base) -> from_chars_result { 617 using __tl = numeric_limits<_Tp>; 618 auto __digits = __tl::digits / log2f(float(__base)); 619 _Tp __a = __in_pattern(*__p++, __base).__val, __b = 0; 620 621 for (int __i = 1; __p != __lastx; ++__i, ++__p) 622 { 623 if (auto __c = __in_pattern(*__p, __base)) 624 { 625 if (__i < __digits - 1) 626 __a = __a * __base + __c.__val; 627 else 628 { 629 if (!__itoa::__mul_overflowed(__a, __base, __a)) 630 ++__p; 631 __b = __c.__val; 632 break; 633 } 634 } 635 else 636 break; 637 } 638 639 if (__p == __lastx || !__in_pattern(*__p, __base)) 640 { 641 if (__tl::max() - __a >= __b) 642 { 643 __value = __a + __b; 644 return {__p, {}}; 645 } 646 } 647 return {__p, errc::result_out_of_range}; 648 }, 649 __base); 650} 651 652template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0> 653inline _LIBCPP_INLINE_VISIBILITY from_chars_result 654__from_chars_integral(const char* __first, const char* __last, _Tp& __value, 655 int __base) 656{ 657 using __t = decltype(__to_unsigned_like(__value)); 658 return __sign_combinator(__first, __last, __value, 659 __from_chars_integral<__t>, __base); 660} 661 662template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> 663inline _LIBCPP_INLINE_VISIBILITY from_chars_result 664from_chars(const char* __first, const char* __last, _Tp& __value) 665{ 666 return __from_chars_atoi(__first, __last, __value); 667} 668 669template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> 670inline _LIBCPP_INLINE_VISIBILITY from_chars_result 671from_chars(const char* __first, const char* __last, _Tp& __value, int __base) 672{ 673 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); 674 return __from_chars_integral(__first, __last, __value, __base); 675} 676 677#endif // _LIBCPP_CXX03_LANG 678 679_LIBCPP_END_NAMESPACE_STD 680 681_LIBCPP_POP_MACROS 682 683#endif // _LIBCPP_CHARCONV 684