1 // Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
3 // Copyright (C) 2017-2018 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>
39 #include <bits/error_constants.h> // for std::errc
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 /// Result type of std::to_chars
46 struct to_chars_result
52 /// Result type of std::from_chars
53 struct from_chars_result
61 template<typename _Tp, typename... _Types>
62 using __is_one_of = __or_<is_same<_Tp, _Types>...>;
64 template<typename _Tp>
65 using __is_int_to_chars_type = __and_<is_integral<_Tp>,
66 __not_<__is_one_of<_Tp, bool, char16_t, char32_t
67 #if _GLIBCXX_USE_WCHAR_T
72 template<typename _Tp>
73 using __integer_to_chars_result_type
74 = enable_if_t<__is_int_to_chars_type<_Tp>::value, to_chars_result>;
76 template<typename _Tp>
77 using __unsigned_least_t
78 = conditional_t<(sizeof(_Tp) <= sizeof(int)), unsigned int,
79 conditional_t<(sizeof(_Tp) <= sizeof(long)), unsigned long,
80 conditional_t<(sizeof(_Tp) <= sizeof(long long)), unsigned long long,
81 #if _GLIBCXX_USE_INT128
82 conditional_t<(sizeof(_Tp) <= sizeof(__int128)), unsigned __int128,
85 #if _GLIBCXX_USE_INT128
90 // Generic implementation for arbitrary bases.
91 template<typename _Tp>
93 __to_chars_len(_Tp __value, int __base = 10) noexcept
95 static_assert(is_integral<_Tp>::value, "implementation bug");
96 static_assert(is_unsigned<_Tp>::value, "implementation bug");
99 const int __b2 = __base * __base;
100 const int __b3 = __b2 * __base;
101 const int __b4 = __b3 * __base;
104 if (__value < __base) return __n;
105 if (__value < __b2) return __n + 1;
106 if (__value < __b3) return __n + 2;
107 if (__value < __b4) return __n + 3;
108 __value /= (unsigned)__b4;
113 template<typename _Tp>
115 __to_chars_len_2(_Tp __value) noexcept
117 static_assert(is_integral<_Tp>::value, "implementation bug");
118 static_assert(is_unsigned<_Tp>::value, "implementation bug");
120 constexpr size_t __nbits = __CHAR_BIT__ * sizeof(_Tp);
122 // N.B. __builtin_clzll is undefined if __value == 0, but std::to_chars
123 // handles zero values directly.
125 // For sizeof(_Tp) > 1 this is an order of magnitude faster than
126 // the generic __to_chars_len.
128 - (__builtin_clzll(__value)
129 - ((__CHAR_BIT__ * sizeof(long long)) - __nbits));
132 template<typename _Tp>
134 __to_chars_len_8(_Tp __value) noexcept
136 static_assert(is_integral<_Tp>::value, "implementation bug");
137 static_assert(is_unsigned<_Tp>::value, "implementation bug");
139 constexpr size_t __nbits = __CHAR_BIT__ * sizeof(_Tp);
141 if _GLIBCXX17_CONSTEXPR (__nbits <= 16)
143 return __value > 077777u ? 6u
144 : __value > 07777u ? 5u
145 : __value > 0777u ? 4u
146 : __value > 077u ? 3u
151 return __to_chars_len(__value, 8);
154 // Generic implementation for arbitrary bases.
155 template<typename _Tp>
157 __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
159 static_assert(is_integral<_Tp>::value, "implementation bug");
160 static_assert(is_unsigned<_Tp>::value, "implementation bug");
162 to_chars_result __res;
164 const unsigned __len = __to_chars_len(__val, __base);
166 if (__builtin_expect((__last - __first) < __len, 0))
169 __res.ec = errc::value_too_large;
173 unsigned __pos = __len - 1;
175 static constexpr char __digits[]
176 = "0123456789abcdefghijklmnopqrstuvwxyz";
178 while (__val >= __base)
180 auto const __quo = __val / __base;
181 auto const __rem = __val % __base;
182 __first[__pos--] = __digits[__rem];
185 *__first = __digits[__val];
187 __res.ptr = __first + __len;
192 template<typename _Tp>
193 __integer_to_chars_result_type<_Tp>
194 __to_chars_16(char* __first, char* __last, _Tp __val) noexcept
196 static_assert(is_integral<_Tp>::value, "implementation bug");
197 static_assert(is_unsigned<_Tp>::value, "implementation bug");
199 to_chars_result __res;
201 const unsigned __len = __to_chars_len(__val, 0x10);
203 if (__builtin_expect((__last - __first) < __len, 0))
206 __res.ec = errc::value_too_large;
210 static constexpr char __digits[513] =
211 "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
212 "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"
213 "404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"
214 "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f"
215 "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f"
216 "a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
217 "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
218 "e0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
219 unsigned __pos = __len - 1;
220 while (__val >= 0x100)
222 auto const __num = (__val % 0x100) * 2;
224 __first[__pos] = __digits[__num + 1];
225 __first[__pos - 1] = __digits[__num];
230 auto const __num = __val * 2;
231 __first[__pos] = __digits[__num + 1];
232 __first[__pos - 1] = __digits[__num];
235 __first[__pos] = "0123456789abcdef"[__val];
236 __res.ptr = __first + __len;
241 template<typename _Tp>
242 __integer_to_chars_result_type<_Tp>
243 __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
245 static_assert(is_integral<_Tp>::value, "implementation bug");
246 static_assert(is_unsigned<_Tp>::value, "implementation bug");
248 to_chars_result __res;
250 const unsigned __len = __to_chars_len(__val, 10);
252 if (__builtin_expect((__last - __first) < __len, 0))
255 __res.ec = errc::value_too_large;
259 static constexpr char __digits[201] =
260 "0001020304050607080910111213141516171819"
261 "2021222324252627282930313233343536373839"
262 "4041424344454647484950515253545556575859"
263 "6061626364656667686970717273747576777879"
264 "8081828384858687888990919293949596979899";
265 unsigned __pos = __len - 1;
268 auto const __num = (__val % 100) * 2;
270 __first[__pos] = __digits[__num + 1];
271 __first[__pos - 1] = __digits[__num];
276 auto const __num = __val * 2;
277 __first[__pos] = __digits[__num + 1];
278 __first[__pos - 1] = __digits[__num];
281 __first[__pos] = '0' + __val;
282 __res.ptr = __first + __len;
287 template<typename _Tp>
288 __integer_to_chars_result_type<_Tp>
289 __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
291 static_assert(is_integral<_Tp>::value, "implementation bug");
292 static_assert(is_unsigned<_Tp>::value, "implementation bug");
294 to_chars_result __res;
296 const unsigned __len = __to_chars_len_8(__val);
298 if (__builtin_expect((__last - __first) < __len, 0))
301 __res.ec = errc::value_too_large;
305 static constexpr char __digits[129] =
306 "00010203040506071011121314151617"
307 "20212223242526273031323334353637"
308 "40414243444546475051525354555657"
309 "60616263646566677071727374757677";
310 unsigned __pos = __len - 1;
311 while (__val >= 0100)
313 auto const __num = (__val % 0100) * 2;
315 __first[__pos] = __digits[__num + 1];
316 __first[__pos - 1] = __digits[__num];
321 auto const __num = __val * 2;
322 __first[__pos] = __digits[__num + 1];
323 __first[__pos - 1] = __digits[__num];
326 __first[__pos] = '0' + __val;
327 __res.ptr = __first + __len;
332 template<typename _Tp>
333 __integer_to_chars_result_type<_Tp>
334 __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
336 static_assert(is_integral<_Tp>::value, "implementation bug");
337 static_assert(is_unsigned<_Tp>::value, "implementation bug");
339 to_chars_result __res;
341 const unsigned __len = __to_chars_len_2(__val);
343 if (__builtin_expect((__last - __first) < __len, 0))
346 __res.ec = errc::value_too_large;
350 unsigned __pos = __len - 1;
354 __first[__pos--] = '0' + (__val & 1);
357 *__first = '0' + (__val & 1);
359 __res.ptr = __first + __len;
364 } // namespace __detail
366 template<typename _Tp>
367 __detail::__integer_to_chars_result_type<_Tp>
368 to_chars(char* __first, char* __last, _Tp __value, int __base = 10)
370 __glibcxx_assert(2 <= __base && __base <= 36);
372 using _Up = __detail::__unsigned_least_t<_Tp>;
373 _Up __unsigned_val = __value;
375 if (__value == 0 && __first != __last)
378 return { __first + 1, errc{} };
381 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
384 if (__builtin_expect(__first != __last, 1))
386 __unsigned_val = _Up(~__value) + _Up(1);
392 return __detail::__to_chars_16(__first, __last, __unsigned_val);
394 return __detail::__to_chars_10(__first, __last, __unsigned_val);
396 return __detail::__to_chars_8(__first, __last, __unsigned_val);
398 return __detail::__to_chars_2(__first, __last, __unsigned_val);
400 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
406 template<typename _Tp>
408 __raise_and_add(_Tp& __val, int __base, unsigned char __c)
410 if (__builtin_mul_overflow(__val, __base, &__val)
411 || __builtin_add_overflow(__val, __c, &__val))
416 /// std::from_chars implementation for integers in base 2.
417 template<typename _Tp>
419 __from_chars_binary(const char*& __first, const char* __last, _Tp& __val)
421 static_assert(is_integral<_Tp>::value, "implementation bug");
422 static_assert(is_unsigned<_Tp>::value, "implementation bug");
424 const ptrdiff_t __len = __last - __first;
428 const unsigned char __c = (unsigned)__first[__i] - '0';
430 __val = (__val << 1) | __c;
436 return __i <= (sizeof(_Tp) * __CHAR_BIT__);
439 /// std::from_chars implementation for integers in bases 3 to 10.
440 template<typename _Tp>
442 __from_chars_digit(const char*& __first, const char* __last, _Tp& __val,
445 static_assert(is_integral<_Tp>::value, "implementation bug");
446 static_assert(is_unsigned<_Tp>::value, "implementation bug");
448 auto __matches = [__base](char __c) {
449 return '0' <= __c && __c <= ('0' + (__base - 1));
452 while (__first != __last)
454 const char __c = *__first;
457 if (!__raise_and_add(__val, __base, __c - '0'))
459 while (++__first != __last && __matches(*__first))
471 constexpr unsigned char
472 __from_chars_alpha_to_num(char __c)
555 return std::numeric_limits<unsigned char>::max();
558 /// std::from_chars implementation for integers in bases 11 to 26.
559 template<typename _Tp>
561 __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
565 while (__first != __last)
567 unsigned char __c = *__first;
568 if (std::isdigit(__c))
572 __c = __from_chars_alpha_to_num(__c);
577 if (__builtin_expect(__valid, 1))
578 __valid = __raise_and_add(__val, __base, __c);
584 template<typename _Tp>
585 using __integer_from_chars_result_type
586 = enable_if_t<__is_int_to_chars_type<_Tp>::value, from_chars_result>;
588 } // namespace __detail
590 /// std::from_chars for integral types.
591 template<typename _Tp>
592 __detail::__integer_from_chars_result_type<_Tp>
593 from_chars(const char* __first, const char* __last, _Tp& __value,
596 __glibcxx_assert(2 <= __base && __base <= 36);
598 from_chars_result __res{__first, {}};
601 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
602 if (__first != __last && *__first == '-')
608 using _Up = __detail::__unsigned_least_t<_Tp>;
611 const auto __start = __first;
614 __valid = __detail::__from_chars_binary(__first, __last, __val);
615 else if (__base <= 10)
616 __valid = __detail::__from_chars_digit(__first, __last, __val, __base);
618 __valid = __detail::__from_chars_alnum(__first, __last, __val, __base);
620 if (__builtin_expect(__first == __start, 0))
621 __res.ec = errc::invalid_argument;
626 __res.ec = errc::result_out_of_range;
629 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
632 if (__builtin_mul_overflow(__val, __sign, &__tmp))
633 __res.ec = errc::result_out_of_range;
639 if _GLIBCXX17_CONSTEXPR
640 (numeric_limits<_Up>::max() > numeric_limits<_Tp>::max())
642 if (__val > numeric_limits<_Tp>::max())
643 __res.ec = errc::result_out_of_range;
655 _GLIBCXX_END_NAMESPACE_VERSION
658 #endif // _GLIBCXX_CHARCONV