charconv 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. #ifndef _LIBCPP_CHARCONV
  10. #define _LIBCPP_CHARCONV
  11. /*
  12. charconv synopsis
  13. namespace std {
  14. // floating-point format for primitive numerical conversion
  15. enum class chars_format {
  16. scientific = unspecified,
  17. fixed = unspecified,
  18. hex = unspecified,
  19. general = fixed | scientific
  20. };
  21. // 23.20.2, primitive numerical output conversion
  22. struct to_chars_result {
  23. char* ptr;
  24. errc ec;
  25. friend bool operator==(const to_chars_result&, const to_chars_result&) = default; // since C++20
  26. };
  27. constexpr to_chars_result to_chars(char* first, char* last, see below value,
  28. int base = 10); // constexpr since C++23
  29. to_chars_result to_chars(char* first, char* last, bool value,
  30. int base = 10) = delete;
  31. to_chars_result to_chars(char* first, char* last, float value);
  32. to_chars_result to_chars(char* first, char* last, double value);
  33. to_chars_result to_chars(char* first, char* last, long double value);
  34. to_chars_result to_chars(char* first, char* last, float value,
  35. chars_format fmt);
  36. to_chars_result to_chars(char* first, char* last, double value,
  37. chars_format fmt);
  38. to_chars_result to_chars(char* first, char* last, long double value,
  39. chars_format fmt);
  40. to_chars_result to_chars(char* first, char* last, float value,
  41. chars_format fmt, int precision);
  42. to_chars_result to_chars(char* first, char* last, double value,
  43. chars_format fmt, int precision);
  44. to_chars_result to_chars(char* first, char* last, long double value,
  45. chars_format fmt, int precision);
  46. // 23.20.3, primitive numerical input conversion
  47. struct from_chars_result {
  48. const char* ptr;
  49. errc ec;
  50. friend bool operator==(const from_chars_result&, const from_chars_result&) = default; // since C++20
  51. };
  52. constexpr from_chars_result from_chars(const char* first, const char* last,
  53. see below& value, int base = 10); // constexpr since C++23
  54. from_chars_result from_chars(const char* first, const char* last,
  55. float& value,
  56. chars_format fmt = chars_format::general);
  57. from_chars_result from_chars(const char* first, const char* last,
  58. double& value,
  59. chars_format fmt = chars_format::general);
  60. from_chars_result from_chars(const char* first, const char* last,
  61. long double& value,
  62. chars_format fmt = chars_format::general);
  63. } // namespace std
  64. */
  65. #include <__algorithm/copy_n.h>
  66. #include <__assert> // all public C++ headers provide the assertion handler
  67. #include <__availability>
  68. #include <__bit/countl.h>
  69. #include <__charconv/chars_format.h>
  70. #include <__charconv/from_chars_result.h>
  71. #include <__charconv/tables.h>
  72. #include <__charconv/to_chars_base_10.h>
  73. #include <__charconv/to_chars_result.h>
  74. #include <__config>
  75. #include <__debug>
  76. #include <__errc>
  77. #include <__memory/addressof.h>
  78. #include <__type_traits/make_32_64_or_128_bit.h>
  79. #include <__utility/unreachable.h>
  80. #include <cmath> // for log2f
  81. #include <cstdint>
  82. #include <cstdlib>
  83. #include <cstring>
  84. #include <limits>
  85. #include <type_traits>
  86. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  87. # pragma GCC system_header
  88. #endif
  89. _LIBCPP_PUSH_MACROS
  90. #include <__undef_macros>
  91. _LIBCPP_BEGIN_NAMESPACE_STD
  92. #if _LIBCPP_STD_VER > 14
  93. to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
  94. from_chars_result from_chars(const char*, const char*, bool, int = 10) = delete;
  95. namespace __itoa
  96. {
  97. template <typename _Tp, typename = void>
  98. struct _LIBCPP_HIDDEN __traits_base;
  99. template <typename _Tp>
  100. struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) <= sizeof(uint32_t)>>
  101. {
  102. using type = uint32_t;
  103. /// The width estimation using a log10 algorithm.
  104. ///
  105. /// The algorithm is based on
  106. /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
  107. /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that
  108. /// function requires its input to have at least one bit set the value of
  109. /// zero is set to one. This means the first element of the lookup table is
  110. /// zero.
  111. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v)
  112. {
  113. auto __t = (32 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
  114. return __t - (__v < __itoa::__pow10_32[__t]) + 1;
  115. }
  116. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v)
  117. {
  118. return __itoa::__base_10_u32(__p, __v);
  119. }
  120. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_32)& __pow() { return __itoa::__pow10_32; }
  121. };
  122. template <typename _Tp>
  123. struct _LIBCPP_HIDDEN
  124. __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(uint64_t)>> {
  125. using type = uint64_t;
  126. /// The width estimation using a log10 algorithm.
  127. ///
  128. /// The algorithm is based on
  129. /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
  130. /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that
  131. /// function requires its input to have at least one bit set the value of
  132. /// zero is set to one. This means the first element of the lookup table is
  133. /// zero.
  134. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) {
  135. auto __t = (64 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
  136. return __t - (__v < __itoa::__pow10_64[__t]) + 1;
  137. }
  138. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u64(__p, __v); }
  139. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_64)& __pow() { return __itoa::__pow10_64; }
  140. };
  141. # ifndef _LIBCPP_HAS_NO_INT128
  142. template <typename _Tp>
  143. struct _LIBCPP_HIDDEN
  144. __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(__uint128_t)> > {
  145. using type = __uint128_t;
  146. /// The width estimation using a log10 algorithm.
  147. ///
  148. /// The algorithm is based on
  149. /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
  150. /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that
  151. /// function requires its input to have at least one bit set the value of
  152. /// zero is set to one. This means the first element of the lookup table is
  153. /// zero.
  154. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) {
  155. _LIBCPP_ASSERT(__v > numeric_limits<uint64_t>::max(), "The optimizations for this algorithm fail when this isn't true.");
  156. // There's always a bit set in the upper 64-bits.
  157. auto __t = (128 - std::__libcpp_clz(static_cast<uint64_t>(__v >> 64))) * 1233 >> 12;
  158. _LIBCPP_ASSERT(__t >= __itoa::__pow10_128_offset, "Index out of bounds");
  159. // __t is adjusted since the lookup table misses the lower entries.
  160. return __t - (__v < __itoa::__pow10_128[__t - __itoa::__pow10_128_offset]) + 1;
  161. }
  162. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u128(__p, __v); }
  163. // TODO FMT This pow function should get an index.
  164. // By moving this to its own header it can be reused by the pow function in to_chars_base_10.
  165. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_128)& __pow() { return __itoa::__pow10_128; }
  166. };
  167. #endif
  168. template <typename _Tp>
  169. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
  170. __mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r)
  171. {
  172. auto __c = __a * __b;
  173. __r = __c;
  174. return __c > numeric_limits<unsigned char>::max();
  175. }
  176. template <typename _Tp>
  177. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
  178. __mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r)
  179. {
  180. auto __c = __a * __b;
  181. __r = __c;
  182. return __c > numeric_limits<unsigned short>::max();
  183. }
  184. template <typename _Tp>
  185. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
  186. __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r)
  187. {
  188. static_assert(is_unsigned<_Tp>::value, "");
  189. return __builtin_mul_overflow(__a, __b, &__r);
  190. }
  191. template <typename _Tp, typename _Up>
  192. inline _LIBCPP_HIDE_FROM_ABI bool
  193. _LIBCPP_CONSTEXPR_SINCE_CXX23 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r)
  194. {
  195. return __itoa::__mul_overflowed(__a, static_cast<_Tp>(__b), __r);
  196. }
  197. template <typename _Tp>
  198. struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp>
  199. {
  200. static constexpr int digits = numeric_limits<_Tp>::digits10 + 1;
  201. using __traits_base<_Tp>::__pow;
  202. using typename __traits_base<_Tp>::type;
  203. // precondition: at least one non-zero character available
  204. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char const*
  205. __read(char const* __p, char const* __ep, type& __a, type& __b)
  206. {
  207. type __cprod[digits];
  208. int __j = digits - 1;
  209. int __i = digits;
  210. do
  211. {
  212. if (*__p < '0' || *__p > '9')
  213. break;
  214. __cprod[--__i] = *__p++ - '0';
  215. } while (__p != __ep && __i != 0);
  216. __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1,
  217. __cprod[__i]);
  218. if (__itoa::__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))
  219. --__p;
  220. return __p;
  221. }
  222. template <typename _It1, typename _It2, class _Up>
  223. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Up
  224. __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init)
  225. {
  226. for (; __first1 < __last1; ++__first1, ++__first2)
  227. __init = __init + *__first1 * *__first2;
  228. return __init;
  229. }
  230. };
  231. } // namespace __itoa
  232. template <typename _Tp>
  233. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Tp
  234. __complement(_Tp __x)
  235. {
  236. static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
  237. return _Tp(~__x + 1);
  238. }
  239. template <typename _Tp>
  240. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  241. __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type);
  242. template <typename _Tp>
  243. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  244. __to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
  245. {
  246. auto __x = std::__to_unsigned_like(__value);
  247. if (__value < 0 && __first != __last)
  248. {
  249. *__first++ = '-';
  250. __x = std::__complement(__x);
  251. }
  252. return std::__to_chars_itoa(__first, __last, __x, false_type());
  253. }
  254. template <typename _Tp>
  255. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  256. __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type)
  257. {
  258. using __tx = __itoa::__traits<_Tp>;
  259. auto __diff = __last - __first;
  260. if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
  261. return {__tx::__convert(__first, __value), errc(0)};
  262. else
  263. return {__last, errc::value_too_large};
  264. }
  265. # ifndef _LIBCPP_HAS_NO_INT128
  266. template <>
  267. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  268. __to_chars_itoa(char* __first, char* __last, __uint128_t __value, false_type)
  269. {
  270. // When the value fits in 64-bits use the 64-bit code path. This reduces
  271. // the number of expensive calculations on 128-bit values.
  272. //
  273. // NOTE the 128-bit code path requires this optimization.
  274. if(__value <= numeric_limits<uint64_t>::max())
  275. return __to_chars_itoa(__first, __last, static_cast<uint64_t>(__value), false_type());
  276. using __tx = __itoa::__traits<__uint128_t>;
  277. auto __diff = __last - __first;
  278. if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
  279. return {__tx::__convert(__first, __value), errc(0)};
  280. else
  281. return {__last, errc::value_too_large};
  282. }
  283. #endif
  284. template <class _Tp>
  285. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  286. __to_chars_integral(char* __first, char* __last, _Tp __value, int __base, false_type);
  287. template <typename _Tp>
  288. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  289. __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
  290. true_type)
  291. {
  292. auto __x = std::__to_unsigned_like(__value);
  293. if (__value < 0 && __first != __last)
  294. {
  295. *__first++ = '-';
  296. __x = std::__complement(__x);
  297. }
  298. return std::__to_chars_integral(__first, __last, __x, __base, false_type());
  299. }
  300. namespace __itoa {
  301. template <unsigned _Base>
  302. struct _LIBCPP_HIDDEN __integral;
  303. template <>
  304. struct _LIBCPP_HIDDEN __integral<2> {
  305. template <typename _Tp>
  306. _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
  307. // If value == 0 still need one digit. If the value != this has no
  308. // effect since the code scans for the most significant bit set. (Note
  309. // that __libcpp_clz doesn't work for 0.)
  310. return numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1);
  311. }
  312. template <typename _Tp>
  313. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) {
  314. ptrdiff_t __cap = __last - __first;
  315. int __n = __width(__value);
  316. if (__n > __cap)
  317. return {__last, errc::value_too_large};
  318. __last = __first + __n;
  319. char* __p = __last;
  320. const unsigned __divisor = 16;
  321. while (__value > __divisor) {
  322. unsigned __c = __value % __divisor;
  323. __value /= __divisor;
  324. __p -= 4;
  325. std::copy_n(&__base_2_lut[4 * __c], 4, __p);
  326. }
  327. do {
  328. unsigned __c = __value % 2;
  329. __value /= 2;
  330. *--__p = "01"[__c];
  331. } while (__value != 0);
  332. return {__last, errc(0)};
  333. }
  334. };
  335. template <>
  336. struct _LIBCPP_HIDDEN __integral<8> {
  337. template <typename _Tp>
  338. _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
  339. // If value == 0 still need one digit. If the value != this has no
  340. // effect since the code scans for the most significat bit set. (Note
  341. // that __libcpp_clz doesn't work for 0.)
  342. return ((numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1)) + 2) / 3;
  343. }
  344. template <typename _Tp>
  345. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) {
  346. ptrdiff_t __cap = __last - __first;
  347. int __n = __width(__value);
  348. if (__n > __cap)
  349. return {__last, errc::value_too_large};
  350. __last = __first + __n;
  351. char* __p = __last;
  352. unsigned __divisor = 64;
  353. while (__value > __divisor) {
  354. unsigned __c = __value % __divisor;
  355. __value /= __divisor;
  356. __p -= 2;
  357. std::copy_n(&__base_8_lut[2 * __c], 2, __p);
  358. }
  359. do {
  360. unsigned __c = __value % 8;
  361. __value /= 8;
  362. *--__p = "01234567"[__c];
  363. } while (__value != 0);
  364. return {__last, errc(0)};
  365. }
  366. };
  367. template <>
  368. struct _LIBCPP_HIDDEN __integral<16> {
  369. template <typename _Tp>
  370. _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
  371. // If value == 0 still need one digit. If the value != this has no
  372. // effect since the code scans for the most significat bit set. (Note
  373. // that __libcpp_clz doesn't work for 0.)
  374. return (numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1) + 3) / 4;
  375. }
  376. template <typename _Tp>
  377. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) {
  378. ptrdiff_t __cap = __last - __first;
  379. int __n = __width(__value);
  380. if (__n > __cap)
  381. return {__last, errc::value_too_large};
  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::copy_n(&__base_16_lut[2 * __c], 2, __p);
  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. } // namespace __itoa
  401. template <unsigned _Base, typename _Tp,
  402. typename enable_if<(sizeof(_Tp) >= sizeof(unsigned)), int>::type = 0>
  403. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int
  404. __to_chars_integral_width(_Tp __value) {
  405. return __itoa::__integral<_Base>::__width(__value);
  406. }
  407. template <unsigned _Base, typename _Tp,
  408. typename enable_if<(sizeof(_Tp) < sizeof(unsigned)), int>::type = 0>
  409. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int
  410. __to_chars_integral_width(_Tp __value) {
  411. return std::__to_chars_integral_width<_Base>(static_cast<unsigned>(__value));
  412. }
  413. template <unsigned _Base, typename _Tp,
  414. typename enable_if<(sizeof(_Tp) >= sizeof(unsigned)), int>::type = 0>
  415. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  416. __to_chars_integral(char* __first, char* __last, _Tp __value) {
  417. return __itoa::__integral<_Base>::__to_chars(__first, __last, __value);
  418. }
  419. template <unsigned _Base, typename _Tp,
  420. typename enable_if<(sizeof(_Tp) < sizeof(unsigned)), int>::type = 0>
  421. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  422. __to_chars_integral(char* __first, char* __last, _Tp __value) {
  423. return std::__to_chars_integral<_Base>(__first, __last, static_cast<unsigned>(__value));
  424. }
  425. template <typename _Tp>
  426. _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int
  427. __to_chars_integral_width(_Tp __value, unsigned __base) {
  428. _LIBCPP_ASSERT(__value >= 0, "The function requires a non-negative value.");
  429. unsigned __base_2 = __base * __base;
  430. unsigned __base_3 = __base_2 * __base;
  431. unsigned __base_4 = __base_2 * __base_2;
  432. int __r = 0;
  433. while (true) {
  434. if (__value < __base)
  435. return __r + 1;
  436. if (__value < __base_2)
  437. return __r + 2;
  438. if (__value < __base_3)
  439. return __r + 3;
  440. if (__value < __base_4)
  441. return __r + 4;
  442. __value /= __base_4;
  443. __r += 4;
  444. }
  445. __libcpp_unreachable();
  446. }
  447. template <typename _Tp>
  448. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  449. __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
  450. false_type)
  451. {
  452. if (__base == 10) [[likely]]
  453. return std::__to_chars_itoa(__first, __last, __value, false_type());
  454. switch (__base) {
  455. case 2:
  456. return std::__to_chars_integral<2>(__first, __last, __value);
  457. case 8:
  458. return std::__to_chars_integral<8>(__first, __last, __value);
  459. case 16:
  460. return std::__to_chars_integral<16>(__first, __last, __value);
  461. }
  462. ptrdiff_t __cap = __last - __first;
  463. int __n = std::__to_chars_integral_width(__value, __base);
  464. if (__n > __cap)
  465. return {__last, errc::value_too_large};
  466. __last = __first + __n;
  467. char* __p = __last;
  468. do {
  469. unsigned __c = __value % __base;
  470. __value /= __base;
  471. *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c];
  472. } while (__value != 0);
  473. return {__last, errc(0)};
  474. }
  475. template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
  476. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  477. to_chars(char* __first, char* __last, _Tp __value)
  478. {
  479. using _Type = __make_32_64_or_128_bit_t<_Tp>;
  480. static_assert(!is_same<_Type, void>::value, "unsupported integral type used in to_chars");
  481. return std::__to_chars_itoa(__first, __last, static_cast<_Type>(__value), is_signed<_Tp>());
  482. }
  483. template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
  484. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result
  485. to_chars(char* __first, char* __last, _Tp __value, int __base)
  486. {
  487. _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
  488. using _Type = __make_32_64_or_128_bit_t<_Tp>;
  489. return std::__to_chars_integral(__first, __last, static_cast<_Type>(__value), __base, is_signed<_Tp>());
  490. }
  491. template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
  492. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  493. __sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
  494. {
  495. using __tl = numeric_limits<_Tp>;
  496. decltype(std::__to_unsigned_like(__value)) __x;
  497. bool __neg = (__first != __last && *__first == '-');
  498. auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);
  499. switch (__r.ec)
  500. {
  501. case errc::invalid_argument:
  502. return {__first, __r.ec};
  503. case errc::result_out_of_range:
  504. return __r;
  505. default:
  506. break;
  507. }
  508. if (__neg)
  509. {
  510. if (__x <= std::__complement(std::__to_unsigned_like(__tl::min())))
  511. {
  512. __x = std::__complement(__x);
  513. std::copy_n(std::addressof(__x), 1, std::addressof(__value));
  514. return __r;
  515. }
  516. }
  517. else
  518. {
  519. if (__x <= std::__to_unsigned_like(__tl::max()))
  520. {
  521. __value = __x;
  522. return __r;
  523. }
  524. }
  525. return {__r.ptr, errc::result_out_of_range};
  526. }
  527. template <typename _Tp>
  528. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
  529. __in_pattern(_Tp __c)
  530. {
  531. return '0' <= __c && __c <= '9';
  532. }
  533. struct _LIBCPP_HIDDEN __in_pattern_result
  534. {
  535. bool __ok;
  536. int __val;
  537. explicit _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI operator bool() const { return __ok; }
  538. };
  539. template <typename _Tp>
  540. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI __in_pattern_result
  541. __in_pattern(_Tp __c, int __base)
  542. {
  543. if (__base <= 10)
  544. return {'0' <= __c && __c < '0' + __base, __c - '0'};
  545. else if (std::__in_pattern(__c))
  546. return {true, __c - '0'};
  547. else if ('a' <= __c && __c < 'a' + __base - 10)
  548. return {true, __c - 'a' + 10};
  549. else
  550. return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10};
  551. }
  552. template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
  553. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  554. __subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f,
  555. _Ts... __args)
  556. {
  557. auto __find_non_zero = [](_It __firstit, _It __lastit) {
  558. for (; __firstit != __lastit; ++__firstit)
  559. if (*__firstit != '0')
  560. break;
  561. return __firstit;
  562. };
  563. auto __p = __find_non_zero(__first, __last);
  564. if (__p == __last || !std::__in_pattern(*__p, __args...))
  565. {
  566. if (__p == __first)
  567. return {__first, errc::invalid_argument};
  568. else
  569. {
  570. __value = 0;
  571. return {__p, {}};
  572. }
  573. }
  574. auto __r = __f(__p, __last, __value, __args...);
  575. if (__r.ec == errc::result_out_of_range)
  576. {
  577. for (; __r.ptr != __last; ++__r.ptr)
  578. {
  579. if (!std::__in_pattern(*__r.ptr, __args...))
  580. break;
  581. }
  582. }
  583. return __r;
  584. }
  585. template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
  586. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  587. __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
  588. {
  589. using __tx = __itoa::__traits<_Tp>;
  590. using __output_type = typename __tx::type;
  591. return std::__subject_seq_combinator(
  592. __first, __last, __value,
  593. [](const char* __f, const char* __l,
  594. _Tp& __val) -> from_chars_result {
  595. __output_type __a, __b;
  596. auto __p = __tx::__read(__f, __l, __a, __b);
  597. if (__p == __l || !std::__in_pattern(*__p))
  598. {
  599. __output_type __m = numeric_limits<_Tp>::max();
  600. if (__m >= __a && __m - __a >= __b)
  601. {
  602. __val = __a + __b;
  603. return {__p, {}};
  604. }
  605. }
  606. return {__p, errc::result_out_of_range};
  607. });
  608. }
  609. template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
  610. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  611. __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
  612. {
  613. using __t = decltype(std::__to_unsigned_like(__value));
  614. return std::__sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
  615. }
  616. /*
  617. // Code used to generate __from_chars_log2f_lut.
  618. #include <cmath>
  619. #include <iostream>
  620. #include <format>
  621. int main() {
  622. for (int i = 2; i <= 36; ++i)
  623. std::cout << std::format("{},\n", log2f(i));
  624. }
  625. */
  626. /// log2f table for bases [2, 36].
  627. inline constexpr float __from_chars_log2f_lut[35] = {
  628. 1, 1.5849625, 2, 2.321928, 2.5849626, 2.807355, 3, 3.169925, 3.321928,
  629. 3.4594316, 3.5849626, 3.7004397, 3.807355, 3.9068906, 4, 4.087463, 4.169925, 4.2479277,
  630. 4.321928, 4.3923173, 4.4594316, 4.523562, 4.5849624, 4.643856, 4.70044, 4.7548876, 4.807355,
  631. 4.857981, 4.9068904, 4.9541965, 5, 5.044394, 5.087463, 5.129283, 5.169925};
  632. template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
  633. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  634. __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
  635. int __base)
  636. {
  637. if (__base == 10)
  638. return std::__from_chars_atoi(__first, __last, __value);
  639. return std::__subject_seq_combinator(
  640. __first, __last, __value,
  641. [](const char* __p, const char* __lastp, _Tp& __val,
  642. int __b) -> from_chars_result {
  643. using __tl = numeric_limits<_Tp>;
  644. // __base is always between 2 and 36 inclusive.
  645. auto __digits = __tl::digits / __from_chars_log2f_lut[__b - 2];
  646. _Tp __x = __in_pattern(*__p++, __b).__val, __y = 0;
  647. for (int __i = 1; __p != __lastp; ++__i, ++__p)
  648. {
  649. if (auto __c = __in_pattern(*__p, __b))
  650. {
  651. if (__i < __digits - 1)
  652. __x = __x * __b + __c.__val;
  653. else
  654. {
  655. if (!__itoa::__mul_overflowed(__x, __b, __x))
  656. ++__p;
  657. __y = __c.__val;
  658. break;
  659. }
  660. }
  661. else
  662. break;
  663. }
  664. if (__p == __lastp || !__in_pattern(*__p, __b))
  665. {
  666. if (__tl::max() - __x >= __y)
  667. {
  668. __val = __x + __y;
  669. return {__p, {}};
  670. }
  671. }
  672. return {__p, errc::result_out_of_range};
  673. },
  674. __base);
  675. }
  676. template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
  677. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  678. __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
  679. int __base)
  680. {
  681. using __t = decltype(std::__to_unsigned_like(__value));
  682. return std::__sign_combinator(__first, __last, __value,
  683. __from_chars_integral<__t>, __base);
  684. }
  685. template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
  686. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  687. from_chars(const char* __first, const char* __last, _Tp& __value)
  688. {
  689. return std::__from_chars_atoi(__first, __last, __value);
  690. }
  691. template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
  692. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  693. from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
  694. {
  695. _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
  696. return std::__from_chars_integral(__first, __last, __value, __base);
  697. }
  698. _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
  699. to_chars_result to_chars(char* __first, char* __last, float __value);
  700. _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
  701. to_chars_result to_chars(char* __first, char* __last, double __value);
  702. _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
  703. to_chars_result to_chars(char* __first, char* __last, long double __value);
  704. _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
  705. to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt);
  706. _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
  707. to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt);
  708. _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
  709. to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt);
  710. _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
  711. to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt, int __precision);
  712. _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
  713. to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt, int __precision);
  714. _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
  715. to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt, int __precision);
  716. #endif // _LIBCPP_STD_VER > 14
  717. _LIBCPP_END_NAMESPACE_STD
  718. _LIBCPP_POP_MACROS
  719. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  720. # include <concepts>
  721. # include <iosfwd>
  722. #endif
  723. #endif // _LIBCPP_CHARCONV