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
91 #include <__undef_macros>
93 _LIBCPP_BEGIN_NAMESPACE_STD
96 _LIBCPP_FUNC_VIS char* __u64toa(uint64_t __value, char* __buffer);
97 _LIBCPP_FUNC_VIS char* __u32toa(uint32_t __value, char* __buffer);
100 #if _LIBCPP_STD_VER > 11
102 enum class _LIBCPP_ENUM_VIS chars_format
107 general = fixed | scientific
110 struct _LIBCPP_TYPE_VIS to_chars_result
116 struct _LIBCPP_TYPE_VIS from_chars_result
122 void to_chars(char*, char*, bool, int = 10) = delete;
123 void from_chars(const char*, const char*, bool, int = 10) = delete;
128 static constexpr uint64_t __pow10_64[] = {
138 UINT64_C(1000000000),
139 UINT64_C(10000000000),
140 UINT64_C(100000000000),
141 UINT64_C(1000000000000),
142 UINT64_C(10000000000000),
143 UINT64_C(100000000000000),
144 UINT64_C(1000000000000000),
145 UINT64_C(10000000000000000),
146 UINT64_C(100000000000000000),
147 UINT64_C(1000000000000000000),
148 UINT64_C(10000000000000000000),
151 static constexpr uint32_t __pow10_32[] = {
152 UINT32_C(0), UINT32_C(10), UINT32_C(100),
153 UINT32_C(1000), UINT32_C(10000), UINT32_C(100000),
154 UINT32_C(1000000), UINT32_C(10000000), UINT32_C(100000000),
155 UINT32_C(1000000000),
158 template <typename _Tp, typename = void>
159 struct _LIBCPP_HIDDEN __traits_base
161 using type = uint64_t;
163 #if !defined(_LIBCPP_COMPILER_MSVC)
164 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
166 auto __t = (64 - __builtin_clzll(__v | 1)) * 1233 >> 12;
167 return __t - (__v < __pow10_64[__t]) + 1;
171 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
173 return __u64toa(__v, __p);
176 static _LIBCPP_INLINE_VISIBILITY auto& __pow() { return __pow10_64; }
179 template <typename _Tp>
180 struct _LIBCPP_HIDDEN
181 __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))>
183 using type = uint32_t;
185 #if !defined(_LIBCPP_COMPILER_MSVC)
186 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
188 auto __t = (32 - __builtin_clz(__v | 1)) * 1233 >> 12;
189 return __t - (__v < __pow10_32[__t]) + 1;
193 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
195 return __u32toa(__v, __p);
198 static _LIBCPP_INLINE_VISIBILITY auto& __pow() { return __pow10_32; }
201 template <typename _Tp>
202 inline _LIBCPP_INLINE_VISIBILITY bool
203 __mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r)
205 auto __c = __a * __b;
207 return __c > (numeric_limits<unsigned char>::max)();
210 template <typename _Tp>
211 inline _LIBCPP_INLINE_VISIBILITY bool
212 __mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r)
214 auto __c = __a * __b;
216 return __c > (numeric_limits<unsigned short>::max)();
219 template <typename _Tp>
220 inline _LIBCPP_INLINE_VISIBILITY bool
221 __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r)
223 static_assert(is_unsigned<_Tp>::value, "");
224 #if !defined(_LIBCPP_COMPILER_MSVC)
225 return __builtin_mul_overflow(__a, __b, &__r);
227 bool __did = __b && ((numeric_limits<_Tp>::max)() / __b) < __a;
233 template <typename _Tp, typename _Up>
234 inline _LIBCPP_INLINE_VISIBILITY bool
235 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r)
237 return __mul_overflowed(__a, static_cast<_Tp>(__b), __r);
240 template <typename _Tp>
241 struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp>
243 static constexpr int digits = numeric_limits<_Tp>::digits10 + 1;
244 using __traits_base<_Tp>::__pow;
245 using typename __traits_base<_Tp>::type;
247 // precondition: at least one non-zero character available
248 static _LIBCPP_INLINE_VISIBILITY char const*
249 __read(char const* __p, char const* __ep, type& __a, type& __b)
251 type __cprod[digits];
252 int __j = digits - 1;
256 if (!('0' <= *__p && *__p <= '9'))
258 __cprod[--__i] = *__p++ - '0';
259 } while (__p != __ep && __i != 0);
261 __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1,
263 if (__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))
268 template <typename _It1, typename _It2, class _Up>
269 static _LIBCPP_INLINE_VISIBILITY _Up
270 __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init)
272 for (; __first1 < __last1; ++__first1, ++__first2)
273 __init = __init + *__first1 * *__first2;
278 } // namespace __itoa
280 template <typename _Tp>
281 inline _LIBCPP_INLINE_VISIBILITY _Tp
282 __complement(_Tp __x)
284 static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
285 return _Tp(~__x + 1);
288 template <typename _Tp>
289 inline _LIBCPP_INLINE_VISIBILITY auto
290 __to_unsigned(_Tp __x)
292 return static_cast<make_unsigned_t<_Tp>>(__x);
295 template <typename _Tp>
296 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
297 __to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
299 auto __x = __to_unsigned(__value);
300 if (__value < 0 && __first != __last)
303 __x = __complement(__x);
306 return __to_chars_itoa(__first, __last, __x, false_type());
309 template <typename _Tp>
310 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
311 __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type)
313 using __tx = __itoa::__traits<_Tp>;
314 auto __diff = __last - __first;
316 #if !defined(_LIBCPP_COMPILER_MSVC)
317 if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
318 return {__tx::__convert(__value, __first), {}};
320 return {__last, errc::value_too_large};
322 if (__tx::digits <= __diff)
323 return {__tx::__convert(__value, __first), {}};
326 char __buf[__tx::digits];
327 auto __p = __tx::__convert(__value, __buf);
328 auto __len = __p - __buf;
331 memcpy(__first, __buf, __len);
332 return {__first + __len, {}};
335 return {__last, errc::value_too_large};
340 template <typename _Tp>
341 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
342 __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
345 auto __x = __to_unsigned(__value);
346 if (__value < 0 && __first != __last)
349 __x = __complement(__x);
352 return __to_chars_integral(__first, __last, __x, __base, false_type());
355 template <typename _Tp>
356 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
357 __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
361 return __to_chars_itoa(__first, __last, __value, false_type());
364 while (__p != __first)
366 auto __c = __value % __base;
368 *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c];
373 auto __len = __last - __p;
374 if (__value != 0 || !__len)
375 return {__last, errc::value_too_large};
378 memmove(__first, __p, __len);
379 return {__first + __len, {}};
383 template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
384 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
385 to_chars(char* __first, char* __last, _Tp __value)
387 return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>());
390 template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
391 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
392 to_chars(char* __first, char* __last, _Tp __value, int __base)
394 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
395 return __to_chars_integral(__first, __last, __value, __base,
399 template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
400 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
401 __sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
403 using __tl = numeric_limits<_Tp>;
404 decltype(__to_unsigned(__value)) __x;
406 bool __neg = (__first != __last && *__first == '-');
407 auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);
410 case errc::invalid_argument:
411 return {__first, __r.ec};
412 case errc::result_out_of_range:
420 if (__x <= __complement(__to_unsigned(__tl::min())))
422 __x = __complement(__x);
423 memcpy(&__value, &__x, sizeof(__x));
429 if (__x <= (__tl::max)())
436 return {__r.ptr, errc::result_out_of_range};
439 template <typename _Tp>
440 inline _LIBCPP_INLINE_VISIBILITY bool
441 __in_pattern(_Tp __c)
443 return '0' <= __c && __c <= '9';
446 struct _LIBCPP_HIDDEN __in_pattern_result
451 explicit _LIBCPP_INLINE_VISIBILITY operator bool() const { return __ok; }
454 template <typename _Tp>
455 inline _LIBCPP_INLINE_VISIBILITY __in_pattern_result
456 __in_pattern(_Tp __c, int __base)
459 return {'0' <= __c && __c < '0' + __base, __c - '0'};
460 else if (__in_pattern(__c))
461 return {true, __c - '0'};
462 else if ('a' <= __c && __c < 'a' + __base - 10)
463 return {true, __c - 'a' + 10};
465 return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10};
468 template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
469 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
470 __subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f,
473 auto __find_non_zero = [](_It __first, _It __last) {
474 for (; __first != __last; ++__first)
480 auto __p = __find_non_zero(__first, __last);
481 if (__p == __last || !__in_pattern(*__p, __args...))
484 return {__first, errc::invalid_argument};
492 auto __r = __f(__p, __last, __value, __args...);
493 if (__r.ec == errc::result_out_of_range)
495 for (; __r.ptr != __last; ++__r.ptr)
497 if (!__in_pattern(*__r.ptr, __args...))
505 template <typename _Tp, enable_if_t<is_unsigned<_Tp>::value, int> = 0>
506 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
507 __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
509 using __tx = __itoa::__traits<_Tp>;
510 using __output_type = typename __tx::type;
512 return __subject_seq_combinator(
513 __first, __last, __value,
514 [](const char* __first, const char* __last,
515 _Tp& __value) -> from_chars_result {
516 __output_type __a, __b;
517 auto __p = __tx::__read(__first, __last, __a, __b);
518 if (__p == __last || !__in_pattern(*__p))
520 __output_type __m = (numeric_limits<_Tp>::max)();
521 if (__m >= __a && __m - __a >= __b)
527 return {__p, errc::result_out_of_range};
531 template <typename _Tp, enable_if_t<is_signed<_Tp>::value, int> = 0>
532 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
533 __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
535 using __t = decltype(__to_unsigned(__value));
536 return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
539 template <typename _Tp, enable_if_t<is_unsigned<_Tp>::value, int> = 0>
540 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
541 __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
545 return __from_chars_atoi(__first, __last, __value);
547 return __subject_seq_combinator(
548 __first, __last, __value,
549 [](const char* __p, const char* __last, _Tp& __value,
550 int __base) -> from_chars_result {
551 using __tl = numeric_limits<_Tp>;
552 auto __digits = __tl::digits / log2f(float(__base));
553 _Tp __a = __in_pattern(*__p++, __base).__val, __b = 0;
555 for (int __i = 1; __p != __last; ++__i, ++__p)
557 if (auto __c = __in_pattern(*__p, __base))
559 if (__i < __digits - 1)
560 __a = __a * __base + __c.__val;
563 if (!__itoa::__mul_overflowed(__a, __base, __a))
573 if (__p == __last || !__in_pattern(*__p, __base))
575 if ((__tl::max)() - __a >= __b)
581 return {__p, errc::result_out_of_range};
586 template <typename _Tp, enable_if_t<is_signed<_Tp>::value, int> = 0>
587 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
588 __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
591 using __t = decltype(__to_unsigned(__value));
592 return __sign_combinator(__first, __last, __value,
593 __from_chars_integral<__t>, __base);
596 template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
597 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
598 from_chars(const char* __first, const char* __last, _Tp& __value)
600 return __from_chars_atoi(__first, __last, __value);
603 template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
604 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
605 from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
607 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
608 return __from_chars_integral(__first, __last, __value, __base);
611 #endif // _LIBCPP_STD_VER > 11
613 _LIBCPP_END_NAMESPACE_STD
617 #endif // _LIBCPP_CHARCONV