29 #ifndef _GLIBCXX_CHARCONV
30 #define _GLIBCXX_CHARCONV 1
32 #pragma GCC system_header
38 #if __cplusplus >= 201402L
50 namespace std _GLIBCXX_VISIBILITY(default)
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
60 #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
72 #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
80 template<
typename _Tp>
81 using __integer_to_chars_result_type
83 __is_unsigned_integer<_Tp>,
92 template<
typename _Tp>
93 struct __to_chars_unsigned_type : __make_unsigned_selector_base
95 using _UInts = _List<
unsigned int,
unsigned long,
unsigned long long
96 #if _GLIBCXX_USE_INT128
100 using type =
typename __select<sizeof(_Tp), _UInts>::__type;
103 template<
typename _Tp>
104 using __unsigned_least_t =
typename __to_chars_unsigned_type<_Tp>::type;
108 template<
typename _Tp>
110 __to_chars_len(_Tp __value,
int __base ) noexcept;
112 template<
typename _Tp>
114 __to_chars_len_2(_Tp __value) noexcept
115 {
return std::__bit_width(__value); }
118 template<
typename _Tp>
120 __to_chars(
char* __first,
char* __last, _Tp __val,
int __base) noexcept
122 static_assert(is_integral<_Tp>::value,
"implementation bug");
123 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
125 to_chars_result __res;
127 const unsigned __len = __to_chars_len(__val,
__base);
129 if (__builtin_expect((__last - __first) < __len, 0))
132 __res.ec = errc::value_too_large;
136 unsigned __pos = __len - 1;
138 static 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 __integer_to_chars_result_type<_Tp>
161 __to_chars_16(
char* __first,
char* __last, _Tp __val) noexcept
163 static_assert(is_integral<_Tp>::value,
"implementation bug");
164 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
166 to_chars_result __res;
168 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
170 if (__builtin_expect((__last - __first) < __len, 0))
173 __res.ec = errc::value_too_large;
177 static constexpr
char __digits[] = {
178 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
179 'a',
'b',
'c',
'd',
'e',
'f'
181 unsigned __pos = __len - 1;
182 while (__val >= 0x100)
184 auto __num = __val & 0xF;
186 __first[__pos] = __digits[__num];
189 __first[__pos - 1] = __digits[__num];
194 const auto __num = __val & 0xF;
196 __first[1] = __digits[__num];
197 __first[0] = __digits[__val];
200 __first[0] = __digits[__val];
201 __res.ptr = __first + __len;
206 template<
typename _Tp>
207 inline __integer_to_chars_result_type<_Tp>
208 __to_chars_10(
char* __first,
char* __last, _Tp __val) noexcept
210 static_assert(is_integral<_Tp>::value,
"implementation bug");
211 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
213 to_chars_result __res;
215 const unsigned __len = __to_chars_len(__val, 10);
217 if (__builtin_expect((__last - __first) < __len, 0))
220 __res.ec = errc::value_too_large;
224 __detail::__to_chars_10_impl(__first, __len, __val);
225 __res.ptr = __first + __len;
230 template<
typename _Tp>
231 __integer_to_chars_result_type<_Tp>
232 __to_chars_8(
char* __first,
char* __last, _Tp __val) noexcept
234 static_assert(is_integral<_Tp>::value,
"implementation bug");
235 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
237 to_chars_result __res;
242 __len = __val > 077777u ? 6u
243 : __val > 07777u ? 5u
250 __len = (__to_chars_len_2(__val) + 2) / 3;
252 if (__builtin_expect((__last - __first) < __len, 0))
255 __res.ec = errc::value_too_large;
259 unsigned __pos = __len - 1;
260 while (__val >= 0100)
262 auto __num = __val & 7;
264 __first[__pos] =
'0' + __num;
267 __first[__pos - 1] =
'0' + __num;
272 auto const __num = __val & 7;
274 __first[1] =
'0' + __num;
275 __first[0] =
'0' + __val;
278 __first[0] =
'0' + __val;
279 __res.ptr = __first + __len;
284 template<
typename _Tp>
285 __integer_to_chars_result_type<_Tp>
286 __to_chars_2(
char* __first,
char* __last, _Tp __val) noexcept
288 static_assert(is_integral<_Tp>::value,
"implementation bug");
289 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
291 to_chars_result __res;
293 const unsigned __len = __to_chars_len_2(__val);
295 if (__builtin_expect((__last - __first) < __len, 0))
298 __res.ec = errc::value_too_large;
302 unsigned __pos = __len - 1;
306 __first[__pos--] =
'0' + (__val & 1);
314 __res.ptr = __first + __len;
321 template<
typename _Tp>
322 __detail::__integer_to_chars_result_type<_Tp>
323 __to_chars_i(
char* __first,
char* __last, _Tp __value,
int __base = 10)
327 using _Up = __detail::__unsigned_least_t<_Tp>;
328 _Up __unsigned_val = __value;
330 if (__first == __last) [[__unlikely__]]
331 return { __last, errc::value_too_large };
336 return { __first + 1, errc{} };
339 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
342 if (__builtin_expect(__first != __last, 1))
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 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 #undef _GLIBCXX_TO_CHARS
397 to_chars_result to_chars(
char*,
char*,
bool,
int = 10) =
delete;
401 template<
typename _Tp>
403 __raise_and_add(_Tp& __val,
int __base,
unsigned char __c)
405 if (__builtin_mul_overflow(__val, __base, &__val)
406 || __builtin_add_overflow(__val, __c, &__val))
412 template<
typename _Tp>
417 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
419 const ptrdiff_t __len = __last - __first;
421 while (__i < __len && __first[__i] ==
'0')
423 const ptrdiff_t __leading_zeroes = __i;
427 const unsigned char __c = (unsigned)__first[__i] -
'0';
429 __val = (__val << 1) | __c;
439 template<
typename _Tp>
445 static_assert(is_unsigned<_Tp>::value,
"implementation bug");
447 auto __matches = [
__base](
char __c) {
448 return '0' <= __c && __c <= (
'0' + (__base - 1));
451 while (__first != __last)
453 const char __c = *__first;
456 if (!__raise_and_add(__val, __base, __c -
'0'))
458 while (++__first != __last && __matches(*__first))
470 constexpr
unsigned char
471 __from_chars_alpha_to_num(
char __c)
558 template<
typename _Tp>
564 while (__first != __last)
566 unsigned char __c = *__first;
571 __c = __from_chars_alpha_to_num(__c);
576 if (__builtin_expect(__valid, 1))
577 __valid = __raise_and_add(__val, __base, __c);
583 template<
typename _Tp>
584 using __integer_from_chars_result_type
586 __is_unsigned_integer<_Tp>,
593 template<
typename _Tp>
594 __detail::__integer_from_chars_result_type<_Tp>
595 from_chars(
const char* __first,
const char* __last, _Tp& __value,
598 __glibcxx_assert(2 <= __base && __base <= 36);
603 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
604 if (__first != __last && *__first ==
'-')
610 using _Up = __detail::__unsigned_least_t<_Tp>;
613 const auto __start = __first;
617 else if (__base <= 10)
622 if (__builtin_expect(__first == __start, 0))
623 __res.ec = errc::invalid_argument;
628 __res.ec = errc::result_out_of_range;
631 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
634 if (__builtin_mul_overflow(__val, __sign, &__tmp))
635 __res.ec = errc::result_out_of_range;
645 __res.ec = errc::result_out_of_range;
665 {
return (
chars_format)((unsigned)__lhs | (
unsigned)__rhs); }
669 {
return (
chars_format)((unsigned)__lhs & (
unsigned)__rhs); }
673 {
return (
chars_format)((unsigned)__lhs ^ (
unsigned)__rhs); }
681 {
return __lhs = __lhs | __rhs; }
685 {
return __lhs = __lhs & __rhs; }
689 {
return __lhs = __lhs ^ __rhs; }
691 _GLIBCXX_END_NAMESPACE_VERSION
694 #endif // _GLIBCXX_CHARCONV
bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
bool __from_chars_binary(const char *&__first, const char *__last, _Tp &__val)
std::from_chars implementation for integers in base 2.
chars_format
floating-point format for primitive numerical conversion
Result type of std::from_chars.
bool __from_chars_alnum(const char *&__first, const char *__last, _Tp &__val, int __base)
std::from_chars implementation for integers in bases 11 to 26.
bool isdigit(_CharT __c, const locale &__loc)
Convenience interface to ctype.is(ctype_base::digit, __c).
bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
ios_base & hex(ios_base &__base)
Calls base.setf(ios_base::hex, ios_base::basefield).
constexpr _Iterator __base(_Iterator __it)
__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.
Result type of std::to_chars.
ios_base & fixed(ios_base &__base)
Calls base.setf(ios_base::fixed, ios_base::floatfield).
ios_base & scientific(ios_base &__base)
Calls base.setf(ios_base::scientific, ios_base::floatfield).
bool __from_chars_digit(const char *&__first, const char *__last, _Tp &__val, int __base)
std::from_chars implementation for integers in bases 3 to 10.
bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.