29#ifndef _GLIBCXX_CHARCONV
30#define _GLIBCXX_CHARCONV 1
32#pragma GCC system_header
40#if __cplusplus >= 201402L
45#include <bits/error_constants.h>
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;
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)
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{} };
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
401 to_chars_result to_chars(
char*,
char*,
bool,
int = 10) =
delete;
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(
__base);
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(
__base);
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,
576 if (__first != __last && *__first ==
'-')
582 using _Up = __detail::__unsigned_least_t<_Tp>;
585 const auto __start = __first;
590 __valid = __detail::__from_chars_pow2_base<true>(__first, __last, __val,
__base);
592 __valid = __detail::__from_chars_pow2_base<false>(__first, __last, __val,
__base);
595 __valid = __detail::__from_chars_alnum<true>(__first, __last, __val,
__base);
597 __valid = __detail::__from_chars_alnum<false>(__first, __last, __val,
__base);
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;
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)
694 inline from_chars_result
695 from_chars(
const char* __first,
const char* __last, _Float16& __value,
699 from_chars_result __res
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)
708 inline from_chars_result
709 from_chars(
const char* __first,
const char* __last, _Float32& __value,
713 from_chars_result __res =
from_chars(__first, __last, __val, __fmt);
714 if (__res.ec == errc{})
720#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
721 inline from_chars_result
722 from_chars(
const char* __first,
const char* __last, _Float64& __value,
726 from_chars_result __res =
from_chars(__first, __last, __val, __fmt);
727 if (__res.ec == errc{})
733#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
734 inline from_chars_result
735 from_chars(
const char* __first,
const char* __last, _Float128& __value,
739 from_chars_result __res =
from_chars(__first, __last, __val, __fmt);
740 if (__res.ec == errc{})
744#elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
745#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
746 __extension__ from_chars_result
747 from_chars(
const char* __first,
const char* __last, __ieee128& __value,
750 inline from_chars_result
751 from_chars(
const char* __first,
const char* __last, _Float128& __value,
754 __extension__ __ieee128 __val;
755 from_chars_result __res =
from_chars(__first, __last, __val, __fmt);
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)
769 inline from_chars_result
770 from_chars(
const char* __first,
const char* __last,
771 __gnu_cxx::__bfloat16_t & __value,
775 from_chars_result __res
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;
789 to_chars_result to_chars(
char* __first,
char* __last,
float __value,
791 to_chars_result to_chars(
char* __first,
char* __last,
float __value,
795 to_chars_result to_chars(
char* __first,
char* __last,
double __value)
noexcept;
796 to_chars_result to_chars(
char* __first,
char* __last,
double __value,
798 to_chars_result to_chars(
char* __first,
char* __last,
double __value,
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,
811 to_chars_result __to_chars_float16_t(
char* __first,
char* __last,
814 to_chars_result __to_chars_bfloat16_t(
char* __first,
char* __last,
818#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
819 inline to_chars_result
820 to_chars(
char* __first,
char* __last, _Float16 __value)
noexcept
822 return __to_chars_float16_t(__first, __last,
float(__value),
825 inline to_chars_result
826 to_chars(
char* __first,
char* __last, _Float16 __value,
828 {
return __to_chars_float16_t(__first, __last,
float(__value), __fmt); }
829 inline to_chars_result
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)
836 inline to_chars_result
837 to_chars(
char* __first,
char* __last, _Float32 __value)
noexcept
838 {
return to_chars(__first, __last,
float(__value)); }
839 inline to_chars_result
840 to_chars(
char* __first,
char* __last, _Float32 __value,
842 {
return to_chars(__first, __last,
float(__value), __fmt); }
843 inline to_chars_result
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)
850 inline to_chars_result
851 to_chars(
char* __first,
char* __last, _Float64 __value)
noexcept
852 {
return to_chars(__first, __last,
double(__value)); }
853 inline to_chars_result
854 to_chars(
char* __first,
char* __last, _Float64 __value,
856 {
return to_chars(__first, __last,
double(__value), __fmt); }
857 inline to_chars_result
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)
864 inline to_chars_result
865 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
866 {
return to_chars(__first, __last,
static_cast<long double>(__value)); }
867 inline to_chars_result
868 to_chars(
char* __first,
char* __last, _Float128 __value,
871 return to_chars(__first, __last,
static_cast<long double>(__value), __fmt);
873 inline to_chars_result
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
882 __extension__ to_chars_result
883 to_chars(
char* __first,
char* __last, __float128 __value)
noexcept;
884 __extension__ to_chars_result
885 to_chars(
char* __first,
char* __last, __float128 __value,
887 __extension__ to_chars_result
888 to_chars(
char* __first,
char* __last, __float128 __value,
891 inline to_chars_result
892 to_chars(
char* __first,
char* __last, _Float128 __value)
noexcept
894 return __extension__ to_chars(__first, __last,
895 static_cast<__float128
>(__value));
897 inline to_chars_result
898 to_chars(
char* __first,
char* __last, _Float128 __value,
902 return __extension__ to_chars(__first, __last,
903 static_cast<__float128
>(__value), __fmt);
905 inline to_chars_result
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)
925 inline to_chars_result
926 to_chars(
char* __first,
char* __last,
927 __gnu_cxx::__bfloat16_t __value)
noexcept
929 return __to_chars_bfloat16_t(__first, __last,
float(__value),
932 inline to_chars_result
933 to_chars(
char* __first,
char* __last, __gnu_cxx::__bfloat16_t __value,
935 {
return __to_chars_bfloat16_t(__first, __last,
float(__value), __fmt); }
936 inline to_chars_result
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.
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.