2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_CHARCONV
11 #define _LIBCPP_CHARCONV
18 // floating-point format for primitive numerical conversion
19 enum class chars_format {
20 scientific = unspecified,
23 general = fixed | scientific
26 // 23.20.2, primitive numerical output conversion
27 struct to_chars_result {
30 friend bool operator==(const to_chars_result&, const to_chars_result&) = default; // since C++20
33 to_chars_result to_chars(char* first, char* last, see below value,
35 to_chars_result to_chars(char* first, char* last, bool value,
36 int base = 10) = delete;
38 to_chars_result to_chars(char* first, char* last, float value);
39 to_chars_result to_chars(char* first, char* last, double value);
40 to_chars_result to_chars(char* first, char* last, long double value);
42 to_chars_result to_chars(char* first, char* last, float value,
44 to_chars_result to_chars(char* first, char* last, double value,
46 to_chars_result to_chars(char* first, char* last, long double value,
49 to_chars_result to_chars(char* first, char* last, float value,
50 chars_format fmt, int precision);
51 to_chars_result to_chars(char* first, char* last, double value,
52 chars_format fmt, int precision);
53 to_chars_result to_chars(char* first, char* last, long double value,
54 chars_format fmt, int precision);
56 // 23.20.3, primitive numerical input conversion
57 struct from_chars_result {
60 friend bool operator==(const from_chars_result&, const from_chars_result&) = default; // since C++20
63 from_chars_result from_chars(const char* first, const char* last,
64 see below& value, int base = 10);
66 from_chars_result from_chars(const char* first, const char* last,
68 chars_format fmt = chars_format::general);
69 from_chars_result from_chars(const char* first, const char* last,
71 chars_format fmt = chars_format::general);
72 from_chars_result from_chars(const char* first, const char* last,
74 chars_format fmt = chars_format::general);
80 #include <__availability>
82 #include <__charconv/chars_format.h>
83 #include <__charconv/from_chars_result.h>
84 #include <__charconv/to_chars_result.h>
87 #include <cmath> // for log2f
89 #include <cstdlib> // for _LIBCPP_UNREACHABLE
92 #include <type_traits>
96 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
97 #pragma GCC system_header
101 #include <__undef_macros>
103 _LIBCPP_BEGIN_NAMESPACE_STD
106 _LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_FUNC_VIS char* __u64toa(uint64_t __value, char* __buffer) _NOEXCEPT;
107 _LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_FUNC_VIS char* __u32toa(uint32_t __value, char* __buffer) _NOEXCEPT;
108 } // namespace __itoa
110 #ifndef _LIBCPP_CXX03_LANG
112 to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
113 from_chars_result from_chars(const char*, const char*, bool, int = 10) = delete;
118 static _LIBCPP_CONSTEXPR uint64_t __pow10_64[] = {
128 UINT64_C(1000000000),
129 UINT64_C(10000000000),
130 UINT64_C(100000000000),
131 UINT64_C(1000000000000),
132 UINT64_C(10000000000000),
133 UINT64_C(100000000000000),
134 UINT64_C(1000000000000000),
135 UINT64_C(10000000000000000),
136 UINT64_C(100000000000000000),
137 UINT64_C(1000000000000000000),
138 UINT64_C(10000000000000000000),
141 static _LIBCPP_CONSTEXPR uint32_t __pow10_32[] = {
142 UINT32_C(0), UINT32_C(10), UINT32_C(100),
143 UINT32_C(1000), UINT32_C(10000), UINT32_C(100000),
144 UINT32_C(1000000), UINT32_C(10000000), UINT32_C(100000000),
145 UINT32_C(1000000000),
148 template <typename _Tp, typename = void>
149 struct _LIBCPP_HIDDEN __traits_base
151 using type = uint64_t;
153 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
155 auto __t = (64 - _VSTD::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
156 return __t - (__v < __pow10_64[__t]) + 1;
159 _LIBCPP_AVAILABILITY_TO_CHARS
160 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
162 return __u64toa(__v, __p);
165 static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_64)& __pow() { return __pow10_64; }
168 template <typename _Tp>
169 struct _LIBCPP_HIDDEN
170 __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))>
172 using type = uint32_t;
174 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
176 auto __t = (32 - _VSTD::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
177 return __t - (__v < __pow10_32[__t]) + 1;
180 _LIBCPP_AVAILABILITY_TO_CHARS
181 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
183 return __u32toa(__v, __p);
186 static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_32)& __pow() { return __pow10_32; }
189 template <typename _Tp>
190 inline _LIBCPP_INLINE_VISIBILITY bool
191 __mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r)
193 auto __c = __a * __b;
195 return __c > numeric_limits<unsigned char>::max();
198 template <typename _Tp>
199 inline _LIBCPP_INLINE_VISIBILITY bool
200 __mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r)
202 auto __c = __a * __b;
204 return __c > numeric_limits<unsigned short>::max();
207 template <typename _Tp>
208 inline _LIBCPP_INLINE_VISIBILITY bool
209 __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r)
211 static_assert(is_unsigned<_Tp>::value, "");
212 #if !defined(_LIBCPP_COMPILER_MSVC)
213 return __builtin_mul_overflow(__a, __b, &__r);
215 bool __did = __b && (numeric_limits<_Tp>::max() / __b) < __a;
221 template <typename _Tp, typename _Up>
222 inline _LIBCPP_INLINE_VISIBILITY bool
223 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r)
225 return __mul_overflowed(__a, static_cast<_Tp>(__b), __r);
228 template <typename _Tp>
229 struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp>
231 static _LIBCPP_CONSTEXPR int digits = numeric_limits<_Tp>::digits10 + 1;
232 using __traits_base<_Tp>::__pow;
233 using typename __traits_base<_Tp>::type;
235 // precondition: at least one non-zero character available
236 static _LIBCPP_INLINE_VISIBILITY char const*
237 __read(char const* __p, char const* __ep, type& __a, type& __b)
239 type __cprod[digits];
240 int __j = digits - 1;
244 if (!('0' <= *__p && *__p <= '9'))
246 __cprod[--__i] = *__p++ - '0';
247 } while (__p != __ep && __i != 0);
249 __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1,
251 if (__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))
256 template <typename _It1, typename _It2, class _Up>
257 static _LIBCPP_INLINE_VISIBILITY _Up
258 __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init)
260 for (; __first1 < __last1; ++__first1, ++__first2)
261 __init = __init + *__first1 * *__first2;
266 } // namespace __itoa
268 template <typename _Tp>
269 inline _LIBCPP_INLINE_VISIBILITY _Tp
270 __complement(_Tp __x)
272 static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
273 return _Tp(~__x + 1);
276 template <typename _Tp>
277 _LIBCPP_AVAILABILITY_TO_CHARS
278 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
279 __to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
281 auto __x = __to_unsigned_like(__value);
282 if (__value < 0 && __first != __last)
285 __x = __complement(__x);
288 return __to_chars_itoa(__first, __last, __x, false_type());
291 template <typename _Tp>
292 _LIBCPP_AVAILABILITY_TO_CHARS
293 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
294 __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type)
296 using __tx = __itoa::__traits<_Tp>;
297 auto __diff = __last - __first;
299 if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
300 return {__tx::__convert(__value, __first), errc(0)};
302 return {__last, errc::value_too_large};
305 template <typename _Tp>
306 _LIBCPP_AVAILABILITY_TO_CHARS
307 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
308 __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
311 auto __x = __to_unsigned_like(__value);
312 if (__value < 0 && __first != __last)
315 __x = __complement(__x);
318 return __to_chars_integral(__first, __last, __x, __base, false_type());
321 template <typename _Tp>
322 _LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_INLINE_VISIBILITY int __to_chars_integral_width(_Tp __value, unsigned __base) {
323 _LIBCPP_ASSERT(__value >= 0, "The function requires a non-negative value.");
325 unsigned __base_2 = __base * __base;
326 unsigned __base_3 = __base_2 * __base;
327 unsigned __base_4 = __base_2 * __base_2;
331 if (__value < __base)
333 if (__value < __base_2)
335 if (__value < __base_3)
337 if (__value < __base_4)
344 _LIBCPP_UNREACHABLE();
347 template <typename _Tp>
348 _LIBCPP_AVAILABILITY_TO_CHARS
349 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
350 __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
354 return __to_chars_itoa(__first, __last, __value, false_type());
356 ptrdiff_t __cap = __last - __first;
357 int __n = __to_chars_integral_width(__value, __base);
359 return {__last, errc::value_too_large};
361 __last = __first + __n;
364 unsigned __c = __value % __base;
366 *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c];
367 } while (__value != 0);
368 return {__last, errc(0)};
371 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
372 _LIBCPP_AVAILABILITY_TO_CHARS
373 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
374 to_chars(char* __first, char* __last, _Tp __value)
376 return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>());
379 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
380 _LIBCPP_AVAILABILITY_TO_CHARS
381 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
382 to_chars(char* __first, char* __last, _Tp __value, int __base)
384 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
385 return __to_chars_integral(__first, __last, __value, __base,
389 template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
390 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
391 __sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
393 using __tl = numeric_limits<_Tp>;
394 decltype(__to_unsigned_like(__value)) __x;
396 bool __neg = (__first != __last && *__first == '-');
397 auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);
400 case errc::invalid_argument:
401 return {__first, __r.ec};
402 case errc::result_out_of_range:
410 if (__x <= __complement(__to_unsigned_like(__tl::min())))
412 __x = __complement(__x);
413 _VSTD::memcpy(&__value, &__x, sizeof(__x));
419 if (__x <= __tl::max())
426 return {__r.ptr, errc::result_out_of_range};
429 template <typename _Tp>
430 inline _LIBCPP_INLINE_VISIBILITY bool
431 __in_pattern(_Tp __c)
433 return '0' <= __c && __c <= '9';
436 struct _LIBCPP_HIDDEN __in_pattern_result
441 explicit _LIBCPP_INLINE_VISIBILITY operator bool() const { return __ok; }
444 template <typename _Tp>
445 inline _LIBCPP_INLINE_VISIBILITY __in_pattern_result
446 __in_pattern(_Tp __c, int __base)
449 return {'0' <= __c && __c < '0' + __base, __c - '0'};
450 else if (__in_pattern(__c))
451 return {true, __c - '0'};
452 else if ('a' <= __c && __c < 'a' + __base - 10)
453 return {true, __c - 'a' + 10};
455 return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10};
458 template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
459 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
460 __subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f,
463 auto __find_non_zero = [](_It __first, _It __last) {
464 for (; __first != __last; ++__first)
470 auto __p = __find_non_zero(__first, __last);
471 if (__p == __last || !__in_pattern(*__p, __args...))
474 return {__first, errc::invalid_argument};
482 auto __r = __f(__p, __last, __value, __args...);
483 if (__r.ec == errc::result_out_of_range)
485 for (; __r.ptr != __last; ++__r.ptr)
487 if (!__in_pattern(*__r.ptr, __args...))
495 template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
496 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
497 __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
499 using __tx = __itoa::__traits<_Tp>;
500 using __output_type = typename __tx::type;
502 return __subject_seq_combinator(
503 __first, __last, __value,
504 [](const char* __first, const char* __last,
505 _Tp& __value) -> from_chars_result {
506 __output_type __a, __b;
507 auto __p = __tx::__read(__first, __last, __a, __b);
508 if (__p == __last || !__in_pattern(*__p))
510 __output_type __m = numeric_limits<_Tp>::max();
511 if (__m >= __a && __m - __a >= __b)
517 return {__p, errc::result_out_of_range};
521 template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
522 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
523 __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
525 using __t = decltype(__to_unsigned_like(__value));
526 return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
529 template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
530 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
531 __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
535 return __from_chars_atoi(__first, __last, __value);
537 return __subject_seq_combinator(
538 __first, __last, __value,
539 [](const char* __p, const char* __lastx, _Tp& __value,
540 int __base) -> from_chars_result {
541 using __tl = numeric_limits<_Tp>;
542 auto __digits = __tl::digits / log2f(float(__base));
543 _Tp __a = __in_pattern(*__p++, __base).__val, __b = 0;
545 for (int __i = 1; __p != __lastx; ++__i, ++__p)
547 if (auto __c = __in_pattern(*__p, __base))
549 if (__i < __digits - 1)
550 __a = __a * __base + __c.__val;
553 if (!__itoa::__mul_overflowed(__a, __base, __a))
563 if (__p == __lastx || !__in_pattern(*__p, __base))
565 if (__tl::max() - __a >= __b)
571 return {__p, errc::result_out_of_range};
576 template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
577 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
578 __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
581 using __t = decltype(__to_unsigned_like(__value));
582 return __sign_combinator(__first, __last, __value,
583 __from_chars_integral<__t>, __base);
586 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
587 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
588 from_chars(const char* __first, const char* __last, _Tp& __value)
590 return __from_chars_atoi(__first, __last, __value);
593 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
594 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
595 from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
597 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
598 return __from_chars_integral(__first, __last, __value, __base);
601 // Floating-point implementation starts here.
602 // Unlike the other parts of charconv this is only available in C++17 and newer.
603 #if _LIBCPP_STD_VER > 14
605 _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
606 to_chars_result to_chars(char* __first, char* __last, float __value);
608 _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
609 to_chars_result to_chars(char* __first, char* __last, double __value);
611 _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
612 to_chars_result to_chars(char* __first, char* __last, long double __value);
614 _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
615 to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt);
617 _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
618 to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt);
620 _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
621 to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt);
623 _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
624 to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt, int __precision);
626 _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
627 to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt, int __precision);
629 _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
630 to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt, int __precision);
632 # endif // _LIBCPP_STD_VER > 14
633 #endif // _LIBCPP_CXX03_LANG
635 _LIBCPP_END_NAMESPACE_STD
639 #endif // _LIBCPP_CHARCONV