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>
100 struct __to_chars_unsigned_type : __make_unsigned_selector_base
102 using _UInts = _List<
unsigned int,
unsigned long,
unsigned long long
103#if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
107 using type =
typename __select<
sizeof(_Tp), _UInts>::__type;
110 template<
typename _Tp>
111 using __unsigned_least_t =
typename __to_chars_unsigned_type<_Tp>::type;
115 template<
typename _Tp>
117 __to_chars_len(_Tp __value,
int __base )
noexcept;
119 template<
typename _Tp>
121 __to_chars_len_2(_Tp __value)
noexcept
122 {
return std::__bit_width(__value); }
125 template<
typename _Tp>
126 constexpr to_chars_result
127 __to_chars(
char* __first,
char* __last, _Tp __val,
int __base)
noexcept
129 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
131 to_chars_result __res;
133 const unsigned __len = __to_chars_len(__val, __base);
135 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
138 __res.ec = errc::value_too_large;
142 unsigned __pos = __len - 1;
144 constexpr char __digits[] = {
145 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
146 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
147 'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
148 'u',
'v',
'w',
'x',
'y',
'z'
151 while (__val >= (
unsigned)__base)
153 auto const __quo = __val /
__base;
154 auto const __rem = __val %
__base;
155 __first[__pos--] = __digits[__rem];
158 *__first = __digits[__val];
160 __res.ptr = __first + __len;
165 template<
typename _Tp>
166 constexpr to_chars_result
167 __to_chars_16(
char* __first,
char* __last, _Tp __val)
noexcept
169 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
171 to_chars_result __res;
173 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
175 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
178 __res.ec = errc::value_too_large;
182 constexpr char __digits[] = {
183 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
184 'a',
'b',
'c',
'd',
'e',
'f'
186 unsigned __pos = __len - 1;
187 while (__val >= 0x100)
189 auto __num = __val & 0xF;
191 __first[__pos] = __digits[__num];
194 __first[__pos - 1] = __digits[__num];
199 const auto __num = __val & 0xF;
201 __first[1] = __digits[__num];
202 __first[0] = __digits[__val];
205 __first[0] = __digits[__val];
206 __res.ptr = __first + __len;
211 template<
typename _Tp>
212 constexpr to_chars_result
213 __to_chars_10(
char* __first,
char* __last, _Tp __val)
noexcept
215 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
217 to_chars_result __res;
219 const unsigned __len = __to_chars_len(__val, 10);
221 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
224 __res.ec = errc::value_too_large;
228 __detail::__to_chars_10_impl(__first, __len, __val);
229 __res.ptr = __first + __len;
234 template<
typename _Tp>
235 constexpr to_chars_result
236 __to_chars_8(
char* __first,
char* __last, _Tp __val)
noexcept
238 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
240 to_chars_result __res;
243 if constexpr (__gnu_cxx::__int_traits<_Tp>::__digits <= 16)
245 __len = __val > 077777u ? 6u
246 : __val > 07777u ? 5u
253 __len = (__to_chars_len_2(__val) + 2) / 3;
255 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
258 __res.ec = errc::value_too_large;
262 unsigned __pos = __len - 1;
263 while (__val >= 0100)
265 auto __num = __val & 7;
267 __first[__pos] =
'0' + __num;
270 __first[__pos - 1] =
'0' + __num;
275 auto const __num = __val & 7;
277 __first[1] =
'0' + __num;
278 __first[0] =
'0' + __val;
281 __first[0] =
'0' + __val;
282 __res.ptr = __first + __len;
287 template<
typename _Tp>
288 constexpr to_chars_result
289 __to_chars_2(
char* __first,
char* __last, _Tp __val)
noexcept
291 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
293 to_chars_result __res;
295 const unsigned __len = __to_chars_len_2(__val);
297 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
300 __res.ec = errc::value_too_large;
304 unsigned __pos = __len - 1;
308 __first[__pos--] =
'0' + (__val & 1);
316 __res.ptr = __first + __len;
323 template<
typename _Tp>
325 __to_chars_i(
char* __first,
char* __last, _Tp __value,
int __base = 10)
327 __glibcxx_assert(2 <= __base && __base <= 36);
329 using _Up = __detail::__unsigned_least_t<_Tp>;
330 _Up __unsigned_val = __value;
332 if (__first >= __last) [[__unlikely__]]
333 return { __last, errc::value_too_large };
338 return { __first + 1, errc{} };
340 else if constexpr (std::is_signed<_Tp>::value)
344 __unsigned_val = _Up(~__value) + _Up(1);
350 return __detail::__to_chars_16(__first, __last, __unsigned_val);
352 return __detail::__to_chars_10(__first, __last, __unsigned_val);
354 return __detail::__to_chars_8(__first, __last, __unsigned_val);
356 return __detail::__to_chars_2(__first, __last, __unsigned_val);
358 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
362#define _GLIBCXX_TO_CHARS(T) \
363 _GLIBCXX23_CONSTEXPR inline to_chars_result \
364 to_chars(char* __first, char* __last, T __value, int __base = 10) \
365 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
366_GLIBCXX_TO_CHARS(
char)
367_GLIBCXX_TO_CHARS(
signed char)
368_GLIBCXX_TO_CHARS(
unsigned char)
369_GLIBCXX_TO_CHARS(
signed short)
370_GLIBCXX_TO_CHARS(
unsigned short)
371_GLIBCXX_TO_CHARS(
signed int)
372_GLIBCXX_TO_CHARS(
unsigned int)
373_GLIBCXX_TO_CHARS(
signed long)
374_GLIBCXX_TO_CHARS(
unsigned long)
375_GLIBCXX_TO_CHARS(
signed long long)
376_GLIBCXX_TO_CHARS(
unsigned long long)
377#if defined(__GLIBCXX_TYPE_INT_N_0)
378_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_0)
379_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_0)
381#if defined(__GLIBCXX_TYPE_INT_N_1)
382_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_1)
383_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_1)
385#if defined(__GLIBCXX_TYPE_INT_N_2)
386_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_2)
387_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_2)
389#if defined(__GLIBCXX_TYPE_INT_N_3)
390_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_3)
391_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_3)
393#if defined __SIZEOF_INT128__ && defined __STRICT_ANSI__
394_GLIBCXX_TO_CHARS(
signed __int128)
395_GLIBCXX_TO_CHARS(
unsigned __int128)
397#undef _GLIBCXX_TO_CHARS
405 template<
typename _Tp>
407 __raise_and_add(_Tp& __val,
int __base,
unsigned char __c)
409 if (__builtin_mul_overflow(__val, __base, &__val)
410 || __builtin_add_overflow(__val, __c, &__val))
415 template<
bool _DecOnly>
416 struct __from_chars_alnum_to_val_table
418 struct type {
unsigned char __data[1u << __CHAR_BIT__] = {}; };
422 static constexpr type
425 constexpr unsigned char __lower_letters[27] =
"abcdefghijklmnopqrstuvwxyz";
426 constexpr unsigned char __upper_letters[27] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
428 for (
auto& __entry : __table.__data)
430 for (
int __i = 0; __i < 10; ++__i)
431 __table.__data[
'0' + __i] = __i;
432 for (
int __i = 0; __i < 26; ++__i)
434 __table.__data[__lower_letters[__i]] = 10 + __i;
435 __table.__data[__upper_letters[__i]] = 10 + __i;
443 static constexpr type value = (_DecOnly, _S_make_table());
446#if ! __cpp_inline_variables
447 template<
bool _DecOnly>
448 const typename __from_chars_alnum_to_val_table<_DecOnly>::type
449 __from_chars_alnum_to_val_table<_DecOnly>::value;
456 template<
bool _DecOnly = false>
457 _GLIBCXX20_CONSTEXPR
unsigned char
458 __from_chars_alnum_to_val(
unsigned char __c)
460 if constexpr (_DecOnly)
461 return static_cast<unsigned char>(__c -
'0');
463 return __from_chars_alnum_to_val_table<_DecOnly>::value.__data[__c];
468 template<
bool _DecOnly,
typename _Tp>
469 _GLIBCXX23_CONSTEXPR
bool
478 const int __log2_base = __countr_zero(
unsigned(__base & 0x3f));
480 const ptrdiff_t __len = __last - __first;
482 while (__i < __len && __first[__i] ==
'0')
484 const ptrdiff_t __leading_zeroes = __i;
485 if (__i >= __len) [[__unlikely__]]
492 unsigned char __leading_c = 0;
495 __leading_c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
497 if (__leading_c >= __base) [[__unlikely__]]
506 for (; __i < __len; ++__i)
508 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
511 __val = (__val << __log2_base) | __c;
514 auto __significant_bits = (__i - __leading_zeroes) * __log2_base;
518 __significant_bits -= __log2_base - __bit_width(__leading_c);
520 return __significant_bits <= __gnu_cxx::__int_traits<_Tp>::__digits;
525 template<
bool _DecOnly,
typename _Tp>
532 const int __bits_per_digit = __bit_width(
unsigned(__base & 0x3f));
533 int __unused_bits_lower_bound = __gnu_cxx::__int_traits<_Tp>::__digits;
534 for (; __first != __last; ++__first)
536 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(*__first);
540 __unused_bits_lower_bound -= __bits_per_digit;
541 if (__unused_bits_lower_bound >= 0) [[__likely__]]
543 __val = __val * __base + __c;
544 else if (!__raise_and_add(__val, __base, __c)) [[__unlikely__]]
546 while (++__first != __last
547 && __from_chars_alnum_to_val<_DecOnly>(*__first) < __base)
558 template<
typename _Tp,
560 is_same<char, remove_cv_t<_Tp>>>::value,
int> = 0>
561 _GLIBCXX23_CONSTEXPR from_chars_result
562 from_chars(
const char* __first,
const char* __last, _Tp& __value,
565 __glibcxx_assert(2 <= __base && __base <= 36);
571 if (__first != __last && *__first ==
'-')
577 using _Up = __detail::__unsigned_least_t<_Tp>;
580 const auto __start = __first;
582 if ((__base & (__base - 1)) == 0)
589 else if (__base <= 10)
594 if (__builtin_expect(__first == __start, 0))
595 __res.ec = errc::invalid_argument;
600 __res.ec = errc::result_out_of_range;
606 if (__builtin_mul_overflow(__val, __sign, &__tmp))
607 __res.ec = errc::result_out_of_range;
613 if constexpr (__gnu_cxx::__int_traits<_Up>::__max
614 > __gnu_cxx::__int_traits<_Tp>::__max)
616 if (__val > __gnu_cxx::__int_traits<_Tp>::__max)
617 __res.ec = errc::result_out_of_range;
638 {
return (
chars_format)((unsigned)__lhs | (unsigned)__rhs); }
643 {
return (
chars_format)((unsigned)__lhs & (unsigned)__rhs); }
648 {
return (
chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
657 {
return __lhs = __lhs | __rhs; }
661 {
return __lhs = __lhs & __rhs; }
665 {
return __lhs = __lhs ^ __rhs; }
667#if defined __cpp_lib_to_chars || _GLIBCXX_HAVE_USELOCALE
669 from_chars(
const char* __first,
const char* __last,
float& __value,
673 from_chars(
const char* __first,
const char* __last,
double& __value,
677 from_chars(
const char* __first,
const char* __last,
long double& __value,
683 __from_chars_float16_t(
const char* __first,
const char* __last,
687 __from_chars_bfloat16_t(
const char* __first,
const char* __last,
691#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
692 && defined(__cpp_lib_to_chars)
694 from_chars(
const char* __first,
const char* __last, _Float16& __value,
699 = __from_chars_float16_t(__first, __last, __val, __fmt);
700 if (__res.ec == errc{})
701 __value = _Float16(__val);
706#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
708 from_chars(
const char* __first,
const char* __last, _Float32& __value,
713 if (__res.ec == errc{})
714 __value = _Float32(__val);
719#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
721 from_chars(
const char* __first,
const char* __last, _Float64& __value,
726 if (__res.ec == errc{})
727 __value = _Float64(__val);
732#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
734 from_chars(
const char* __first,
const char* __last, _Float128& __value,
739 if (__res.ec == errc{})
740 __value = _Float128(__val);
743#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
744#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
746 from_chars(
const char* __first,
const char* __last, __ieee128& __value,
750 from_chars(
const char* __first,
const char* __last, _Float128& __value,
753 __extension__ __ieee128 __val;
755 if (__res.ec == errc{})
756 __value = _Float128(__val);
761 from_chars(
const char* __first,
const char* __last, _Float128& __value,
766#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
767 && defined(__cpp_lib_to_chars)
769 from_chars(
const char* __first,
const char* __last,
770 __gnu_cxx::__bfloat16_t & __value,
775 = __from_chars_bfloat16_t(__first, __last, __val, __fmt);
776 if (__res.ec == errc{})
777 __value = __gnu_cxx::__bfloat16_t(__val);
783#if defined __cpp_lib_to_chars
787 to_chars_result to_chars(
char* __first,
char* __last,
float __value)
noexcept;
794 to_chars_result to_chars(
char* __first,
char* __last,
double __value)
noexcept;
801 to_chars_result to_chars(
char* __first,
char* __last,
long double __value)
803 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
805 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
817#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
819 to_chars(
char* __first,
char* __last, _Float16 __value)
noexcept
821 return __to_chars_float16_t(__first, __last,
float(__value),
825 to_chars(
char* __first,
char* __last, _Float16 __value,
827 {
return __to_chars_float16_t(__first, __last,
float(__value), __fmt); }
829 to_chars(
char* __first,
char* __last, _Float16 __value,
831 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
834#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
836 to_chars(
char* __first,
char* __last, _Float32 __value)
noexcept
837 {
return to_chars(__first, __last,
float(__value)); }
839 to_chars(
char* __first,
char* __last, _Float32 __value,
841 {
return to_chars(__first, __last,
float(__value), __fmt); }
843 to_chars(
char* __first,
char* __last, _Float32 __value,
845 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
848#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
850 to_chars(
char* __first,
char* __last, _Float64 __value)
noexcept
851 {
return to_chars(__first, __last,
double(__value)); }
853 to_chars(
char* __first,
char* __last, _Float64 __value,
855 {
return to_chars(__first, __last,
double(__value), __fmt); }
857 to_chars(
char* __first,
char* __last, _Float64 __value,
859 {
return to_chars(__first, __last,
double(__value), __fmt, __precision); }
862#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
864 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
865 {
return to_chars(__first, __last,
static_cast<long double>(__value)); }
867 to_chars(
char* __first,
char* __last, _Float128 __value,
870 return to_chars(__first, __last,
static_cast<long double>(__value), __fmt);
873 to_chars(
char* __first,
char* __last, _Float128 __value,
876 return to_chars(__first, __last,
static_cast<long double>(__value), __fmt,
879#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
880#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
882 to_chars(
char* __first,
char* __last, __float128 __value)
noexcept;
884 to_chars(
char* __first,
char* __last, __float128 __value,
887 to_chars(
char* __first,
char* __last, __float128 __value,
891 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
893 return __extension__ to_chars(__first, __last,
894 static_cast<__float128
>(__value));
897 to_chars(
char* __first,
char* __last, _Float128 __value,
901 return __extension__ to_chars(__first, __last,
902 static_cast<__float128
>(__value), __fmt);
905 to_chars(
char* __first,
char* __last, _Float128 __value,
909 return __extension__ to_chars(__first, __last,
910 static_cast<__float128
>(__value), __fmt,
914 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value)
916 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value,
918 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value,
923#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
925 to_chars(
char* __first,
char* __last,
926 __gnu_cxx::__bfloat16_t __value)
noexcept
928 return __to_chars_bfloat16_t(__first, __last,
float(__value),
932 to_chars(
char* __first,
char* __last, __gnu_cxx::__bfloat16_t __value,
934 {
return __to_chars_bfloat16_t(__first, __last,
float(__value), __fmt); }
936 to_chars(
char* __first,
char* __last, __gnu_cxx::__bfloat16_t __value,
938 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
942_GLIBCXX_END_NAMESPACE_VERSION
945#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.