29#ifndef _GLIBCXX_CHARCONV
30#define _GLIBCXX_CHARCONV 1
33#pragma GCC system_header
36#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wpedantic"
45#if __cplusplus >= 201402L
53#define __glibcxx_want_to_chars
54#define __glibcxx_want_constexpr_charconv
57namespace std _GLIBCXX_VISIBILITY(default)
59_GLIBCXX_BEGIN_NAMESPACE_VERSION
67#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
71#if __cplusplus > 202302L
72 constexpr explicit operator bool()
const noexcept {
return ec == errc{}; }
82#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
86#if __cplusplus > 202302L
87 constexpr explicit operator bool()
const noexcept {
return ec == errc{}; }
98 template<
typename _Tp>
99 struct __to_chars_unsigned_type : __make_unsigned_selector_base
101 using _UInts = _List<
unsigned int,
unsigned long,
unsigned long long
102#if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
106 using type =
typename __select<
sizeof(_Tp), _UInts>::__type;
109 template<
typename _Tp>
110 using __unsigned_least_t =
typename __to_chars_unsigned_type<_Tp>::type;
114 template<
typename _Tp>
116 __to_chars_len(_Tp __value,
int __base )
noexcept;
118 template<
typename _Tp>
120 __to_chars_len_2(_Tp __value)
noexcept
121 {
return std::__bit_width(__value); }
124 template<
typename _Tp>
125 constexpr to_chars_result
126 __to_chars(
char* __first,
char* __last, _Tp __val,
int __base)
noexcept
128 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
130 to_chars_result __res;
132 const unsigned __len = __to_chars_len(__val, __base);
134 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
137 __res.ec = errc::value_too_large;
141 unsigned __pos = __len - 1;
143 constexpr char __digits[] = {
144 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
145 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
146 'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
147 'u',
'v',
'w',
'x',
'y',
'z'
150 while (__val >= (
unsigned)__base)
152 auto const __quo = __val /
__base;
153 auto const __rem = __val %
__base;
154 __first[__pos--] = __digits[__rem];
157 *__first = __digits[__val];
159 __res.ptr = __first + __len;
164 template<
typename _Tp>
165 constexpr to_chars_result
166 __to_chars_16(
char* __first,
char* __last, _Tp __val)
noexcept
168 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
170 to_chars_result __res;
172 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
174 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
177 __res.ec = errc::value_too_large;
181 constexpr char __digits[] = {
182 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
183 'a',
'b',
'c',
'd',
'e',
'f'
185 unsigned __pos = __len - 1;
186 while (__val >= 0x100)
188 auto __num = __val & 0xF;
190 __first[__pos] = __digits[__num];
193 __first[__pos - 1] = __digits[__num];
198 const auto __num = __val & 0xF;
200 __first[1] = __digits[__num];
201 __first[0] = __digits[__val];
204 __first[0] = __digits[__val];
205 __res.ptr = __first + __len;
210 template<
typename _Tp>
211 constexpr to_chars_result
212 __to_chars_10(
char* __first,
char* __last, _Tp __val)
noexcept
214 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
216 to_chars_result __res;
218 const unsigned __len = __to_chars_len(__val, 10);
220 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
223 __res.ec = errc::value_too_large;
227 __detail::__to_chars_10_impl(__first, __len, __val);
228 __res.ptr = __first + __len;
233 template<
typename _Tp>
234 constexpr to_chars_result
235 __to_chars_8(
char* __first,
char* __last, _Tp __val)
noexcept
237 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
239 to_chars_result __res;
242 if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Tp>::__digits <= 16)
244 __len = __val > 077777u ? 6u
245 : __val > 07777u ? 5u
252 __len = (__to_chars_len_2(__val) + 2) / 3;
254 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
257 __res.ec = errc::value_too_large;
261 unsigned __pos = __len - 1;
262 while (__val >= 0100)
264 auto __num = __val & 7;
266 __first[__pos] =
'0' + __num;
269 __first[__pos - 1] =
'0' + __num;
274 auto const __num = __val & 7;
276 __first[1] =
'0' + __num;
277 __first[0] =
'0' + __val;
280 __first[0] =
'0' + __val;
281 __res.ptr = __first + __len;
286 template<
typename _Tp>
287 constexpr to_chars_result
288 __to_chars_2(
char* __first,
char* __last, _Tp __val)
noexcept
290 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
292 to_chars_result __res;
294 const unsigned __len = __to_chars_len_2(__val);
296 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
299 __res.ec = errc::value_too_large;
303 unsigned __pos = __len - 1;
307 __first[__pos--] =
'0' + (__val & 1);
315 __res.ptr = __first + __len;
322 template<
typename _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{} };
339 else if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
343 __unsigned_val = _Up(~__value) + _Up(1);
349 return __detail::__to_chars_16(__first, __last, __unsigned_val);
351 return __detail::__to_chars_10(__first, __last, __unsigned_val);
353 return __detail::__to_chars_8(__first, __last, __unsigned_val);
355 return __detail::__to_chars_2(__first, __last, __unsigned_val);
357 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
361#define _GLIBCXX_TO_CHARS(T) \
362 _GLIBCXX23_CONSTEXPR inline to_chars_result \
363 to_chars(char* __first, char* __last, T __value, int __base = 10) \
364 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
365_GLIBCXX_TO_CHARS(
char)
366_GLIBCXX_TO_CHARS(
signed char)
367_GLIBCXX_TO_CHARS(
unsigned char)
368_GLIBCXX_TO_CHARS(
signed short)
369_GLIBCXX_TO_CHARS(
unsigned short)
370_GLIBCXX_TO_CHARS(
signed int)
371_GLIBCXX_TO_CHARS(
unsigned int)
372_GLIBCXX_TO_CHARS(
signed long)
373_GLIBCXX_TO_CHARS(
unsigned long)
374_GLIBCXX_TO_CHARS(
signed long long)
375_GLIBCXX_TO_CHARS(
unsigned long long)
376#if defined(__GLIBCXX_TYPE_INT_N_0)
377_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_0)
378_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_0)
380#if defined(__GLIBCXX_TYPE_INT_N_1)
381_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_1)
382_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_1)
384#if defined(__GLIBCXX_TYPE_INT_N_2)
385_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_2)
386_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_2)
388#if defined(__GLIBCXX_TYPE_INT_N_3)
389_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_3)
390_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_3)
392#undef _GLIBCXX_TO_CHARS
400 template<
typename _Tp>
402 __raise_and_add(_Tp& __val,
int __base,
unsigned char __c)
404 if (__builtin_mul_overflow(__val, __base, &__val)
405 || __builtin_add_overflow(__val, __c, &__val))
410 template<
bool _DecOnly>
411 struct __from_chars_alnum_to_val_table
413 struct type {
unsigned char __data[1u << __CHAR_BIT__] = {}; };
417 static constexpr type
420 constexpr unsigned char __lower_letters[27] =
"abcdefghijklmnopqrstuvwxyz";
421 constexpr unsigned char __upper_letters[27] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
423 for (
auto& __entry : __table.__data)
425 for (
int __i = 0; __i < 10; ++__i)
426 __table.__data[
'0' + __i] = __i;
427 for (
int __i = 0; __i < 26; ++__i)
429 __table.__data[__lower_letters[__i]] = 10 + __i;
430 __table.__data[__upper_letters[__i]] = 10 + __i;
438 static constexpr type value = (_DecOnly, _S_make_table());
441#if ! __cpp_inline_variables
442 template<
bool _DecOnly>
443 const typename __from_chars_alnum_to_val_table<_DecOnly>::type
444 __from_chars_alnum_to_val_table<_DecOnly>::value;
451 template<
bool _DecOnly = false>
452 _GLIBCXX20_CONSTEXPR
unsigned char
453 __from_chars_alnum_to_val(
unsigned char __c)
455 if _GLIBCXX17_CONSTEXPR (_DecOnly)
456 return static_cast<unsigned char>(__c -
'0');
458 return __from_chars_alnum_to_val_table<_DecOnly>::value.__data[__c];
463 template<
bool _DecOnly,
typename _Tp>
464 _GLIBCXX23_CONSTEXPR
bool
473 const int __log2_base = __countr_zero(
unsigned(__base & 0x3f));
475 const ptrdiff_t __len = __last - __first;
477 while (__i < __len && __first[__i] ==
'0')
479 const ptrdiff_t __leading_zeroes = __i;
480 if (__i >= __len) [[__unlikely__]]
487 unsigned char __leading_c = 0;
490 __leading_c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
492 if (__leading_c >= __base) [[__unlikely__]]
501 for (; __i < __len; ++__i)
503 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
506 __val = (__val << __log2_base) | __c;
509 auto __significant_bits = (__i - __leading_zeroes) * __log2_base;
513 __significant_bits -= __log2_base - __bit_width(__leading_c);
515 return __significant_bits <= __gnu_cxx::__int_traits<_Tp>::__digits;
520 template<
bool _DecOnly,
typename _Tp>
527 const int __bits_per_digit = __bit_width(
unsigned(__base & 0x3f));
528 int __unused_bits_lower_bound = __gnu_cxx::__int_traits<_Tp>::__digits;
529 for (; __first != __last; ++__first)
531 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(*__first);
535 __unused_bits_lower_bound -= __bits_per_digit;
536 if (__unused_bits_lower_bound >= 0) [[__likely__]]
538 __val = __val * __base + __c;
539 else if (!__raise_and_add(__val, __base, __c)) [[__unlikely__]]
541 while (++__first != __last
542 && __from_chars_alnum_to_val<_DecOnly>(*__first) < __base)
553 template<
typename _Tp,
555 is_same<char, remove_cv_t<_Tp>>>::value,
int> = 0>
556 _GLIBCXX23_CONSTEXPR from_chars_result
557 from_chars(
const char* __first,
const char* __last, _Tp& __value,
560 __glibcxx_assert(2 <= __base && __base <= 36);
566 if (__first != __last && *__first ==
'-')
572 using _Up = __detail::__unsigned_least_t<_Tp>;
575 const auto __start = __first;
577 if ((__base & (__base - 1)) == 0)
584 else if (__base <= 10)
589 if (__builtin_expect(__first == __start, 0))
590 __res.ec = errc::invalid_argument;
595 __res.ec = errc::result_out_of_range;
601 if (__builtin_mul_overflow(__val, __sign, &__tmp))
602 __res.ec = errc::result_out_of_range;
608 if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Up>::__max
609 > __gnu_cxx::__int_traits<_Tp>::__max)
611 if (__val > __gnu_cxx::__int_traits<_Tp>::__max)
612 __res.ec = errc::result_out_of_range;
633 {
return (
chars_format)((unsigned)__lhs | (unsigned)__rhs); }
638 {
return (
chars_format)((unsigned)__lhs & (unsigned)__rhs); }
643 {
return (
chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
652 {
return __lhs = __lhs | __rhs; }
656 {
return __lhs = __lhs & __rhs; }
660 {
return __lhs = __lhs ^ __rhs; }
662#if defined __cpp_lib_to_chars || _GLIBCXX_HAVE_USELOCALE
664 from_chars(
const char* __first,
const char* __last,
float& __value,
668 from_chars(
const char* __first,
const char* __last,
double& __value,
672 from_chars(
const char* __first,
const char* __last,
long double& __value,
678 __from_chars_float16_t(
const char* __first,
const char* __last,
682 __from_chars_bfloat16_t(
const char* __first,
const char* __last,
686#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
687 && defined(__cpp_lib_to_chars)
689 from_chars(
const char* __first,
const char* __last, _Float16& __value,
694 = __from_chars_float16_t(__first, __last, __val, __fmt);
695 if (__res.ec == errc{})
696 __value = _Float16(__val);
701#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
703 from_chars(
const char* __first,
const char* __last, _Float32& __value,
708 if (__res.ec == errc{})
709 __value = _Float32(__val);
714#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
716 from_chars(
const char* __first,
const char* __last, _Float64& __value,
721 if (__res.ec == errc{})
722 __value = _Float64(__val);
727#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
729 from_chars(
const char* __first,
const char* __last, _Float128& __value,
734 if (__res.ec == errc{})
735 __value = _Float128(__val);
738#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
739#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
741 from_chars(
const char* __first,
const char* __last, __ieee128& __value,
745 from_chars(
const char* __first,
const char* __last, _Float128& __value,
748 __extension__ __ieee128 __val;
750 if (__res.ec == errc{})
751 __value = _Float128(__val);
756 from_chars(
const char* __first,
const char* __last, _Float128& __value,
761#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
762 && defined(__cpp_lib_to_chars)
764 from_chars(
const char* __first,
const char* __last,
765 __gnu_cxx::__bfloat16_t & __value,
770 = __from_chars_bfloat16_t(__first, __last, __val, __fmt);
771 if (__res.ec == errc{})
772 __value = __gnu_cxx::__bfloat16_t(__val);
778#if defined __cpp_lib_to_chars
782 to_chars_result to_chars(
char* __first,
char* __last,
float __value)
noexcept;
789 to_chars_result to_chars(
char* __first,
char* __last,
double __value)
noexcept;
796 to_chars_result to_chars(
char* __first,
char* __last,
long double __value)
798 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
800 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
812#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
814 to_chars(
char* __first,
char* __last, _Float16 __value)
noexcept
816 return __to_chars_float16_t(__first, __last,
float(__value),
820 to_chars(
char* __first,
char* __last, _Float16 __value,
822 {
return __to_chars_float16_t(__first, __last,
float(__value), __fmt); }
824 to_chars(
char* __first,
char* __last, _Float16 __value,
826 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
829#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
831 to_chars(
char* __first,
char* __last, _Float32 __value)
noexcept
832 {
return to_chars(__first, __last,
float(__value)); }
834 to_chars(
char* __first,
char* __last, _Float32 __value,
836 {
return to_chars(__first, __last,
float(__value), __fmt); }
838 to_chars(
char* __first,
char* __last, _Float32 __value,
840 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
843#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
845 to_chars(
char* __first,
char* __last, _Float64 __value)
noexcept
846 {
return to_chars(__first, __last,
double(__value)); }
848 to_chars(
char* __first,
char* __last, _Float64 __value,
850 {
return to_chars(__first, __last,
double(__value), __fmt); }
852 to_chars(
char* __first,
char* __last, _Float64 __value,
854 {
return to_chars(__first, __last,
double(__value), __fmt, __precision); }
857#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
859 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
860 {
return to_chars(__first, __last,
static_cast<long double>(__value)); }
862 to_chars(
char* __first,
char* __last, _Float128 __value,
865 return to_chars(__first, __last,
static_cast<long double>(__value), __fmt);
868 to_chars(
char* __first,
char* __last, _Float128 __value,
871 return to_chars(__first, __last,
static_cast<long double>(__value), __fmt,
874#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
875#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
877 to_chars(
char* __first,
char* __last, __float128 __value)
noexcept;
879 to_chars(
char* __first,
char* __last, __float128 __value,
882 to_chars(
char* __first,
char* __last, __float128 __value,
886 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
888 return __extension__ to_chars(__first, __last,
889 static_cast<__float128
>(__value));
892 to_chars(
char* __first,
char* __last, _Float128 __value,
896 return __extension__ to_chars(__first, __last,
897 static_cast<__float128
>(__value), __fmt);
900 to_chars(
char* __first,
char* __last, _Float128 __value,
904 return __extension__ to_chars(__first, __last,
905 static_cast<__float128
>(__value), __fmt,
909 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value)
911 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value,
913 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value,
918#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
920 to_chars(
char* __first,
char* __last,
921 __gnu_cxx::__bfloat16_t __value)
noexcept
923 return __to_chars_bfloat16_t(__first, __last,
float(__value),
927 to_chars(
char* __first,
char* __last, __gnu_cxx::__bfloat16_t __value,
929 {
return __to_chars_bfloat16_t(__first, __last,
float(__value), __fmt); }
931 to_chars(
char* __first,
char* __last, __gnu_cxx::__bfloat16_t __value,
933 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
937_GLIBCXX_END_NAMESPACE_VERSION
940#pragma GCC diagnostic pop
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
ISO C++ entities toplevel namespace is std.
ios_base & scientific(ios_base &__base)
Calls base.setf(ios_base::scientific, ios_base::floatfield).
ios_base & hex(ios_base &__base)
Calls base.setf(ios_base::hex, ios_base::basefield).
chars_format
floating-point format for primitive numerical conversion
constexpr bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
ios_base & fixed(ios_base &__base)
Calls base.setf(ios_base::fixed, ios_base::floatfield).
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
constexpr bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
constexpr from_chars_result from_chars(const char *__first, const char *__last, _Tp &__value, int __base=10)
std::from_chars for integral types.
Implementation details not part of the namespace std interface.
constexpr bool __from_chars_alnum(const char *&__first, const char *__last, _Tp &__val, int __base)
std::from_chars implementation for integers in any base. If _DecOnly is true, then we may assume __ba...
constexpr bool __from_chars_pow2_base(const char *&__first, const char *__last, _Tp &__val, int __base)
std::from_chars implementation for integers in a power-of-two base. If _DecOnly is true,...
constexpr _Iterator __base(_Iterator __it)
Result type of std::to_chars.
Result type of std::from_chars.