29#ifndef _GLIBCXX_CHARCONV
30#define _GLIBCXX_CHARCONV 1
32#pragma GCC system_header
38#if __cplusplus >= 201402L
46#if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \
47 && __SIZE_WIDTH__ >= 32
48# define __cpp_lib_to_chars 201611L
51namespace std _GLIBCXX_VISIBILITY(default)
53_GLIBCXX_BEGIN_NAMESPACE_VERSION
61#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
73#if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
81 template<
typename _Tp>
82 using __integer_to_chars_result_type
84 __is_unsigned_integer<_Tp>,
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>
121 __to_chars(
char* __first,
char* __last, _Tp __val,
int __base)
noexcept
123 static_assert(is_integral<_Tp>::value,
"implementation bug");
124 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
126 to_chars_result __res;
128 const unsigned __len = __to_chars_len(__val, __base);
130 if (__builtin_expect((__last - __first) < __len, 0))
133 __res.ec = errc::value_too_large;
137 unsigned __pos = __len - 1;
139 static constexpr char __digits[] = {
140 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
141 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
142 'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
143 'u',
'v',
'w',
'x',
'y',
'z'
146 while (__val >= (
unsigned)__base)
148 auto const __quo = __val /
__base;
149 auto const __rem = __val %
__base;
150 __first[__pos--] = __digits[__rem];
153 *__first = __digits[__val];
155 __res.ptr = __first + __len;
160 template<
typename _Tp>
161 __integer_to_chars_result_type<_Tp>
162 __to_chars_16(
char* __first,
char* __last, _Tp __val)
noexcept
164 static_assert(is_integral<_Tp>::value,
"implementation bug");
165 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
167 to_chars_result __res;
169 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
171 if (__builtin_expect((__last - __first) < __len, 0))
174 __res.ec = errc::value_too_large;
178 static constexpr char __digits[] = {
179 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
180 'a',
'b',
'c',
'd',
'e',
'f'
182 unsigned __pos = __len - 1;
183 while (__val >= 0x100)
185 auto __num = __val & 0xF;
187 __first[__pos] = __digits[__num];
190 __first[__pos - 1] = __digits[__num];
195 const auto __num = __val & 0xF;
197 __first[1] = __digits[__num];
198 __first[0] = __digits[__val];
201 __first[0] = __digits[__val];
202 __res.ptr = __first + __len;
207 template<
typename _Tp>
208 inline __integer_to_chars_result_type<_Tp>
209 __to_chars_10(
char* __first,
char* __last, _Tp __val)
noexcept
211 static_assert(is_integral<_Tp>::value,
"implementation bug");
212 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
214 to_chars_result __res;
216 const unsigned __len = __to_chars_len(__val, 10);
218 if (__builtin_expect((__last - __first) < __len, 0))
221 __res.ec = errc::value_too_large;
225 __detail::__to_chars_10_impl(__first, __len, __val);
226 __res.ptr = __first + __len;
231 template<
typename _Tp>
232 __integer_to_chars_result_type<_Tp>
233 __to_chars_8(
char* __first,
char* __last, _Tp __val)
noexcept
235 static_assert(is_integral<_Tp>::value,
"implementation bug");
236 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
238 to_chars_result __res;
243 __len = __val > 077777u ? 6u
244 : __val > 07777u ? 5u
251 __len = (__to_chars_len_2(__val) + 2) / 3;
253 if (__builtin_expect((__last - __first) < __len, 0))
256 __res.ec = errc::value_too_large;
260 unsigned __pos = __len - 1;
261 while (__val >= 0100)
263 auto __num = __val & 7;
265 __first[__pos] =
'0' + __num;
268 __first[__pos - 1] =
'0' + __num;
273 auto const __num = __val & 7;
275 __first[1] =
'0' + __num;
276 __first[0] =
'0' + __val;
279 __first[0] =
'0' + __val;
280 __res.ptr = __first + __len;
285 template<
typename _Tp>
286 __integer_to_chars_result_type<_Tp>
287 __to_chars_2(
char* __first,
char* __last, _Tp __val)
noexcept
289 static_assert(is_integral<_Tp>::value,
"implementation bug");
290 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
292 to_chars_result __res;
294 const unsigned __len = __to_chars_len_2(__val);
296 if (__builtin_expect((__last - __first) < __len, 0))
299 __res.ec = errc::value_too_large;
303 unsigned __pos = __len - 1;
307 __first[__pos--] =
'0' + (__val & 1);
315 __res.ptr = __first + __len;
322 template<
typename _Tp>
323 __detail::__integer_to_chars_result_type<_Tp>
324 __to_chars_i(
char* __first,
char* __last, _Tp __value,
int __base = 10)
326 __glibcxx_assert(2 <= __base && __base <= 36);
328 using _Up = __detail::__unsigned_least_t<_Tp>;
329 _Up __unsigned_val = __value;
331 if (__first == __last) [[__unlikely__]]
332 return { __last, errc::value_too_large };
337 return { __first + 1, errc{} };
343 __unsigned_val = _Up(~__value) + _Up(1);
349 return __detail::__to_chars_16(__first, __last, __unsigned_val);
351 return __detail::__to_chars_10(__first, __last, __unsigned_val);
353 return __detail::__to_chars_8(__first, __last, __unsigned_val);
355 return __detail::__to_chars_2(__first, __last, __unsigned_val);
357 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
361#define _GLIBCXX_TO_CHARS(T) \
362 inline to_chars_result \
363 to_chars(char* __first, char* __last, T __value, int __base = 10) \
364 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
365_GLIBCXX_TO_CHARS(
char)
366_GLIBCXX_TO_CHARS(
signed char)
367_GLIBCXX_TO_CHARS(
unsigned char)
368_GLIBCXX_TO_CHARS(
signed short)
369_GLIBCXX_TO_CHARS(
unsigned short)
370_GLIBCXX_TO_CHARS(
signed int)
371_GLIBCXX_TO_CHARS(
unsigned int)
372_GLIBCXX_TO_CHARS(
signed long)
373_GLIBCXX_TO_CHARS(
unsigned long)
374_GLIBCXX_TO_CHARS(
signed long long)
375_GLIBCXX_TO_CHARS(
unsigned long long)
376#if defined(__GLIBCXX_TYPE_INT_N_0)
377_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_0)
378_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_0)
380#if defined(__GLIBCXX_TYPE_INT_N_1)
381_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_1)
382_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_1)
384#if defined(__GLIBCXX_TYPE_INT_N_2)
385_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_2)
386_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_2)
388#if defined(__GLIBCXX_TYPE_INT_N_3)
389_GLIBCXX_TO_CHARS(
signed __GLIBCXX_TYPE_INT_N_3)
390_GLIBCXX_TO_CHARS(
unsigned __GLIBCXX_TYPE_INT_N_3)
392#undef _GLIBCXX_TO_CHARS
396 to_chars_result to_chars(
char*,
char*,
bool,
int = 10) =
delete;
400 template<
typename _Tp>
402 __raise_and_add(_Tp& __val,
int __base,
unsigned char __c)
404 if (__builtin_mul_overflow(__val, __base, &__val)
405 || __builtin_add_overflow(__val, __c, &__val))
413 __from_chars_alnum_to_val_table()
415 constexpr unsigned char __lower_letters[27] =
"abcdefghijklmnopqrstuvwxyz";
416 constexpr unsigned char __upper_letters[27] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
417 struct {
unsigned char __data[1u << __CHAR_BIT__] = {}; } __table;
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;
434 template<
bool _DecOnly = false>
436 __from_chars_alnum_to_val(
unsigned char __c)
438 if _GLIBCXX17_CONSTEXPR (_DecOnly)
439 return static_cast<unsigned char>(__c -
'0');
444 static constexpr auto __table = (_DecOnly, __from_chars_alnum_to_val_table());
445 return __table.__data[__c];
451 template<
bool _DecOnly,
typename _Tp>
461 const int __log2_base = __countr_zero(__base);
463 const ptrdiff_t __len = __last - __first;
465 while (__i < __len && __first[__i] ==
'0')
467 const ptrdiff_t __leading_zeroes = __i;
468 if (__i >= __len) [[__unlikely__]]
475 unsigned char __leading_c = 0;
478 __leading_c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
480 if (__leading_c >= __base) [[__unlikely__]]
489 for (; __i < __len; ++__i)
491 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
494 __val = (__val << __log2_base) | __c;
497 auto __significant_bits = (__i - __leading_zeroes) * __log2_base;
501 __significant_bits -= __log2_base - __bit_width(__leading_c);
503 return __significant_bits <= __gnu_cxx::__int_traits<_Tp>::__digits;
508 template<
bool _DecOnly,
typename _Tp>
515 const int __bits_per_digit = __bit_width(__base);
517 for (; __first != __last; ++__first)
519 const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(*__first);
523 __unused_bits_lower_bound -= __bits_per_digit;
524 if (__unused_bits_lower_bound >= 0) [[__likely__]]
526 __val = __val * __base + __c;
527 else if (!__raise_and_add(__val, __base, __c)) [[__unlikely__]]
529 while (++__first != __last
530 && __from_chars_alnum_to_val<_DecOnly>(*__first) < __base)
538 template<
typename _Tp>
539 using __integer_from_chars_result_type
541 __is_unsigned_integer<_Tp>,
548 template<
typename _Tp>
549 __detail::__integer_from_chars_result_type<_Tp>
550 from_chars(
const char* __first,
const char* __last, _Tp& __value,
553 __glibcxx_assert(2 <= __base && __base <= 36);
559 if (__first != __last && *__first ==
'-')
565 using _Up = __detail::__unsigned_least_t<_Tp>;
568 const auto __start = __first;
570 if ((__base & (__base - 1)) == 0)
573 __valid = __detail::__from_chars_pow2_base<true>(__first, __last, __val, __base);
575 __valid = __detail::__from_chars_pow2_base<false>(__first, __last, __val, __base);
577 else if (__base <= 10)
578 __valid = __detail::__from_chars_alnum<true>(__first, __last, __val, __base);
580 __valid = __detail::__from_chars_alnum<false>(__first, __last, __val, __base);
582 if (__builtin_expect(__first == __start, 0))
583 __res.ec = errc::invalid_argument;
588 __res.ec = errc::result_out_of_range;
594 if (__builtin_mul_overflow(__val, __sign, &__tmp))
595 __res.ec = errc::result_out_of_range;
605 __res.ec = errc::result_out_of_range;
625 {
return (
chars_format)((unsigned)__lhs | (
unsigned)__rhs); }
629 {
return (
chars_format)((unsigned)__lhs & (
unsigned)__rhs); }
633 {
return (
chars_format)((unsigned)__lhs ^ (
unsigned)__rhs); }
641 {
return __lhs = __lhs | __rhs; }
645 {
return __lhs = __lhs & __rhs; }
649 {
return __lhs = __lhs ^ __rhs; }
651#if defined __cpp_lib_to_chars || _GLIBCXX_HAVE_USELOCALE
653 from_chars(
const char* __first,
const char* __last,
float& __value,
657 from_chars(
const char* __first,
const char* __last,
double& __value,
661 from_chars(
const char* __first,
const char* __last,
long double& __value,
665#if defined __cpp_lib_to_chars
669 to_chars_result to_chars(
char* __first,
char* __last,
float __value)
noexcept;
670 to_chars_result to_chars(
char* __first,
char* __last,
float __value,
672 to_chars_result to_chars(
char* __first,
char* __last,
float __value,
676 to_chars_result to_chars(
char* __first,
char* __last,
double __value)
noexcept;
677 to_chars_result to_chars(
char* __first,
char* __last,
double __value,
679 to_chars_result to_chars(
char* __first,
char* __last,
double __value,
683 to_chars_result to_chars(
char* __first,
char* __last,
long double __value)
685 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
687 to_chars_result to_chars(
char* __first,
char* __last,
long double __value,
691_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
bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
__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.
ios_base & fixed(ios_base &__base)
Calls base.setf(ios_base::fixed, ios_base::floatfield).
bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
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...
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.