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 <__config> 77#include <__availability> 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> 335inline _LIBCPP_INLINE_VISIBILITY typename make_unsigned<_Tp>::type 336__to_unsigned(_Tp __x) 337{ 338 return static_cast<typename make_unsigned<_Tp>::type>(__x); 339} 340 341template <typename _Tp> 342_LIBCPP_AVAILABILITY_TO_CHARS 343inline _LIBCPP_INLINE_VISIBILITY to_chars_result 344__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type) 345{ 346 auto __x = __to_unsigned(__value); 347 if (__value < 0 && __first != __last) 348 { 349 *__first++ = '-'; 350 __x = __complement(__x); 351 } 352 353 return __to_chars_itoa(__first, __last, __x, false_type()); 354} 355 356template <typename _Tp> 357_LIBCPP_AVAILABILITY_TO_CHARS 358inline _LIBCPP_INLINE_VISIBILITY to_chars_result 359__to_chars_itoa(char* __first, char* __last, _Tp __value, false_type) 360{ 361 using __tx = __itoa::__traits<_Tp>; 362 auto __diff = __last - __first; 363 364#if !defined(_LIBCPP_COMPILER_MSVC) 365 if (__tx::digits <= __diff || __tx::__width(__value) <= __diff) 366 return {__tx::__convert(__value, __first), errc(0)}; 367 else 368 return {__last, errc::value_too_large}; 369#else 370 if (__tx::digits <= __diff) 371 return {__tx::__convert(__value, __first), {}}; 372 else 373 { 374 char __buf[__tx::digits]; 375 auto __p = __tx::__convert(__value, __buf); 376 auto __len = __p - __buf; 377 if (__len <= __diff) 378 { 379 _VSTD::memcpy(__first, __buf, __len); 380 return {__first + __len, {}}; 381 } 382 else 383 return {__last, errc::value_too_large}; 384 } 385#endif 386} 387 388template <typename _Tp> 389_LIBCPP_AVAILABILITY_TO_CHARS 390inline _LIBCPP_INLINE_VISIBILITY to_chars_result 391__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, 392 true_type) 393{ 394 auto __x = __to_unsigned(__value); 395 if (__value < 0 && __first != __last) 396 { 397 *__first++ = '-'; 398 __x = __complement(__x); 399 } 400 401 return __to_chars_integral(__first, __last, __x, __base, false_type()); 402} 403 404template <typename _Tp> 405_LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_INLINE_VISIBILITY int __to_chars_integral_width(_Tp __value, unsigned __base) { 406 _LIBCPP_ASSERT(__value >= 0, "The function requires a non-negative value."); 407 408 unsigned __base_2 = __base * __base; 409 unsigned __base_3 = __base_2 * __base; 410 unsigned __base_4 = __base_2 * __base_2; 411 412 int __r = 0; 413 while (true) { 414 if (__value < __base) 415 return __r + 1; 416 if (__value < __base_2) 417 return __r + 2; 418 if (__value < __base_3) 419 return __r + 3; 420 if (__value < __base_4) 421 return __r + 4; 422 423 __value /= __base_4; 424 __r += 4; 425 } 426 427 _LIBCPP_UNREACHABLE(); 428} 429 430template <typename _Tp> 431_LIBCPP_AVAILABILITY_TO_CHARS 432inline _LIBCPP_INLINE_VISIBILITY to_chars_result 433__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, 434 false_type) 435{ 436 if (__base == 10) 437 return __to_chars_itoa(__first, __last, __value, false_type()); 438 439 ptrdiff_t __cap = __last - __first; 440 int __n = __to_chars_integral_width(__value, __base); 441 if (__n > __cap) 442 return {__last, errc::value_too_large}; 443 444 __last = __first + __n; 445 char* __p = __last; 446 do { 447 unsigned __c = __value % __base; 448 __value /= __base; 449 *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c]; 450 } while (__value != 0); 451 return {__last, errc(0)}; 452} 453 454template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> 455_LIBCPP_AVAILABILITY_TO_CHARS 456inline _LIBCPP_INLINE_VISIBILITY to_chars_result 457to_chars(char* __first, char* __last, _Tp __value) 458{ 459 return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>()); 460} 461 462template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> 463_LIBCPP_AVAILABILITY_TO_CHARS 464inline _LIBCPP_INLINE_VISIBILITY to_chars_result 465to_chars(char* __first, char* __last, _Tp __value, int __base) 466{ 467 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); 468 return __to_chars_integral(__first, __last, __value, __base, 469 is_signed<_Tp>()); 470} 471 472template <typename _It, typename _Tp, typename _Fn, typename... _Ts> 473inline _LIBCPP_INLINE_VISIBILITY from_chars_result 474__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) 475{ 476 using __tl = numeric_limits<_Tp>; 477 decltype(__to_unsigned(__value)) __x; 478 479 bool __neg = (__first != __last && *__first == '-'); 480 auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...); 481 switch (__r.ec) 482 { 483 case errc::invalid_argument: 484 return {__first, __r.ec}; 485 case errc::result_out_of_range: 486 return __r; 487 default: 488 break; 489 } 490 491 if (__neg) 492 { 493 if (__x <= __complement(__to_unsigned(__tl::min()))) 494 { 495 __x = __complement(__x); 496 _VSTD::memcpy(&__value, &__x, sizeof(__x)); 497 return __r; 498 } 499 } 500 else 501 { 502 if (__x <= __tl::max()) 503 { 504 __value = __x; 505 return __r; 506 } 507 } 508 509 return {__r.ptr, errc::result_out_of_range}; 510} 511 512template <typename _Tp> 513inline _LIBCPP_INLINE_VISIBILITY bool 514__in_pattern(_Tp __c) 515{ 516 return '0' <= __c && __c <= '9'; 517} 518 519struct _LIBCPP_HIDDEN __in_pattern_result 520{ 521 bool __ok; 522 int __val; 523 524 explicit _LIBCPP_INLINE_VISIBILITY operator bool() const { return __ok; } 525}; 526 527template <typename _Tp> 528inline _LIBCPP_INLINE_VISIBILITY __in_pattern_result 529__in_pattern(_Tp __c, int __base) 530{ 531 if (__base <= 10) 532 return {'0' <= __c && __c < '0' + __base, __c - '0'}; 533 else if (__in_pattern(__c)) 534 return {true, __c - '0'}; 535 else if ('a' <= __c && __c < 'a' + __base - 10) 536 return {true, __c - 'a' + 10}; 537 else 538 return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10}; 539} 540 541template <typename _It, typename _Tp, typename _Fn, typename... _Ts> 542inline _LIBCPP_INLINE_VISIBILITY from_chars_result 543__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, 544 _Ts... __args) 545{ 546 auto __find_non_zero = [](_It __first, _It __last) { 547 for (; __first != __last; ++__first) 548 if (*__first != '0') 549 break; 550 return __first; 551 }; 552 553 auto __p = __find_non_zero(__first, __last); 554 if (__p == __last || !__in_pattern(*__p, __args...)) 555 { 556 if (__p == __first) 557 return {__first, errc::invalid_argument}; 558 else 559 { 560 __value = 0; 561 return {__p, {}}; 562 } 563 } 564 565 auto __r = __f(__p, __last, __value, __args...); 566 if (__r.ec == errc::result_out_of_range) 567 { 568 for (; __r.ptr != __last; ++__r.ptr) 569 { 570 if (!__in_pattern(*__r.ptr, __args...)) 571 break; 572 } 573 } 574 575 return __r; 576} 577 578template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0> 579inline _LIBCPP_INLINE_VISIBILITY from_chars_result 580__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) 581{ 582 using __tx = __itoa::__traits<_Tp>; 583 using __output_type = typename __tx::type; 584 585 return __subject_seq_combinator( 586 __first, __last, __value, 587 [](const char* __first, const char* __last, 588 _Tp& __value) -> from_chars_result { 589 __output_type __a, __b; 590 auto __p = __tx::__read(__first, __last, __a, __b); 591 if (__p == __last || !__in_pattern(*__p)) 592 { 593 __output_type __m = numeric_limits<_Tp>::max(); 594 if (__m >= __a && __m - __a >= __b) 595 { 596 __value = __a + __b; 597 return {__p, {}}; 598 } 599 } 600 return {__p, errc::result_out_of_range}; 601 }); 602} 603 604template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0> 605inline _LIBCPP_INLINE_VISIBILITY from_chars_result 606__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) 607{ 608 using __t = decltype(__to_unsigned(__value)); 609 return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>); 610} 611 612template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0> 613inline _LIBCPP_INLINE_VISIBILITY from_chars_result 614__from_chars_integral(const char* __first, const char* __last, _Tp& __value, 615 int __base) 616{ 617 if (__base == 10) 618 return __from_chars_atoi(__first, __last, __value); 619 620 return __subject_seq_combinator( 621 __first, __last, __value, 622 [](const char* __p, const char* __last, _Tp& __value, 623 int __base) -> from_chars_result { 624 using __tl = numeric_limits<_Tp>; 625 auto __digits = __tl::digits / log2f(float(__base)); 626 _Tp __a = __in_pattern(*__p++, __base).__val, __b = 0; 627 628 for (int __i = 1; __p != __last; ++__i, ++__p) 629 { 630 if (auto __c = __in_pattern(*__p, __base)) 631 { 632 if (__i < __digits - 1) 633 __a = __a * __base + __c.__val; 634 else 635 { 636 if (!__itoa::__mul_overflowed(__a, __base, __a)) 637 ++__p; 638 __b = __c.__val; 639 break; 640 } 641 } 642 else 643 break; 644 } 645 646 if (__p == __last || !__in_pattern(*__p, __base)) 647 { 648 if (__tl::max() - __a >= __b) 649 { 650 __value = __a + __b; 651 return {__p, {}}; 652 } 653 } 654 return {__p, errc::result_out_of_range}; 655 }, 656 __base); 657} 658 659template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0> 660inline _LIBCPP_INLINE_VISIBILITY from_chars_result 661__from_chars_integral(const char* __first, const char* __last, _Tp& __value, 662 int __base) 663{ 664 using __t = decltype(__to_unsigned(__value)); 665 return __sign_combinator(__first, __last, __value, 666 __from_chars_integral<__t>, __base); 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) 672{ 673 return __from_chars_atoi(__first, __last, __value); 674} 675 676template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> 677inline _LIBCPP_INLINE_VISIBILITY from_chars_result 678from_chars(const char* __first, const char* __last, _Tp& __value, int __base) 679{ 680 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); 681 return __from_chars_integral(__first, __last, __value, __base); 682} 683 684#endif // _LIBCPP_CXX03_LANG 685 686_LIBCPP_END_NAMESPACE_STD 687 688_LIBCPP_POP_MACROS 689 690#endif // _LIBCPP_CHARCONV 691