You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

695 lines
18KB

  1. // Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
  2. // Copyright (C) 2017-2020 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. // As an extension we support <charconv> in C++14, but this header should not
  27. // be included by any other library headers in C++14 mode. This ensures that
  28. // the names defined in this header are not added to namespace std unless a
  29. // user explicitly includes <charconv> in C++14 code.
  30. #if __cplusplus >= 201402L
  31. #include <type_traits>
  32. #include <bit> // for __bit_width
  33. #include <cctype> // for isdigit
  34. #include <bits/charconv.h> // for __to_chars_len, __to_chars_10_impl
  35. #include <bits/error_constants.h> // for std::errc
  36. #include <ext/numeric_traits.h>
  37. // FIXME: Define when floating point is supported:
  38. // #define __cpp_lib_to_chars 201611L
  39. namespace std _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. /// Result type of std::to_chars
  43. struct to_chars_result
  44. {
  45. char* ptr;
  46. errc ec;
  47. #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
  48. friend bool
  49. operator==(const to_chars_result&, const to_chars_result&) = default;
  50. #endif
  51. };
  52. /// Result type of std::from_chars
  53. struct from_chars_result
  54. {
  55. const char* ptr;
  56. errc ec;
  57. #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
  58. friend bool
  59. operator==(const from_chars_result&, const from_chars_result&) = default;
  60. #endif
  61. };
  62. namespace __detail
  63. {
  64. template<typename _Tp>
  65. using __integer_to_chars_result_type
  66. = enable_if_t<__or_<__is_signed_integer<_Tp>,
  67. __is_unsigned_integer<_Tp>,
  68. is_same<char, remove_cv_t<_Tp>>>::value,
  69. to_chars_result>;
  70. // Pick an unsigned type of suitable size. This is used to reduce the
  71. // number of specializations of __to_chars_len, __to_chars etc. that
  72. // get instantiated. For example, to_chars<char> and to_chars<short>
  73. // and to_chars<unsigned> will all use the same code, and so will
  74. // to_chars<long> when sizeof(int) == sizeof(long).
  75. template<typename _Tp>
  76. struct __to_chars_unsigned_type : __make_unsigned_selector_base
  77. {
  78. using _UInts = _List<unsigned int, unsigned long, unsigned long long
  79. #if _GLIBCXX_USE_INT128
  80. , unsigned __int128
  81. #endif
  82. >;
  83. using type = typename __select<sizeof(_Tp), _UInts>::__type;
  84. };
  85. template<typename _Tp>
  86. using __unsigned_least_t = typename __to_chars_unsigned_type<_Tp>::type;
  87. // Generic implementation for arbitrary bases.
  88. // Defined in <bits/charconv.h>.
  89. template<typename _Tp>
  90. constexpr unsigned
  91. __to_chars_len(_Tp __value, int __base /* = 10 */) noexcept;
  92. template<typename _Tp>
  93. constexpr unsigned
  94. __to_chars_len_2(_Tp __value) noexcept
  95. { return std::__bit_width(__value); }
  96. // Generic implementation for arbitrary bases.
  97. template<typename _Tp>
  98. to_chars_result
  99. __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
  100. {
  101. static_assert(is_integral<_Tp>::value, "implementation bug");
  102. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  103. to_chars_result __res;
  104. const unsigned __len = __to_chars_len(__val, __base);
  105. if (__builtin_expect((__last - __first) < __len, 0))
  106. {
  107. __res.ptr = __last;
  108. __res.ec = errc::value_too_large;
  109. return __res;
  110. }
  111. unsigned __pos = __len - 1;
  112. static constexpr char __digits[] = {
  113. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  114. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  115. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  116. 'u', 'v', 'w', 'x', 'y', 'z'
  117. };
  118. while (__val >= (unsigned)__base)
  119. {
  120. auto const __quo = __val / __base;
  121. auto const __rem = __val % __base;
  122. __first[__pos--] = __digits[__rem];
  123. __val = __quo;
  124. }
  125. *__first = __digits[__val];
  126. __res.ptr = __first + __len;
  127. __res.ec = {};
  128. return __res;
  129. }
  130. template<typename _Tp>
  131. __integer_to_chars_result_type<_Tp>
  132. __to_chars_16(char* __first, char* __last, _Tp __val) noexcept
  133. {
  134. static_assert(is_integral<_Tp>::value, "implementation bug");
  135. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  136. to_chars_result __res;
  137. const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
  138. if (__builtin_expect((__last - __first) < __len, 0))
  139. {
  140. __res.ptr = __last;
  141. __res.ec = errc::value_too_large;
  142. return __res;
  143. }
  144. static constexpr char __digits[] = {
  145. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  146. 'a', 'b', 'c', 'd', 'e', 'f'
  147. };
  148. unsigned __pos = __len - 1;
  149. while (__val >= 0x100)
  150. {
  151. auto __num = __val & 0xF;
  152. __val >>= 4;
  153. __first[__pos] = __digits[__num];
  154. __num = __val & 0xF;
  155. __val >>= 4;
  156. __first[__pos - 1] = __digits[__num];
  157. __pos -= 2;
  158. }
  159. if (__val >= 0x10)
  160. {
  161. const auto __num = __val & 0xF;
  162. __val >>= 4;
  163. __first[1] = __digits[__num];
  164. __first[0] = __digits[__val];
  165. }
  166. else
  167. __first[0] = __digits[__val];
  168. __res.ptr = __first + __len;
  169. __res.ec = {};
  170. return __res;
  171. }
  172. template<typename _Tp>
  173. inline __integer_to_chars_result_type<_Tp>
  174. __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
  175. {
  176. static_assert(is_integral<_Tp>::value, "implementation bug");
  177. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  178. to_chars_result __res;
  179. const unsigned __len = __to_chars_len(__val, 10);
  180. if (__builtin_expect((__last - __first) < __len, 0))
  181. {
  182. __res.ptr = __last;
  183. __res.ec = errc::value_too_large;
  184. return __res;
  185. }
  186. __detail::__to_chars_10_impl(__first, __len, __val);
  187. __res.ptr = __first + __len;
  188. __res.ec = {};
  189. return __res;
  190. }
  191. template<typename _Tp>
  192. __integer_to_chars_result_type<_Tp>
  193. __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
  194. {
  195. static_assert(is_integral<_Tp>::value, "implementation bug");
  196. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  197. to_chars_result __res;
  198. unsigned __len;
  199. if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Tp>::__digits <= 16)
  200. {
  201. __len = __val > 077777u ? 6u
  202. : __val > 07777u ? 5u
  203. : __val > 0777u ? 4u
  204. : __val > 077u ? 3u
  205. : __val > 07u ? 2u
  206. : 1u;
  207. }
  208. else
  209. __len = (__to_chars_len_2(__val) + 2) / 3;
  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. unsigned __pos = __len - 1;
  217. while (__val >= 0100)
  218. {
  219. auto __num = __val & 7;
  220. __val >>= 3;
  221. __first[__pos] = '0' + __num;
  222. __num = __val & 7;
  223. __val >>= 3;
  224. __first[__pos - 1] = '0' + __num;
  225. __pos -= 2;
  226. }
  227. if (__val >= 010)
  228. {
  229. auto const __num = __val & 7;
  230. __val >>= 3;
  231. __first[1] = '0' + __num;
  232. __first[0] = '0' + __val;
  233. }
  234. else
  235. __first[0] = '0' + __val;
  236. __res.ptr = __first + __len;
  237. __res.ec = {};
  238. return __res;
  239. }
  240. template<typename _Tp>
  241. __integer_to_chars_result_type<_Tp>
  242. __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
  243. {
  244. static_assert(is_integral<_Tp>::value, "implementation bug");
  245. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  246. to_chars_result __res;
  247. const unsigned __len = __to_chars_len_2(__val);
  248. if (__builtin_expect((__last - __first) < __len, 0))
  249. {
  250. __res.ptr = __last;
  251. __res.ec = errc::value_too_large;
  252. return __res;
  253. }
  254. unsigned __pos = __len - 1;
  255. while (__pos)
  256. {
  257. __first[__pos--] = '0' + (__val & 1);
  258. __val >>= 1;
  259. }
  260. // First digit is always '1' because __to_chars_len_2 skips
  261. // leading zero bits and std::to_chars handles zero values
  262. // directly.
  263. __first[0] = '1';
  264. __res.ptr = __first + __len;
  265. __res.ec = {};
  266. return __res;
  267. }
  268. } // namespace __detail
  269. template<typename _Tp>
  270. __detail::__integer_to_chars_result_type<_Tp>
  271. __to_chars_i(char* __first, char* __last, _Tp __value, int __base = 10)
  272. {
  273. __glibcxx_assert(2 <= __base && __base <= 36);
  274. using _Up = __detail::__unsigned_least_t<_Tp>;
  275. _Up __unsigned_val = __value;
  276. if (__first == __last) [[__unlikely__]]
  277. return { __last, errc::value_too_large };
  278. if (__value == 0)
  279. {
  280. *__first = '0';
  281. return { __first + 1, errc{} };
  282. }
  283. if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
  284. if (__value < 0)
  285. {
  286. if (__builtin_expect(__first != __last, 1))
  287. *__first++ = '-';
  288. __unsigned_val = _Up(~__value) + _Up(1);
  289. }
  290. switch (__base)
  291. {
  292. case 16:
  293. return __detail::__to_chars_16(__first, __last, __unsigned_val);
  294. case 10:
  295. return __detail::__to_chars_10(__first, __last, __unsigned_val);
  296. case 8:
  297. return __detail::__to_chars_8(__first, __last, __unsigned_val);
  298. case 2:
  299. return __detail::__to_chars_2(__first, __last, __unsigned_val);
  300. default:
  301. return __detail::__to_chars(__first, __last, __unsigned_val, __base);
  302. }
  303. }
  304. #define _GLIBCXX_TO_CHARS(T) \
  305. inline to_chars_result \
  306. to_chars(char* __first, char* __last, T __value, int __base = 10) \
  307. { return std::__to_chars_i<T>(__first, __last, __value, __base); }
  308. _GLIBCXX_TO_CHARS(char)
  309. _GLIBCXX_TO_CHARS(signed char)
  310. _GLIBCXX_TO_CHARS(unsigned char)
  311. _GLIBCXX_TO_CHARS(signed short)
  312. _GLIBCXX_TO_CHARS(unsigned short)
  313. _GLIBCXX_TO_CHARS(signed int)
  314. _GLIBCXX_TO_CHARS(unsigned int)
  315. _GLIBCXX_TO_CHARS(signed long)
  316. _GLIBCXX_TO_CHARS(unsigned long)
  317. _GLIBCXX_TO_CHARS(signed long long)
  318. _GLIBCXX_TO_CHARS(unsigned long long)
  319. #if defined(__GLIBCXX_TYPE_INT_N_0)
  320. _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_0)
  321. _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_0)
  322. #endif
  323. #if defined(__GLIBCXX_TYPE_INT_N_1)
  324. _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_1)
  325. _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_1)
  326. #endif
  327. #if defined(__GLIBCXX_TYPE_INT_N_2)
  328. _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_2)
  329. _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_2)
  330. #endif
  331. #if defined(__GLIBCXX_TYPE_INT_N_3)
  332. _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_3)
  333. _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_3)
  334. #endif
  335. #undef _GLIBCXX_TO_CHARS
  336. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  337. // 3266. to_chars(bool) should be deleted
  338. to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
  339. namespace __detail
  340. {
  341. template<typename _Tp>
  342. bool
  343. __raise_and_add(_Tp& __val, int __base, unsigned char __c)
  344. {
  345. if (__builtin_mul_overflow(__val, __base, &__val)
  346. || __builtin_add_overflow(__val, __c, &__val))
  347. return false;
  348. return true;
  349. }
  350. /// std::from_chars implementation for integers in base 2.
  351. template<typename _Tp>
  352. bool
  353. __from_chars_binary(const char*& __first, const char* __last, _Tp& __val)
  354. {
  355. static_assert(is_integral<_Tp>::value, "implementation bug");
  356. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  357. const ptrdiff_t __len = __last - __first;
  358. ptrdiff_t __i = 0;
  359. while (__i < __len && __first[__i] == '0')
  360. ++__i;
  361. const ptrdiff_t __leading_zeroes = __i;
  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 - __leading_zeroes) <= __gnu_cxx::__int_traits<_Tp>::__digits;
  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 __gnu_cxx::__int_traits<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<__or_<__is_signed_integer<_Tp>,
  516. __is_unsigned_integer<_Tp>,
  517. is_same<char, remove_cv_t<_Tp>>>::value,
  518. 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 (__gnu_cxx::__int_traits<_Up>::__max
  565. > __gnu_cxx::__int_traits<_Tp>::__max)
  566. {
  567. if (__val > __gnu_cxx::__int_traits<_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. /// floating-point format for primitive numerical conversion
  580. enum class chars_format
  581. {
  582. scientific = 1, fixed = 2, hex = 4, general = fixed | scientific
  583. };
  584. constexpr chars_format
  585. operator|(chars_format __lhs, chars_format __rhs) noexcept
  586. { return (chars_format)((unsigned)__lhs | (unsigned)__rhs); }
  587. constexpr chars_format
  588. operator&(chars_format __lhs, chars_format __rhs) noexcept
  589. { return (chars_format)((unsigned)__lhs & (unsigned)__rhs); }
  590. constexpr chars_format
  591. operator^(chars_format __lhs, chars_format __rhs) noexcept
  592. { return (chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
  593. constexpr chars_format
  594. operator~(chars_format __fmt) noexcept
  595. { return (chars_format)~(unsigned)__fmt; }
  596. constexpr chars_format&
  597. operator|=(chars_format& __lhs, chars_format __rhs) noexcept
  598. { return __lhs = __lhs | __rhs; }
  599. constexpr chars_format&
  600. operator&=(chars_format& __lhs, chars_format __rhs) noexcept
  601. { return __lhs = __lhs & __rhs; }
  602. constexpr chars_format&
  603. operator^=(chars_format& __lhs, chars_format __rhs) noexcept
  604. { return __lhs = __lhs ^ __rhs; }
  605. _GLIBCXX_END_NAMESPACE_VERSION
  606. } // namespace std
  607. #endif // C++14
  608. #endif // _GLIBCXX_CHARCONV