Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
Louis Dionne | 9bd9388 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 2 | //===----------------------------------------------------------------------===// |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_CHARCONV |
| 11 | #define _LIBCPP_CHARCONV |
| 12 | |
| 13 | /* |
| 14 | charconv synopsis |
| 15 | |
| 16 | namespace std { |
| 17 | |
| 18 | // floating-point format for primitive numerical conversion |
| 19 | enum class chars_format { |
| 20 | scientific = unspecified, |
| 21 | fixed = unspecified, |
| 22 | hex = unspecified, |
| 23 | general = fixed | scientific |
| 24 | }; |
| 25 | |
| 26 | // 23.20.2, primitive numerical output conversion |
| 27 | struct to_chars_result { |
| 28 | char* ptr; |
| 29 | errc ec; |
Mark de Wever | 8b9aad9 | 2021-10-23 18:28:31 +0200 | [diff] [blame] | 30 | friend bool operator==(const to_chars_result&, const to_chars_result&) = default; // since C++20 |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | to_chars_result to_chars(char* first, char* last, see below value, |
| 34 | int base = 10); |
Konstantin Varlamov | 1071d35 | 2021-10-15 17:40:42 +0200 | [diff] [blame] | 35 | to_chars_result to_chars(char* first, char* last, bool value, |
| 36 | int base = 10) = delete; |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 37 | |
| 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); |
| 41 | |
| 42 | to_chars_result to_chars(char* first, char* last, float value, |
| 43 | chars_format fmt); |
| 44 | to_chars_result to_chars(char* first, char* last, double value, |
| 45 | chars_format fmt); |
| 46 | to_chars_result to_chars(char* first, char* last, long double value, |
| 47 | chars_format fmt); |
| 48 | |
| 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); |
| 55 | |
| 56 | // 23.20.3, primitive numerical input conversion |
| 57 | struct from_chars_result { |
| 58 | const char* ptr; |
| 59 | errc ec; |
Mark de Wever | 8b9aad9 | 2021-10-23 18:28:31 +0200 | [diff] [blame] | 60 | friend bool operator==(const from_chars_result&, const from_chars_result&) = default; // since C++20 |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | from_chars_result from_chars(const char* first, const char* last, |
| 64 | see below& value, int base = 10); |
| 65 | |
| 66 | from_chars_result from_chars(const char* first, const char* last, |
| 67 | float& value, |
| 68 | chars_format fmt = chars_format::general); |
| 69 | from_chars_result from_chars(const char* first, const char* last, |
| 70 | double& value, |
| 71 | chars_format fmt = chars_format::general); |
| 72 | from_chars_result from_chars(const char* first, const char* last, |
| 73 | long double& value, |
| 74 | chars_format fmt = chars_format::general); |
| 75 | |
| 76 | } // namespace std |
| 77 | |
| 78 | */ |
| 79 | |
Louis Dionne | 73912b2 | 2020-11-04 15:01:25 -0500 | [diff] [blame] | 80 | #include <__availability> |
Mark de Wever | de2667c | 2021-08-08 11:02:07 +0200 | [diff] [blame] | 81 | #include <__bits> |
Mark de Wever | fd2375f | 2021-08-30 19:47:55 +0200 | [diff] [blame] | 82 | #include <__charconv/chars_format.h> |
| 83 | #include <__charconv/from_chars_result.h> |
| 84 | #include <__charconv/to_chars_result.h> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 85 | #include <__config> |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 86 | #include <__errc> |
Arthur O'Dwyer | 79a4afc | 2020-12-07 21:45:29 -0500 | [diff] [blame] | 87 | #include <cmath> // for log2f |
| 88 | #include <cstdint> |
Mark de Wever | 3ae9119 | 2021-02-27 16:52:39 +0100 | [diff] [blame] | 89 | #include <cstdlib> // for _LIBCPP_UNREACHABLE |
Arthur O'Dwyer | 79a4afc | 2020-12-07 21:45:29 -0500 | [diff] [blame] | 90 | #include <cstring> |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 91 | #include <limits> |
Arthur O'Dwyer | 79a4afc | 2020-12-07 21:45:29 -0500 | [diff] [blame] | 92 | #include <type_traits> |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 93 | |
| 94 | #include <__debug> |
| 95 | |
| 96 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 97 | #pragma GCC system_header |
| 98 | #endif |
| 99 | |
Marshall Clow | 5598be7 | 2018-10-26 01:00:56 +0000 | [diff] [blame] | 100 | _LIBCPP_PUSH_MACROS |
| 101 | #include <__undef_macros> |
| 102 | |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 103 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 104 | |
Louis Dionne | 5254b37 | 2018-10-25 12:13:43 +0000 | [diff] [blame] | 105 | namespace __itoa { |
Louis Dionne | 4c7d4f1 | 2020-05-21 10:25:15 -0400 | [diff] [blame] | 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; |
Nikolas Klauser | d26407a | 2021-12-02 14:12:51 +0100 | [diff] [blame] | 108 | } // namespace __itoa |
Louis Dionne | 5254b37 | 2018-10-25 12:13:43 +0000 | [diff] [blame] | 109 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 110 | #ifndef _LIBCPP_CXX03_LANG |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 111 | |
Mark de Wever | 10abe8a | 2021-11-16 16:04:29 +0100 | [diff] [blame] | 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; |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 114 | |
| 115 | namespace __itoa |
| 116 | { |
| 117 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 118 | static _LIBCPP_CONSTEXPR uint64_t __pow10_64[] = { |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 119 | UINT64_C(0), |
| 120 | UINT64_C(10), |
| 121 | UINT64_C(100), |
| 122 | UINT64_C(1000), |
| 123 | UINT64_C(10000), |
| 124 | UINT64_C(100000), |
| 125 | UINT64_C(1000000), |
| 126 | UINT64_C(10000000), |
| 127 | UINT64_C(100000000), |
| 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), |
| 139 | }; |
| 140 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 141 | static _LIBCPP_CONSTEXPR uint32_t __pow10_32[] = { |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 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), |
| 146 | }; |
| 147 | |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 148 | template <typename _Tp, typename = void> |
| 149 | struct _LIBCPP_HIDDEN __traits_base |
| 150 | { |
| 151 | using type = uint64_t; |
| 152 | |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 153 | static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v) |
| 154 | { |
Mark de Wever | de2667c | 2021-08-08 11:02:07 +0200 | [diff] [blame] | 155 | auto __t = (64 - _VSTD::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12; |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 156 | return __t - (__v < __pow10_64[__t]) + 1; |
| 157 | } |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 158 | |
Louis Dionne | c78d735 | 2020-02-14 15:19:47 +0100 | [diff] [blame] | 159 | _LIBCPP_AVAILABILITY_TO_CHARS |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 160 | static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p) |
| 161 | { |
| 162 | return __u64toa(__v, __p); |
| 163 | } |
| 164 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 165 | static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_64)& __pow() { return __pow10_64; } |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | template <typename _Tp> |
| 169 | struct _LIBCPP_HIDDEN |
| 170 | __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))> |
| 171 | { |
| 172 | using type = uint32_t; |
| 173 | |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 174 | static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v) |
| 175 | { |
Mark de Wever | de2667c | 2021-08-08 11:02:07 +0200 | [diff] [blame] | 176 | auto __t = (32 - _VSTD::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12; |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 177 | return __t - (__v < __pow10_32[__t]) + 1; |
| 178 | } |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 179 | |
Louis Dionne | c78d735 | 2020-02-14 15:19:47 +0100 | [diff] [blame] | 180 | _LIBCPP_AVAILABILITY_TO_CHARS |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 181 | static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p) |
| 182 | { |
| 183 | return __u32toa(__v, __p); |
| 184 | } |
| 185 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 186 | static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_32)& __pow() { return __pow10_32; } |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | template <typename _Tp> |
| 190 | inline _LIBCPP_INLINE_VISIBILITY bool |
| 191 | __mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r) |
| 192 | { |
| 193 | auto __c = __a * __b; |
| 194 | __r = __c; |
Arthur O'Dwyer | 518c184 | 2020-11-27 14:13:05 -0500 | [diff] [blame] | 195 | return __c > numeric_limits<unsigned char>::max(); |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | template <typename _Tp> |
| 199 | inline _LIBCPP_INLINE_VISIBILITY bool |
| 200 | __mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r) |
| 201 | { |
| 202 | auto __c = __a * __b; |
| 203 | __r = __c; |
Arthur O'Dwyer | 518c184 | 2020-11-27 14:13:05 -0500 | [diff] [blame] | 204 | return __c > numeric_limits<unsigned short>::max(); |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | template <typename _Tp> |
| 208 | inline _LIBCPP_INLINE_VISIBILITY bool |
| 209 | __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r) |
| 210 | { |
| 211 | static_assert(is_unsigned<_Tp>::value, ""); |
| 212 | #if !defined(_LIBCPP_COMPILER_MSVC) |
| 213 | return __builtin_mul_overflow(__a, __b, &__r); |
| 214 | #else |
Arthur O'Dwyer | 518c184 | 2020-11-27 14:13:05 -0500 | [diff] [blame] | 215 | bool __did = __b && (numeric_limits<_Tp>::max() / __b) < __a; |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 216 | __r = __a * __b; |
| 217 | return __did; |
| 218 | #endif |
| 219 | } |
| 220 | |
| 221 | template <typename _Tp, typename _Up> |
| 222 | inline _LIBCPP_INLINE_VISIBILITY bool |
| 223 | __mul_overflowed(_Tp __a, _Up __b, _Tp& __r) |
| 224 | { |
| 225 | return __mul_overflowed(__a, static_cast<_Tp>(__b), __r); |
| 226 | } |
| 227 | |
| 228 | template <typename _Tp> |
| 229 | struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp> |
| 230 | { |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 231 | static _LIBCPP_CONSTEXPR int digits = numeric_limits<_Tp>::digits10 + 1; |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 232 | using __traits_base<_Tp>::__pow; |
| 233 | using typename __traits_base<_Tp>::type; |
| 234 | |
| 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) |
| 238 | { |
| 239 | type __cprod[digits]; |
| 240 | int __j = digits - 1; |
| 241 | int __i = digits; |
| 242 | do |
| 243 | { |
| 244 | if (!('0' <= *__p && *__p <= '9')) |
| 245 | break; |
| 246 | __cprod[--__i] = *__p++ - '0'; |
| 247 | } while (__p != __ep && __i != 0); |
| 248 | |
| 249 | __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1, |
| 250 | __cprod[__i]); |
| 251 | if (__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b)) |
| 252 | --__p; |
| 253 | return __p; |
| 254 | } |
| 255 | |
| 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) |
| 259 | { |
| 260 | for (; __first1 < __last1; ++__first1, ++__first2) |
| 261 | __init = __init + *__first1 * *__first2; |
| 262 | return __init; |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | } // namespace __itoa |
| 267 | |
| 268 | template <typename _Tp> |
| 269 | inline _LIBCPP_INLINE_VISIBILITY _Tp |
| 270 | __complement(_Tp __x) |
| 271 | { |
| 272 | static_assert(is_unsigned<_Tp>::value, "cast to unsigned first"); |
| 273 | return _Tp(~__x + 1); |
| 274 | } |
| 275 | |
| 276 | template <typename _Tp> |
Louis Dionne | c78d735 | 2020-02-14 15:19:47 +0100 | [diff] [blame] | 277 | _LIBCPP_AVAILABILITY_TO_CHARS |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 278 | inline _LIBCPP_INLINE_VISIBILITY to_chars_result |
| 279 | __to_chars_itoa(char* __first, char* __last, _Tp __value, true_type) |
| 280 | { |
Mark de Wever | 5f63976 | 2021-05-12 17:46:24 +0200 | [diff] [blame] | 281 | auto __x = __to_unsigned_like(__value); |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 282 | if (__value < 0 && __first != __last) |
| 283 | { |
| 284 | *__first++ = '-'; |
| 285 | __x = __complement(__x); |
| 286 | } |
| 287 | |
| 288 | return __to_chars_itoa(__first, __last, __x, false_type()); |
| 289 | } |
| 290 | |
| 291 | template <typename _Tp> |
Louis Dionne | c78d735 | 2020-02-14 15:19:47 +0100 | [diff] [blame] | 292 | _LIBCPP_AVAILABILITY_TO_CHARS |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 293 | inline _LIBCPP_INLINE_VISIBILITY to_chars_result |
| 294 | __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type) |
| 295 | { |
| 296 | using __tx = __itoa::__traits<_Tp>; |
| 297 | auto __diff = __last - __first; |
| 298 | |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 299 | if (__tx::digits <= __diff || __tx::__width(__value) <= __diff) |
Thomas Anderson | fe73332 | 2019-06-13 22:27:24 +0000 | [diff] [blame] | 300 | return {__tx::__convert(__value, __first), errc(0)}; |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 301 | else |
| 302 | return {__last, errc::value_too_large}; |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | template <typename _Tp> |
Louis Dionne | c78d735 | 2020-02-14 15:19:47 +0100 | [diff] [blame] | 306 | _LIBCPP_AVAILABILITY_TO_CHARS |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 307 | inline _LIBCPP_INLINE_VISIBILITY to_chars_result |
| 308 | __to_chars_integral(char* __first, char* __last, _Tp __value, int __base, |
| 309 | true_type) |
| 310 | { |
Mark de Wever | 5f63976 | 2021-05-12 17:46:24 +0200 | [diff] [blame] | 311 | auto __x = __to_unsigned_like(__value); |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 312 | if (__value < 0 && __first != __last) |
| 313 | { |
| 314 | *__first++ = '-'; |
| 315 | __x = __complement(__x); |
| 316 | } |
| 317 | |
| 318 | return __to_chars_integral(__first, __last, __x, __base, false_type()); |
| 319 | } |
| 320 | |
| 321 | template <typename _Tp> |
Mark de Wever | 3ae9119 | 2021-02-27 16:52:39 +0100 | [diff] [blame] | 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."); |
| 324 | |
| 325 | unsigned __base_2 = __base * __base; |
| 326 | unsigned __base_3 = __base_2 * __base; |
| 327 | unsigned __base_4 = __base_2 * __base_2; |
| 328 | |
| 329 | int __r = 0; |
| 330 | while (true) { |
| 331 | if (__value < __base) |
| 332 | return __r + 1; |
| 333 | if (__value < __base_2) |
| 334 | return __r + 2; |
| 335 | if (__value < __base_3) |
| 336 | return __r + 3; |
| 337 | if (__value < __base_4) |
| 338 | return __r + 4; |
| 339 | |
| 340 | __value /= __base_4; |
| 341 | __r += 4; |
| 342 | } |
| 343 | |
| 344 | _LIBCPP_UNREACHABLE(); |
| 345 | } |
| 346 | |
| 347 | template <typename _Tp> |
Louis Dionne | c78d735 | 2020-02-14 15:19:47 +0100 | [diff] [blame] | 348 | _LIBCPP_AVAILABILITY_TO_CHARS |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 349 | inline _LIBCPP_INLINE_VISIBILITY to_chars_result |
| 350 | __to_chars_integral(char* __first, char* __last, _Tp __value, int __base, |
| 351 | false_type) |
| 352 | { |
Mark de Wever | 3ae9119 | 2021-02-27 16:52:39 +0100 | [diff] [blame] | 353 | if (__base == 10) |
| 354 | return __to_chars_itoa(__first, __last, __value, false_type()); |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 355 | |
Mark de Wever | 3ae9119 | 2021-02-27 16:52:39 +0100 | [diff] [blame] | 356 | ptrdiff_t __cap = __last - __first; |
| 357 | int __n = __to_chars_integral_width(__value, __base); |
| 358 | if (__n > __cap) |
| 359 | return {__last, errc::value_too_large}; |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 360 | |
Mark de Wever | 3ae9119 | 2021-02-27 16:52:39 +0100 | [diff] [blame] | 361 | __last = __first + __n; |
| 362 | char* __p = __last; |
| 363 | do { |
| 364 | unsigned __c = __value % __base; |
| 365 | __value /= __base; |
| 366 | *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c]; |
| 367 | } while (__value != 0); |
| 368 | return {__last, errc(0)}; |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 371 | template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> |
Louis Dionne | c78d735 | 2020-02-14 15:19:47 +0100 | [diff] [blame] | 372 | _LIBCPP_AVAILABILITY_TO_CHARS |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 373 | inline _LIBCPP_INLINE_VISIBILITY to_chars_result |
| 374 | to_chars(char* __first, char* __last, _Tp __value) |
| 375 | { |
| 376 | return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>()); |
| 377 | } |
| 378 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 379 | template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> |
Louis Dionne | c78d735 | 2020-02-14 15:19:47 +0100 | [diff] [blame] | 380 | _LIBCPP_AVAILABILITY_TO_CHARS |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 381 | inline _LIBCPP_INLINE_VISIBILITY to_chars_result |
| 382 | to_chars(char* __first, char* __last, _Tp __value, int __base) |
| 383 | { |
| 384 | _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); |
| 385 | return __to_chars_integral(__first, __last, __value, __base, |
| 386 | is_signed<_Tp>()); |
| 387 | } |
| 388 | |
| 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) |
| 392 | { |
| 393 | using __tl = numeric_limits<_Tp>; |
Mark de Wever | 5f63976 | 2021-05-12 17:46:24 +0200 | [diff] [blame] | 394 | decltype(__to_unsigned_like(__value)) __x; |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 395 | |
| 396 | bool __neg = (__first != __last && *__first == '-'); |
| 397 | auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...); |
| 398 | switch (__r.ec) |
| 399 | { |
| 400 | case errc::invalid_argument: |
| 401 | return {__first, __r.ec}; |
| 402 | case errc::result_out_of_range: |
| 403 | return __r; |
| 404 | default: |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | if (__neg) |
| 409 | { |
Mark de Wever | 5f63976 | 2021-05-12 17:46:24 +0200 | [diff] [blame] | 410 | if (__x <= __complement(__to_unsigned_like(__tl::min()))) |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 411 | { |
| 412 | __x = __complement(__x); |
Arthur O'Dwyer | 79a4afc | 2020-12-07 21:45:29 -0500 | [diff] [blame] | 413 | _VSTD::memcpy(&__value, &__x, sizeof(__x)); |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 414 | return __r; |
| 415 | } |
| 416 | } |
| 417 | else |
| 418 | { |
Arthur O'Dwyer | 518c184 | 2020-11-27 14:13:05 -0500 | [diff] [blame] | 419 | if (__x <= __tl::max()) |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 420 | { |
| 421 | __value = __x; |
| 422 | return __r; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return {__r.ptr, errc::result_out_of_range}; |
| 427 | } |
| 428 | |
| 429 | template <typename _Tp> |
| 430 | inline _LIBCPP_INLINE_VISIBILITY bool |
| 431 | __in_pattern(_Tp __c) |
| 432 | { |
| 433 | return '0' <= __c && __c <= '9'; |
| 434 | } |
| 435 | |
| 436 | struct _LIBCPP_HIDDEN __in_pattern_result |
| 437 | { |
| 438 | bool __ok; |
| 439 | int __val; |
| 440 | |
| 441 | explicit _LIBCPP_INLINE_VISIBILITY operator bool() const { return __ok; } |
| 442 | }; |
| 443 | |
| 444 | template <typename _Tp> |
| 445 | inline _LIBCPP_INLINE_VISIBILITY __in_pattern_result |
| 446 | __in_pattern(_Tp __c, int __base) |
| 447 | { |
| 448 | if (__base <= 10) |
| 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}; |
| 454 | else |
| 455 | return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10}; |
| 456 | } |
| 457 | |
| 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, |
| 461 | _Ts... __args) |
| 462 | { |
| 463 | auto __find_non_zero = [](_It __first, _It __last) { |
| 464 | for (; __first != __last; ++__first) |
| 465 | if (*__first != '0') |
| 466 | break; |
| 467 | return __first; |
| 468 | }; |
| 469 | |
| 470 | auto __p = __find_non_zero(__first, __last); |
| 471 | if (__p == __last || !__in_pattern(*__p, __args...)) |
| 472 | { |
| 473 | if (__p == __first) |
| 474 | return {__first, errc::invalid_argument}; |
| 475 | else |
| 476 | { |
| 477 | __value = 0; |
| 478 | return {__p, {}}; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | auto __r = __f(__p, __last, __value, __args...); |
| 483 | if (__r.ec == errc::result_out_of_range) |
| 484 | { |
| 485 | for (; __r.ptr != __last; ++__r.ptr) |
| 486 | { |
| 487 | if (!__in_pattern(*__r.ptr, __args...)) |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | return __r; |
| 493 | } |
| 494 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 495 | template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0> |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 496 | inline _LIBCPP_INLINE_VISIBILITY from_chars_result |
| 497 | __from_chars_atoi(const char* __first, const char* __last, _Tp& __value) |
| 498 | { |
| 499 | using __tx = __itoa::__traits<_Tp>; |
| 500 | using __output_type = typename __tx::type; |
| 501 | |
| 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)) |
| 509 | { |
Arthur O'Dwyer | 518c184 | 2020-11-27 14:13:05 -0500 | [diff] [blame] | 510 | __output_type __m = numeric_limits<_Tp>::max(); |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 511 | if (__m >= __a && __m - __a >= __b) |
| 512 | { |
| 513 | __value = __a + __b; |
| 514 | return {__p, {}}; |
| 515 | } |
| 516 | } |
| 517 | return {__p, errc::result_out_of_range}; |
| 518 | }); |
| 519 | } |
| 520 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 521 | template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0> |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 522 | inline _LIBCPP_INLINE_VISIBILITY from_chars_result |
| 523 | __from_chars_atoi(const char* __first, const char* __last, _Tp& __value) |
| 524 | { |
Mark de Wever | 5f63976 | 2021-05-12 17:46:24 +0200 | [diff] [blame] | 525 | using __t = decltype(__to_unsigned_like(__value)); |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 526 | return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>); |
| 527 | } |
| 528 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 529 | template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0> |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 530 | inline _LIBCPP_INLINE_VISIBILITY from_chars_result |
| 531 | __from_chars_integral(const char* __first, const char* __last, _Tp& __value, |
| 532 | int __base) |
| 533 | { |
| 534 | if (__base == 10) |
| 535 | return __from_chars_atoi(__first, __last, __value); |
| 536 | |
| 537 | return __subject_seq_combinator( |
| 538 | __first, __last, __value, |
Louis Dionne | af6be62 | 2021-07-27 17:30:47 -0400 | [diff] [blame] | 539 | [](const char* __p, const char* __lastx, _Tp& __value, |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 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; |
| 544 | |
Louis Dionne | af6be62 | 2021-07-27 17:30:47 -0400 | [diff] [blame] | 545 | for (int __i = 1; __p != __lastx; ++__i, ++__p) |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 546 | { |
| 547 | if (auto __c = __in_pattern(*__p, __base)) |
| 548 | { |
| 549 | if (__i < __digits - 1) |
| 550 | __a = __a * __base + __c.__val; |
| 551 | else |
| 552 | { |
| 553 | if (!__itoa::__mul_overflowed(__a, __base, __a)) |
| 554 | ++__p; |
| 555 | __b = __c.__val; |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | else |
| 560 | break; |
| 561 | } |
| 562 | |
Louis Dionne | af6be62 | 2021-07-27 17:30:47 -0400 | [diff] [blame] | 563 | if (__p == __lastx || !__in_pattern(*__p, __base)) |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 564 | { |
Arthur O'Dwyer | 518c184 | 2020-11-27 14:13:05 -0500 | [diff] [blame] | 565 | if (__tl::max() - __a >= __b) |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 566 | { |
| 567 | __value = __a + __b; |
| 568 | return {__p, {}}; |
| 569 | } |
| 570 | } |
| 571 | return {__p, errc::result_out_of_range}; |
| 572 | }, |
| 573 | __base); |
| 574 | } |
| 575 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 576 | template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0> |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 577 | inline _LIBCPP_INLINE_VISIBILITY from_chars_result |
| 578 | __from_chars_integral(const char* __first, const char* __last, _Tp& __value, |
| 579 | int __base) |
| 580 | { |
Mark de Wever | 5f63976 | 2021-05-12 17:46:24 +0200 | [diff] [blame] | 581 | using __t = decltype(__to_unsigned_like(__value)); |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 582 | return __sign_combinator(__first, __last, __value, |
| 583 | __from_chars_integral<__t>, __base); |
| 584 | } |
| 585 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 586 | template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 587 | inline _LIBCPP_INLINE_VISIBILITY from_chars_result |
| 588 | from_chars(const char* __first, const char* __last, _Tp& __value) |
| 589 | { |
| 590 | return __from_chars_atoi(__first, __last, __value); |
| 591 | } |
| 592 | |
Marshall Clow | a8f29b9 | 2019-03-20 18:13:23 +0000 | [diff] [blame] | 593 | template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 594 | inline _LIBCPP_INLINE_VISIBILITY from_chars_result |
| 595 | from_chars(const char* __first, const char* __last, _Tp& __value, int __base) |
| 596 | { |
| 597 | _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); |
| 598 | return __from_chars_integral(__first, __last, __value, __base); |
| 599 | } |
| 600 | |
Mark de Wever | fa36ec7 | 2021-02-09 17:52:41 +0100 | [diff] [blame] | 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 |
| 604 | |
| 605 | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
| 606 | to_chars_result to_chars(char* __first, char* __last, float __value); |
| 607 | |
| 608 | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
| 609 | to_chars_result to_chars(char* __first, char* __last, double __value); |
| 610 | |
| 611 | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
| 612 | to_chars_result to_chars(char* __first, char* __last, long double __value); |
| 613 | |
| 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); |
| 616 | |
| 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); |
| 619 | |
| 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); |
| 622 | |
| 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); |
| 625 | |
| 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); |
| 628 | |
| 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); |
| 631 | |
| 632 | # endif // _LIBCPP_STD_VER > 14 |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 633 | #endif // _LIBCPP_CXX03_LANG |
Zhihao Yuan | 7bf1905 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 634 | |
| 635 | _LIBCPP_END_NAMESPACE_STD |
| 636 | |
Marshall Clow | 5598be7 | 2018-10-26 01:00:56 +0000 | [diff] [blame] | 637 | _LIBCPP_POP_MACROS |
| 638 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 639 | #endif // _LIBCPP_CHARCONV |