charconv 28 KB

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