1 // Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
3 // Copyright (C) 2017-2021 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/charconv
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_CHARCONV
30 #define _GLIBCXX_CHARCONV 1
32 #pragma GCC system_header
34 // As an extension we support <charconv> in C++14, but this header should not
35 // be included by any other library headers in C++14 mode. This ensures that
36 // the names defined in this header are not added to namespace std unless a
37 // user explicitly includes <charconv> in C++14 code.
38 #if __cplusplus >= 201402L
40 #include <type_traits>
41 #include <bit> // for __bit_width
42 #include <cctype> // for isdigit
43 #include <bits/charconv.h> // for __to_chars_len, __to_chars_10_impl
44 #include <bits/error_constants.h> // for std::errc
45 #include <ext/numeric_traits.h>
47 #if _GLIBCXX_HAVE_USELOCALE
48 # define __cpp_lib_to_chars 201611L
51 namespace std _GLIBCXX_VISIBILITY(default)
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 /// Result type of std::to_chars
56 struct to_chars_result
61 #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
63 operator==(const to_chars_result&, const to_chars_result&) = default;
67 /// Result type of std::from_chars
68 struct from_chars_result
73 #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
75 operator==(const from_chars_result&, const from_chars_result&) = default;
81 template<typename _Tp>
82 using __integer_to_chars_result_type
83 = enable_if_t<__or_<__is_signed_integer<_Tp>,
84 __is_unsigned_integer<_Tp>,
85 is_same<char, remove_cv_t<_Tp>>>::value,
88 // Pick an unsigned type of suitable size. This is used to reduce the
89 // number of specializations of __to_chars_len, __to_chars etc. that
90 // get instantiated. For example, to_chars<char> and to_chars<short>
91 // and to_chars<unsigned> will all use the same code, and so will
92 // to_chars<long> when sizeof(int) == sizeof(long).
93 template<typename _Tp>
94 struct __to_chars_unsigned_type : __make_unsigned_selector_base
96 using _UInts = _List<unsigned int, unsigned long, unsigned long long
97 #if _GLIBCXX_USE_INT128
101 using type = typename __select<sizeof(_Tp), _UInts>::__type;
104 template<typename _Tp>
105 using __unsigned_least_t = typename __to_chars_unsigned_type<_Tp>::type;
107 // Generic implementation for arbitrary bases.
108 // Defined in <bits/charconv.h>.
109 template<typename _Tp>
111 __to_chars_len(_Tp __value, int __base /* = 10 */) noexcept;
113 template<typename _Tp>
115 __to_chars_len_2(_Tp __value) noexcept
116 { return std::__bit_width(__value); }
118 // Generic implementation for arbitrary bases.
119 template<typename _Tp>
121 __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
123 static_assert(is_integral<_Tp>::value, "implementation bug");
124 static_assert(is_unsigned<_Tp>::value, "implementation bug");
126 to_chars_result __res;
128 const unsigned __len = __to_chars_len(__val, __base);
130 if (__builtin_expect((__last - __first) < __len, 0))
133 __res.ec = errc::value_too_large;
137 unsigned __pos = __len - 1;
139 static constexpr char __digits[] = {
140 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
141 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
142 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
143 'u', 'v', 'w', 'x', 'y', 'z'
146 while (__val >= (unsigned)__base)
148 auto const __quo = __val / __base;
149 auto const __rem = __val % __base;
150 __first[__pos--] = __digits[__rem];
153 *__first = __digits[__val];
155 __res.ptr = __first + __len;
160 template<typename _Tp>
161 __integer_to_chars_result_type<_Tp>
162 __to_chars_16(char* __first, char* __last, _Tp __val) noexcept
164 static_assert(is_integral<_Tp>::value, "implementation bug");
165 static_assert(is_unsigned<_Tp>::value, "implementation bug");
167 to_chars_result __res;
169 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
171 if (__builtin_expect((__last - __first) < __len, 0))
174 __res.ec = errc::value_too_large;
178 static constexpr char __digits[] = {
179 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
180 'a', 'b', 'c', 'd', 'e', 'f'
182 unsigned __pos = __len - 1;
183 while (__val >= 0x100)
185 auto __num = __val & 0xF;
187 __first[__pos] = __digits[__num];
190 __first[__pos - 1] = __digits[__num];
195 const auto __num = __val & 0xF;
197 __first[1] = __digits[__num];
198 __first[0] = __digits[__val];
201 __first[0] = __digits[__val];
202 __res.ptr = __first + __len;
207 template<typename _Tp>
208 inline __integer_to_chars_result_type<_Tp>
209 __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
211 static_assert(is_integral<_Tp>::value, "implementation bug");
212 static_assert(is_unsigned<_Tp>::value, "implementation bug");
214 to_chars_result __res;
216 const unsigned __len = __to_chars_len(__val, 10);
218 if (__builtin_expect((__last - __first) < __len, 0))
221 __res.ec = errc::value_too_large;
225 __detail::__to_chars_10_impl(__first, __len, __val);
226 __res.ptr = __first + __len;
231 template<typename _Tp>
232 __integer_to_chars_result_type<_Tp>
233 __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
235 static_assert(is_integral<_Tp>::value, "implementation bug");
236 static_assert(is_unsigned<_Tp>::value, "implementation bug");
238 to_chars_result __res;
241 if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Tp>::__digits <= 16)
243 __len = __val > 077777u ? 6u
244 : __val > 07777u ? 5u
251 __len = (__to_chars_len_2(__val) + 2) / 3;
253 if (__builtin_expect((__last - __first) < __len, 0))
256 __res.ec = errc::value_too_large;
260 unsigned __pos = __len - 1;
261 while (__val >= 0100)
263 auto __num = __val & 7;
265 __first[__pos] = '0' + __num;
268 __first[__pos - 1] = '0' + __num;
273 auto const __num = __val & 7;
275 __first[1] = '0' + __num;
276 __first[0] = '0' + __val;
279 __first[0] = '0' + __val;
280 __res.ptr = __first + __len;
285 template<typename _Tp>
286 __integer_to_chars_result_type<_Tp>
287 __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
289 static_assert(is_integral<_Tp>::value, "implementation bug");
290 static_assert(is_unsigned<_Tp>::value, "implementation bug");
292 to_chars_result __res;
294 const unsigned __len = __to_chars_len_2(__val);
296 if (__builtin_expect((__last - __first) < __len, 0))
299 __res.ec = errc::value_too_large;
303 unsigned __pos = __len - 1;
307 __first[__pos--] = '0' + (__val & 1);
310 // First digit is always '1' because __to_chars_len_2 skips
311 // leading zero bits and std::to_chars handles zero values
315 __res.ptr = __first + __len;
320 } // namespace __detail
322 template<typename _Tp>
323 __detail::__integer_to_chars_result_type<_Tp>
324 __to_chars_i(char* __first, char* __last, _Tp __value, int __base = 10)
326 __glibcxx_assert(2 <= __base && __base <= 36);
328 using _Up = __detail::__unsigned_least_t<_Tp>;
329 _Up __unsigned_val = __value;
331 if (__first == __last) [[__unlikely__]]
332 return { __last, errc::value_too_large };
337 return { __first + 1, errc{} };
340 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
343 if (__builtin_expect(__first != __last, 1))
345 __unsigned_val = _Up(~__value) + _Up(1);
351 return __detail::__to_chars_16(__first, __last, __unsigned_val);
353 return __detail::__to_chars_10(__first, __last, __unsigned_val);
355 return __detail::__to_chars_8(__first, __last, __unsigned_val);
357 return __detail::__to_chars_2(__first, __last, __unsigned_val);
359 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
363 #define _GLIBCXX_TO_CHARS(T) \
364 inline to_chars_result \
365 to_chars(char* __first, char* __last, T __value, int __base = 10) \
366 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
367 _GLIBCXX_TO_CHARS(char)
368 _GLIBCXX_TO_CHARS(signed char)
369 _GLIBCXX_TO_CHARS(unsigned char)
370 _GLIBCXX_TO_CHARS(signed short)
371 _GLIBCXX_TO_CHARS(unsigned short)
372 _GLIBCXX_TO_CHARS(signed int)
373 _GLIBCXX_TO_CHARS(unsigned int)
374 _GLIBCXX_TO_CHARS(signed long)
375 _GLIBCXX_TO_CHARS(unsigned long)
376 _GLIBCXX_TO_CHARS(signed long long)
377 _GLIBCXX_TO_CHARS(unsigned long long)
378 #if defined(__GLIBCXX_TYPE_INT_N_0)
379 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_0)
380 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_0)
382 #if defined(__GLIBCXX_TYPE_INT_N_1)
383 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_1)
384 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_1)
386 #if defined(__GLIBCXX_TYPE_INT_N_2)
387 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_2)
388 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_2)
390 #if defined(__GLIBCXX_TYPE_INT_N_3)
391 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_3)
392 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_3)
394 #undef _GLIBCXX_TO_CHARS
396 // _GLIBCXX_RESOLVE_LIB_DEFECTS
397 // 3266. to_chars(bool) should be deleted
398 to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
402 template<typename _Tp>
404 __raise_and_add(_Tp& __val, int __base, unsigned char __c)
406 if (__builtin_mul_overflow(__val, __base, &__val)
407 || __builtin_add_overflow(__val, __c, &__val))
412 /// std::from_chars implementation for integers in base 2.
413 template<typename _Tp>
415 __from_chars_binary(const char*& __first, const char* __last, _Tp& __val)
417 static_assert(is_integral<_Tp>::value, "implementation bug");
418 static_assert(is_unsigned<_Tp>::value, "implementation bug");
420 const ptrdiff_t __len = __last - __first;
422 while (__i < __len && __first[__i] == '0')
424 const ptrdiff_t __leading_zeroes = __i;
428 const unsigned char __c = (unsigned)__first[__i] - '0';
430 __val = (__val << 1) | __c;
436 return (__i - __leading_zeroes) <= __gnu_cxx::__int_traits<_Tp>::__digits;
439 /// std::from_chars implementation for integers in bases 3 to 10.
440 template<typename _Tp>
442 __from_chars_digit(const char*& __first, const char* __last, _Tp& __val,
445 static_assert(is_integral<_Tp>::value, "implementation bug");
446 static_assert(is_unsigned<_Tp>::value, "implementation bug");
448 auto __matches = [__base](char __c) {
449 return '0' <= __c && __c <= ('0' + (__base - 1));
452 while (__first != __last)
454 const char __c = *__first;
457 if (!__raise_and_add(__val, __base, __c - '0'))
459 while (++__first != __last && __matches(*__first))
471 constexpr unsigned char
472 __from_chars_alpha_to_num(char __c)
555 return __gnu_cxx::__int_traits<unsigned char>::__max;
558 /// std::from_chars implementation for integers in bases 11 to 26.
559 template<typename _Tp>
561 __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
565 while (__first != __last)
567 unsigned char __c = *__first;
568 if (std::isdigit(__c))
572 __c = __from_chars_alpha_to_num(__c);
577 if (__builtin_expect(__valid, 1))
578 __valid = __raise_and_add(__val, __base, __c);
584 template<typename _Tp>
585 using __integer_from_chars_result_type
586 = enable_if_t<__or_<__is_signed_integer<_Tp>,
587 __is_unsigned_integer<_Tp>,
588 is_same<char, remove_cv_t<_Tp>>>::value,
591 } // namespace __detail
593 /// std::from_chars for integral types.
594 template<typename _Tp>
595 __detail::__integer_from_chars_result_type<_Tp>
596 from_chars(const char* __first, const char* __last, _Tp& __value,
599 __glibcxx_assert(2 <= __base && __base <= 36);
601 from_chars_result __res{__first, {}};
604 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
605 if (__first != __last && *__first == '-')
611 using _Up = __detail::__unsigned_least_t<_Tp>;
614 const auto __start = __first;
617 __valid = __detail::__from_chars_binary(__first, __last, __val);
618 else if (__base <= 10)
619 __valid = __detail::__from_chars_digit(__first, __last, __val, __base);
621 __valid = __detail::__from_chars_alnum(__first, __last, __val, __base);
623 if (__builtin_expect(__first == __start, 0))
624 __res.ec = errc::invalid_argument;
629 __res.ec = errc::result_out_of_range;
632 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
635 if (__builtin_mul_overflow(__val, __sign, &__tmp))
636 __res.ec = errc::result_out_of_range;
642 if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Up>::__max
643 > __gnu_cxx::__int_traits<_Tp>::__max)
645 if (__val > __gnu_cxx::__int_traits<_Tp>::__max)
646 __res.ec = errc::result_out_of_range;
658 /// floating-point format for primitive numerical conversion
659 enum class chars_format
661 scientific = 1, fixed = 2, hex = 4, general = fixed | scientific
664 constexpr chars_format
665 operator|(chars_format __lhs, chars_format __rhs) noexcept
666 { return (chars_format)((unsigned)__lhs | (unsigned)__rhs); }
668 constexpr chars_format
669 operator&(chars_format __lhs, chars_format __rhs) noexcept
670 { return (chars_format)((unsigned)__lhs & (unsigned)__rhs); }
672 constexpr chars_format
673 operator^(chars_format __lhs, chars_format __rhs) noexcept
674 { return (chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
676 constexpr chars_format
677 operator~(chars_format __fmt) noexcept
678 { return (chars_format)~(unsigned)__fmt; }
680 constexpr chars_format&
681 operator|=(chars_format& __lhs, chars_format __rhs) noexcept
682 { return __lhs = __lhs | __rhs; }
684 constexpr chars_format&
685 operator&=(chars_format& __lhs, chars_format __rhs) noexcept
686 { return __lhs = __lhs & __rhs; }
688 constexpr chars_format&
689 operator^=(chars_format& __lhs, chars_format __rhs) noexcept
690 { return __lhs = __lhs ^ __rhs; }
692 #if _GLIBCXX_HAVE_USELOCALE
694 from_chars(const char* __first, const char* __last, float& __value,
695 chars_format __fmt = chars_format::general) noexcept;
698 from_chars(const char* __first, const char* __last, double& __value,
699 chars_format __fmt = chars_format::general) noexcept;
702 from_chars(const char* __first, const char* __last, long double& __value,
703 chars_format __fmt = chars_format::general) noexcept;
706 #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \
707 && __SIZE_WIDTH__ >= 32
708 // Floating-point std::to_chars
710 // Overloads for float.
711 to_chars_result to_chars(char* __first, char* __last, float __value) noexcept;
712 to_chars_result to_chars(char* __first, char* __last, float __value,
713 chars_format __fmt) noexcept;
714 to_chars_result to_chars(char* __first, char* __last, float __value,
715 chars_format __fmt, int __precision) noexcept;
717 // Overloads for double.
718 to_chars_result to_chars(char* __first, char* __last, double __value) noexcept;
719 to_chars_result to_chars(char* __first, char* __last, double __value,
720 chars_format __fmt) noexcept;
721 to_chars_result to_chars(char* __first, char* __last, double __value,
722 chars_format __fmt, int __precision) noexcept;
724 // Overloads for long double.
725 to_chars_result to_chars(char* __first, char* __last, long double __value)
727 to_chars_result to_chars(char* __first, char* __last, long double __value,
728 chars_format __fmt) noexcept;
729 to_chars_result to_chars(char* __first, char* __last, long double __value,
730 chars_format __fmt, int __precision) noexcept;
733 _GLIBCXX_END_NAMESPACE_VERSION
736 #endif // _GLIBCXX_CHARCONV