2 //===------------------------------ charconv ------------------------------===//
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 {
32 to_chars_result to_chars(char* first, char* last, see below value,
35 to_chars_result to_chars(char* first, char* last, float value);
36 to_chars_result to_chars(char* first, char* last, double value);
37 to_chars_result to_chars(char* first, char* last, long double value);
39 to_chars_result to_chars(char* first, char* last, float value,
41 to_chars_result to_chars(char* first, char* last, double value,
43 to_chars_result to_chars(char* first, char* last, long double value,
46 to_chars_result to_chars(char* first, char* last, float value,
47 chars_format fmt, int precision);
48 to_chars_result to_chars(char* first, char* last, double value,
49 chars_format fmt, int precision);
50 to_chars_result to_chars(char* first, char* last, long double value,
51 chars_format fmt, int precision);
53 // 23.20.3, primitive numerical input conversion
54 struct from_chars_result {
59 from_chars_result from_chars(const char* first, const char* last,
60 see below& value, int base = 10);
62 from_chars_result from_chars(const char* first, const char* last,
64 chars_format fmt = chars_format::general);
65 from_chars_result from_chars(const char* first, const char* last,
67 chars_format fmt = chars_format::general);
68 from_chars_result from_chars(const char* first, const char* last,
70 chars_format fmt = chars_format::general);
77 #include <type_traits>
85 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
86 #pragma GCC system_header
90 #include <__undef_macros>
92 _LIBCPP_BEGIN_NAMESPACE_STD
95 _LIBCPP_FUNC_VIS char* __u64toa(uint64_t __value, char* __buffer);
96 _LIBCPP_FUNC_VIS char* __u32toa(uint32_t __value, char* __buffer);
99 #ifndef _LIBCPP_CXX03_LANG
101 enum class _LIBCPP_ENUM_VIS chars_format
106 general = fixed | scientific
109 struct _LIBCPP_TYPE_VIS to_chars_result
115 struct _LIBCPP_TYPE_VIS from_chars_result
121 void to_chars(char*, char*, bool, int = 10) = delete;
122 void from_chars(const char*, const char*, bool, int = 10) = delete;
127 static _LIBCPP_CONSTEXPR uint64_t __pow10_64[] = {
137 UINT64_C(1000000000),
138 UINT64_C(10000000000),
139 UINT64_C(100000000000),
140 UINT64_C(1000000000000),
141 UINT64_C(10000000000000),
142 UINT64_C(100000000000000),
143 UINT64_C(1000000000000000),
144 UINT64_C(10000000000000000),
145 UINT64_C(100000000000000000),
146 UINT64_C(1000000000000000000),
147 UINT64_C(10000000000000000000),
150 static _LIBCPP_CONSTEXPR uint32_t __pow10_32[] = {
151 UINT32_C(0), UINT32_C(10), UINT32_C(100),
152 UINT32_C(1000), UINT32_C(10000), UINT32_C(100000),
153 UINT32_C(1000000), UINT32_C(10000000), UINT32_C(100000000),
154 UINT32_C(1000000000),
157 template <typename _Tp, typename = void>
158 struct _LIBCPP_HIDDEN __traits_base
160 using type = uint64_t;
162 #if !defined(_LIBCPP_COMPILER_MSVC)
163 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
165 auto __t = (64 - __builtin_clzll(__v | 1)) * 1233 >> 12;
166 return __t - (__v < __pow10_64[__t]) + 1;
170 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
172 return __u64toa(__v, __p);
175 static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_64)& __pow() { return __pow10_64; }
178 template <typename _Tp>
179 struct _LIBCPP_HIDDEN
180 __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))>
182 using type = uint32_t;
184 #if !defined(_LIBCPP_COMPILER_MSVC)
185 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
187 auto __t = (32 - __builtin_clz(__v | 1)) * 1233 >> 12;
188 return __t - (__v < __pow10_32[__t]) + 1;
192 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
194 return __u32toa(__v, __p);
197 static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_32)& __pow() { return __pow10_32; }
200 template <typename _Tp>
201 inline _LIBCPP_INLINE_VISIBILITY bool
202 __mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r)
204 auto __c = __a * __b;
206 return __c > (numeric_limits<unsigned char>::max)();
209 template <typename _Tp>
210 inline _LIBCPP_INLINE_VISIBILITY bool
211 __mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r)
213 auto __c = __a * __b;
215 return __c > (numeric_limits<unsigned short>::max)();
218 template <typename _Tp>
219 inline _LIBCPP_INLINE_VISIBILITY bool
220 __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r)
222 static_assert(is_unsigned<_Tp>::value, "");
223 #if !defined(_LIBCPP_COMPILER_MSVC)
224 return __builtin_mul_overflow(__a, __b, &__r);
226 bool __did = __b && ((numeric_limits<_Tp>::max)() / __b) < __a;
232 template <typename _Tp, typename _Up>
233 inline _LIBCPP_INLINE_VISIBILITY bool
234 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r)
236 return __mul_overflowed(__a, static_cast<_Tp>(__b), __r);
239 template <typename _Tp>
240 struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp>
242 static _LIBCPP_CONSTEXPR int digits = numeric_limits<_Tp>::digits10 + 1;
243 using __traits_base<_Tp>::__pow;
244 using typename __traits_base<_Tp>::type;
246 // precondition: at least one non-zero character available
247 static _LIBCPP_INLINE_VISIBILITY char const*
248 __read(char const* __p, char const* __ep, type& __a, type& __b)
250 type __cprod[digits];
251 int __j = digits - 1;
255 if (!('0' <= *__p && *__p <= '9'))
257 __cprod[--__i] = *__p++ - '0';
258 } while (__p != __ep && __i != 0);
260 __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1,
262 if (__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))
267 template <typename _It1, typename _It2, class _Up>
268 static _LIBCPP_INLINE_VISIBILITY _Up
269 __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init)
271 for (; __first1 < __last1; ++__first1, ++__first2)
272 __init = __init + *__first1 * *__first2;
277 } // namespace __itoa
279 template <typename _Tp>
280 inline _LIBCPP_INLINE_VISIBILITY _Tp
281 __complement(_Tp __x)
283 static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
284 return _Tp(~__x + 1);
287 template <typename _Tp>
288 inline _LIBCPP_INLINE_VISIBILITY typename make_unsigned<_Tp>::type
289 __to_unsigned(_Tp __x)
291 return static_cast<typename make_unsigned<_Tp>::type>(__x);
294 template <typename _Tp>
295 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
296 __to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
298 auto __x = __to_unsigned(__value);
299 if (__value < 0 && __first != __last)
302 __x = __complement(__x);
305 return __to_chars_itoa(__first, __last, __x, false_type());
308 template <typename _Tp>
309 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
310 __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type)
312 using __tx = __itoa::__traits<_Tp>;
313 auto __diff = __last - __first;
315 #if !defined(_LIBCPP_COMPILER_MSVC)
316 if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
317 return {__tx::__convert(__value, __first), errc(0)};
319 return {__last, errc::value_too_large};
321 if (__tx::digits <= __diff)
322 return {__tx::__convert(__value, __first), {}};
325 char __buf[__tx::digits];
326 auto __p = __tx::__convert(__value, __buf);
327 auto __len = __p - __buf;
330 memcpy(__first, __buf, __len);
331 return {__first + __len, {}};
334 return {__last, errc::value_too_large};
339 template <typename _Tp>
340 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
341 __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
344 auto __x = __to_unsigned(__value);
345 if (__value < 0 && __first != __last)
348 __x = __complement(__x);
351 return __to_chars_integral(__first, __last, __x, __base, false_type());
354 template <typename _Tp>
355 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
356 __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
360 return __to_chars_itoa(__first, __last, __value, false_type());
363 while (__p != __first)
365 auto __c = __value % __base;
367 *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c];
372 auto __len = __last - __p;
373 if (__value != 0 || !__len)
374 return {__last, errc::value_too_large};
377 memmove(__first, __p, __len);
378 return {__first + __len, {}};
382 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
383 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
384 to_chars(char* __first, char* __last, _Tp __value)
386 return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>());
389 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
390 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
391 to_chars(char* __first, char* __last, _Tp __value, int __base)
393 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
394 return __to_chars_integral(__first, __last, __value, __base,
398 template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
399 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
400 __sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
402 using __tl = numeric_limits<_Tp>;
403 decltype(__to_unsigned(__value)) __x;
405 bool __neg = (__first != __last && *__first == '-');
406 auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);
409 case errc::invalid_argument:
410 return {__first, __r.ec};
411 case errc::result_out_of_range:
419 if (__x <= __complement(__to_unsigned(__tl::min())))
421 __x = __complement(__x);
422 memcpy(&__value, &__x, sizeof(__x));
428 if (__x <= (__tl::max)())
435 return {__r.ptr, errc::result_out_of_range};
438 template <typename _Tp>
439 inline _LIBCPP_INLINE_VISIBILITY bool
440 __in_pattern(_Tp __c)
442 return '0' <= __c && __c <= '9';
445 struct _LIBCPP_HIDDEN __in_pattern_result
450 explicit _LIBCPP_INLINE_VISIBILITY operator bool() const { return __ok; }
453 template <typename _Tp>
454 inline _LIBCPP_INLINE_VISIBILITY __in_pattern_result
455 __in_pattern(_Tp __c, int __base)
458 return {'0' <= __c && __c < '0' + __base, __c - '0'};
459 else if (__in_pattern(__c))
460 return {true, __c - '0'};
461 else if ('a' <= __c && __c < 'a' + __base - 10)
462 return {true, __c - 'a' + 10};
464 return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10};
467 template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
468 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
469 __subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f,
472 auto __find_non_zero = [](_It __first, _It __last) {
473 for (; __first != __last; ++__first)
479 auto __p = __find_non_zero(__first, __last);
480 if (__p == __last || !__in_pattern(*__p, __args...))
483 return {__first, errc::invalid_argument};
491 auto __r = __f(__p, __last, __value, __args...);
492 if (__r.ec == errc::result_out_of_range)
494 for (; __r.ptr != __last; ++__r.ptr)
496 if (!__in_pattern(*__r.ptr, __args...))
504 template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
505 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
506 __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
508 using __tx = __itoa::__traits<_Tp>;
509 using __output_type = typename __tx::type;
511 return __subject_seq_combinator(
512 __first, __last, __value,
513 [](const char* __first, const char* __last,
514 _Tp& __value) -> from_chars_result {
515 __output_type __a, __b;
516 auto __p = __tx::__read(__first, __last, __a, __b);
517 if (__p == __last || !__in_pattern(*__p))
519 __output_type __m = (numeric_limits<_Tp>::max)();
520 if (__m >= __a && __m - __a >= __b)
526 return {__p, errc::result_out_of_range};
530 template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
531 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
532 __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
534 using __t = decltype(__to_unsigned(__value));
535 return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
538 template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
539 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
540 __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
544 return __from_chars_atoi(__first, __last, __value);
546 return __subject_seq_combinator(
547 __first, __last, __value,
548 [](const char* __p, const char* __last, _Tp& __value,
549 int __base) -> from_chars_result {
550 using __tl = numeric_limits<_Tp>;
551 auto __digits = __tl::digits / log2f(float(__base));
552 _Tp __a = __in_pattern(*__p++, __base).__val, __b = 0;
554 for (int __i = 1; __p != __last; ++__i, ++__p)
556 if (auto __c = __in_pattern(*__p, __base))
558 if (__i < __digits - 1)
559 __a = __a * __base + __c.__val;
562 if (!__itoa::__mul_overflowed(__a, __base, __a))
572 if (__p == __last || !__in_pattern(*__p, __base))
574 if ((__tl::max)() - __a >= __b)
580 return {__p, errc::result_out_of_range};
585 template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
586 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
587 __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
590 using __t = decltype(__to_unsigned(__value));
591 return __sign_combinator(__first, __last, __value,
592 __from_chars_integral<__t>, __base);
595 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
596 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
597 from_chars(const char* __first, const char* __last, _Tp& __value)
599 return __from_chars_atoi(__first, __last, __value);
602 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
603 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
604 from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
606 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
607 return __from_chars_integral(__first, __last, __value, __base);
610 #endif // _LIBCPP_CXX03_LANG
612 _LIBCPP_END_NAMESPACE_STD
616 #endif // _LIBCPP_CHARCONV