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);
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_AVAILABILITY_TO_CHARS _LIBCPP_FUNC_VIS char* __u64toa(uint64_t __value, char* __buffer) _NOEXCEPT;
97 _LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_FUNC_VIS char* __u32toa(uint32_t __value, char* __buffer) _NOEXCEPT;
100 #ifndef _LIBCPP_CXX03_LANG
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 _LIBCPP_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 _LIBCPP_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 _LIBCPP_AVAILABILITY_TO_CHARS
172 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
174 return __u64toa(__v, __p);
177 static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_64)& __pow() { return __pow10_64; }
180 template <typename _Tp>
181 struct _LIBCPP_HIDDEN
182 __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))>
184 using type = uint32_t;
186 #if !defined(_LIBCPP_COMPILER_MSVC)
187 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
189 auto __t = (32 - __builtin_clz(__v | 1)) * 1233 >> 12;
190 return __t - (__v < __pow10_32[__t]) + 1;
194 _LIBCPP_AVAILABILITY_TO_CHARS
195 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
197 return __u32toa(__v, __p);
200 static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_32)& __pow() { return __pow10_32; }
203 template <typename _Tp>
204 inline _LIBCPP_INLINE_VISIBILITY bool
205 __mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r)
207 auto __c = __a * __b;
209 return __c > (numeric_limits<unsigned char>::max)();
212 template <typename _Tp>
213 inline _LIBCPP_INLINE_VISIBILITY bool
214 __mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r)
216 auto __c = __a * __b;
218 return __c > (numeric_limits<unsigned short>::max)();
221 template <typename _Tp>
222 inline _LIBCPP_INLINE_VISIBILITY bool
223 __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r)
225 static_assert(is_unsigned<_Tp>::value, "");
226 #if !defined(_LIBCPP_COMPILER_MSVC)
227 return __builtin_mul_overflow(__a, __b, &__r);
229 bool __did = __b && ((numeric_limits<_Tp>::max)() / __b) < __a;
235 template <typename _Tp, typename _Up>
236 inline _LIBCPP_INLINE_VISIBILITY bool
237 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r)
239 return __mul_overflowed(__a, static_cast<_Tp>(__b), __r);
242 template <typename _Tp>
243 struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp>
245 static _LIBCPP_CONSTEXPR int digits = numeric_limits<_Tp>::digits10 + 1;
246 using __traits_base<_Tp>::__pow;
247 using typename __traits_base<_Tp>::type;
249 // precondition: at least one non-zero character available
250 static _LIBCPP_INLINE_VISIBILITY char const*
251 __read(char const* __p, char const* __ep, type& __a, type& __b)
253 type __cprod[digits];
254 int __j = digits - 1;
258 if (!('0' <= *__p && *__p <= '9'))
260 __cprod[--__i] = *__p++ - '0';
261 } while (__p != __ep && __i != 0);
263 __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1,
265 if (__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))
270 template <typename _It1, typename _It2, class _Up>
271 static _LIBCPP_INLINE_VISIBILITY _Up
272 __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init)
274 for (; __first1 < __last1; ++__first1, ++__first2)
275 __init = __init + *__first1 * *__first2;
280 } // namespace __itoa
282 template <typename _Tp>
283 inline _LIBCPP_INLINE_VISIBILITY _Tp
284 __complement(_Tp __x)
286 static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
287 return _Tp(~__x + 1);
290 template <typename _Tp>
291 inline _LIBCPP_INLINE_VISIBILITY typename make_unsigned<_Tp>::type
292 __to_unsigned(_Tp __x)
294 return static_cast<typename make_unsigned<_Tp>::type>(__x);
297 template <typename _Tp>
298 _LIBCPP_AVAILABILITY_TO_CHARS
299 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
300 __to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
302 auto __x = __to_unsigned(__value);
303 if (__value < 0 && __first != __last)
306 __x = __complement(__x);
309 return __to_chars_itoa(__first, __last, __x, false_type());
312 template <typename _Tp>
313 _LIBCPP_AVAILABILITY_TO_CHARS
314 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
315 __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type)
317 using __tx = __itoa::__traits<_Tp>;
318 auto __diff = __last - __first;
320 #if !defined(_LIBCPP_COMPILER_MSVC)
321 if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
322 return {__tx::__convert(__value, __first), errc(0)};
324 return {__last, errc::value_too_large};
326 if (__tx::digits <= __diff)
327 return {__tx::__convert(__value, __first), {}};
330 char __buf[__tx::digits];
331 auto __p = __tx::__convert(__value, __buf);
332 auto __len = __p - __buf;
335 memcpy(__first, __buf, __len);
336 return {__first + __len, {}};
339 return {__last, errc::value_too_large};
344 template <typename _Tp>
345 _LIBCPP_AVAILABILITY_TO_CHARS
346 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
347 __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
350 auto __x = __to_unsigned(__value);
351 if (__value < 0 && __first != __last)
354 __x = __complement(__x);
357 return __to_chars_integral(__first, __last, __x, __base, false_type());
360 template <typename _Tp>
361 _LIBCPP_AVAILABILITY_TO_CHARS
362 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
363 __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
367 return __to_chars_itoa(__first, __last, __value, false_type());
370 while (__p != __first)
372 auto __c = __value % __base;
374 *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c];
379 auto __len = __last - __p;
380 if (__value != 0 || !__len)
381 return {__last, errc::value_too_large};
384 memmove(__first, __p, __len);
385 return {__first + __len, {}};
389 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
390 _LIBCPP_AVAILABILITY_TO_CHARS
391 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
392 to_chars(char* __first, char* __last, _Tp __value)
394 return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>());
397 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
398 _LIBCPP_AVAILABILITY_TO_CHARS
399 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
400 to_chars(char* __first, char* __last, _Tp __value, int __base)
402 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
403 return __to_chars_integral(__first, __last, __value, __base,
407 template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
408 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
409 __sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
411 using __tl = numeric_limits<_Tp>;
412 decltype(__to_unsigned(__value)) __x;
414 bool __neg = (__first != __last && *__first == '-');
415 auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);
418 case errc::invalid_argument:
419 return {__first, __r.ec};
420 case errc::result_out_of_range:
428 if (__x <= __complement(__to_unsigned(__tl::min())))
430 __x = __complement(__x);
431 memcpy(&__value, &__x, sizeof(__x));
437 if (__x <= (__tl::max)())
444 return {__r.ptr, errc::result_out_of_range};
447 template <typename _Tp>
448 inline _LIBCPP_INLINE_VISIBILITY bool
449 __in_pattern(_Tp __c)
451 return '0' <= __c && __c <= '9';
454 struct _LIBCPP_HIDDEN __in_pattern_result
459 explicit _LIBCPP_INLINE_VISIBILITY operator bool() const { return __ok; }
462 template <typename _Tp>
463 inline _LIBCPP_INLINE_VISIBILITY __in_pattern_result
464 __in_pattern(_Tp __c, int __base)
467 return {'0' <= __c && __c < '0' + __base, __c - '0'};
468 else if (__in_pattern(__c))
469 return {true, __c - '0'};
470 else if ('a' <= __c && __c < 'a' + __base - 10)
471 return {true, __c - 'a' + 10};
473 return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10};
476 template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
477 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
478 __subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f,
481 auto __find_non_zero = [](_It __first, _It __last) {
482 for (; __first != __last; ++__first)
488 auto __p = __find_non_zero(__first, __last);
489 if (__p == __last || !__in_pattern(*__p, __args...))
492 return {__first, errc::invalid_argument};
500 auto __r = __f(__p, __last, __value, __args...);
501 if (__r.ec == errc::result_out_of_range)
503 for (; __r.ptr != __last; ++__r.ptr)
505 if (!__in_pattern(*__r.ptr, __args...))
513 template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
514 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
515 __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
517 using __tx = __itoa::__traits<_Tp>;
518 using __output_type = typename __tx::type;
520 return __subject_seq_combinator(
521 __first, __last, __value,
522 [](const char* __first, const char* __last,
523 _Tp& __value) -> from_chars_result {
524 __output_type __a, __b;
525 auto __p = __tx::__read(__first, __last, __a, __b);
526 if (__p == __last || !__in_pattern(*__p))
528 __output_type __m = (numeric_limits<_Tp>::max)();
529 if (__m >= __a && __m - __a >= __b)
535 return {__p, errc::result_out_of_range};
539 template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
540 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
541 __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
543 using __t = decltype(__to_unsigned(__value));
544 return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
547 template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
548 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
549 __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
553 return __from_chars_atoi(__first, __last, __value);
555 return __subject_seq_combinator(
556 __first, __last, __value,
557 [](const char* __p, const char* __last, _Tp& __value,
558 int __base) -> from_chars_result {
559 using __tl = numeric_limits<_Tp>;
560 auto __digits = __tl::digits / log2f(float(__base));
561 _Tp __a = __in_pattern(*__p++, __base).__val, __b = 0;
563 for (int __i = 1; __p != __last; ++__i, ++__p)
565 if (auto __c = __in_pattern(*__p, __base))
567 if (__i < __digits - 1)
568 __a = __a * __base + __c.__val;
571 if (!__itoa::__mul_overflowed(__a, __base, __a))
581 if (__p == __last || !__in_pattern(*__p, __base))
583 if ((__tl::max)() - __a >= __b)
589 return {__p, errc::result_out_of_range};
594 template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
595 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
596 __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
599 using __t = decltype(__to_unsigned(__value));
600 return __sign_combinator(__first, __last, __value,
601 __from_chars_integral<__t>, __base);
604 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
605 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
606 from_chars(const char* __first, const char* __last, _Tp& __value)
608 return __from_chars_atoi(__first, __last, __value);
611 template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
612 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
613 from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
615 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
616 return __from_chars_integral(__first, __last, __value, __base);
619 #endif // _LIBCPP_CXX03_LANG
621 _LIBCPP_END_NAMESPACE_STD
625 #endif // _LIBCPP_CHARCONV