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