charconv 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. // Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
  2. // Copyright (C) 2017-2022 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 <bits/charconv.h> // for __to_chars_len, __to_chars_10_impl
  34. #include <bits/error_constants.h> // for std::errc
  35. #include <ext/numeric_traits.h>
  36. #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \
  37. && __SIZE_WIDTH__ >= 32
  38. # define __cpp_lib_to_chars 201611L
  39. #endif
  40. namespace std _GLIBCXX_VISIBILITY(default)
  41. {
  42. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  43. /// Result type of std::to_chars
  44. struct to_chars_result
  45. {
  46. char* ptr;
  47. errc ec;
  48. #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
  49. friend bool
  50. operator==(const to_chars_result&, const to_chars_result&) = default;
  51. #endif
  52. };
  53. /// Result type of std::from_chars
  54. struct from_chars_result
  55. {
  56. const char* ptr;
  57. errc ec;
  58. #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
  59. friend bool
  60. operator==(const from_chars_result&, const from_chars_result&) = default;
  61. #endif
  62. };
  63. namespace __detail
  64. {
  65. template<typename _Tp>
  66. using __integer_to_chars_result_type
  67. = enable_if_t<__or_<__is_signed_integer<_Tp>,
  68. __is_unsigned_integer<_Tp>,
  69. is_same<char, remove_cv_t<_Tp>>>::value,
  70. to_chars_result>;
  71. // Pick an unsigned type of suitable size. This is used to reduce the
  72. // number of specializations of __to_chars_len, __to_chars etc. that
  73. // get instantiated. For example, to_chars<char> and to_chars<short>
  74. // and to_chars<unsigned> will all use the same code, and so will
  75. // to_chars<long> when sizeof(int) == sizeof(long).
  76. template<typename _Tp>
  77. struct __to_chars_unsigned_type : __make_unsigned_selector_base
  78. {
  79. using _UInts = _List<unsigned int, unsigned long, unsigned long long
  80. #if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
  81. , unsigned __int128
  82. #endif
  83. >;
  84. using type = typename __select<sizeof(_Tp), _UInts>::__type;
  85. };
  86. template<typename _Tp>
  87. using __unsigned_least_t = typename __to_chars_unsigned_type<_Tp>::type;
  88. // Generic implementation for arbitrary bases.
  89. // Defined in <bits/charconv.h>.
  90. template<typename _Tp>
  91. constexpr unsigned
  92. __to_chars_len(_Tp __value, int __base /* = 10 */) noexcept;
  93. template<typename _Tp>
  94. constexpr unsigned
  95. __to_chars_len_2(_Tp __value) noexcept
  96. { return std::__bit_width(__value); }
  97. // Generic implementation for arbitrary bases.
  98. template<typename _Tp>
  99. to_chars_result
  100. __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
  101. {
  102. static_assert(is_integral<_Tp>::value, "implementation bug");
  103. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  104. to_chars_result __res;
  105. const unsigned __len = __to_chars_len(__val, __base);
  106. if (__builtin_expect((__last - __first) < __len, 0))
  107. {
  108. __res.ptr = __last;
  109. __res.ec = errc::value_too_large;
  110. return __res;
  111. }
  112. unsigned __pos = __len - 1;
  113. static constexpr char __digits[] = {
  114. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  115. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  116. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  117. 'u', 'v', 'w', 'x', 'y', 'z'
  118. };
  119. while (__val >= (unsigned)__base)
  120. {
  121. auto const __quo = __val / __base;
  122. auto const __rem = __val % __base;
  123. __first[__pos--] = __digits[__rem];
  124. __val = __quo;
  125. }
  126. *__first = __digits[__val];
  127. __res.ptr = __first + __len;
  128. __res.ec = {};
  129. return __res;
  130. }
  131. template<typename _Tp>
  132. __integer_to_chars_result_type<_Tp>
  133. __to_chars_16(char* __first, char* __last, _Tp __val) noexcept
  134. {
  135. static_assert(is_integral<_Tp>::value, "implementation bug");
  136. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  137. to_chars_result __res;
  138. const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
  139. if (__builtin_expect((__last - __first) < __len, 0))
  140. {
  141. __res.ptr = __last;
  142. __res.ec = errc::value_too_large;
  143. return __res;
  144. }
  145. static constexpr char __digits[] = {
  146. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  147. 'a', 'b', 'c', 'd', 'e', 'f'
  148. };
  149. unsigned __pos = __len - 1;
  150. while (__val >= 0x100)
  151. {
  152. auto __num = __val & 0xF;
  153. __val >>= 4;
  154. __first[__pos] = __digits[__num];
  155. __num = __val & 0xF;
  156. __val >>= 4;
  157. __first[__pos - 1] = __digits[__num];
  158. __pos -= 2;
  159. }
  160. if (__val >= 0x10)
  161. {
  162. const auto __num = __val & 0xF;
  163. __val >>= 4;
  164. __first[1] = __digits[__num];
  165. __first[0] = __digits[__val];
  166. }
  167. else
  168. __first[0] = __digits[__val];
  169. __res.ptr = __first + __len;
  170. __res.ec = {};
  171. return __res;
  172. }
  173. template<typename _Tp>
  174. inline __integer_to_chars_result_type<_Tp>
  175. __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
  176. {
  177. static_assert(is_integral<_Tp>::value, "implementation bug");
  178. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  179. to_chars_result __res;
  180. const unsigned __len = __to_chars_len(__val, 10);
  181. if (__builtin_expect((__last - __first) < __len, 0))
  182. {
  183. __res.ptr = __last;
  184. __res.ec = errc::value_too_large;
  185. return __res;
  186. }
  187. __detail::__to_chars_10_impl(__first, __len, __val);
  188. __res.ptr = __first + __len;
  189. __res.ec = {};
  190. return __res;
  191. }
  192. template<typename _Tp>
  193. __integer_to_chars_result_type<_Tp>
  194. __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
  195. {
  196. static_assert(is_integral<_Tp>::value, "implementation bug");
  197. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  198. to_chars_result __res;
  199. unsigned __len;
  200. if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Tp>::__digits <= 16)
  201. {
  202. __len = __val > 077777u ? 6u
  203. : __val > 07777u ? 5u
  204. : __val > 0777u ? 4u
  205. : __val > 077u ? 3u
  206. : __val > 07u ? 2u
  207. : 1u;
  208. }
  209. else
  210. __len = (__to_chars_len_2(__val) + 2) / 3;
  211. if (__builtin_expect((__last - __first) < __len, 0))
  212. {
  213. __res.ptr = __last;
  214. __res.ec = errc::value_too_large;
  215. return __res;
  216. }
  217. unsigned __pos = __len - 1;
  218. while (__val >= 0100)
  219. {
  220. auto __num = __val & 7;
  221. __val >>= 3;
  222. __first[__pos] = '0' + __num;
  223. __num = __val & 7;
  224. __val >>= 3;
  225. __first[__pos - 1] = '0' + __num;
  226. __pos -= 2;
  227. }
  228. if (__val >= 010)
  229. {
  230. auto const __num = __val & 7;
  231. __val >>= 3;
  232. __first[1] = '0' + __num;
  233. __first[0] = '0' + __val;
  234. }
  235. else
  236. __first[0] = '0' + __val;
  237. __res.ptr = __first + __len;
  238. __res.ec = {};
  239. return __res;
  240. }
  241. template<typename _Tp>
  242. __integer_to_chars_result_type<_Tp>
  243. __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
  244. {
  245. static_assert(is_integral<_Tp>::value, "implementation bug");
  246. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  247. to_chars_result __res;
  248. const unsigned __len = __to_chars_len_2(__val);
  249. if (__builtin_expect((__last - __first) < __len, 0))
  250. {
  251. __res.ptr = __last;
  252. __res.ec = errc::value_too_large;
  253. return __res;
  254. }
  255. unsigned __pos = __len - 1;
  256. while (__pos)
  257. {
  258. __first[__pos--] = '0' + (__val & 1);
  259. __val >>= 1;
  260. }
  261. // First digit is always '1' because __to_chars_len_2 skips
  262. // leading zero bits and std::to_chars handles zero values
  263. // directly.
  264. __first[0] = '1';
  265. __res.ptr = __first + __len;
  266. __res.ec = {};
  267. return __res;
  268. }
  269. } // namespace __detail
  270. template<typename _Tp>
  271. __detail::__integer_to_chars_result_type<_Tp>
  272. __to_chars_i(char* __first, char* __last, _Tp __value, int __base = 10)
  273. {
  274. __glibcxx_assert(2 <= __base && __base <= 36);
  275. using _Up = __detail::__unsigned_least_t<_Tp>;
  276. _Up __unsigned_val = __value;
  277. if (__first == __last) [[__unlikely__]]
  278. return { __last, errc::value_too_large };
  279. if (__value == 0)
  280. {
  281. *__first = '0';
  282. return { __first + 1, errc{} };
  283. }
  284. else if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
  285. if (__value < 0)
  286. {
  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 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 127;
  488. }
  489. /// std::from_chars implementation for integers in bases 11 to 36.
  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. char __c = *__first;
  499. if ('0' <= __c && __c <= '9') // isdigit
  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. #if defined __cpp_lib_to_chars || _GLIBCXX_HAVE_USELOCALE
  606. from_chars_result
  607. from_chars(const char* __first, const char* __last, float& __value,
  608. chars_format __fmt = chars_format::general) noexcept;
  609. from_chars_result
  610. from_chars(const char* __first, const char* __last, double& __value,
  611. chars_format __fmt = chars_format::general) noexcept;
  612. from_chars_result
  613. from_chars(const char* __first, const char* __last, long double& __value,
  614. chars_format __fmt = chars_format::general) noexcept;
  615. #endif
  616. #if defined __cpp_lib_to_chars
  617. // Floating-point std::to_chars
  618. // Overloads for float.
  619. to_chars_result to_chars(char* __first, char* __last, float __value) noexcept;
  620. to_chars_result to_chars(char* __first, char* __last, float __value,
  621. chars_format __fmt) noexcept;
  622. to_chars_result to_chars(char* __first, char* __last, float __value,
  623. chars_format __fmt, int __precision) noexcept;
  624. // Overloads for double.
  625. to_chars_result to_chars(char* __first, char* __last, double __value) noexcept;
  626. to_chars_result to_chars(char* __first, char* __last, double __value,
  627. chars_format __fmt) noexcept;
  628. to_chars_result to_chars(char* __first, char* __last, double __value,
  629. chars_format __fmt, int __precision) noexcept;
  630. // Overloads for long double.
  631. to_chars_result to_chars(char* __first, char* __last, long double __value)
  632. noexcept;
  633. to_chars_result to_chars(char* __first, char* __last, long double __value,
  634. chars_format __fmt) noexcept;
  635. to_chars_result to_chars(char* __first, char* __last, long double __value,
  636. chars_format __fmt, int __precision) noexcept;
  637. #endif
  638. _GLIBCXX_END_NAMESPACE_VERSION
  639. } // namespace std
  640. #endif // C++14
  641. #endif // _GLIBCXX_CHARCONV