2 //===------------------------------ charconv ------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_CHARCONV
12 #define _LIBCPP_CHARCONV
19 // floating-point format for primitive numerical conversion
20 enum class chars_format {
21 scientific = unspecified,
24 general = fixed | scientific
27 // 23.20.2, primitive numerical output conversion
28 struct to_chars_result {
33 to_chars_result to_chars(char* first, char* last, see below value,
36 to_chars_result to_chars(char* first, char* last, float value);
37 to_chars_result to_chars(char* first, char* last, double value);
38 to_chars_result to_chars(char* first, char* last, long double value);
40 to_chars_result to_chars(char* first, char* last, float value,
42 to_chars_result to_chars(char* first, char* last, double value,
44 to_chars_result to_chars(char* first, char* last, long double value,
47 to_chars_result to_chars(char* first, char* last, float value,
48 chars_format fmt, int precision);
49 to_chars_result to_chars(char* first, char* last, double value,
50 chars_format fmt, int precision);
51 to_chars_result to_chars(char* first, char* last, long double value,
52 chars_format fmt, int precision);
54 // 23.20.3, primitive numerical input conversion
55 struct from_chars_result {
60 from_chars_result from_chars(const char* first, const char* last,
61 see below& value, int base = 10);
63 from_chars_result from_chars(const char* first, const char* last,
65 chars_format fmt = chars_format::general);
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);
78 #include <type_traits>
86 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
87 #pragma GCC system_header
90 _LIBCPP_BEGIN_NAMESPACE_STD
92 #if _LIBCPP_STD_VER > 11
94 enum class _LIBCPP_ENUM_VIS chars_format
99 general = fixed | scientific
102 struct _LIBCPP_TYPE_VIS to_chars_result
108 struct _LIBCPP_TYPE_VIS from_chars_result
114 void to_chars(char*, char*, bool, int = 10) = delete;
115 void from_chars(const char*, const char*, bool, int = 10) = delete;
120 static constexpr uint64_t __pow10_64[] = {
130 UINT64_C(1000000000),
131 UINT64_C(10000000000),
132 UINT64_C(100000000000),
133 UINT64_C(1000000000000),
134 UINT64_C(10000000000000),
135 UINT64_C(100000000000000),
136 UINT64_C(1000000000000000),
137 UINT64_C(10000000000000000),
138 UINT64_C(100000000000000000),
139 UINT64_C(1000000000000000000),
140 UINT64_C(10000000000000000000),
143 static constexpr uint32_t __pow10_32[] = {
144 UINT32_C(0), UINT32_C(10), UINT32_C(100),
145 UINT32_C(1000), UINT32_C(10000), UINT32_C(100000),
146 UINT32_C(1000000), UINT32_C(10000000), UINT32_C(100000000),
147 UINT32_C(1000000000),
150 _LIBCPP_FUNC_VIS char* __u64toa(uint64_t __value, char* __buffer);
151 _LIBCPP_FUNC_VIS char* __u32toa(uint32_t __value, char* __buffer);
153 template <typename _Tp, typename = void>
154 struct _LIBCPP_HIDDEN __traits_base
156 using type = uint64_t;
158 #if !defined(_LIBCPP_COMPILER_MSVC)
159 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
161 auto __t = (64 - __builtin_clzll(__v | 1)) * 1233 >> 12;
162 return __t - (__v < __pow10_64[__t]) + 1;
166 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
168 return __u64toa(__v, __p);
171 static _LIBCPP_INLINE_VISIBILITY auto& __pow() { return __pow10_64; }
174 template <typename _Tp>
175 struct _LIBCPP_HIDDEN
176 __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))>
178 using type = uint32_t;
180 #if !defined(_LIBCPP_COMPILER_MSVC)
181 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
183 auto __t = (32 - __builtin_clz(__v | 1)) * 1233 >> 12;
184 return __t - (__v < __pow10_32[__t]) + 1;
188 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
190 return __u32toa(__v, __p);
193 static _LIBCPP_INLINE_VISIBILITY auto& __pow() { return __pow10_32; }
196 template <typename _Tp>
197 inline _LIBCPP_INLINE_VISIBILITY bool
198 __mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r)
200 auto __c = __a * __b;
202 return __c > (numeric_limits<unsigned char>::max)();
205 template <typename _Tp>
206 inline _LIBCPP_INLINE_VISIBILITY bool
207 __mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r)
209 auto __c = __a * __b;
211 return __c > (numeric_limits<unsigned short>::max)();
214 template <typename _Tp>
215 inline _LIBCPP_INLINE_VISIBILITY bool
216 __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r)
218 static_assert(is_unsigned<_Tp>::value, "");
219 #if !defined(_LIBCPP_COMPILER_MSVC)
220 return __builtin_mul_overflow(__a, __b, &__r);
222 bool __did = __b && ((numeric_limits<_Tp>::max)() / __b) < __a;
228 template <typename _Tp, typename _Up>
229 inline _LIBCPP_INLINE_VISIBILITY bool
230 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r)
232 return __mul_overflowed(__a, static_cast<_Tp>(__b), __r);
235 template <typename _Tp>
236 struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp>
238 static constexpr int digits = numeric_limits<_Tp>::digits10 + 1;
239 using __traits_base<_Tp>::__pow;
240 using typename __traits_base<_Tp>::type;
242 // precondition: at least one non-zero character available
243 static _LIBCPP_INLINE_VISIBILITY char const*
244 __read(char const* __p, char const* __ep, type& __a, type& __b)
246 type __cprod[digits];
247 int __j = digits - 1;
251 if (!('0' <= *__p && *__p <= '9'))
253 __cprod[--__i] = *__p++ - '0';
254 } while (__p != __ep && __i != 0);
256 __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1,
258 if (__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))
263 template <typename _It1, typename _It2, class _Up>
264 static _LIBCPP_INLINE_VISIBILITY _Up
265 __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init)
267 for (; __first1 < __last1; ++__first1, ++__first2)
268 __init = __init + *__first1 * *__first2;
273 } // namespace __itoa
275 template <typename _Tp>
276 inline _LIBCPP_INLINE_VISIBILITY _Tp
277 __complement(_Tp __x)
279 static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
280 return _Tp(~__x + 1);
283 template <typename _Tp>
284 inline _LIBCPP_INLINE_VISIBILITY auto
285 __to_unsigned(_Tp __x)
287 return static_cast<make_unsigned_t<_Tp>>(__x);
290 template <typename _Tp>
291 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
292 __to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
294 auto __x = __to_unsigned(__value);
295 if (__value < 0 && __first != __last)
298 __x = __complement(__x);
301 return __to_chars_itoa(__first, __last, __x, false_type());
304 template <typename _Tp>
305 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
306 __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type)
308 using __tx = __itoa::__traits<_Tp>;
309 auto __diff = __last - __first;
311 #if !defined(_LIBCPP_COMPILER_MSVC)
312 if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
313 return {__tx::__convert(__value, __first), {}};
315 return {__last, errc::value_too_large};
317 if (__tx::digits <= __diff)
318 return {__tx::__convert(__value, __first), {}};
321 char __buf[__tx::digits];
322 auto __p = __tx::__convert(__value, __buf);
323 auto __len = __p - __buf;
326 memcpy(__first, __buf, __len);
327 return {__first + __len, {}};
330 return {__last, errc::value_too_large};
335 template <typename _Tp>
336 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
337 __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
340 auto __x = __to_unsigned(__value);
341 if (__value < 0 && __first != __last)
344 __x = __complement(__x);
347 return __to_chars_integral(__first, __last, __x, __base, false_type());
350 template <typename _Tp>
351 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
352 __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
356 return __to_chars_itoa(__first, __last, __value, false_type());
359 while (__p != __first)
361 auto __c = __value % __base;
363 *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c];
368 auto __len = __last - __p;
369 if (__value != 0 || !__len)
370 return {__last, errc::value_too_large};
373 memmove(__first, __p, __len);
374 return {__first + __len, {}};
378 template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
379 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
380 to_chars(char* __first, char* __last, _Tp __value)
382 return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>());
385 template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
386 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
387 to_chars(char* __first, char* __last, _Tp __value, int __base)
389 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
390 return __to_chars_integral(__first, __last, __value, __base,
394 template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
395 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
396 __sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
398 using __tl = numeric_limits<_Tp>;
399 decltype(__to_unsigned(__value)) __x;
401 bool __neg = (__first != __last && *__first == '-');
402 auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);
405 case errc::invalid_argument:
406 return {__first, __r.ec};
407 case errc::result_out_of_range:
415 if (__x <= __complement(__to_unsigned(__tl::min())))
417 __x = __complement(__x);
418 memcpy(&__value, &__x, sizeof(__x));
424 if (__x <= (__tl::max)())
431 return {__r.ptr, errc::result_out_of_range};
434 template <typename _Tp>
435 inline _LIBCPP_INLINE_VISIBILITY bool
436 __in_pattern(_Tp __c)
438 return '0' <= __c && __c <= '9';
441 struct _LIBCPP_HIDDEN __in_pattern_result
446 explicit _LIBCPP_INLINE_VISIBILITY operator bool() const { return __ok; }
449 template <typename _Tp>
450 inline _LIBCPP_INLINE_VISIBILITY __in_pattern_result
451 __in_pattern(_Tp __c, int __base)
454 return {'0' <= __c && __c < '0' + __base, __c - '0'};
455 else if (__in_pattern(__c))
456 return {true, __c - '0'};
457 else if ('a' <= __c && __c < 'a' + __base - 10)
458 return {true, __c - 'a' + 10};
460 return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10};
463 template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
464 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
465 __subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f,
468 auto __find_non_zero = [](_It __first, _It __last) {
469 for (; __first != __last; ++__first)
475 auto __p = __find_non_zero(__first, __last);
476 if (__p == __last || !__in_pattern(*__p, __args...))
479 return {__first, errc::invalid_argument};
487 auto __r = __f(__p, __last, __value, __args...);
488 if (__r.ec == errc::result_out_of_range)
490 for (; __r.ptr != __last; ++__r.ptr)
492 if (!__in_pattern(*__r.ptr, __args...))
500 template <typename _Tp, enable_if_t<is_unsigned<_Tp>::value, int> = 0>
501 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
502 __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
504 using __tx = __itoa::__traits<_Tp>;
505 using __output_type = typename __tx::type;
507 return __subject_seq_combinator(
508 __first, __last, __value,
509 [](const char* __first, const char* __last,
510 _Tp& __value) -> from_chars_result {
511 __output_type __a, __b;
512 auto __p = __tx::__read(__first, __last, __a, __b);
513 if (__p == __last || !__in_pattern(*__p))
515 __output_type __m = (numeric_limits<_Tp>::max)();
516 if (__m >= __a && __m - __a >= __b)
522 return {__p, errc::result_out_of_range};
526 template <typename _Tp, enable_if_t<is_signed<_Tp>::value, int> = 0>
527 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
528 __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
530 using __t = decltype(__to_unsigned(__value));
531 return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
534 template <typename _Tp, enable_if_t<is_unsigned<_Tp>::value, int> = 0>
535 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
536 __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
540 return __from_chars_atoi(__first, __last, __value);
542 return __subject_seq_combinator(
543 __first, __last, __value,
544 [](const char* __p, const char* __last, _Tp& __value,
545 int __base) -> from_chars_result {
546 using __tl = numeric_limits<_Tp>;
547 auto __digits = __tl::digits / log2f(float(__base));
548 _Tp __a = __in_pattern(*__p++, __base).__val, __b = 0;
550 for (int __i = 1; __p != __last; ++__i, ++__p)
552 if (auto __c = __in_pattern(*__p, __base))
554 if (__i < __digits - 1)
555 __a = __a * __base + __c.__val;
558 if (!__itoa::__mul_overflowed(__a, __base, __a))
568 if (__p == __last || !__in_pattern(*__p, __base))
570 if ((__tl::max)() - __a >= __b)
576 return {__p, errc::result_out_of_range};
581 template <typename _Tp, enable_if_t<is_signed<_Tp>::value, int> = 0>
582 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
583 __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
586 using __t = decltype(__to_unsigned(__value));
587 return __sign_combinator(__first, __last, __value,
588 __from_chars_integral<__t>, __base);
591 template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
592 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
593 from_chars(const char* __first, const char* __last, _Tp& __value)
595 return __from_chars_atoi(__first, __last, __value);
598 template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
599 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
600 from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
602 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
603 return __from_chars_integral(__first, __last, __value, __base);
606 #endif // _LIBCPP_STD_VER > 11
608 _LIBCPP_END_NAMESPACE_STD
610 #endif // _LIBCPP_CHARCONV