29#ifndef _GLIBCXX_CHARCONV
30#define _GLIBCXX_CHARCONV 1
32#pragma GCC system_header
40#if __cplusplus >= 201402L
48#define __glibcxx_want_to_chars
49#define __glibcxx_want_constexpr_charconv
52namespace std _GLIBCXX_VISIBILITY(default)
54_GLIBCXX_BEGIN_NAMESPACE_VERSION
62#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
66#if __cplusplus > 202302L
67 constexpr explicit operator bool()
const noexcept {
return ec == errc{}; }
77#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
81#if __cplusplus > 202302L
82 constexpr explicit operator bool()
const noexcept {
return ec == errc{}; }
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 __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
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;
109 template<
typename _Tp>
111 __to_chars_len(_Tp __value,
int __base )
noexcept;
113 template<
typename _Tp>
115 __to_chars_len_2(_Tp __value)
noexcept
116 {
return std::__bit_width(__value); }
119 template<
typename _Tp>
120 constexpr to_chars_result
121 __to_chars(
char* __first,
char* __last, _Tp __val,
int __base)
noexcept
123 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
125 to_chars_result __res;
127 const unsigned __len = __to_chars_len(__val, __base);
129 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
132 __res.ec = errc::value_too_large;
136 unsigned __pos = __len - 1;
138 constexpr char __digits[] = {
139 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
140 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
141 'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
142 'u',
'v',
'w',
'x',
'y',
'z'
145 while (__val >= (
unsigned)__base)
147 auto const __quo = __val /
__base;
148 auto const __rem = __val %
__base;
149 __first[__pos--] = __digits[__rem];
152 *__first = __digits[__val];
154 __res.ptr = __first + __len;
159 template<
typename _Tp>
160 constexpr to_chars_result
161 __to_chars_16(
char* __first,
char* __last, _Tp __val)
noexcept
163 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
165 to_chars_result __res;
167 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
169 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
172 __res.ec = errc::value_too_large;
176 constexpr char __digits[] = {
177 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
178 'a',
'b',
'c',
'd',
'e',
'f'
180 unsigned __pos = __len - 1;
181 while (__val >= 0x100)
183 auto __num = __val & 0xF;
185 __first[__pos] = __digits[__num];
188 __first[__pos - 1] = __digits[__num];
193 const auto __num = __val & 0xF;
195 __first[1] = __digits[__num];
196 __first[0] = __digits[__val];
199 __first[0] = __digits[__val];
200 __res.ptr = __first + __len;
205 template<
typename _Tp>
206 constexpr to_chars_result
207 __to_chars_10(
char* __first,
char* __last, _Tp __val)
noexcept
209 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
211 to_chars_result __res;
213 const unsigned __len = __to_chars_len(__val, 10);
215 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
218 __res.ec = errc::value_too_large;
222 __detail::__to_chars_10_impl(__first, __len, __val);
223 __res.ptr = __first + __len;
228 template<
typename _Tp>
229 constexpr to_chars_result
230 __to_chars_8(
char* __first,
char* __last, _Tp __val)
noexcept
232 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
234 to_chars_result __res;
239 __len = __val > 077777u ? 6u
240 : __val > 07777u ? 5u
247 __len = (__to_chars_len_2(__val) + 2) / 3;
249 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
252 __res.ec = errc::value_too_large;
256 unsigned __pos = __len - 1;
257 while (__val >= 0100)
259 auto __num = __val & 7;
261 __first[__pos] =
'0' + __num;
264 __first[__pos - 1] =
'0' + __num;
269 auto const __num = __val & 7;
271 __first[1] =
'0' + __num;
272 __first[0] =
'0' + __val;
275 __first[0] =
'0' + __val;
276 __res.ptr = __first + __len;
281 template<
typename _Tp>
282 constexpr to_chars_result
283 __to_chars_2(
char* __first,
char* __last, _Tp __val)
noexcept
285 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
287 to_chars_result __res;
289 const unsigned __len = __to_chars_len_2(__val);
291 if (__builtin_expect(
size_t(__last - __first) < __len, 0))
294 __res.ec = errc::value_too_large;
298 unsigned __pos = __len - 1;
302 __first[__pos--] =
'0' + (__val & 1);
310 __res.ptr = __first + __len;
317 template<
typename _Tp>
318 constexpr to_chars_result
319 __to_chars_i(
char* __first,
char* __last, _Tp __value,
int __base = 10)
321 __glibcxx_assert(2 <= __base && __base <= 36);
323 using _Up = __detail::__unsigned_least_t<_Tp>;
324 _Up __unsigned_val = __value;
326 if (__first >= __last) [[__unlikely__]]
327 return { __last, errc::value_too_large };
332 return { __first + 1, errc{} };
338 __unsigned_val = _Up(~__value) + _Up(1);
344 return __detail::__to_chars_16(__first, __last, __unsigned_val);
346 return __detail::__to_chars_10(__first, __last, __unsigned_val);
348 return __detail::__to_chars_8(__first, __last, __unsigned_val);
350 return __detail::__to_chars_2(__first, __last, __unsigned_val);
352 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
356#define _GLIBCXX_TO_CHARS(T) \
357 _GLIBCXX23_CONSTEXPR inline to_chars_result \
358 to_chars(char* __first, char* __last, T __value, int __base = 10) \
359 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
360_GLIBCXX_TO_CHARS(
char)
361_GLIBCXX_TO_CHARS(
signed char)
362_GLIBCXX_TO_CHARS(
unsigned char)
363_GLIBCXX_TO_CHARS(
signed short)
364_GLIBCXX_TO_CHARS(
unsigned short)
365_GLIBCXX_TO_CHARS(
signed int)
366_GLIBCXX_TO_CHARS(
unsigned int)
367_GLIBCXX_TO_CHARS(
signed long)
368_GLIBCXX_TO_CHARS(
unsigned long)
369_GLIBCXX_TO_CHARS(
signed long long)
370_GLIBCXX_TO_CHARS(
unsigned long long)
371#if defined(__GLIBCXX_TYPE_INT_N_0)
372_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_0)
373_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_0)
375#if defined(__GLIBCXX_TYPE_INT_N_1)
376_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_1)
377_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_1)
379#if defined(__GLIBCXX_TYPE_INT_N_2)
380_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_2)
381_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_2)
383#if defined(__GLIBCXX_TYPE_INT_N_3)
384_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_3)
385_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_3)
387#undef _GLIBCXX_TO_CHARS
391 to_chars_result to_chars(
char*,
char*,
bool,
int = 10) =
delete;
395 template<
typename _Tp>
397 __raise_and_add(_Tp& __val,
int __base,
unsigned char __c)
399 if (__builtin_mul_overflow(__val, __base, &__val)
400 || __builtin_add_overflow(__val, __c, &__val))
405 template<
bool _DecOnly>
406 struct __from_chars_alnum_to_val_table
408 struct type {
unsigned char __data[1u << __CHAR_BIT__] = {}; };
412 static constexpr type
415 constexpr unsigned char __lower_letters[27] =
"abcdefghijklmnopqrstuvwxyz";
416 constexpr unsigned char __upper_letters[27] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
418 for (
auto& __entry : __table.__data)
420 for (
int __i = 0; __i < 10; ++__i)
421 __table.__data[
'0' + __i] = __i;
422 for (
int __i = 0; __i < 26; ++__i)
424 __table.__data[__lower_letters[__i]] = 10 + __i;
425 __table.__data[__upper_letters[__i]] = 10 + __i;
433 static constexpr type value = (_DecOnly, _S_make_table());
436#if ! __cpp_inline_variables
437 template<
bool _DecOnly>
438 const typename __from_chars_alnum_to_val_table<_DecOnly>::type
439 __from_chars_alnum_to_val_table<_DecOnly>::value;
446 template<
bool _DecOnly = false>
447 _GLIBCXX20_CONSTEXPR
unsigned char
448 __from_chars_alnum_to_val(
unsigned char __c)
450 if _GLIBCXX17_CONSTEXPR (_DecOnly)
451 return static_cast<unsigned char>(__c -
'0');
453 return __from_chars_alnum_to_val_table<_DecOnly>::value.__data[__c];
458 template<
bool _DecOnly,
typename _Tp>
459 _GLIBCXX23_CONSTEXPR
bool
468 const int __log2_base = __countr_zero(
unsigned(__base & 0x3f));
470 const ptrdiff_t __len = __last - __first;
472 while (__i < __len && __first[__i] ==
'0')
474 const ptrdiff_t __leading_zeroes = __i;
475 if (__i >= __len) [[__unlikely__]]
482 unsigned char __leading_c = 0;
485 __leading_c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
487 if (__leading_c >= __base) [[__unlikely__]]
496 for (; __i < __len; ++__i)
498 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
501 __val = (__val << __log2_base) | __c;
504 auto __significant_bits = (__i - __leading_zeroes) * __log2_base;
508 __significant_bits -= __log2_base - __bit_width(__leading_c);
510 return __significant_bits <= __gnu_cxx::__int_traits<_Tp>::__digits;
515 template<
bool _DecOnly,
typename _Tp>
522 const int __bits_per_digit = __bit_width(
unsigned(__base & 0x3f));
524 for (; __first != __last; ++__first)
526 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(*__first);
530 __unused_bits_lower_bound -= __bits_per_digit;
531 if (__unused_bits_lower_bound >= 0) [[__likely__]]
533 __val = __val * __base + __c;
534 else if (!__raise_and_add(__val, __base, __c)) [[__unlikely__]]
536 while (++__first != __last
537 && __from_chars_alnum_to_val<_DecOnly>(*__first) < __base)
548 template<
typename _Tp,
549 enable_if_t<__or_<__is_standard_integer<_Tp>,
550 is_same<char, remove_cv_t<_Tp>>>::value,
int> = 0>
551 _GLIBCXX23_CONSTEXPR from_chars_result
552 from_chars(
const char* __first,
const char* __last, _Tp& __value,
555 __glibcxx_assert(2 <= __base && __base <= 36);
561 if (__first != __last && *__first ==
'-')
567 using _Up = __detail::__unsigned_least_t<_Tp>;
570 const auto __start = __first;
572 if ((__base & (__base - 1)) == 0)
575 __valid = __detail::__from_chars_pow2_base<true>(__first, __last, __val, __base);
577 __valid = __detail::__from_chars_pow2_base<false>(__first, __last, __val, __base);
579 else if (__base <= 10)
580 __valid = __detail::__from_chars_alnum<true>(__first, __last, __val, __base);
582 __valid = __detail::__from_chars_alnum<false>(__first, __last, __val, __base);
584 if (__builtin_expect(__first == __start, 0))
585 __res.ec = errc::invalid_argument;
590 __res.ec = errc::result_out_of_range;
596 if (__builtin_mul_overflow(__val, __sign, &__tmp))
597 __res.ec = errc::result_out_of_range;
607 __res.ec = errc::result_out_of_range;
628 {
return (
chars_format)((unsigned)__lhs | (
unsigned)__rhs); }
633 {
return (
chars_format)((unsigned)__lhs & (
unsigned)__rhs); }
638 {
return (
chars_format)((unsigned)__lhs ^ (
unsigned)__rhs); }
647 {
return __lhs = __lhs | __rhs; }
651 {
return __lhs = __lhs & __rhs; }
655 {
return __lhs = __lhs ^ __rhs; }
657#if defined __cpp_lib_to_chars || _GLIBCXX_HAVE_USELOCALE
659 from_chars(
const char* __first,
const char* __last,
float& __value,
663 from_chars(
const char* __first,
const char* __last,
double& __value,
667 from_chars(
const char* __first,
const char* __last,
long double& __value,
673 __from_chars_float16_t(
const char* __first,
const char* __last,
677 __from_chars_bfloat16_t(
const char* __first,
const char* __last,
681#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
682 && defined(__cpp_lib_to_chars)
683 inline from_chars_result
684 from_chars(
const char* __first,
const char* __last, _Float16& __value,
688 from_chars_result __res
689 = __from_chars_float16_t(__first, __last, __val, __fmt);
690 if (__res.ec == errc{})
691 __value = _Float16(__val);
696#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
697 inline from_chars_result
698 from_chars(
const char* __first,
const char* __last, _Float32& __value,
702 from_chars_result __res =
from_chars(__first, __last, __val, __fmt);
703 if (__res.ec == errc{})
704 __value = _Float32(__val);
709#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
710 inline from_chars_result
711 from_chars(
const char* __first,
const char* __last, _Float64& __value,
715 from_chars_result __res =
from_chars(__first, __last, __val, __fmt);
716 if (__res.ec == errc{})
717 __value = _Float64(__val);
722#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
723 inline from_chars_result
724 from_chars(
const char* __first,
const char* __last, _Float128& __value,
728 from_chars_result __res =
from_chars(__first, __last, __val, __fmt);
729 if (__res.ec == errc{})
730 __value = _Float128(__val);
733#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
734#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
735 __extension__ from_chars_result
736 from_chars(
const char* __first,
const char* __last, __ieee128& __value,
739 inline from_chars_result
740 from_chars(
const char* __first,
const char* __last, _Float128& __value,
743 __extension__ __ieee128 __val;
744 from_chars_result __res =
from_chars(__first, __last, __val, __fmt);
745 if (__res.ec == errc{})
746 __value = _Float128(__val);
751 from_chars(
const char* __first,
const char* __last, _Float128& __value,
756#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
757 && defined(__cpp_lib_to_chars)
758 inline from_chars_result
759 from_chars(
const char* __first,
const char* __last,
760 __gnu_cxx::__bfloat16_t & __value,
764 from_chars_result __res
765 = __from_chars_bfloat16_t(__first, __last, __val, __fmt);
766 if (__res.ec == errc{})
767 __value = __gnu_cxx::__bfloat16_t(__val);
773#if defined __cpp_lib_to_chars
777 to_chars_result to_chars(
char* __first,
char* __last,
float __value)
noexcept;
778 to_chars_result to_chars(
char* __first,
char* __last,
float __value,
780 to_chars_result to_chars(
char* __first,
char* __last,
float __value,
784 to_chars_result to_chars(
char* __first,
char* __last,
double __value)
noexcept;
785 to_chars_result to_chars(
char* __first,
char* __last,
double __value,
787 to_chars_result to_chars(
char* __first,
char* __last,
double __value,
791 to_chars_result to_chars(
char* __first,
char* __last,
long double __value)
793 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
795 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
800 to_chars_result __to_chars_float16_t(
char* __first,
char* __last,
803 to_chars_result __to_chars_bfloat16_t(
char* __first,
char* __last,
807#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
808 inline to_chars_result
809 to_chars(
char* __first,
char* __last, _Float16 __value)
noexcept
811 return __to_chars_float16_t(__first, __last,
float(__value),
814 inline to_chars_result
815 to_chars(
char* __first,
char* __last, _Float16 __value,
817 {
return __to_chars_float16_t(__first, __last,
float(__value), __fmt); }
818 inline to_chars_result
819 to_chars(
char* __first,
char* __last, _Float16 __value,
821 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
824#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
825 inline to_chars_result
826 to_chars(
char* __first,
char* __last, _Float32 __value)
noexcept
827 {
return to_chars(__first, __last,
float(__value)); }
828 inline to_chars_result
829 to_chars(
char* __first,
char* __last, _Float32 __value,
831 {
return to_chars(__first, __last,
float(__value), __fmt); }
832 inline to_chars_result
833 to_chars(
char* __first,
char* __last, _Float32 __value,
835 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
838#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
839 inline to_chars_result
840 to_chars(
char* __first,
char* __last, _Float64 __value)
noexcept
841 {
return to_chars(__first, __last,
double(__value)); }
842 inline to_chars_result
843 to_chars(
char* __first,
char* __last, _Float64 __value,
845 {
return to_chars(__first, __last,
double(__value), __fmt); }
846 inline to_chars_result
847 to_chars(
char* __first,
char* __last, _Float64 __value,
849 {
return to_chars(__first, __last,
double(__value), __fmt, __precision); }
852#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
853 inline to_chars_result
854 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
855 {
return to_chars(__first, __last,
static_cast<long double>(__value)); }
856 inline to_chars_result
857 to_chars(
char* __first,
char* __last, _Float128 __value,
860 return to_chars(__first, __last,
static_cast<long double>(__value), __fmt);
862 inline to_chars_result
863 to_chars(
char* __first,
char* __last, _Float128 __value,
866 return to_chars(__first, __last,
static_cast<long double>(__value), __fmt,
869#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
870#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
871 __extension__ to_chars_result
872 to_chars(
char* __first,
char* __last, __float128 __value)
noexcept;
873 __extension__ to_chars_result
874 to_chars(
char* __first,
char* __last, __float128 __value,
876 __extension__ to_chars_result
877 to_chars(
char* __first,
char* __last, __float128 __value,
880 inline to_chars_result
881 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
883 return __extension__ to_chars(__first, __last,
884 static_cast<__float128
>(__value));
886 inline to_chars_result
887 to_chars(
char* __first,
char* __last, _Float128 __value,
891 return __extension__ to_chars(__first, __last,
892 static_cast<__float128
>(__value), __fmt);
894 inline to_chars_result
895 to_chars(
char* __first,
char* __last, _Float128 __value,
899 return __extension__ to_chars(__first, __last,
900 static_cast<__float128
>(__value), __fmt,
904 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value)
906 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value,
908 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value,
913#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
914 inline to_chars_result
915 to_chars(
char* __first,
char* __last,
916 __gnu_cxx::__bfloat16_t __value)
noexcept
918 return __to_chars_bfloat16_t(__first, __last,
float(__value),
921 inline to_chars_result
922 to_chars(
char* __first,
char* __last, __gnu_cxx::__bfloat16_t __value,
924 {
return __to_chars_bfloat16_t(__first, __last,
float(__value), __fmt); }
925 inline to_chars_result
926 to_chars(
char* __first,
char* __last, __gnu_cxx::__bfloat16_t __value,
928 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
932_GLIBCXX_END_NAMESPACE_VERSION
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.
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,...
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
constexpr _Iterator __base(_Iterator __it)
Result type of std::to_chars.
Result type of std::from_chars.