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