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