charconv 16 KB

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