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 <bits/charconv.h> // for __to_chars_len, __to_chars_10_impl
43 #include <bits/error_constants.h> // for std::errc
44 #include <ext/numeric_traits.h>
46 // FIXME: Define when floating point is supported:
47 // #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
59 #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
61 operator==(const to_chars_result&, const to_chars_result&) = default;
65 /// Result type of std::from_chars
66 struct from_chars_result
71 #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
73 operator==(const from_chars_result&, const from_chars_result&) = default;
79 template<typename _Tp>
80 using __integer_to_chars_result_type
81 = enable_if_t<__or_<__is_signed_integer<_Tp>,
82 __is_unsigned_integer<_Tp>,
83 is_same<char, remove_cv_t<_Tp>>>::value,
86 // Pick an unsigned type of suitable size. This is used to reduce the
87 // number of specializations of __to_chars_len, __to_chars etc. that
88 // get instantiated. For example, to_chars<char> and to_chars<short>
89 // and to_chars<unsigned> will all use the same code, and so will
90 // to_chars<long> when sizeof(int) == sizeof(long).
91 template<typename _Tp>
92 struct __to_chars_unsigned_type : __make_unsigned_selector_base
94 using _UInts = _List<unsigned int, unsigned long, unsigned long long
95 #if _GLIBCXX_USE_INT128
99 using type = typename __select<sizeof(_Tp), _UInts>::__type;
102 template<typename _Tp>
103 using __unsigned_least_t = typename __to_chars_unsigned_type<_Tp>::type;
105 // Generic implementation for arbitrary bases.
106 // Defined in <bits/charconv.h>.
107 template<typename _Tp>
109 __to_chars_len(_Tp __value, int __base /* = 10 */) noexcept;
111 template<typename _Tp>
113 __to_chars_len_2(_Tp __value) noexcept
114 { return std::__bit_width(__value); }
116 // Generic implementation for arbitrary bases.
117 template<typename _Tp>
119 __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
121 static_assert(is_integral<_Tp>::value, "implementation bug");
122 static_assert(is_unsigned<_Tp>::value, "implementation bug");
124 to_chars_result __res;
126 const unsigned __len = __to_chars_len(__val, __base);
128 if (__builtin_expect((__last - __first) < __len, 0))
131 __res.ec = errc::value_too_large;
135 unsigned __pos = __len - 1;
137 static constexpr char __digits[] = {
138 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
139 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
140 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
141 'u', 'v', 'w', 'x', 'y', 'z'
144 while (__val >= (unsigned)__base)
146 auto const __quo = __val / __base;
147 auto const __rem = __val % __base;
148 __first[__pos--] = __digits[__rem];
151 *__first = __digits[__val];
153 __res.ptr = __first + __len;
158 template<typename _Tp>
159 __integer_to_chars_result_type<_Tp>
160 __to_chars_16(char* __first, char* __last, _Tp __val) noexcept
162 static_assert(is_integral<_Tp>::value, "implementation bug");
163 static_assert(is_unsigned<_Tp>::value, "implementation bug");
165 to_chars_result __res;
167 const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
169 if (__builtin_expect((__last - __first) < __len, 0))
172 __res.ec = errc::value_too_large;
176 static constexpr char __digits[] = {
177 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
178 'a', 'b', 'c', 'd', 'e', 'f'
180 unsigned __pos = __len - 1;
181 while (__val >= 0x100)
183 auto __num = __val & 0xF;
185 __first[__pos] = __digits[__num];
188 __first[__pos - 1] = __digits[__num];
193 const auto __num = __val & 0xF;
195 __first[1] = __digits[__num];
196 __first[0] = __digits[__val];
199 __first[0] = __digits[__val];
200 __res.ptr = __first + __len;
205 template<typename _Tp>
206 inline __integer_to_chars_result_type<_Tp>
207 __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
209 static_assert(is_integral<_Tp>::value, "implementation bug");
210 static_assert(is_unsigned<_Tp>::value, "implementation bug");
212 to_chars_result __res;
214 const unsigned __len = __to_chars_len(__val, 10);
216 if (__builtin_expect((__last - __first) < __len, 0))
219 __res.ec = errc::value_too_large;
223 __detail::__to_chars_10_impl(__first, __len, __val);
224 __res.ptr = __first + __len;
229 template<typename _Tp>
230 __integer_to_chars_result_type<_Tp>
231 __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
233 static_assert(is_integral<_Tp>::value, "implementation bug");
234 static_assert(is_unsigned<_Tp>::value, "implementation bug");
236 to_chars_result __res;
239 if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Tp>::__digits <= 16)
241 __len = __val > 077777u ? 6u
242 : __val > 07777u ? 5u
249 __len = (__to_chars_len_2(__val) + 2) / 3;
251 if (__builtin_expect((__last - __first) < __len, 0))
254 __res.ec = errc::value_too_large;
258 unsigned __pos = __len - 1;
259 while (__val >= 0100)
261 auto __num = __val & 7;
263 __first[__pos] = '0' + __num;
266 __first[__pos - 1] = '0' + __num;
271 auto const __num = __val & 7;
273 __first[1] = '0' + __num;
274 __first[0] = '0' + __val;
277 __first[0] = '0' + __val;
278 __res.ptr = __first + __len;
283 template<typename _Tp>
284 __integer_to_chars_result_type<_Tp>
285 __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
287 static_assert(is_integral<_Tp>::value, "implementation bug");
288 static_assert(is_unsigned<_Tp>::value, "implementation bug");
290 to_chars_result __res;
292 const unsigned __len = __to_chars_len_2(__val);
294 if (__builtin_expect((__last - __first) < __len, 0))
297 __res.ec = errc::value_too_large;
301 unsigned __pos = __len - 1;
305 __first[__pos--] = '0' + (__val & 1);
308 // First digit is always '1' because __to_chars_len_2 skips
309 // leading zero bits and std::to_chars handles zero values
313 __res.ptr = __first + __len;
318 } // namespace __detail
320 template<typename _Tp>
321 __detail::__integer_to_chars_result_type<_Tp>
322 __to_chars_i(char* __first, char* __last, _Tp __value, int __base = 10)
324 __glibcxx_assert(2 <= __base && __base <= 36);
326 using _Up = __detail::__unsigned_least_t<_Tp>;
327 _Up __unsigned_val = __value;
329 if (__first == __last) [[__unlikely__]]
330 return { __last, errc::value_too_large };
335 return { __first + 1, errc{} };
337 else if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
341 __unsigned_val = _Up(~__value) + _Up(1);
347 return __detail::__to_chars_16(__first, __last, __unsigned_val);
349 return __detail::__to_chars_10(__first, __last, __unsigned_val);
351 return __detail::__to_chars_8(__first, __last, __unsigned_val);
353 return __detail::__to_chars_2(__first, __last, __unsigned_val);
355 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
359 #define _GLIBCXX_TO_CHARS(T) \
360 inline to_chars_result \
361 to_chars(char* __first, char* __last, T __value, int __base = 10) \
362 { return std::__to_chars_i<T>(__first, __last, __value, __base); }
363 _GLIBCXX_TO_CHARS(char)
364 _GLIBCXX_TO_CHARS(signed char)
365 _GLIBCXX_TO_CHARS(unsigned char)
366 _GLIBCXX_TO_CHARS(signed short)
367 _GLIBCXX_TO_CHARS(unsigned short)
368 _GLIBCXX_TO_CHARS(signed int)
369 _GLIBCXX_TO_CHARS(unsigned int)
370 _GLIBCXX_TO_CHARS(signed long)
371 _GLIBCXX_TO_CHARS(unsigned long)
372 _GLIBCXX_TO_CHARS(signed long long)
373 _GLIBCXX_TO_CHARS(unsigned long long)
374 #if defined(__GLIBCXX_TYPE_INT_N_0)
375 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_0)
376 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_0)
378 #if defined(__GLIBCXX_TYPE_INT_N_1)
379 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_1)
380 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_1)
382 #if defined(__GLIBCXX_TYPE_INT_N_2)
383 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_2)
384 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_2)
386 #if defined(__GLIBCXX_TYPE_INT_N_3)
387 _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_3)
388 _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_3)
390 #undef _GLIBCXX_TO_CHARS
392 // _GLIBCXX_RESOLVE_LIB_DEFECTS
393 // 3266. to_chars(bool) should be deleted
394 to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
398 template<typename _Tp>
400 __raise_and_add(_Tp& __val, int __base, unsigned char __c)
402 if (__builtin_mul_overflow(__val, __base, &__val)
403 || __builtin_add_overflow(__val, __c, &__val))
408 /// std::from_chars implementation for integers in base 2.
409 template<typename _Tp>
411 __from_chars_binary(const char*& __first, const char* __last, _Tp& __val)
413 static_assert(is_integral<_Tp>::value, "implementation bug");
414 static_assert(is_unsigned<_Tp>::value, "implementation bug");
416 const ptrdiff_t __len = __last - __first;
418 while (__i < __len && __first[__i] == '0')
420 const ptrdiff_t __leading_zeroes = __i;
424 const unsigned char __c = (unsigned)__first[__i] - '0';
426 __val = (__val << 1) | __c;
432 return (__i - __leading_zeroes) <= __gnu_cxx::__int_traits<_Tp>::__digits;
435 /// std::from_chars implementation for integers in bases 3 to 10.
436 template<typename _Tp>
438 __from_chars_digit(const char*& __first, const char* __last, _Tp& __val,
441 static_assert(is_integral<_Tp>::value, "implementation bug");
442 static_assert(is_unsigned<_Tp>::value, "implementation bug");
444 auto __matches = [__base](char __c) {
445 return '0' <= __c && __c <= ('0' + (__base - 1));
448 while (__first != __last)
450 const char __c = *__first;
453 if (!__raise_and_add(__val, __base, __c - '0'))
455 while (++__first != __last && __matches(*__first))
468 __from_chars_alpha_to_num(char __c)
554 /// std::from_chars implementation for integers in bases 11 to 36.
555 template<typename _Tp>
557 __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
561 while (__first != __last)
564 if ('0' <= __c && __c <= '9') // isdigit
568 __c = __from_chars_alpha_to_num(__c);
573 if (__builtin_expect(__valid, 1))
574 __valid = __raise_and_add(__val, __base, __c);
580 template<typename _Tp>
581 using __integer_from_chars_result_type
582 = enable_if_t<__or_<__is_signed_integer<_Tp>,
583 __is_unsigned_integer<_Tp>,
584 is_same<char, remove_cv_t<_Tp>>>::value,
587 } // namespace __detail
589 /// std::from_chars for integral types.
590 template<typename _Tp>
591 __detail::__integer_from_chars_result_type<_Tp>
592 from_chars(const char* __first, const char* __last, _Tp& __value,
595 __glibcxx_assert(2 <= __base && __base <= 36);
597 from_chars_result __res{__first, {}};
600 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
601 if (__first != __last && *__first == '-')
607 using _Up = __detail::__unsigned_least_t<_Tp>;
610 const auto __start = __first;
613 __valid = __detail::__from_chars_binary(__first, __last, __val);
614 else if (__base <= 10)
615 __valid = __detail::__from_chars_digit(__first, __last, __val, __base);
617 __valid = __detail::__from_chars_alnum(__first, __last, __val, __base);
619 if (__builtin_expect(__first == __start, 0))
620 __res.ec = errc::invalid_argument;
625 __res.ec = errc::result_out_of_range;
628 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
631 if (__builtin_mul_overflow(__val, __sign, &__tmp))
632 __res.ec = errc::result_out_of_range;
638 if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Up>::__max
639 > __gnu_cxx::__int_traits<_Tp>::__max)
641 if (__val > __gnu_cxx::__int_traits<_Tp>::__max)
642 __res.ec = errc::result_out_of_range;
654 /// floating-point format for primitive numerical conversion
655 enum class chars_format
657 scientific = 1, fixed = 2, hex = 4, general = fixed | scientific
660 constexpr chars_format
661 operator|(chars_format __lhs, chars_format __rhs) noexcept
662 { return (chars_format)((unsigned)__lhs | (unsigned)__rhs); }
664 constexpr chars_format
665 operator&(chars_format __lhs, chars_format __rhs) noexcept
666 { return (chars_format)((unsigned)__lhs & (unsigned)__rhs); }
668 constexpr chars_format
669 operator^(chars_format __lhs, chars_format __rhs) noexcept
670 { return (chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
672 constexpr chars_format
673 operator~(chars_format __fmt) noexcept
674 { return (chars_format)~(unsigned)__fmt; }
676 constexpr chars_format&
677 operator|=(chars_format& __lhs, chars_format __rhs) noexcept
678 { return __lhs = __lhs | __rhs; }
680 constexpr chars_format&
681 operator&=(chars_format& __lhs, chars_format __rhs) noexcept
682 { return __lhs = __lhs & __rhs; }
684 constexpr chars_format&
685 operator^=(chars_format& __lhs, chars_format __rhs) noexcept
686 { return __lhs = __lhs ^ __rhs; }
688 _GLIBCXX_END_NAMESPACE_VERSION
691 #endif // _GLIBCXX_CHARCONV