1 // Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
3 // Copyright (C) 2017-2020 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/charconv
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_CHARCONV
30 #define _GLIBCXX_CHARCONV 1
32 #pragma GCC system_header
34 #if __cplusplus >= 201402L
36 #include <type_traits>
38 #include <bit> // for __log2p1
39 #include <cctype> // for isdigit
40 #include <bits/charconv.h> // for __to_chars_len, __to_chars_10_impl
41 #include <bits/error_constants.h> // for std::errc
43 // Define when floating point is supported: #define __cpp_lib_to_chars 201611L
45 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 /// Result type of std::to_chars
50 struct to_chars_result
56 /// Result type of std::from_chars
57 struct from_chars_result
65 template<typename _Tp>
66 using __integer_to_chars_result_type
67 = enable_if_t<__or_v<__is_signed_integer<_Tp>,
68 __is_unsigned_integer<_Tp>,
69 is_same<char, remove_cv_t<_Tp>>>,
72 // Pick an unsigned type of suitable size. This is used to reduce the
73 // number of specializations of __to_chars_len, __to_chars etc. that
74 // get instantiated. For example, to_chars<char> and to_chars<short>
75 // and to_chars<unsigned> will all use the same code, and so will
76 // to_chars<long> when sizeof(int) == sizeof(long).
77 template<typename _Tp>
78 struct __to_chars_unsigned_type : __make_unsigned_selector_base
80 using _UInts = _List<unsigned int, unsigned long, unsigned long long
81 #if _GLIBCXX_USE_INT128
85 using type = typename __select<sizeof(_Tp), _UInts>::__type;
88 template<typename _Tp>
89 using __unsigned_least_t = typename __to_chars_unsigned_type<_Tp>::type;
91 // Generic implementation for arbitrary bases.
92 // Defined in <bits/charconv.h>.
93 template<typename _Tp>
95 __to_chars_len(_Tp __value, int __base /* = 10 */) noexcept;
97 template<typename _Tp>
99 __to_chars_len_2(_Tp __value) noexcept
100 { return std::__log2p1(__value); }
102 // Generic implementation for arbitrary bases.
103 template<typename _Tp>
105 __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
107 static_assert(is_integral<_Tp>::value, "implementation bug");
108 static_assert(is_unsigned<_Tp>::value, "implementation bug");
110 to_chars_result __res;
112 const unsigned __len = __to_chars_len(__val, __base);
114 if (__builtin_expect((__last - __first) < __len, 0))
117 __res.ec = errc::value_too_large;
121 unsigned __pos = __len - 1;
123 static constexpr char __digits[] = {
124 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
125 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
126 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
127 'u', 'v', 'w', 'x', 'y', 'z'
130 while (__val >= __base)
132 auto const __quo = __val / __base;
133 auto const __rem = __val % __base;
134 __first[__pos--] = __digits[__rem];
137 *__first = __digits[__val];
139 __res.ptr = __first + __len;
144 template<typename _Tp>
145 __integer_to_chars_result_type<_Tp>
146 __to_chars_16(char* __first, char* __last, _Tp __val) noexcept
148 static_assert(is_integral<_Tp>::value, "implementation bug");
149 static_assert(is_unsigned<_Tp>::value, "implementation bug");
151 to_chars_result __res;
153 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
155 if (__builtin_expect((__last - __first) < __len, 0))
158 __res.ec = errc::value_too_large;
162 static constexpr char __digits[] = {
163 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
164 'a', 'b', 'c', 'd', 'e', 'f'
166 unsigned __pos = __len - 1;
167 while (__val >= 0x100)
169 auto __num = __val & 0xF;
171 __first[__pos] = __digits[__num];
174 __first[__pos - 1] = __digits[__num];
179 const auto __num = __val & 0xF;
181 __first[1] = __digits[__num];
182 __first[0] = __digits[__val];
185 __first[0] = __digits[__val];
186 __res.ptr = __first + __len;
191 template<typename _Tp>
192 inline __integer_to_chars_result_type<_Tp>
193 __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
195 static_assert(is_integral<_Tp>::value, "implementation bug");
196 static_assert(is_unsigned<_Tp>::value, "implementation bug");
198 to_chars_result __res;
200 const unsigned __len = __to_chars_len(__val, 10);
202 if (__builtin_expect((__last - __first) < __len, 0))
205 __res.ec = errc::value_too_large;
209 __detail::__to_chars_10_impl(__first, __len, __val);
210 __res.ptr = __first + __len;
215 template<typename _Tp>
216 __integer_to_chars_result_type<_Tp>
217 __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
219 static_assert(is_integral<_Tp>::value, "implementation bug");
220 static_assert(is_unsigned<_Tp>::value, "implementation bug");
222 to_chars_result __res;
225 if _GLIBCXX17_CONSTEXPR (numeric_limits<_Tp>::digits <= 16)
227 __len = __val > 077777u ? 6u
228 : __val > 07777u ? 5u
235 __len = (__to_chars_len_2(__val) + 2) / 3;
237 if (__builtin_expect((__last - __first) < __len, 0))
240 __res.ec = errc::value_too_large;
244 unsigned __pos = __len - 1;
245 while (__val >= 0100)
247 auto __num = __val & 7;
249 __first[__pos] = '0' + __num;
252 __first[__pos - 1] = '0' + __num;
257 auto const __num = __val & 7;
259 __first[1] = '0' + __num;
260 __first[0] = '0' + __val;
263 __first[0] = '0' + __val;
264 __res.ptr = __first + __len;
269 template<typename _Tp>
270 __integer_to_chars_result_type<_Tp>
271 __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
273 static_assert(is_integral<_Tp>::value, "implementation bug");
274 static_assert(is_unsigned<_Tp>::value, "implementation bug");
276 to_chars_result __res;
278 const unsigned __len = __to_chars_len_2(__val);
280 if (__builtin_expect((__last - __first) < __len, 0))
283 __res.ec = errc::value_too_large;
287 unsigned __pos = __len - 1;
291 __first[__pos--] = '0' + (__val & 1);
294 // First digit is always '1' because __to_chars_len_2 skips
295 // leading zero bits and std::to_chars handles zero values
299 __res.ptr = __first + __len;
304 } // namespace __detail
306 template<typename _Tp>
307 __detail::__integer_to_chars_result_type<_Tp>
308 __to_chars_i(char* __first, char* __last, _Tp __value, int __base = 10)
310 __glibcxx_assert(2 <= __base && __base <= 36);
312 using _Up = __detail::__unsigned_least_t<_Tp>;
313 _Up __unsigned_val = __value;
315 if (__value == 0 && __first != __last)
318 return { __first + 1, errc{} };
321 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
324 if (__builtin_expect(__first != __last, 1))
326 __unsigned_val = _Up(~__value) + _Up(1);
332 return __detail::__to_chars_16(__first, __last, __unsigned_val);
334 return __detail::__to_chars_10(__first, __last, __unsigned_val);
336 return __detail::__to_chars_8(__first, __last, __unsigned_val);
338 return __detail::__to_chars_2(__first, __last, __unsigned_val);
340 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
344 #define _GLIBCXX_TO_CHARS(T) \
345 inline to_chars_result \
346 to_chars(char* __first, char* __last, T __value, int __base = 10) \
347 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
348 _GLIBCXX_TO_CHARS(char)
349 _GLIBCXX_TO_CHARS(signed char)
350 _GLIBCXX_TO_CHARS(unsigned char)
351 _GLIBCXX_TO_CHARS(signed short)
352 _GLIBCXX_TO_CHARS(unsigned short)
353 _GLIBCXX_TO_CHARS(signed int)
354 _GLIBCXX_TO_CHARS(unsigned int)
355 _GLIBCXX_TO_CHARS(signed long)
356 _GLIBCXX_TO_CHARS(unsigned long)
357 _GLIBCXX_TO_CHARS(signed long long)
358 _GLIBCXX_TO_CHARS(unsigned long long)
359 #if defined(__GLIBCXX_TYPE_INT_N_0)
360 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_0)
361 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_0)
363 #if defined(__GLIBCXX_TYPE_INT_N_1)
364 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_1)
365 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_1)
367 #if defined(__GLIBCXX_TYPE_INT_N_2)
368 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_2)
369 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_2)
371 #if defined(__GLIBCXX_TYPE_INT_N_3)
372 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_3)
373 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_3)
375 #undef _GLIBCXX_TO_CHARS
377 // _GLIBCXX_RESOLVE_LIB_DEFECTS
378 // 3266. to_chars(bool) should be deleted
379 to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
383 template<typename _Tp>
385 __raise_and_add(_Tp& __val, int __base, unsigned char __c)
387 if (__builtin_mul_overflow(__val, __base, &__val)
388 || __builtin_add_overflow(__val, __c, &__val))
393 /// std::from_chars implementation for integers in base 2.
394 template<typename _Tp>
396 __from_chars_binary(const char*& __first, const char* __last, _Tp& __val)
398 static_assert(is_integral<_Tp>::value, "implementation bug");
399 static_assert(is_unsigned<_Tp>::value, "implementation bug");
401 const ptrdiff_t __len = __last - __first;
405 const unsigned char __c = (unsigned)__first[__i] - '0';
407 __val = (__val << 1) | __c;
413 return __i <= numeric_limits<_Tp>::digits;
416 /// std::from_chars implementation for integers in bases 3 to 10.
417 template<typename _Tp>
419 __from_chars_digit(const char*& __first, const char* __last, _Tp& __val,
422 static_assert(is_integral<_Tp>::value, "implementation bug");
423 static_assert(is_unsigned<_Tp>::value, "implementation bug");
425 auto __matches = [__base](char __c) {
426 return '0' <= __c && __c <= ('0' + (__base - 1));
429 while (__first != __last)
431 const char __c = *__first;
434 if (!__raise_and_add(__val, __base, __c - '0'))
436 while (++__first != __last && __matches(*__first))
448 constexpr unsigned char
449 __from_chars_alpha_to_num(char __c)
532 return std::numeric_limits<unsigned char>::max();
535 /// std::from_chars implementation for integers in bases 11 to 26.
536 template<typename _Tp>
538 __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
542 while (__first != __last)
544 unsigned char __c = *__first;
545 if (std::isdigit(__c))
549 __c = __from_chars_alpha_to_num(__c);
554 if (__builtin_expect(__valid, 1))
555 __valid = __raise_and_add(__val, __base, __c);
561 template<typename _Tp>
562 using __integer_from_chars_result_type
563 = enable_if_t<__or_v<__is_signed_integer<_Tp>,
564 __is_unsigned_integer<_Tp>,
565 is_same<char, remove_cv_t<_Tp>>>,
568 } // namespace __detail
570 /// std::from_chars for integral types.
571 template<typename _Tp>
572 __detail::__integer_from_chars_result_type<_Tp>
573 from_chars(const char* __first, const char* __last, _Tp& __value,
576 __glibcxx_assert(2 <= __base && __base <= 36);
578 from_chars_result __res{__first, {}};
581 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
582 if (__first != __last && *__first == '-')
588 using _Up = __detail::__unsigned_least_t<_Tp>;
591 const auto __start = __first;
594 __valid = __detail::__from_chars_binary(__first, __last, __val);
595 else if (__base <= 10)
596 __valid = __detail::__from_chars_digit(__first, __last, __val, __base);
598 __valid = __detail::__from_chars_alnum(__first, __last, __val, __base);
600 if (__builtin_expect(__first == __start, 0))
601 __res.ec = errc::invalid_argument;
606 __res.ec = errc::result_out_of_range;
609 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
612 if (__builtin_mul_overflow(__val, __sign, &__tmp))
613 __res.ec = errc::result_out_of_range;
619 if _GLIBCXX17_CONSTEXPR
620 (numeric_limits<_Up>::max() > numeric_limits<_Tp>::max())
622 if (__val > numeric_limits<_Tp>::max())
623 __res.ec = errc::result_out_of_range;
635 /// floating-point format for primitive numerical conversion
636 enum class chars_format
638 scientific = 1, fixed = 2, hex = 4, general = fixed | scientific
641 constexpr chars_format
642 operator|(chars_format __lhs, chars_format __rhs) noexcept
643 { return (chars_format)((unsigned)__lhs | (unsigned)__rhs); }
645 constexpr chars_format
646 operator&(chars_format __lhs, chars_format __rhs) noexcept
647 { return (chars_format)((unsigned)__lhs & (unsigned)__rhs); }
649 constexpr chars_format
650 operator^(chars_format __lhs, chars_format __rhs) noexcept
651 { return (chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
653 constexpr chars_format
654 operator~(chars_format __fmt) noexcept
655 { return (chars_format)~(unsigned)__fmt; }
657 constexpr chars_format&
658 operator|=(chars_format& __lhs, chars_format __rhs) noexcept
659 { return __lhs = __lhs | __rhs; }
661 constexpr chars_format&
662 operator&=(chars_format& __lhs, chars_format __rhs) noexcept
663 { return __lhs = __lhs & __rhs; }
665 constexpr chars_format&
666 operator^=(chars_format& __lhs, chars_format __rhs) noexcept
667 { return __lhs = __lhs ^ __rhs; }
669 _GLIBCXX_END_NAMESPACE_VERSION
672 #endif // _GLIBCXX_CHARCONV