29#ifndef _GLIBCXX_CHARCONV
30#define _GLIBCXX_CHARCONV 1
32#pragma GCC system_header
40#if __cplusplus >= 201402L
48#if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \
49 && __SIZE_WIDTH__ >= 32 && _GLIBCXX_HOSTED
50# define __cpp_lib_to_chars 201611L
53#if __cplusplus > 202002L
54# define __cpp_lib_constexpr_charconv 202207L
57namespace std _GLIBCXX_VISIBILITY(default)
59_GLIBCXX_BEGIN_NAMESPACE_VERSION
67#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
79#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
87 template<
typename _Tp>
88 using __integer_to_chars_result_type
90 __is_unsigned_integer<_Tp>,
91#if defined __SIZEOF_INT128__ && defined __STRICT_ANSI__
103 template<
typename _Tp>
104 struct __to_chars_unsigned_type : __make_unsigned_selector_base
106 using _UInts = _List<
unsigned int,
unsigned long,
unsigned long long
107#if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
111 using type =
typename __select<
sizeof(_Tp), _UInts>::__type;
114 template<
typename _Tp>
115 using __unsigned_least_t =
typename __to_chars_unsigned_type<_Tp>::type;
119 template<
typename _Tp>
121 __to_chars_len(_Tp __value,
int __base )
noexcept;
123 template<
typename _Tp>
125 __to_chars_len_2(_Tp __value)
noexcept
126 {
return std::__bit_width(__value); }
129 template<
typename _Tp>
130 constexpr to_chars_result
131 __to_chars(
char* __first,
char* __last, _Tp __val,
int __base)
noexcept
133 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
135 to_chars_result __res;
137 const unsigned __len = __to_chars_len(__val, __base);
139 if (__builtin_expect((__last - __first) < __len, 0))
142 __res.ec = errc::value_too_large;
146 unsigned __pos = __len - 1;
148 constexpr char __digits[] = {
149 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
150 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
151 'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
152 'u',
'v',
'w',
'x',
'y',
'z'
155 while (__val >= (
unsigned)__base)
157 auto const __quo = __val /
__base;
158 auto const __rem = __val %
__base;
159 __first[__pos--] = __digits[__rem];
162 *__first = __digits[__val];
164 __res.ptr = __first + __len;
169 template<
typename _Tp>
170 constexpr __integer_to_chars_result_type<_Tp>
171 __to_chars_16(
char* __first,
char* __last, _Tp __val)
noexcept
173 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
175 to_chars_result __res;
177 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
179 if (__builtin_expect((__last - __first) < __len, 0))
182 __res.ec = errc::value_too_large;
186 constexpr char __digits[] = {
187 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
188 'a',
'b',
'c',
'd',
'e',
'f'
190 unsigned __pos = __len - 1;
191 while (__val >= 0x100)
193 auto __num = __val & 0xF;
195 __first[__pos] = __digits[__num];
198 __first[__pos - 1] = __digits[__num];
203 const auto __num = __val & 0xF;
205 __first[1] = __digits[__num];
206 __first[0] = __digits[__val];
209 __first[0] = __digits[__val];
210 __res.ptr = __first + __len;
215 template<
typename _Tp>
216 constexpr __integer_to_chars_result_type<_Tp>
217 __to_chars_10(
char* __first,
char* __last, _Tp __val)
noexcept
219 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
221 to_chars_result __res;
223 const unsigned __len = __to_chars_len(__val, 10);
225 if (__builtin_expect((__last - __first) < __len, 0))
228 __res.ec = errc::value_too_large;
232 __detail::__to_chars_10_impl(__first, __len, __val);
233 __res.ptr = __first + __len;
238 template<
typename _Tp>
239 constexpr __integer_to_chars_result_type<_Tp>
240 __to_chars_8(
char* __first,
char* __last, _Tp __val)
noexcept
242 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
244 to_chars_result __res;
247 if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Tp>::__digits <= 16)
249 __len = __val > 077777u ? 6u
250 : __val > 07777u ? 5u
257 __len = (__to_chars_len_2(__val) + 2) / 3;
259 if (__builtin_expect((__last - __first) < __len, 0))
262 __res.ec = errc::value_too_large;
266 unsigned __pos = __len - 1;
267 while (__val >= 0100)
269 auto __num = __val & 7;
271 __first[__pos] =
'0' + __num;
274 __first[__pos - 1] =
'0' + __num;
279 auto const __num = __val & 7;
281 __first[1] =
'0' + __num;
282 __first[0] =
'0' + __val;
285 __first[0] =
'0' + __val;
286 __res.ptr = __first + __len;
291 template<
typename _Tp>
292 constexpr __integer_to_chars_result_type<_Tp>
293 __to_chars_2(
char* __first,
char* __last, _Tp __val)
noexcept
295 static_assert(__integer_to_chars_is_unsigned<_Tp>,
"implementation bug");
297 to_chars_result __res;
299 const unsigned __len = __to_chars_len_2(__val);
301 if (__builtin_expect((__last - __first) < __len, 0))
304 __res.ec = errc::value_too_large;
308 unsigned __pos = __len - 1;
312 __first[__pos--] =
'0' + (__val & 1);
320 __res.ptr = __first + __len;
327 template<
typename _Tp>
328 constexpr __detail::__integer_to_chars_result_type<_Tp>
329 __to_chars_i(
char* __first,
char* __last, _Tp __value,
int __base = 10)
331 __glibcxx_assert(2 <= __base && __base <= 36);
333 using _Up = __detail::__unsigned_least_t<_Tp>;
334 _Up __unsigned_val = __value;
336 if (__first == __last) [[__unlikely__]]
337 return { __last, errc::value_too_large };
342 return { __first + 1, errc{} };
344 else if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
348 __unsigned_val = _Up(~__value) + _Up(1);
354 return __detail::__to_chars_16(__first, __last, __unsigned_val);
356 return __detail::__to_chars_10(__first, __last, __unsigned_val);
358 return __detail::__to_chars_8(__first, __last, __unsigned_val);
360 return __detail::__to_chars_2(__first, __last, __unsigned_val);
362 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
366#define _GLIBCXX_TO_CHARS(T) \
367 _GLIBCXX23_CONSTEXPR inline to_chars_result \
368 to_chars(char* __first, char* __last, T __value, int __base = 10) \
369 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
370_GLIBCXX_TO_CHARS(
char)
371_GLIBCXX_TO_CHARS(
signed char)
372_GLIBCXX_TO_CHARS(
unsigned char)
373_GLIBCXX_TO_CHARS(
signed short)
374_GLIBCXX_TO_CHARS(
unsigned short)
375_GLIBCXX_TO_CHARS(
signed int)
376_GLIBCXX_TO_CHARS(
unsigned int)
377_GLIBCXX_TO_CHARS(
signed long)
378_GLIBCXX_TO_CHARS(
unsigned long)
379_GLIBCXX_TO_CHARS(
signed long long)
380_GLIBCXX_TO_CHARS(
unsigned long long)
381#if defined(__GLIBCXX_TYPE_INT_N_0)
382_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_0)
383_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_0)
385#if defined(__GLIBCXX_TYPE_INT_N_1)
386_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_1)
387_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_1)
389#if defined(__GLIBCXX_TYPE_INT_N_2)
390_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_2)
391_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_2)
393#if defined(__GLIBCXX_TYPE_INT_N_3)
394_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_3)
395_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_3)
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 _GLIBCXX17_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)
555 template<
typename _Tp>
556 using __integer_from_chars_result_type
558 __is_unsigned_integer<_Tp>,
565 template<
typename _Tp>
566 _GLIBCXX23_CONSTEXPR __detail::__integer_from_chars_result_type<_Tp>
567 from_chars(
const char* __first,
const char* __last, _Tp& __value,
570 __glibcxx_assert(2 <= __base && __base <= 36);
576 if (__first != __last && *__first ==
'-')
582 using _Up = __detail::__unsigned_least_t<_Tp>;
585 const auto __start = __first;
587 if ((__base & (__base - 1)) == 0)
594 else if (__base <= 10)
599 if (__builtin_expect(__first == __start, 0))
600 __res.ec = errc::invalid_argument;
605 __res.ec = errc::result_out_of_range;
611 if (__builtin_mul_overflow(__val, __sign, &__tmp))
612 __res.ec = errc::result_out_of_range;
618 if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Up>::__max
619 > __gnu_cxx::__int_traits<_Tp>::__max)
621 if (__val > __gnu_cxx::__int_traits<_Tp>::__max)
622 __res.ec = errc::result_out_of_range;
642 {
return (
chars_format)((unsigned)__lhs | (unsigned)__rhs); }
646 {
return (
chars_format)((unsigned)__lhs & (unsigned)__rhs); }
650 {
return (
chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
658 {
return __lhs = __lhs | __rhs; }
662 {
return __lhs = __lhs & __rhs; }
666 {
return __lhs = __lhs ^ __rhs; }
668#if defined __cpp_lib_to_chars || _GLIBCXX_HAVE_USELOCALE
670 from_chars(
const char* __first,
const char* __last,
float& __value,
674 from_chars(
const char* __first,
const char* __last,
double& __value,
678 from_chars(
const char* __first,
const char* __last,
long double& __value,
684 __from_chars_float16_t(
const char* __first,
const char* __last,
688 __from_chars_bfloat16_t(
const char* __first,
const char* __last,
692#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
693 && defined(__cpp_lib_to_chars)
695 from_chars(
const char* __first,
const char* __last, _Float16& __value,
700 = __from_chars_float16_t(__first, __last, __val, __fmt);
701 if (__res.ec == errc{})
707#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
709 from_chars(
const char* __first,
const char* __last, _Float32& __value,
714 if (__res.ec == errc{})
720#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
722 from_chars(
const char* __first,
const char* __last, _Float64& __value,
727 if (__res.ec == errc{})
733#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
735 from_chars(
const char* __first,
const char* __last, _Float128& __value,
740 if (__res.ec == errc{})
744#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
745#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
747 from_chars(
const char* __first,
const char* __last, __ieee128& __value,
751 from_chars(
const char* __first,
const char* __last, _Float128& __value,
754 __extension__ __ieee128 __val;
756 if (__res.ec == errc{})
762 from_chars(
const char* __first,
const char* __last, _Float128& __value,
767#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) \
768 && defined(__cpp_lib_to_chars)
770 from_chars(
const char* __first,
const char* __last,
771 __gnu_cxx::__bfloat16_t & __value,
776 = __from_chars_bfloat16_t(__first, __last, __val, __fmt);
777 if (__res.ec == errc{})
784#if defined __cpp_lib_to_chars
788 to_chars_result to_chars(
char* __first,
char* __last,
float __value)
noexcept;
795 to_chars_result to_chars(
char* __first,
char* __last,
double __value)
noexcept;
802 to_chars_result to_chars(
char* __first,
char* __last,
long double __value)
804 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
806 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
818#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
820 to_chars(
char* __first,
char* __last, _Float16 __value)
noexcept
822 return __to_chars_float16_t(__first, __last,
float(__value),
826 to_chars(
char* __first,
char* __last, _Float16 __value,
828 {
return __to_chars_float16_t(__first, __last,
float(__value), __fmt); }
830 to_chars(
char* __first,
char* __last, _Float16 __value,
832 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
835#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
837 to_chars(
char* __first,
char* __last, _Float32 __value)
noexcept
838 {
return to_chars(__first, __last,
float(__value)); }
840 to_chars(
char* __first,
char* __last, _Float32 __value,
842 {
return to_chars(__first, __last,
float(__value), __fmt); }
844 to_chars(
char* __first,
char* __last, _Float32 __value,
846 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
849#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
851 to_chars(
char* __first,
char* __last, _Float64 __value)
noexcept
852 {
return to_chars(__first, __last,
double(__value)); }
854 to_chars(
char* __first,
char* __last, _Float64 __value,
856 {
return to_chars(__first, __last,
double(__value), __fmt); }
858 to_chars(
char* __first,
char* __last, _Float64 __value,
860 {
return to_chars(__first, __last,
double(__value), __fmt, __precision); }
863#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
865 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
866 {
return to_chars(__first, __last,
static_cast<long double>(__value)); }
868 to_chars(
char* __first,
char* __last, _Float128 __value,
871 return to_chars(__first, __last,
static_cast<long double>(__value), __fmt);
874 to_chars(
char* __first,
char* __last, _Float128 __value,
877 return to_chars(__first, __last,
static_cast<long double>(__value), __fmt,
880#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
881#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
883 to_chars(
char* __first,
char* __last, __float128 __value)
noexcept;
885 to_chars(
char* __first,
char* __last, __float128 __value,
888 to_chars(
char* __first,
char* __last, __float128 __value,
892 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
894 return __extension__ to_chars(__first, __last,
895 static_cast<__float128
>(__value));
898 to_chars(
char* __first,
char* __last, _Float128 __value,
902 return __extension__ to_chars(__first, __last,
903 static_cast<__float128
>(__value), __fmt);
906 to_chars(
char* __first,
char* __last, _Float128 __value,
910 return __extension__ to_chars(__first, __last,
911 static_cast<__float128
>(__value), __fmt,
915 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value)
917 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value,
919 to_chars_result to_chars(
char* __first,
char* __last, _Float128 __value,
924#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
926 to_chars(
char* __first,
char* __last,
927 __gnu_cxx::__bfloat16_t __value)
noexcept
929 return __to_chars_bfloat16_t(__first, __last,
float(__value),
933 to_chars(
char* __first,
char* __last, __gnu_cxx::__bfloat16_t __value,
935 {
return __to_chars_bfloat16_t(__first, __last,
float(__value), __fmt); }
937 to_chars(
char* __first,
char* __last, __gnu_cxx::__bfloat16_t __value,
939 {
return to_chars(__first, __last,
float(__value), __fmt, __precision); }
943_GLIBCXX_END_NAMESPACE_VERSION
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 __detail::__integer_from_chars_result_type< _Tp > from_chars(const char *__first, const char *__last, _Tp &__value, int __base=10)
std::from_chars for integral types.
constexpr bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
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.