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"
38#pragma GCC diagnostic ignored "-Wc++17-extensions"
46#if __cplusplus >= 201402L
54#define __glibcxx_want_to_chars
55#define __glibcxx_want_constexpr_charconv
58namespace std _GLIBCXX_VISIBILITY(default)
60_GLIBCXX_BEGIN_NAMESPACE_VERSION
68#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
72#if __cplusplus > 202302L
73 constexpr explicit operator bool()
const noexcept {
return ec == errc{}; }
83#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
87#if __cplusplus > 202302L
88 constexpr explicit operator bool()
const noexcept {
return ec == errc{}; }
99 template<
typename _Tp>
101 __to_chars_unsigned_type()
103 if constexpr (
sizeof(_Tp) <=
sizeof(
unsigned int))
104 return __type_identity<unsigned int>{};
105 else if constexpr (
sizeof(_Tp) <=
sizeof(
unsigned long))
106 return __type_identity<unsigned long>{};
107 else if constexpr (
sizeof(_Tp) <=
sizeof(
unsigned long long))
108 return __type_identity<unsigned long long>{};
109#if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
110 else if constexpr (
sizeof(_Tp) <=
sizeof(
unsigned __int128))
111 return __type_identity<unsigned __int128>{};
115 template<
typename _Tp>
116 using __unsigned_least_t
117 =
typename decltype(__to_chars_unsigned_type<_Tp>())::type;
121 template<
typename _Tp>
123 __to_chars_len(_Tp __value,
int __base )
noexcept;
125 template<
typename _Tp>
127 __to_chars_len_2(_Tp __value)
noexcept
128 {
return std::__bit_width(__value); }
131 template<
typename _Tp>
132 constexpr to_chars_result
133 __to_chars(
char* __first,
char* __last, _Tp __val,
int __base)
noexcept
135 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
137 to_chars_result __res;
139 const unsigned __len = __to_chars_len(__val, __base);
141 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
144 __res.ec = errc::value_too_large;
148 unsigned __pos = __len - 1;
150 constexpr char __digits[] = {
151 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
152 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
153 'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
154 'u',
'v',
'w',
'x',
'y',
'z'
157 while (__val >= (
unsigned)__base)
159 auto const __quo = __val /
__base;
160 auto const __rem = __val %
__base;
161 __first[__pos--] = __digits[__rem];
164 *__first = __digits[__val];
166 __res.ptr = __first + __len;
171 template<
typename _Tp>
172 constexpr to_chars_result
173 __to_chars_16(
char* __first,
char* __last, _Tp __val)
noexcept
175 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
177 to_chars_result __res;
179 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
181 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
184 __res.ec = errc::value_too_large;
188 constexpr char __digits[] = {
189 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
190 'a',
'b',
'c',
'd',
'e',
'f'
192 unsigned __pos = __len - 1;
193 while (__val >= 0x100)
195 auto __num = __val & 0xF;
197 __first[__pos] = __digits[__num];
200 __first[__pos - 1] = __digits[__num];
205 const auto __num = __val & 0xF;
207 __first[1] = __digits[__num];
208 __first[0] = __digits[__val];
211 __first[0] = __digits[__val];
212 __res.ptr = __first + __len;
217 template<
typename _Tp>
218 constexpr to_chars_result
219 __to_chars_10(
char* __first,
char* __last, _Tp __val)
noexcept
221 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
223 to_chars_result __res;
225 const unsigned __len = __to_chars_len(__val, 10);
227 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
230 __res.ec = errc::value_too_large;
234 __detail::__to_chars_10_impl(__first, __len, __val);
235 __res.ptr = __first + __len;
240 template<
typename _Tp>
241 constexpr to_chars_result
242 __to_chars_8(
char* __first,
char* __last, _Tp __val)
noexcept
244 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
246 to_chars_result __res;
249 if constexpr (__gnu_cxx::__int_traits<_Tp>::__digits <= 16)
251#if __cpp_0o_octals >= 202611L
252 __len = __val > 0o77777u ? 6u
253 : __val > 0o7777u ? 5u
254 : __val > 0o777u ? 4u
259 __len = __val > 077777u ? 6u
260 : __val > 07777u ? 5u
268 __len = (__to_chars_len_2(__val) + 2) / 3;
270 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
273 __res.ec = errc::value_too_large;
277 unsigned __pos = __len - 1;
278 while (__val >= 8 * 8)
280 auto __num = __val & 7;
282 __first[__pos] =
'0' + __num;
285 __first[__pos - 1] =
'0' + __num;
290 auto const __num = __val & 7;
292 __first[1] =
'0' + __num;
293 __first[0] =
'0' + __val;
296 __first[0] =
'0' + __val;
297 __res.ptr = __first + __len;
302 template<
typename _Tp>
303 constexpr to_chars_result
304 __to_chars_2(
char* __first,
char* __last, _Tp __val)
noexcept
306 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
308 to_chars_result __res;
310 const unsigned __len = __to_chars_len_2(__val);
312 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
315 __res.ec = errc::value_too_large;
319 unsigned __pos = __len - 1;
323 __first[__pos--] =
'0' + (__val & 1);
331 __res.ptr = __first + __len;
338 template<
typename _Tp>
340 __to_chars_i(
char* __first,
char* __last, _Tp __value,
int __base = 10)
342 __glibcxx_assert(2 <= __base && __base <= 36);
344 using _Up = __detail::__unsigned_least_t<_Tp>;
345 _Up __unsigned_val = __value;
347 if (__first >= __last) [[__unlikely__]]
348 return { __last, errc::value_too_large };
353 return { __first + 1, errc{} };
355 else if constexpr (std::is_signed<_Tp>::value)
359 __unsigned_val = _Up(~__value) + _Up(1);
365 return __detail::__to_chars_16(__first, __last, __unsigned_val);
367 return __detail::__to_chars_10(__first, __last, __unsigned_val);
369 return __detail::__to_chars_8(__first, __last, __unsigned_val);
371 return __detail::__to_chars_2(__first, __last, __unsigned_val);
373 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
377#define _GLIBCXX_TO_CHARS(T) \
378 _GLIBCXX23_CONSTEXPR inline to_chars_result \
379 to_chars(char* __first, char* __last, T __value, int __base = 10) \
380 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
381_GLIBCXX_TO_CHARS(
char)
382_GLIBCXX_TO_CHARS(
signed char)
383_GLIBCXX_TO_CHARS(
unsigned char)
384_GLIBCXX_TO_CHARS(
signed short)
385_GLIBCXX_TO_CHARS(
unsigned short)
386_GLIBCXX_TO_CHARS(
signed int)
387_GLIBCXX_TO_CHARS(
unsigned int)
388_GLIBCXX_TO_CHARS(
signed long)
389_GLIBCXX_TO_CHARS(
unsigned long)
390_GLIBCXX_TO_CHARS(
signed long long)
391_GLIBCXX_TO_CHARS(
unsigned long long)
392#if defined(__GLIBCXX_TYPE_INT_N_0)
393_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_0)
394_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_0)
396#if defined(__GLIBCXX_TYPE_INT_N_1)
397_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_1)
398_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_1)
400#if defined(__GLIBCXX_TYPE_INT_N_2)
401_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_2)
402_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_2)
404#if defined(__GLIBCXX_TYPE_INT_N_3)
405_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_3)
406_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_3)
408#if defined __SIZEOF_INT128__ && defined __STRICT_ANSI__
409_GLIBCXX_TO_CHARS(
signed __int128)
410_GLIBCXX_TO_CHARS(
unsigned __int128)
412#undef _GLIBCXX_TO_CHARS
420 template<
typename _Tp>
422 __raise_and_add(_Tp& __val,
int __base,
unsigned char __c)
424 if (__builtin_mul_overflow(__val, __base, &__val)
425 || __builtin_add_overflow(__val, __c, &__val))
430 template<
bool _DecOnly>
431 struct __from_chars_alnum_to_val_table
433 struct type {
unsigned char __data[1u << __CHAR_BIT__] = {}; };
437 static constexpr type
440 constexpr unsigned char __lower_letters[27] =
"abcdefghijklmnopqrstuvwxyz";
441 constexpr unsigned char __upper_letters[27] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
443 for (
auto& __entry : __table.__data)
445 for (
int __i = 0; __i < 10; ++__i)
446 __table.__data[
'0' + __i] = __i;
447 for (
int __i = 0; __i < 26; ++__i)
449 __table.__data[__lower_letters[__i]] = 10 + __i;
450 __table.__data[__upper_letters[__i]] = 10 + __i;
458 static constexpr type value = (_DecOnly, _S_make_table());
461#if ! __cpp_inline_variables
462 template<
bool _DecOnly>
463 const typename __from_chars_alnum_to_val_table<_DecOnly>::type
464 __from_chars_alnum_to_val_table<_DecOnly>::value;
471 template<
bool _DecOnly = false>
472 _GLIBCXX20_CONSTEXPR
unsigned char
473 __from_chars_alnum_to_val(
unsigned char __c)
475 if constexpr (_DecOnly)
476 return static_cast<unsigned char>(__c -
'0');
478 return __from_chars_alnum_to_val_table<_DecOnly>::value.__data[__c];
483 template<
bool _DecOnly,
typename _Tp>
484 _GLIBCXX23_CONSTEXPR
bool
493 const int __log2_base = __countr_zero(
unsigned(__base & 0x3f));
495 const ptrdiff_t __len = __last - __first;
497 while (__i < __len && __first[__i] ==
'0')
499 const ptrdiff_t __leading_zeroes = __i;
500 if (__i >= __len) [[__unlikely__]]
507 unsigned char __leading_c = 0;
510 __leading_c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
512 if (__leading_c >= __base) [[__unlikely__]]
521 for (; __i < __len; ++__i)
523 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
526 __val = (__val << __log2_base) | __c;
529 auto __significant_bits = (__i - __leading_zeroes) * __log2_base;
533 __significant_bits -= __log2_base - __bit_width(__leading_c);
535 return __significant_bits <= __gnu_cxx::__int_traits<_Tp>::__digits;
540 template<
bool _DecOnly,
typename _Tp>
547 const int __bits_per_digit = __bit_width(
unsigned(__base & 0x3f));
548 int __unused_bits_lower_bound = __gnu_cxx::__int_traits<_Tp>::__digits;
549 for (; __first != __last; ++__first)
551 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(*__first);
555 __unused_bits_lower_bound -= __bits_per_digit;
556 if (__unused_bits_lower_bound >= 0) [[__likely__]]
558 __val = __val * __base + __c;
559 else if (!__raise_and_add(__val, __base, __c)) [[__unlikely__]]
561 while (++__first != __last
562 && __from_chars_alnum_to_val<_DecOnly>(*__first) < __base)
573 template<
typename _Tp,
575 is_same<char, _Tp>>::value,
int> = 0>
576 _GLIBCXX23_CONSTEXPR from_chars_result
577 from_chars(
const char* __first,
const char* __last, _Tp& __value,
580 __glibcxx_assert(2 <= __base && __base <= 36);
586 if (__first != __last && *__first ==
'-')
592 using _Up = __detail::__unsigned_least_t<_Tp>;
595 const auto __start = __first;
597 if ((__base & (__base - 1)) == 0)
604 else if (__base <= 10)
609 if (__builtin_expect(__first == __start, 0))
610 __res.ec = errc::invalid_argument;
615 __res.ec = errc::result_out_of_range;
621 if (__builtin_mul_overflow(__val, __sign, &__tmp))
622 __res.ec = errc::result_out_of_range;
628 if constexpr (__gnu_cxx::__int_traits<_Up>::__max
629 > __gnu_cxx::__int_traits<_Tp>::__max)
631 if (__val > __gnu_cxx::__int_traits<_Tp>::__max)
632 __res.ec = errc::result_out_of_range;
653 {
return (
chars_format)((unsigned)__lhs | (unsigned)__rhs); }
658 {
return (
chars_format)((unsigned)__lhs & (unsigned)__rhs); }
663 {
return (
chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
672 {
return __lhs = __lhs | __rhs; }
676 {
return __lhs = __lhs & __rhs; }
680 {
return __lhs = __lhs ^ __rhs; }
682#if defined __cpp_lib_to_chars || _GLIBCXX_HAVE_USELOCALE
684 from_chars(
const char* __first,
const char* __last,
float& __value,
688 from_chars(
const char* __first,
const char* __last,
double& __value,
692 from_chars(
const char* __first,
const char* __last,
long double& __value,
698 __from_chars_float16_t(
const char* __first,
const char* __last,
702 __from_chars_bfloat16_t(
const char* __first,
const char* __last,
706#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
707 && defined(__cpp_lib_to_chars)
709 from_chars(
const char* __first,
const char* __last, _Float16& __value,
714 = __from_chars_float16_t(__first, __last, __val, __fmt);
715 if (__res.ec == errc{})
716 __value = _Float16(__val);
721#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
723 from_chars(
const char* __first,
const char* __last, _Float32& __value,
728 if (__res.ec == errc{})
729 __value = _Float32(__val);
734#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
736 from_chars(
const char* __first,
const char* __last, _Float64& __value,
741 if (__res.ec == errc{})
742 __value = _Float64(__val);
747#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
749 from_chars(
const char* __first,
const char* __last, _Float128& __value,
754 if (__res.ec == errc{})
755 __value = _Float128(__val);
758#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
759#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
761 from_chars(
const char* __first,
const char* __last, __ieee128& __value,
765 from_chars(
const char* __first,
const char* __last, _Float128& __value,
768 __extension__ __ieee128 __val;
770 if (__res.ec == errc{})
771 __value = _Float128(__val);
776 from_chars(
const char* __first,
const char* __last, _Float128& __value,
781#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
782 && defined(__cpp_lib_to_chars)
784 from_chars(
const char* __first,
const char* __last,
785 __gnu_cxx::__bfloat16_t & __value,
790 = __from_chars_bfloat16_t(__first, __last, __val, __fmt);
791 if (__res.ec == errc{})
792 __value = __gnu_cxx::__bfloat16_t(__val);
798#if defined __cpp_lib_to_chars
802 to_chars_result to_chars(
char* __first,
char* __last,
float __value)
noexcept;
809 to_chars_result to_chars(
char* __first,
char* __last,
double __value)
noexcept;
816 to_chars_result to_chars(
char* __first,
char* __last,
long double __value)
818 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
820 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
832#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
834 to_chars(
char* __first,
char* __last, _Float16 __value)
noexcept
836 return __to_chars_float16_t(__first, __last,
float(__value),
840 to_chars(
char* __first,
char* __last, _Float16 __value,
842 {
return __to_chars_float16_t(__first, __last,
float(__value), __fmt); }
844 to_chars(
char* __first,
char* __last, _Float16 __value,
846 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
849#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
851 to_chars(
char* __first,
char* __last, _Float32 __value)
noexcept
852 {
return to_chars(__first, __last,
float(__value)); }
854 to_chars(
char* __first,
char* __last, _Float32 __value,
856 {
return to_chars(__first, __last,
float(__value), __fmt); }
858 to_chars(
char* __first,
char* __last, _Float32 __value,
860 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
863#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
865 to_chars(
char* __first,
char* __last, _Float64 __value)
noexcept
866 {
return to_chars(__first, __last,
double(__value)); }
868 to_chars(
char* __first,
char* __last, _Float64 __value,
870 {
return to_chars(__first, __last,
double(__value), __fmt); }
872 to_chars(
char* __first,
char* __last, _Float64 __value,
874 {
return to_chars(__first, __last,
double(__value), __fmt, __precision); }
877#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
879 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
880 {
return to_chars(__first, __last,
static_cast<long double>(__value)); }
882 to_chars(
char* __first,
char* __last, _Float128 __value,
885 return to_chars(__first, __last,
static_cast<long double>(__value), __fmt);
888 to_chars(
char* __first,
char* __last, _Float128 __value,
891 return to_chars(__first, __last,
static_cast<long double>(__value), __fmt,
894#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
895#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
897 to_chars(
char* __first,
char* __last, __float128 __value)
noexcept;
899 to_chars(
char* __first,
char* __last, __float128 __value,
902 to_chars(
char* __first,
char* __last, __float128 __value,
906 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
908 return __extension__ to_chars(__first, __last,
909 static_cast<__float128
>(__value));
912 to_chars(
char* __first,
char* __last, _Float128 __value,
916 return __extension__ to_chars(__first, __last,
917 static_cast<__float128
>(__value), __fmt);
920 to_chars(
char* __first,
char* __last, _Float128 __value,
924 return __extension__ to_chars(__first, __last,
925 static_cast<__float128
>(__value), __fmt,
929 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value)
931 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value,
933 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value,
938#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
940 to_chars(
char* __first,
char* __last,
941 __gnu_cxx::__bfloat16_t __value)
noexcept
943 return __to_chars_bfloat16_t(__first, __last,
float(__value),
947 to_chars(
char* __first,
char* __last, __gnu_cxx::__bfloat16_t __value,
949 {
return __to_chars_bfloat16_t(__first, __last,
float(__value), __fmt); }
951 to_chars(
char* __first,
char* __last, __gnu_cxx::__bfloat16_t __value,
953 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
957_GLIBCXX_END_NAMESPACE_VERSION
960#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 integer 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.