charconv 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. // Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
  2. // Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/charconv
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_CHARCONV
  24. #define _GLIBCXX_CHARCONV 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201402L
  27. #include <type_traits>
  28. #include <limits>
  29. #include <cctype>
  30. #include <bits/error_constants.h> // for std::errc
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. /// Result type of std::to_chars
  35. struct to_chars_result
  36. {
  37. char* ptr;
  38. errc ec;
  39. };
  40. /// Result type of std::from_chars
  41. struct from_chars_result
  42. {
  43. const char* ptr;
  44. errc ec;
  45. };
  46. namespace __detail
  47. {
  48. template<typename _Tp, typename... _Types>
  49. using __is_one_of = __or_<is_same<_Tp, _Types>...>;
  50. template<typename _Tp>
  51. using __is_int_to_chars_type = __and_<is_integral<_Tp>,
  52. __not_<__is_one_of<_Tp, bool, char16_t, char32_t
  53. #if _GLIBCXX_USE_WCHAR_T
  54. , wchar_t
  55. #endif
  56. #if _GLIBCXX_USE_CHAR8_T
  57. , char8_t
  58. #endif
  59. >>>;
  60. template<typename _Tp>
  61. using __integer_to_chars_result_type
  62. = enable_if_t<__is_int_to_chars_type<_Tp>::value, to_chars_result>;
  63. template<typename _Tp>
  64. using __unsigned_least_t
  65. = conditional_t<(sizeof(_Tp) <= sizeof(int)), unsigned int,
  66. conditional_t<(sizeof(_Tp) <= sizeof(long)), unsigned long,
  67. conditional_t<(sizeof(_Tp) <= sizeof(long long)), unsigned long long,
  68. #if _GLIBCXX_USE_INT128
  69. conditional_t<(sizeof(_Tp) <= sizeof(__int128)), unsigned __int128,
  70. #endif
  71. void
  72. #if _GLIBCXX_USE_INT128
  73. >
  74. #endif
  75. >>>;
  76. // Generic implementation for arbitrary bases.
  77. template<typename _Tp>
  78. constexpr unsigned
  79. __to_chars_len(_Tp __value, int __base = 10) noexcept
  80. {
  81. static_assert(is_integral<_Tp>::value, "implementation bug");
  82. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  83. unsigned __n = 1;
  84. const int __b2 = __base * __base;
  85. const int __b3 = __b2 * __base;
  86. const int __b4 = __b3 * __base;
  87. for (;;)
  88. {
  89. if (__value < __base) return __n;
  90. if (__value < __b2) return __n + 1;
  91. if (__value < __b3) return __n + 2;
  92. if (__value < __b4) return __n + 3;
  93. __value /= (unsigned)__b4;
  94. __n += 4;
  95. }
  96. }
  97. template<typename _Tp>
  98. constexpr unsigned
  99. __to_chars_len_2(_Tp __value) noexcept
  100. {
  101. static_assert(is_integral<_Tp>::value, "implementation bug");
  102. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  103. constexpr size_t __nbits = __CHAR_BIT__ * sizeof(_Tp);
  104. // N.B. __builtin_clzll is undefined if __value == 0, but std::to_chars
  105. // handles zero values directly.
  106. // For sizeof(_Tp) > 1 this is an order of magnitude faster than
  107. // the generic __to_chars_len.
  108. return __nbits
  109. - (__builtin_clzll(__value)
  110. - ((__CHAR_BIT__ * sizeof(long long)) - __nbits));
  111. }
  112. template<typename _Tp>
  113. constexpr unsigned
  114. __to_chars_len_8(_Tp __value) noexcept
  115. {
  116. static_assert(is_integral<_Tp>::value, "implementation bug");
  117. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  118. constexpr size_t __nbits = __CHAR_BIT__ * sizeof(_Tp);
  119. if _GLIBCXX17_CONSTEXPR (__nbits <= 16)
  120. {
  121. return __value > 077777u ? 6u
  122. : __value > 07777u ? 5u
  123. : __value > 0777u ? 4u
  124. : __value > 077u ? 3u
  125. : __value > 07u ? 2u
  126. : 1u;
  127. }
  128. else
  129. return __to_chars_len(__value, 8);
  130. }
  131. // Generic implementation for arbitrary bases.
  132. template<typename _Tp>
  133. to_chars_result
  134. __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
  135. {
  136. static_assert(is_integral<_Tp>::value, "implementation bug");
  137. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  138. to_chars_result __res;
  139. const unsigned __len = __to_chars_len(__val, __base);
  140. if (__builtin_expect((__last - __first) < __len, 0))
  141. {
  142. __res.ptr = __last;
  143. __res.ec = errc::value_too_large;
  144. return __res;
  145. }
  146. unsigned __pos = __len - 1;
  147. static constexpr char __digits[]
  148. = "0123456789abcdefghijklmnopqrstuvwxyz";
  149. while (__val >= __base)
  150. {
  151. auto const __quo = __val / __base;
  152. auto const __rem = __val % __base;
  153. __first[__pos--] = __digits[__rem];
  154. __val = __quo;
  155. }
  156. *__first = __digits[__val];
  157. __res.ptr = __first + __len;
  158. __res.ec = {};
  159. return __res;
  160. }
  161. template<typename _Tp>
  162. __integer_to_chars_result_type<_Tp>
  163. __to_chars_16(char* __first, char* __last, _Tp __val) noexcept
  164. {
  165. static_assert(is_integral<_Tp>::value, "implementation bug");
  166. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  167. to_chars_result __res;
  168. const unsigned __len = __to_chars_len(__val, 0x10);
  169. if (__builtin_expect((__last - __first) < __len, 0))
  170. {
  171. __res.ptr = __last;
  172. __res.ec = errc::value_too_large;
  173. return __res;
  174. }
  175. static constexpr char __digits[513] =
  176. "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
  177. "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"
  178. "404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"
  179. "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f"
  180. "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f"
  181. "a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
  182. "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
  183. "e0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
  184. unsigned __pos = __len - 1;
  185. while (__val >= 0x100)
  186. {
  187. auto const __num = (__val % 0x100) * 2;
  188. __val /= 0x100;
  189. __first[__pos] = __digits[__num + 1];
  190. __first[__pos - 1] = __digits[__num];
  191. __pos -= 2;
  192. }
  193. if (__val >= 0x10)
  194. {
  195. auto const __num = __val * 2;
  196. __first[__pos] = __digits[__num + 1];
  197. __first[__pos - 1] = __digits[__num];
  198. }
  199. else
  200. __first[__pos] = "0123456789abcdef"[__val];
  201. __res.ptr = __first + __len;
  202. __res.ec = {};
  203. return __res;
  204. }
  205. template<typename _Tp>
  206. __integer_to_chars_result_type<_Tp>
  207. __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
  208. {
  209. static_assert(is_integral<_Tp>::value, "implementation bug");
  210. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  211. to_chars_result __res;
  212. const unsigned __len = __to_chars_len(__val, 10);
  213. if (__builtin_expect((__last - __first) < __len, 0))
  214. {
  215. __res.ptr = __last;
  216. __res.ec = errc::value_too_large;
  217. return __res;
  218. }
  219. static constexpr char __digits[201] =
  220. "0001020304050607080910111213141516171819"
  221. "2021222324252627282930313233343536373839"
  222. "4041424344454647484950515253545556575859"
  223. "6061626364656667686970717273747576777879"
  224. "8081828384858687888990919293949596979899";
  225. unsigned __pos = __len - 1;
  226. while (__val >= 100)
  227. {
  228. auto const __num = (__val % 100) * 2;
  229. __val /= 100;
  230. __first[__pos] = __digits[__num + 1];
  231. __first[__pos - 1] = __digits[__num];
  232. __pos -= 2;
  233. }
  234. if (__val >= 10)
  235. {
  236. auto const __num = __val * 2;
  237. __first[__pos] = __digits[__num + 1];
  238. __first[__pos - 1] = __digits[__num];
  239. }
  240. else
  241. __first[__pos] = '0' + __val;
  242. __res.ptr = __first + __len;
  243. __res.ec = {};
  244. return __res;
  245. }
  246. template<typename _Tp>
  247. __integer_to_chars_result_type<_Tp>
  248. __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
  249. {
  250. static_assert(is_integral<_Tp>::value, "implementation bug");
  251. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  252. to_chars_result __res;
  253. const unsigned __len = __to_chars_len_8(__val);
  254. if (__builtin_expect((__last - __first) < __len, 0))
  255. {
  256. __res.ptr = __last;
  257. __res.ec = errc::value_too_large;
  258. return __res;
  259. }
  260. static constexpr char __digits[129] =
  261. "00010203040506071011121314151617"
  262. "20212223242526273031323334353637"
  263. "40414243444546475051525354555657"
  264. "60616263646566677071727374757677";
  265. unsigned __pos = __len - 1;
  266. while (__val >= 0100)
  267. {
  268. auto const __num = (__val % 0100) * 2;
  269. __val /= 0100;
  270. __first[__pos] = __digits[__num + 1];
  271. __first[__pos - 1] = __digits[__num];
  272. __pos -= 2;
  273. }
  274. if (__val >= 010)
  275. {
  276. auto const __num = __val * 2;
  277. __first[__pos] = __digits[__num + 1];
  278. __first[__pos - 1] = __digits[__num];
  279. }
  280. else
  281. __first[__pos] = '0' + __val;
  282. __res.ptr = __first + __len;
  283. __res.ec = {};
  284. return __res;
  285. }
  286. template<typename _Tp>
  287. __integer_to_chars_result_type<_Tp>
  288. __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
  289. {
  290. static_assert(is_integral<_Tp>::value, "implementation bug");
  291. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  292. to_chars_result __res;
  293. const unsigned __len = __to_chars_len_2(__val);
  294. if (__builtin_expect((__last - __first) < __len, 0))
  295. {
  296. __res.ptr = __last;
  297. __res.ec = errc::value_too_large;
  298. return __res;
  299. }
  300. unsigned __pos = __len - 1;
  301. while (__pos)
  302. {
  303. __first[__pos--] = '0' + (__val & 1);
  304. __val >>= 1;
  305. }
  306. *__first = '0' + (__val & 1);
  307. __res.ptr = __first + __len;
  308. __res.ec = {};
  309. return __res;
  310. }
  311. } // namespace __detail
  312. template<typename _Tp>
  313. __detail::__integer_to_chars_result_type<_Tp>
  314. to_chars(char* __first, char* __last, _Tp __value, int __base = 10)
  315. {
  316. __glibcxx_assert(2 <= __base && __base <= 36);
  317. using _Up = __detail::__unsigned_least_t<_Tp>;
  318. _Up __unsigned_val = __value;
  319. if (__value == 0 && __first != __last)
  320. {
  321. *__first = '0';
  322. return { __first + 1, errc{} };
  323. }
  324. if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
  325. if (__value < 0)
  326. {
  327. if (__builtin_expect(__first != __last, 1))
  328. *__first++ = '-';
  329. __unsigned_val = _Up(~__value) + _Up(1);
  330. }
  331. switch (__base)
  332. {
  333. case 16:
  334. return __detail::__to_chars_16(__first, __last, __unsigned_val);
  335. case 10:
  336. return __detail::__to_chars_10(__first, __last, __unsigned_val);
  337. case 8:
  338. return __detail::__to_chars_8(__first, __last, __unsigned_val);
  339. case 2:
  340. return __detail::__to_chars_2(__first, __last, __unsigned_val);
  341. default:
  342. return __detail::__to_chars(__first, __last, __unsigned_val, __base);
  343. }
  344. }
  345. namespace __detail
  346. {
  347. template<typename _Tp>
  348. bool
  349. __raise_and_add(_Tp& __val, int __base, unsigned char __c)
  350. {
  351. if (__builtin_mul_overflow(__val, __base, &__val)
  352. || __builtin_add_overflow(__val, __c, &__val))
  353. return false;
  354. return true;
  355. }
  356. /// std::from_chars implementation for integers in base 2.
  357. template<typename _Tp>
  358. bool
  359. __from_chars_binary(const char*& __first, const char* __last, _Tp& __val)
  360. {
  361. static_assert(is_integral<_Tp>::value, "implementation bug");
  362. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  363. const ptrdiff_t __len = __last - __first;
  364. int __i = 0;
  365. while (__i < __len)
  366. {
  367. const unsigned char __c = (unsigned)__first[__i] - '0';
  368. if (__c < 2)
  369. __val = (__val << 1) | __c;
  370. else
  371. break;
  372. __i++;
  373. }
  374. __first += __i;
  375. return __i <= (sizeof(_Tp) * __CHAR_BIT__);
  376. }
  377. /// std::from_chars implementation for integers in bases 3 to 10.
  378. template<typename _Tp>
  379. bool
  380. __from_chars_digit(const char*& __first, const char* __last, _Tp& __val,
  381. int __base)
  382. {
  383. static_assert(is_integral<_Tp>::value, "implementation bug");
  384. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  385. auto __matches = [__base](char __c) {
  386. return '0' <= __c && __c <= ('0' + (__base - 1));
  387. };
  388. while (__first != __last)
  389. {
  390. const char __c = *__first;
  391. if (__matches(__c))
  392. {
  393. if (!__raise_and_add(__val, __base, __c - '0'))
  394. {
  395. while (++__first != __last && __matches(*__first))
  396. ;
  397. return false;
  398. }
  399. __first++;
  400. }
  401. else
  402. return true;
  403. }
  404. return true;
  405. }
  406. constexpr unsigned char
  407. __from_chars_alpha_to_num(char __c)
  408. {
  409. switch (__c)
  410. {
  411. case 'a':
  412. case 'A':
  413. return 10;
  414. case 'b':
  415. case 'B':
  416. return 11;
  417. case 'c':
  418. case 'C':
  419. return 12;
  420. case 'd':
  421. case 'D':
  422. return 13;
  423. case 'e':
  424. case 'E':
  425. return 14;
  426. case 'f':
  427. case 'F':
  428. return 15;
  429. case 'g':
  430. case 'G':
  431. return 16;
  432. case 'h':
  433. case 'H':
  434. return 17;
  435. case 'i':
  436. case 'I':
  437. return 18;
  438. case 'j':
  439. case 'J':
  440. return 19;
  441. case 'k':
  442. case 'K':
  443. return 20;
  444. case 'l':
  445. case 'L':
  446. return 21;
  447. case 'm':
  448. case 'M':
  449. return 22;
  450. case 'n':
  451. case 'N':
  452. return 23;
  453. case 'o':
  454. case 'O':
  455. return 24;
  456. case 'p':
  457. case 'P':
  458. return 25;
  459. case 'q':
  460. case 'Q':
  461. return 26;
  462. case 'r':
  463. case 'R':
  464. return 27;
  465. case 's':
  466. case 'S':
  467. return 28;
  468. case 't':
  469. case 'T':
  470. return 29;
  471. case 'u':
  472. case 'U':
  473. return 30;
  474. case 'v':
  475. case 'V':
  476. return 31;
  477. case 'w':
  478. case 'W':
  479. return 32;
  480. case 'x':
  481. case 'X':
  482. return 33;
  483. case 'y':
  484. case 'Y':
  485. return 34;
  486. case 'z':
  487. case 'Z':
  488. return 35;
  489. }
  490. return std::numeric_limits<unsigned char>::max();
  491. }
  492. /// std::from_chars implementation for integers in bases 11 to 26.
  493. template<typename _Tp>
  494. bool
  495. __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
  496. int __base)
  497. {
  498. bool __valid = true;
  499. while (__first != __last)
  500. {
  501. unsigned char __c = *__first;
  502. if (std::isdigit(__c))
  503. __c -= '0';
  504. else
  505. {
  506. __c = __from_chars_alpha_to_num(__c);
  507. if (__c >= __base)
  508. break;
  509. }
  510. if (__builtin_expect(__valid, 1))
  511. __valid = __raise_and_add(__val, __base, __c);
  512. __first++;
  513. }
  514. return __valid;
  515. }
  516. template<typename _Tp>
  517. using __integer_from_chars_result_type
  518. = enable_if_t<__is_int_to_chars_type<_Tp>::value, from_chars_result>;
  519. } // namespace __detail
  520. /// std::from_chars for integral types.
  521. template<typename _Tp>
  522. __detail::__integer_from_chars_result_type<_Tp>
  523. from_chars(const char* __first, const char* __last, _Tp& __value,
  524. int __base = 10)
  525. {
  526. __glibcxx_assert(2 <= __base && __base <= 36);
  527. from_chars_result __res{__first, {}};
  528. int __sign = 1;
  529. if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
  530. if (__first != __last && *__first == '-')
  531. {
  532. __sign = -1;
  533. ++__first;
  534. }
  535. using _Up = __detail::__unsigned_least_t<_Tp>;
  536. _Up __val = 0;
  537. const auto __start = __first;
  538. bool __valid;
  539. if (__base == 2)
  540. __valid = __detail::__from_chars_binary(__first, __last, __val);
  541. else if (__base <= 10)
  542. __valid = __detail::__from_chars_digit(__first, __last, __val, __base);
  543. else
  544. __valid = __detail::__from_chars_alnum(__first, __last, __val, __base);
  545. if (__builtin_expect(__first == __start, 0))
  546. __res.ec = errc::invalid_argument;
  547. else
  548. {
  549. __res.ptr = __first;
  550. if (!__valid)
  551. __res.ec = errc::result_out_of_range;
  552. else
  553. {
  554. if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
  555. {
  556. _Tp __tmp;
  557. if (__builtin_mul_overflow(__val, __sign, &__tmp))
  558. __res.ec = errc::result_out_of_range;
  559. else
  560. __value = __tmp;
  561. }
  562. else
  563. {
  564. if _GLIBCXX17_CONSTEXPR
  565. (numeric_limits<_Up>::max() > numeric_limits<_Tp>::max())
  566. {
  567. if (__val > numeric_limits<_Tp>::max())
  568. __res.ec = errc::result_out_of_range;
  569. else
  570. __value = __val;
  571. }
  572. else
  573. __value = __val;
  574. }
  575. }
  576. }
  577. return __res;
  578. }
  579. _GLIBCXX_END_NAMESPACE_VERSION
  580. } // namespace std
  581. #endif // C++14
  582. #endif // _GLIBCXX_CHARCONV