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