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 // As an extension we support <charconv> in C++14, but this header should not
35 // be included by any other library headers in C++14 mode. This ensures that
36 // the names defined in this header are not added to namespace std unless a
37 // user explicitly includes <charconv> in C++14 code.
38 #if __cplusplus >= 201402L
40 #include <type_traits>
41 #include <bit> // for __bit_width
42 #include <cctype> // for isdigit
43 #include <bits/charconv.h> // for __to_chars_len, __to_chars_10_impl
44 #include <bits/error_constants.h> // for std::errc
45 #include <bits/int_limits.h>
47 // Define when floating point is supported: #define __cpp_lib_to_chars 201611L
49 namespace std _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 /// Result type of std::to_chars
54 struct to_chars_result
60 /// Result type of std::from_chars
61 struct from_chars_result
69 template<typename _Tp>
70 using __integer_to_chars_result_type
71 = enable_if_t<__or_v<__is_signed_integer<_Tp>,
72 __is_unsigned_integer<_Tp>,
73 is_same<char, remove_cv_t<_Tp>>>,
76 // Pick an unsigned type of suitable size. This is used to reduce the
77 // number of specializations of __to_chars_len, __to_chars etc. that
78 // get instantiated. For example, to_chars<char> and to_chars<short>
79 // and to_chars<unsigned> will all use the same code, and so will
80 // to_chars<long> when sizeof(int) == sizeof(long).
81 template<typename _Tp>
82 struct __to_chars_unsigned_type : __make_unsigned_selector_base
84 using _UInts = _List<unsigned int, unsigned long, unsigned long long
85 #if _GLIBCXX_USE_INT128
89 using type = typename __select<sizeof(_Tp), _UInts>::__type;
92 template<typename _Tp>
93 using __unsigned_least_t = typename __to_chars_unsigned_type<_Tp>::type;
95 // Generic implementation for arbitrary bases.
96 // Defined in <bits/charconv.h>.
97 template<typename _Tp>
99 __to_chars_len(_Tp __value, int __base /* = 10 */) noexcept;
101 template<typename _Tp>
103 __to_chars_len_2(_Tp __value) noexcept
104 { return std::__bit_width(__value); }
106 // Generic implementation for arbitrary bases.
107 template<typename _Tp>
109 __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
111 static_assert(is_integral<_Tp>::value, "implementation bug");
112 static_assert(is_unsigned<_Tp>::value, "implementation bug");
114 to_chars_result __res;
116 const unsigned __len = __to_chars_len(__val, __base);
118 if (__builtin_expect((__last - __first) < __len, 0))
121 __res.ec = errc::value_too_large;
125 unsigned __pos = __len - 1;
127 static constexpr char __digits[] = {
128 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
129 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
130 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
131 'u', 'v', 'w', 'x', 'y', 'z'
134 while (__val >= __base)
136 auto const __quo = __val / __base;
137 auto const __rem = __val % __base;
138 __first[__pos--] = __digits[__rem];
141 *__first = __digits[__val];
143 __res.ptr = __first + __len;
148 template<typename _Tp>
149 __integer_to_chars_result_type<_Tp>
150 __to_chars_16(char* __first, char* __last, _Tp __val) noexcept
152 static_assert(is_integral<_Tp>::value, "implementation bug");
153 static_assert(is_unsigned<_Tp>::value, "implementation bug");
155 to_chars_result __res;
157 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
159 if (__builtin_expect((__last - __first) < __len, 0))
162 __res.ec = errc::value_too_large;
166 static constexpr char __digits[] = {
167 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
168 'a', 'b', 'c', 'd', 'e', 'f'
170 unsigned __pos = __len - 1;
171 while (__val >= 0x100)
173 auto __num = __val & 0xF;
175 __first[__pos] = __digits[__num];
178 __first[__pos - 1] = __digits[__num];
183 const auto __num = __val & 0xF;
185 __first[1] = __digits[__num];
186 __first[0] = __digits[__val];
189 __first[0] = __digits[__val];
190 __res.ptr = __first + __len;
195 template<typename _Tp>
196 inline __integer_to_chars_result_type<_Tp>
197 __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
199 static_assert(is_integral<_Tp>::value, "implementation bug");
200 static_assert(is_unsigned<_Tp>::value, "implementation bug");
202 to_chars_result __res;
204 const unsigned __len = __to_chars_len(__val, 10);
206 if (__builtin_expect((__last - __first) < __len, 0))
209 __res.ec = errc::value_too_large;
213 __detail::__to_chars_10_impl(__first, __len, __val);
214 __res.ptr = __first + __len;
219 template<typename _Tp>
220 __integer_to_chars_result_type<_Tp>
221 __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
223 static_assert(is_integral<_Tp>::value, "implementation bug");
224 static_assert(is_unsigned<_Tp>::value, "implementation bug");
226 to_chars_result __res;
229 if _GLIBCXX17_CONSTEXPR (__detail::__int_limits<_Tp>::digits <= 16)
231 __len = __val > 077777u ? 6u
232 : __val > 07777u ? 5u
239 __len = (__to_chars_len_2(__val) + 2) / 3;
241 if (__builtin_expect((__last - __first) < __len, 0))
244 __res.ec = errc::value_too_large;
248 unsigned __pos = __len - 1;
249 while (__val >= 0100)
251 auto __num = __val & 7;
253 __first[__pos] = '0' + __num;
256 __first[__pos - 1] = '0' + __num;
261 auto const __num = __val & 7;
263 __first[1] = '0' + __num;
264 __first[0] = '0' + __val;
267 __first[0] = '0' + __val;
268 __res.ptr = __first + __len;
273 template<typename _Tp>
274 __integer_to_chars_result_type<_Tp>
275 __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
277 static_assert(is_integral<_Tp>::value, "implementation bug");
278 static_assert(is_unsigned<_Tp>::value, "implementation bug");
280 to_chars_result __res;
282 const unsigned __len = __to_chars_len_2(__val);
284 if (__builtin_expect((__last - __first) < __len, 0))
287 __res.ec = errc::value_too_large;
291 unsigned __pos = __len - 1;
295 __first[__pos--] = '0' + (__val & 1);
298 // First digit is always '1' because __to_chars_len_2 skips
299 // leading zero bits and std::to_chars handles zero values
303 __res.ptr = __first + __len;
308 } // namespace __detail
310 template<typename _Tp>
311 __detail::__integer_to_chars_result_type<_Tp>
312 __to_chars_i(char* __first, char* __last, _Tp __value, int __base = 10)
314 __glibcxx_assert(2 <= __base && __base <= 36);
316 using _Up = __detail::__unsigned_least_t<_Tp>;
317 _Up __unsigned_val = __value;
319 if (__value == 0 && __first != __last)
322 return { __first + 1, errc{} };
325 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
328 if (__builtin_expect(__first != __last, 1))
330 __unsigned_val = _Up(~__value) + _Up(1);
336 return __detail::__to_chars_16(__first, __last, __unsigned_val);
338 return __detail::__to_chars_10(__first, __last, __unsigned_val);
340 return __detail::__to_chars_8(__first, __last, __unsigned_val);
342 return __detail::__to_chars_2(__first, __last, __unsigned_val);
344 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
348 #define _GLIBCXX_TO_CHARS(T) \
349 inline to_chars_result \
350 to_chars(char* __first, char* __last, T __value, int __base = 10) \
351 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
352 _GLIBCXX_TO_CHARS(char)
353 _GLIBCXX_TO_CHARS(signed char)
354 _GLIBCXX_TO_CHARS(unsigned char)
355 _GLIBCXX_TO_CHARS(signed short)
356 _GLIBCXX_TO_CHARS(unsigned short)
357 _GLIBCXX_TO_CHARS(signed int)
358 _GLIBCXX_TO_CHARS(unsigned int)
359 _GLIBCXX_TO_CHARS(signed long)
360 _GLIBCXX_TO_CHARS(unsigned long)
361 _GLIBCXX_TO_CHARS(signed long long)
362 _GLIBCXX_TO_CHARS(unsigned long long)
363 #if defined(__GLIBCXX_TYPE_INT_N_0)
364 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_0)
365 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_0)
367 #if defined(__GLIBCXX_TYPE_INT_N_1)
368 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_1)
369 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_1)
371 #if defined(__GLIBCXX_TYPE_INT_N_2)
372 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_2)
373 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_2)
375 #if defined(__GLIBCXX_TYPE_INT_N_3)
376 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_3)
377 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_3)
379 #undef _GLIBCXX_TO_CHARS
381 // _GLIBCXX_RESOLVE_LIB_DEFECTS
382 // 3266. to_chars(bool) should be deleted
383 to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
387 template<typename _Tp>
389 __raise_and_add(_Tp& __val, int __base, unsigned char __c)
391 if (__builtin_mul_overflow(__val, __base, &__val)
392 || __builtin_add_overflow(__val, __c, &__val))
397 /// std::from_chars implementation for integers in base 2.
398 template<typename _Tp>
400 __from_chars_binary(const char*& __first, const char* __last, _Tp& __val)
402 static_assert(is_integral<_Tp>::value, "implementation bug");
403 static_assert(is_unsigned<_Tp>::value, "implementation bug");
405 const ptrdiff_t __len = __last - __first;
409 const unsigned char __c = (unsigned)__first[__i] - '0';
411 __val = (__val << 1) | __c;
417 return __i <= __detail::__int_limits<_Tp>::digits;
420 /// std::from_chars implementation for integers in bases 3 to 10.
421 template<typename _Tp>
423 __from_chars_digit(const char*& __first, const char* __last, _Tp& __val,
426 static_assert(is_integral<_Tp>::value, "implementation bug");
427 static_assert(is_unsigned<_Tp>::value, "implementation bug");
429 auto __matches = [__base](char __c) {
430 return '0' <= __c && __c <= ('0' + (__base - 1));
433 while (__first != __last)
435 const char __c = *__first;
438 if (!__raise_and_add(__val, __base, __c - '0'))
440 while (++__first != __last && __matches(*__first))
452 constexpr unsigned char
453 __from_chars_alpha_to_num(char __c)
536 return __detail::__int_limits<unsigned char>::max();
539 /// std::from_chars implementation for integers in bases 11 to 26.
540 template<typename _Tp>
542 __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
546 while (__first != __last)
548 unsigned char __c = *__first;
549 if (std::isdigit(__c))
553 __c = __from_chars_alpha_to_num(__c);
558 if (__builtin_expect(__valid, 1))
559 __valid = __raise_and_add(__val, __base, __c);
565 template<typename _Tp>
566 using __integer_from_chars_result_type
567 = enable_if_t<__or_v<__is_signed_integer<_Tp>,
568 __is_unsigned_integer<_Tp>,
569 is_same<char, remove_cv_t<_Tp>>>,
572 } // namespace __detail
574 /// std::from_chars for integral types.
575 template<typename _Tp>
576 __detail::__integer_from_chars_result_type<_Tp>
577 from_chars(const char* __first, const char* __last, _Tp& __value,
580 __glibcxx_assert(2 <= __base && __base <= 36);
582 from_chars_result __res{__first, {}};
585 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
586 if (__first != __last && *__first == '-')
592 using _Up = __detail::__unsigned_least_t<_Tp>;
595 const auto __start = __first;
598 __valid = __detail::__from_chars_binary(__first, __last, __val);
599 else if (__base <= 10)
600 __valid = __detail::__from_chars_digit(__first, __last, __val, __base);
602 __valid = __detail::__from_chars_alnum(__first, __last, __val, __base);
604 if (__builtin_expect(__first == __start, 0))
605 __res.ec = errc::invalid_argument;
610 __res.ec = errc::result_out_of_range;
613 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
616 if (__builtin_mul_overflow(__val, __sign, &__tmp))
617 __res.ec = errc::result_out_of_range;
623 if _GLIBCXX17_CONSTEXPR (__detail::__int_limits<_Up>::max()
624 > __detail::__int_limits<_Tp>::max())
626 if (__val > __detail::__int_limits<_Tp>::max())
627 __res.ec = errc::result_out_of_range;
639 /// floating-point format for primitive numerical conversion
640 enum class chars_format
642 scientific = 1, fixed = 2, hex = 4, general = fixed | scientific
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 __lhs, chars_format __rhs) noexcept
655 { return (chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
657 constexpr chars_format
658 operator~(chars_format __fmt) noexcept
659 { return (chars_format)~(unsigned)__fmt; }
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 constexpr chars_format&
670 operator^=(chars_format& __lhs, chars_format __rhs) noexcept
671 { return __lhs = __lhs ^ __rhs; }
673 _GLIBCXX_END_NAMESPACE_VERSION
676 #endif // _GLIBCXX_CHARCONV