charconv 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. // Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
  2. // Copyright (C) 2017-2021 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. #if _GLIBCXX_HAVE_USELOCALE
  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 _GLIBCXX_USE_INT128
  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. if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
  285. if (__value < 0)
  286. {
  287. if (__builtin_expect(__first != __last, 1))
  288. *__first++ = '-';
  289. __unsigned_val = _Up(~__value) + _Up(1);
  290. }
  291. switch (__base)
  292. {
  293. case 16:
  294. return __detail::__to_chars_16(__first, __last, __unsigned_val);
  295. case 10:
  296. return __detail::__to_chars_10(__first, __last, __unsigned_val);
  297. case 8:
  298. return __detail::__to_chars_8(__first, __last, __unsigned_val);
  299. case 2:
  300. return __detail::__to_chars_2(__first, __last, __unsigned_val);
  301. default:
  302. return __detail::__to_chars(__first, __last, __unsigned_val, __base);
  303. }
  304. }
  305. #define _GLIBCXX_TO_CHARS(T) \
  306. inline to_chars_result \
  307. to_chars(char* __first, char* __last, T __value, int __base = 10) \
  308. { return std::__to_chars_i<T>(__first, __last, __value, __base); }
  309. _GLIBCXX_TO_CHARS(char)
  310. _GLIBCXX_TO_CHARS(signed char)
  311. _GLIBCXX_TO_CHARS(unsigned char)
  312. _GLIBCXX_TO_CHARS(signed short)
  313. _GLIBCXX_TO_CHARS(unsigned short)
  314. _GLIBCXX_TO_CHARS(signed int)
  315. _GLIBCXX_TO_CHARS(unsigned int)
  316. _GLIBCXX_TO_CHARS(signed long)
  317. _GLIBCXX_TO_CHARS(unsigned long)
  318. _GLIBCXX_TO_CHARS(signed long long)
  319. _GLIBCXX_TO_CHARS(unsigned long long)
  320. #if defined(__GLIBCXX_TYPE_INT_N_0)
  321. _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_0)
  322. _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_0)
  323. #endif
  324. #if defined(__GLIBCXX_TYPE_INT_N_1)
  325. _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_1)
  326. _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_1)
  327. #endif
  328. #if defined(__GLIBCXX_TYPE_INT_N_2)
  329. _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_2)
  330. _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_2)
  331. #endif
  332. #if defined(__GLIBCXX_TYPE_INT_N_3)
  333. _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_3)
  334. _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_3)
  335. #endif
  336. #undef _GLIBCXX_TO_CHARS
  337. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  338. // 3266. to_chars(bool) should be deleted
  339. to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
  340. namespace __detail
  341. {
  342. template<typename _Tp>
  343. bool
  344. __raise_and_add(_Tp& __val, int __base, unsigned char __c)
  345. {
  346. if (__builtin_mul_overflow(__val, __base, &__val)
  347. || __builtin_add_overflow(__val, __c, &__val))
  348. return false;
  349. return true;
  350. }
  351. /// std::from_chars implementation for integers in base 2.
  352. template<typename _Tp>
  353. bool
  354. __from_chars_binary(const char*& __first, const char* __last, _Tp& __val)
  355. {
  356. static_assert(is_integral<_Tp>::value, "implementation bug");
  357. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  358. const ptrdiff_t __len = __last - __first;
  359. ptrdiff_t __i = 0;
  360. while (__i < __len && __first[__i] == '0')
  361. ++__i;
  362. const ptrdiff_t __leading_zeroes = __i;
  363. while (__i < __len)
  364. {
  365. const unsigned char __c = (unsigned)__first[__i] - '0';
  366. if (__c < 2)
  367. __val = (__val << 1) | __c;
  368. else
  369. break;
  370. __i++;
  371. }
  372. __first += __i;
  373. return (__i - __leading_zeroes) <= __gnu_cxx::__int_traits<_Tp>::__digits;
  374. }
  375. /// std::from_chars implementation for integers in bases 3 to 10.
  376. template<typename _Tp>
  377. bool
  378. __from_chars_digit(const char*& __first, const char* __last, _Tp& __val,
  379. int __base)
  380. {
  381. static_assert(is_integral<_Tp>::value, "implementation bug");
  382. static_assert(is_unsigned<_Tp>::value, "implementation bug");
  383. auto __matches = [__base](char __c) {
  384. return '0' <= __c && __c <= ('0' + (__base - 1));
  385. };
  386. while (__first != __last)
  387. {
  388. const char __c = *__first;
  389. if (__matches(__c))
  390. {
  391. if (!__raise_and_add(__val, __base, __c - '0'))
  392. {
  393. while (++__first != __last && __matches(*__first))
  394. ;
  395. return false;
  396. }
  397. __first++;
  398. }
  399. else
  400. return true;
  401. }
  402. return true;
  403. }
  404. constexpr unsigned char
  405. __from_chars_alpha_to_num(char __c)
  406. {
  407. switch (__c)
  408. {
  409. case 'a':
  410. case 'A':
  411. return 10;
  412. case 'b':
  413. case 'B':
  414. return 11;
  415. case 'c':
  416. case 'C':
  417. return 12;
  418. case 'd':
  419. case 'D':
  420. return 13;
  421. case 'e':
  422. case 'E':
  423. return 14;
  424. case 'f':
  425. case 'F':
  426. return 15;
  427. case 'g':
  428. case 'G':
  429. return 16;
  430. case 'h':
  431. case 'H':
  432. return 17;
  433. case 'i':
  434. case 'I':
  435. return 18;
  436. case 'j':
  437. case 'J':
  438. return 19;
  439. case 'k':
  440. case 'K':
  441. return 20;
  442. case 'l':
  443. case 'L':
  444. return 21;
  445. case 'm':
  446. case 'M':
  447. return 22;
  448. case 'n':
  449. case 'N':
  450. return 23;
  451. case 'o':
  452. case 'O':
  453. return 24;
  454. case 'p':
  455. case 'P':
  456. return 25;
  457. case 'q':
  458. case 'Q':
  459. return 26;
  460. case 'r':
  461. case 'R':
  462. return 27;
  463. case 's':
  464. case 'S':
  465. return 28;
  466. case 't':
  467. case 'T':
  468. return 29;
  469. case 'u':
  470. case 'U':
  471. return 30;
  472. case 'v':
  473. case 'V':
  474. return 31;
  475. case 'w':
  476. case 'W':
  477. return 32;
  478. case 'x':
  479. case 'X':
  480. return 33;
  481. case 'y':
  482. case 'Y':
  483. return 34;
  484. case 'z':
  485. case 'Z':
  486. return 35;
  487. }
  488. return __gnu_cxx::__int_traits<unsigned char>::__max;
  489. }
  490. /// std::from_chars implementation for integers in bases 11 to 26.
  491. template<typename _Tp>
  492. bool
  493. __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
  494. int __base)
  495. {
  496. bool __valid = true;
  497. while (__first != __last)
  498. {
  499. unsigned char __c = *__first;
  500. if (std::isdigit(__c))
  501. __c -= '0';
  502. else
  503. {
  504. __c = __from_chars_alpha_to_num(__c);
  505. if (__c >= __base)
  506. break;
  507. }
  508. if (__builtin_expect(__valid, 1))
  509. __valid = __raise_and_add(__val, __base, __c);
  510. __first++;
  511. }
  512. return __valid;
  513. }
  514. template<typename _Tp>
  515. using __integer_from_chars_result_type
  516. = enable_if_t<__or_<__is_signed_integer<_Tp>,
  517. __is_unsigned_integer<_Tp>,
  518. is_same<char, remove_cv_t<_Tp>>>::value,
  519. from_chars_result>;
  520. } // namespace __detail
  521. /// std::from_chars for integral types.
  522. template<typename _Tp>
  523. __detail::__integer_from_chars_result_type<_Tp>
  524. from_chars(const char* __first, const char* __last, _Tp& __value,
  525. int __base = 10)
  526. {
  527. __glibcxx_assert(2 <= __base && __base <= 36);
  528. from_chars_result __res{__first, {}};
  529. int __sign = 1;
  530. if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
  531. if (__first != __last && *__first == '-')
  532. {
  533. __sign = -1;
  534. ++__first;
  535. }
  536. using _Up = __detail::__unsigned_least_t<_Tp>;
  537. _Up __val = 0;
  538. const auto __start = __first;
  539. bool __valid;
  540. if (__base == 2)
  541. __valid = __detail::__from_chars_binary(__first, __last, __val);
  542. else if (__base <= 10)
  543. __valid = __detail::__from_chars_digit(__first, __last, __val, __base);
  544. else
  545. __valid = __detail::__from_chars_alnum(__first, __last, __val, __base);
  546. if (__builtin_expect(__first == __start, 0))
  547. __res.ec = errc::invalid_argument;
  548. else
  549. {
  550. __res.ptr = __first;
  551. if (!__valid)
  552. __res.ec = errc::result_out_of_range;
  553. else
  554. {
  555. if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
  556. {
  557. _Tp __tmp;
  558. if (__builtin_mul_overflow(__val, __sign, &__tmp))
  559. __res.ec = errc::result_out_of_range;
  560. else
  561. __value = __tmp;
  562. }
  563. else
  564. {
  565. if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Up>::__max
  566. > __gnu_cxx::__int_traits<_Tp>::__max)
  567. {
  568. if (__val > __gnu_cxx::__int_traits<_Tp>::__max)
  569. __res.ec = errc::result_out_of_range;
  570. else
  571. __value = __val;
  572. }
  573. else
  574. __value = __val;
  575. }
  576. }
  577. }
  578. return __res;
  579. }
  580. /// floating-point format for primitive numerical conversion
  581. enum class chars_format
  582. {
  583. scientific = 1, fixed = 2, hex = 4, general = fixed | scientific
  584. };
  585. constexpr chars_format
  586. operator|(chars_format __lhs, chars_format __rhs) noexcept
  587. { return (chars_format)((unsigned)__lhs | (unsigned)__rhs); }
  588. constexpr chars_format
  589. operator&(chars_format __lhs, chars_format __rhs) noexcept
  590. { return (chars_format)((unsigned)__lhs & (unsigned)__rhs); }
  591. constexpr chars_format
  592. operator^(chars_format __lhs, chars_format __rhs) noexcept
  593. { return (chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
  594. constexpr chars_format
  595. operator~(chars_format __fmt) noexcept
  596. { return (chars_format)~(unsigned)__fmt; }
  597. constexpr chars_format&
  598. operator|=(chars_format& __lhs, chars_format __rhs) noexcept
  599. { return __lhs = __lhs | __rhs; }
  600. constexpr chars_format&
  601. operator&=(chars_format& __lhs, chars_format __rhs) noexcept
  602. { return __lhs = __lhs & __rhs; }
  603. constexpr chars_format&
  604. operator^=(chars_format& __lhs, chars_format __rhs) noexcept
  605. { return __lhs = __lhs ^ __rhs; }
  606. #if _GLIBCXX_HAVE_USELOCALE
  607. from_chars_result
  608. from_chars(const char* __first, const char* __last, float& __value,
  609. chars_format __fmt = chars_format::general) noexcept;
  610. from_chars_result
  611. from_chars(const char* __first, const char* __last, double& __value,
  612. chars_format __fmt = chars_format::general) noexcept;
  613. from_chars_result
  614. from_chars(const char* __first, const char* __last, long double& __value,
  615. chars_format __fmt = chars_format::general) noexcept;
  616. #endif
  617. #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64
  618. // Floating-point std::to_chars
  619. // Overloads for float.
  620. to_chars_result to_chars(char* __first, char* __last, float __value) noexcept;
  621. to_chars_result to_chars(char* __first, char* __last, float __value,
  622. chars_format __fmt) noexcept;
  623. to_chars_result to_chars(char* __first, char* __last, float __value,
  624. chars_format __fmt, int __precision) noexcept;
  625. // Overloads for double.
  626. to_chars_result to_chars(char* __first, char* __last, double __value) noexcept;
  627. to_chars_result to_chars(char* __first, char* __last, double __value,
  628. chars_format __fmt) noexcept;
  629. to_chars_result to_chars(char* __first, char* __last, double __value,
  630. chars_format __fmt, int __precision) noexcept;
  631. // Overloads for long double.
  632. to_chars_result to_chars(char* __first, char* __last, long double __value)
  633. noexcept;
  634. to_chars_result to_chars(char* __first, char* __last, long double __value,
  635. chars_format __fmt) noexcept;
  636. to_chars_result to_chars(char* __first, char* __last, long double __value,
  637. chars_format __fmt, int __precision) noexcept;
  638. #endif
  639. _GLIBCXX_END_NAMESPACE_VERSION
  640. } // namespace std
  641. #endif // C++14
  642. #endif // _GLIBCXX_CHARCONV