1 // Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
3 // Copyright (C) 2017-2019 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
70 #if _GLIBCXX_USE_CHAR8_T
75 template<typename _Tp>
76 using __integer_to_chars_result_type
77 = enable_if_t<__is_int_to_chars_type<_Tp>::value, to_chars_result>;
79 template<typename _Tp>
80 using __unsigned_least_t
81 = conditional_t<(sizeof(_Tp) <= sizeof(int)), unsigned int,
82 conditional_t<(sizeof(_Tp) <= sizeof(long)), unsigned long,
83 conditional_t<(sizeof(_Tp) <= sizeof(long long)), unsigned long long,
84 #if _GLIBCXX_USE_INT128
85 conditional_t<(sizeof(_Tp) <= sizeof(__int128)), unsigned __int128,
88 #if _GLIBCXX_USE_INT128
93 // Generic implementation for arbitrary bases.
94 template<typename _Tp>
96 __to_chars_len(_Tp __value, int __base = 10) noexcept
98 static_assert(is_integral<_Tp>::value, "implementation bug");
99 static_assert(is_unsigned<_Tp>::value, "implementation bug");
102 const int __b2 = __base * __base;
103 const int __b3 = __b2 * __base;
104 const int __b4 = __b3 * __base;
107 if (__value < __base) return __n;
108 if (__value < __b2) return __n + 1;
109 if (__value < __b3) return __n + 2;
110 if (__value < __b4) return __n + 3;
111 __value /= (unsigned)__b4;
116 template<typename _Tp>
118 __to_chars_len_2(_Tp __value) noexcept
120 static_assert(is_integral<_Tp>::value, "implementation bug");
121 static_assert(is_unsigned<_Tp>::value, "implementation bug");
123 constexpr size_t __nbits = __CHAR_BIT__ * sizeof(_Tp);
125 // N.B. __builtin_clzll is undefined if __value == 0, but std::to_chars
126 // handles zero values directly.
128 // For sizeof(_Tp) > 1 this is an order of magnitude faster than
129 // the generic __to_chars_len.
131 - (__builtin_clzll(__value)
132 - ((__CHAR_BIT__ * sizeof(long long)) - __nbits));
135 template<typename _Tp>
137 __to_chars_len_8(_Tp __value) noexcept
139 static_assert(is_integral<_Tp>::value, "implementation bug");
140 static_assert(is_unsigned<_Tp>::value, "implementation bug");
142 constexpr size_t __nbits = __CHAR_BIT__ * sizeof(_Tp);
144 if _GLIBCXX17_CONSTEXPR (__nbits <= 16)
146 return __value > 077777u ? 6u
147 : __value > 07777u ? 5u
148 : __value > 0777u ? 4u
149 : __value > 077u ? 3u
154 return __to_chars_len(__value, 8);
157 // Generic implementation for arbitrary bases.
158 template<typename _Tp>
160 __to_chars(char* __first, char* __last, _Tp __val, int __base) 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(__val, __base);
169 if (__builtin_expect((__last - __first) < __len, 0))
172 __res.ec = errc::value_too_large;
176 unsigned __pos = __len - 1;
178 static constexpr char __digits[]
179 = "0123456789abcdefghijklmnopqrstuvwxyz";
181 while (__val >= __base)
183 auto const __quo = __val / __base;
184 auto const __rem = __val % __base;
185 __first[__pos--] = __digits[__rem];
188 *__first = __digits[__val];
190 __res.ptr = __first + __len;
195 template<typename _Tp>
196 __integer_to_chars_result_type<_Tp>
197 __to_chars_16(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, 0x10);
206 if (__builtin_expect((__last - __first) < __len, 0))
209 __res.ec = errc::value_too_large;
213 static constexpr char __digits[513] =
214 "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
215 "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"
216 "404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"
217 "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f"
218 "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f"
219 "a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
220 "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
221 "e0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
222 unsigned __pos = __len - 1;
223 while (__val >= 0x100)
225 auto const __num = (__val % 0x100) * 2;
227 __first[__pos] = __digits[__num + 1];
228 __first[__pos - 1] = __digits[__num];
233 auto const __num = __val * 2;
234 __first[__pos] = __digits[__num + 1];
235 __first[__pos - 1] = __digits[__num];
238 __first[__pos] = "0123456789abcdef"[__val];
239 __res.ptr = __first + __len;
244 template<typename _Tp>
245 __integer_to_chars_result_type<_Tp>
246 __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
248 static_assert(is_integral<_Tp>::value, "implementation bug");
249 static_assert(is_unsigned<_Tp>::value, "implementation bug");
251 to_chars_result __res;
253 const unsigned __len = __to_chars_len(__val, 10);
255 if (__builtin_expect((__last - __first) < __len, 0))
258 __res.ec = errc::value_too_large;
262 static constexpr char __digits[201] =
263 "0001020304050607080910111213141516171819"
264 "2021222324252627282930313233343536373839"
265 "4041424344454647484950515253545556575859"
266 "6061626364656667686970717273747576777879"
267 "8081828384858687888990919293949596979899";
268 unsigned __pos = __len - 1;
271 auto const __num = (__val % 100) * 2;
273 __first[__pos] = __digits[__num + 1];
274 __first[__pos - 1] = __digits[__num];
279 auto const __num = __val * 2;
280 __first[__pos] = __digits[__num + 1];
281 __first[__pos - 1] = __digits[__num];
284 __first[__pos] = '0' + __val;
285 __res.ptr = __first + __len;
290 template<typename _Tp>
291 __integer_to_chars_result_type<_Tp>
292 __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
294 static_assert(is_integral<_Tp>::value, "implementation bug");
295 static_assert(is_unsigned<_Tp>::value, "implementation bug");
297 to_chars_result __res;
299 const unsigned __len = __to_chars_len_8(__val);
301 if (__builtin_expect((__last - __first) < __len, 0))
304 __res.ec = errc::value_too_large;
308 static constexpr char __digits[129] =
309 "00010203040506071011121314151617"
310 "20212223242526273031323334353637"
311 "40414243444546475051525354555657"
312 "60616263646566677071727374757677";
313 unsigned __pos = __len - 1;
314 while (__val >= 0100)
316 auto const __num = (__val % 0100) * 2;
318 __first[__pos] = __digits[__num + 1];
319 __first[__pos - 1] = __digits[__num];
324 auto const __num = __val * 2;
325 __first[__pos] = __digits[__num + 1];
326 __first[__pos - 1] = __digits[__num];
329 __first[__pos] = '0' + __val;
330 __res.ptr = __first + __len;
335 template<typename _Tp>
336 __integer_to_chars_result_type<_Tp>
337 __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
339 static_assert(is_integral<_Tp>::value, "implementation bug");
340 static_assert(is_unsigned<_Tp>::value, "implementation bug");
342 to_chars_result __res;
344 const unsigned __len = __to_chars_len_2(__val);
346 if (__builtin_expect((__last - __first) < __len, 0))
349 __res.ec = errc::value_too_large;
353 unsigned __pos = __len - 1;
357 __first[__pos--] = '0' + (__val & 1);
360 *__first = '0' + (__val & 1);
362 __res.ptr = __first + __len;
367 } // namespace __detail
369 template<typename _Tp>
370 __detail::__integer_to_chars_result_type<_Tp>
371 to_chars(char* __first, char* __last, _Tp __value, int __base = 10)
373 __glibcxx_assert(2 <= __base && __base <= 36);
375 using _Up = __detail::__unsigned_least_t<_Tp>;
376 _Up __unsigned_val = __value;
378 if (__value == 0 && __first != __last)
381 return { __first + 1, errc{} };
384 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
387 if (__builtin_expect(__first != __last, 1))
389 __unsigned_val = _Up(~__value) + _Up(1);
395 return __detail::__to_chars_16(__first, __last, __unsigned_val);
397 return __detail::__to_chars_10(__first, __last, __unsigned_val);
399 return __detail::__to_chars_8(__first, __last, __unsigned_val);
401 return __detail::__to_chars_2(__first, __last, __unsigned_val);
403 return __detail::__to_chars(__first, __last, __unsigned_val, __base);
409 template<typename _Tp>
411 __raise_and_add(_Tp& __val, int __base, unsigned char __c)
413 if (__builtin_mul_overflow(__val, __base, &__val)
414 || __builtin_add_overflow(__val, __c, &__val))
419 /// std::from_chars implementation for integers in base 2.
420 template<typename _Tp>
422 __from_chars_binary(const char*& __first, const char* __last, _Tp& __val)
424 static_assert(is_integral<_Tp>::value, "implementation bug");
425 static_assert(is_unsigned<_Tp>::value, "implementation bug");
427 const ptrdiff_t __len = __last - __first;
431 const unsigned char __c = (unsigned)__first[__i] - '0';
433 __val = (__val << 1) | __c;
439 return __i <= (sizeof(_Tp) * __CHAR_BIT__);
442 /// std::from_chars implementation for integers in bases 3 to 10.
443 template<typename _Tp>
445 __from_chars_digit(const char*& __first, const char* __last, _Tp& __val,
448 static_assert(is_integral<_Tp>::value, "implementation bug");
449 static_assert(is_unsigned<_Tp>::value, "implementation bug");
451 auto __matches = [__base](char __c) {
452 return '0' <= __c && __c <= ('0' + (__base - 1));
455 while (__first != __last)
457 const char __c = *__first;
460 if (!__raise_and_add(__val, __base, __c - '0'))
462 while (++__first != __last && __matches(*__first))
474 constexpr unsigned char
475 __from_chars_alpha_to_num(char __c)
558 return std::numeric_limits<unsigned char>::max();
561 /// std::from_chars implementation for integers in bases 11 to 26.
562 template<typename _Tp>
564 __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
568 while (__first != __last)
570 unsigned char __c = *__first;
571 if (std::isdigit(__c))
575 __c = __from_chars_alpha_to_num(__c);
580 if (__builtin_expect(__valid, 1))
581 __valid = __raise_and_add(__val, __base, __c);
587 template<typename _Tp>
588 using __integer_from_chars_result_type
589 = enable_if_t<__is_int_to_chars_type<_Tp>::value, from_chars_result>;
591 } // namespace __detail
593 /// std::from_chars for integral types.
594 template<typename _Tp>
595 __detail::__integer_from_chars_result_type<_Tp>
596 from_chars(const char* __first, const char* __last, _Tp& __value,
599 __glibcxx_assert(2 <= __base && __base <= 36);
601 from_chars_result __res{__first, {}};
604 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
605 if (__first != __last && *__first == '-')
611 using _Up = __detail::__unsigned_least_t<_Tp>;
614 const auto __start = __first;
617 __valid = __detail::__from_chars_binary(__first, __last, __val);
618 else if (__base <= 10)
619 __valid = __detail::__from_chars_digit(__first, __last, __val, __base);
621 __valid = __detail::__from_chars_alnum(__first, __last, __val, __base);
623 if (__builtin_expect(__first == __start, 0))
624 __res.ec = errc::invalid_argument;
629 __res.ec = errc::result_out_of_range;
632 if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
635 if (__builtin_mul_overflow(__val, __sign, &__tmp))
636 __res.ec = errc::result_out_of_range;
642 if _GLIBCXX17_CONSTEXPR
643 (numeric_limits<_Up>::max() > numeric_limits<_Tp>::max())
645 if (__val > numeric_limits<_Tp>::max())
646 __res.ec = errc::result_out_of_range;
658 _GLIBCXX_END_NAMESPACE_VERSION
661 #endif // _GLIBCXX_CHARCONV