/src/llvm-project-16.0.6.build/include/c++/v1/charconv
Line | Count | Source (jump to first uncovered line) |
1 | | // -*- C++ -*- |
2 | | //===----------------------------------------------------------------------===// |
3 | | // |
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 |
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 | | friend bool operator==(const to_chars_result&, const to_chars_result&) = default; // since C++20 |
31 | | }; |
32 | | |
33 | | constexpr to_chars_result to_chars(char* first, char* last, see below value, |
34 | | int base = 10); // constexpr since C++23 |
35 | | to_chars_result to_chars(char* first, char* last, bool value, |
36 | | int base = 10) = delete; |
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; |
60 | | friend bool operator==(const from_chars_result&, const from_chars_result&) = default; // since C++20 |
61 | | }; |
62 | | |
63 | | constexpr from_chars_result from_chars(const char* first, const char* last, |
64 | | see below& value, int base = 10); // constexpr since C++23 |
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 | | |
80 | | #include <__algorithm/copy_n.h> |
81 | | #include <__assert> // all public C++ headers provide the assertion handler |
82 | | #include <__availability> |
83 | | #include <__bit/countl.h> |
84 | | #include <__charconv/chars_format.h> |
85 | | #include <__charconv/from_chars_result.h> |
86 | | #include <__charconv/tables.h> |
87 | | #include <__charconv/to_chars_base_10.h> |
88 | | #include <__charconv/to_chars_result.h> |
89 | | #include <__config> |
90 | | #include <__debug> |
91 | | #include <__errc> |
92 | | #include <__memory/addressof.h> |
93 | | #include <__type_traits/make_32_64_or_128_bit.h> |
94 | | #include <__utility/unreachable.h> |
95 | | #include <cmath> // for log2f |
96 | | #include <cstdint> |
97 | | #include <cstdlib> |
98 | | #include <cstring> |
99 | | #include <limits> |
100 | | #include <type_traits> |
101 | | |
102 | | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
103 | | # pragma GCC system_header |
104 | | #endif |
105 | | |
106 | | _LIBCPP_PUSH_MACROS |
107 | | #include <__undef_macros> |
108 | | |
109 | | _LIBCPP_BEGIN_NAMESPACE_STD |
110 | | |
111 | | #if _LIBCPP_STD_VER > 14 |
112 | | |
113 | | to_chars_result to_chars(char*, char*, bool, int = 10) = delete; |
114 | | from_chars_result from_chars(const char*, const char*, bool, int = 10) = delete; |
115 | | |
116 | | namespace __itoa |
117 | | { |
118 | | |
119 | | template <typename _Tp, typename = void> |
120 | | struct _LIBCPP_HIDDEN __traits_base; |
121 | | |
122 | | template <typename _Tp> |
123 | | struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) <= sizeof(uint32_t)>> |
124 | | { |
125 | | using type = uint32_t; |
126 | | |
127 | | /// The width estimation using a log10 algorithm. |
128 | | /// |
129 | | /// The algorithm is based on |
130 | | /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 |
131 | | /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that |
132 | | /// function requires its input to have at least one bit set the value of |
133 | | /// zero is set to one. This means the first element of the lookup table is |
134 | | /// zero. |
135 | | static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) |
136 | 0 | { |
137 | 0 | auto __t = (32 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12; |
138 | 0 | return __t - (__v < __itoa::__pow10_32[__t]) + 1; |
139 | 0 | } |
140 | | |
141 | | static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) |
142 | 0 | { |
143 | 0 | return __itoa::__base_10_u32(__p, __v); |
144 | 0 | } |
145 | | |
146 | | static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_32)& __pow() { return __itoa::__pow10_32; } |
147 | | }; |
148 | | |
149 | | template <typename _Tp> |
150 | | struct _LIBCPP_HIDDEN |
151 | | __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(uint64_t)>> { |
152 | | using type = uint64_t; |
153 | | |
154 | | /// The width estimation using a log10 algorithm. |
155 | | /// |
156 | | /// The algorithm is based on |
157 | | /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 |
158 | | /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that |
159 | | /// function requires its input to have at least one bit set the value of |
160 | | /// zero is set to one. This means the first element of the lookup table is |
161 | | /// zero. |
162 | 0 | static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) { |
163 | 0 | auto __t = (64 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12; |
164 | 0 | return __t - (__v < __itoa::__pow10_64[__t]) + 1; |
165 | 0 | } |
166 | | |
167 | 0 | static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u64(__p, __v); } |
168 | | |
169 | | static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_64)& __pow() { return __itoa::__pow10_64; } |
170 | | }; |
171 | | |
172 | | |
173 | | # ifndef _LIBCPP_HAS_NO_INT128 |
174 | | template <typename _Tp> |
175 | | struct _LIBCPP_HIDDEN |
176 | | __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(__uint128_t)> > { |
177 | | using type = __uint128_t; |
178 | | |
179 | | /// The width estimation using a log10 algorithm. |
180 | | /// |
181 | | /// The algorithm is based on |
182 | | /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 |
183 | | /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that |
184 | | /// function requires its input to have at least one bit set the value of |
185 | | /// zero is set to one. This means the first element of the lookup table is |
186 | | /// zero. |
187 | 0 | static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) { |
188 | 0 | _LIBCPP_ASSERT(__v > numeric_limits<uint64_t>::max(), "The optimizations for this algorithm fail when this isn't true."); |
189 | 0 | // There's always a bit set in the upper 64-bits. |
190 | 0 | auto __t = (128 - std::__libcpp_clz(static_cast<uint64_t>(__v >> 64))) * 1233 >> 12; |
191 | 0 | _LIBCPP_ASSERT(__t >= __itoa::__pow10_128_offset, "Index out of bounds"); |
192 | 0 | // __t is adjusted since the lookup table misses the lower entries. |
193 | 0 | return __t - (__v < __itoa::__pow10_128[__t - __itoa::__pow10_128_offset]) + 1; |
194 | 0 | } |
195 | | |
196 | 0 | static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u128(__p, __v); } |
197 | | |
198 | | // TODO FMT This pow function should get an index. |
199 | | // By moving this to its own header it can be reused by the pow function in to_chars_base_10. |
200 | | static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_128)& __pow() { return __itoa::__pow10_128; } |
201 | | }; |
202 | | #endif |
203 | | |
204 | | template <typename _Tp> |
205 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool |
206 | | __mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r) |
207 | | { |
208 | | auto __c = __a * __b; |
209 | | __r = __c; |
210 | | return __c > numeric_limits<unsigned char>::max(); |
211 | | } |
212 | | |
213 | | template <typename _Tp> |
214 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool |
215 | | __mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r) |
216 | | { |
217 | | auto __c = __a * __b; |
218 | | __r = __c; |
219 | | return __c > numeric_limits<unsigned short>::max(); |
220 | | } |
221 | | |
222 | | template <typename _Tp> |
223 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool |
224 | | __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r) |
225 | | { |
226 | | static_assert(is_unsigned<_Tp>::value, ""); |
227 | | return __builtin_mul_overflow(__a, __b, &__r); |
228 | | } |
229 | | |
230 | | template <typename _Tp, typename _Up> |
231 | | inline _LIBCPP_HIDE_FROM_ABI bool |
232 | | _LIBCPP_CONSTEXPR_SINCE_CXX23 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r) |
233 | | { |
234 | | return __itoa::__mul_overflowed(__a, static_cast<_Tp>(__b), __r); |
235 | | } |
236 | | |
237 | | template <typename _Tp> |
238 | | struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp> |
239 | | { |
240 | | static constexpr int digits = numeric_limits<_Tp>::digits10 + 1; |
241 | | using __traits_base<_Tp>::__pow; |
242 | | using typename __traits_base<_Tp>::type; |
243 | | |
244 | | // precondition: at least one non-zero character available |
245 | | static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char const* |
246 | | __read(char const* __p, char const* __ep, type& __a, type& __b) |
247 | | { |
248 | | type __cprod[digits]; |
249 | | int __j = digits - 1; |
250 | | int __i = digits; |
251 | | do |
252 | | { |
253 | | if (*__p < '0' || *__p > '9') |
254 | | break; |
255 | | __cprod[--__i] = *__p++ - '0'; |
256 | | } while (__p != __ep && __i != 0); |
257 | | |
258 | | __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1, |
259 | | __cprod[__i]); |
260 | | if (__itoa::__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b)) |
261 | | --__p; |
262 | | return __p; |
263 | | } |
264 | | |
265 | | template <typename _It1, typename _It2, class _Up> |
266 | | static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Up |
267 | | __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init) |
268 | | { |
269 | | for (; __first1 < __last1; ++__first1, ++__first2) |
270 | | __init = __init + *__first1 * *__first2; |
271 | | return __init; |
272 | | } |
273 | | }; |
274 | | |
275 | | } // namespace __itoa |
276 | | |
277 | | template <typename _Tp> |
278 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Tp |
279 | | __complement(_Tp __x) |
280 | 0 | { |
281 | 0 | static_assert(is_unsigned<_Tp>::value, "cast to unsigned first"); |
282 | 0 | return _Tp(~__x + 1); |
283 | 0 | } Unexecuted instantiation: unsigned int std::__1::__complement[abi:v160006]<unsigned int>(unsigned int) Unexecuted instantiation: unsigned long std::__1::__complement[abi:v160006]<unsigned long>(unsigned long) |
284 | | |
285 | | template <typename _Tp> |
286 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result |
287 | | __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type); |
288 | | |
289 | | template <typename _Tp> |
290 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result |
291 | | __to_chars_itoa(char* __first, char* __last, _Tp __value, true_type) |
292 | 0 | { |
293 | 0 | auto __x = std::__to_unsigned_like(__value); |
294 | 0 | if (__value < 0 && __first != __last) |
295 | 0 | { |
296 | 0 | *__first++ = '-'; |
297 | 0 | __x = std::__complement(__x); |
298 | 0 | } |
299 | |
|
300 | 0 | return std::__to_chars_itoa(__first, __last, __x, false_type()); |
301 | 0 | } Unexecuted instantiation: std::__1::to_chars_result std::__1::__to_chars_itoa[abi:v160006]<int>(char*, char*, int, std::__1::integral_constant<bool, true>) Unexecuted instantiation: std::__1::to_chars_result std::__1::__to_chars_itoa[abi:v160006]<long>(char*, char*, long, std::__1::integral_constant<bool, true>) |
302 | | |
303 | | template <typename _Tp> |
304 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result |
305 | | __to_chars_itoa(char* __first, char* __last, _Tp __value, false_type) |
306 | 0 | { |
307 | 0 | using __tx = __itoa::__traits<_Tp>; |
308 | 0 | auto __diff = __last - __first; |
309 | |
|
310 | 0 | if (__tx::digits <= __diff || __tx::__width(__value) <= __diff) |
311 | 0 | return {__tx::__convert(__first, __value), errc(0)}; |
312 | 0 | else |
313 | 0 | return {__last, errc::value_too_large}; |
314 | 0 | } Unexecuted instantiation: std::__1::to_chars_result std::__1::__to_chars_itoa[abi:v160006]<unsigned int>(char*, char*, unsigned int, std::__1::integral_constant<bool, false>) Unexecuted instantiation: std::__1::to_chars_result std::__1::__to_chars_itoa[abi:v160006]<unsigned long>(char*, char*, unsigned long, std::__1::integral_constant<bool, false>) |
315 | | |
316 | | # ifndef _LIBCPP_HAS_NO_INT128 |
317 | | template <> |
318 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result |
319 | | __to_chars_itoa(char* __first, char* __last, __uint128_t __value, false_type) |
320 | 0 | { |
321 | 0 | // When the value fits in 64-bits use the 64-bit code path. This reduces |
322 | 0 | // the number of expensive calculations on 128-bit values. |
323 | 0 | // |
324 | 0 | // NOTE the 128-bit code path requires this optimization. |
325 | 0 | if(__value <= numeric_limits<uint64_t>::max()) |
326 | 0 | return __to_chars_itoa(__first, __last, static_cast<uint64_t>(__value), false_type()); |
327 | 0 |
|
328 | 0 | using __tx = __itoa::__traits<__uint128_t>; |
329 | 0 | auto __diff = __last - __first; |
330 | 0 |
|
331 | 0 | if (__tx::digits <= __diff || __tx::__width(__value) <= __diff) |
332 | 0 | return {__tx::__convert(__first, __value), errc(0)}; |
333 | 0 | else |
334 | 0 | return {__last, errc::value_too_large}; |
335 | 0 | } |
336 | | #endif |
337 | | |
338 | | template <class _Tp> |
339 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result |
340 | | __to_chars_integral(char* __first, char* __last, _Tp __value, int __base, false_type); |
341 | | |
342 | | template <typename _Tp> |
343 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result |
344 | | __to_chars_integral(char* __first, char* __last, _Tp __value, int __base, |
345 | | true_type) |
346 | | { |
347 | | auto __x = std::__to_unsigned_like(__value); |
348 | | if (__value < 0 && __first != __last) |
349 | | { |
350 | | *__first++ = '-'; |
351 | | __x = std::__complement(__x); |
352 | | } |
353 | | |
354 | | return std::__to_chars_integral(__first, __last, __x, __base, false_type()); |
355 | | } |
356 | | |
357 | | namespace __itoa { |
358 | | |
359 | | template <unsigned _Base> |
360 | | struct _LIBCPP_HIDDEN __integral; |
361 | | |
362 | | template <> |
363 | | struct _LIBCPP_HIDDEN __integral<2> { |
364 | | template <typename _Tp> |
365 | | _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept { |
366 | | // If value == 0 still need one digit. If the value != this has no |
367 | | // effect since the code scans for the most significant bit set. (Note |
368 | | // that __libcpp_clz doesn't work for 0.) |
369 | | return numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1); |
370 | | } |
371 | | |
372 | | template <typename _Tp> |
373 | | _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) { |
374 | | ptrdiff_t __cap = __last - __first; |
375 | | int __n = __width(__value); |
376 | | if (__n > __cap) |
377 | | return {__last, errc::value_too_large}; |
378 | | |
379 | | __last = __first + __n; |
380 | | char* __p = __last; |
381 | | const unsigned __divisor = 16; |
382 | | while (__value > __divisor) { |
383 | | unsigned __c = __value % __divisor; |
384 | | __value /= __divisor; |
385 | | __p -= 4; |
386 | | std::copy_n(&__base_2_lut[4 * __c], 4, __p); |
387 | | } |
388 | | do { |
389 | | unsigned __c = __value % 2; |
390 | | __value /= 2; |
391 | | *--__p = "01"[__c]; |
392 | | } while (__value != 0); |
393 | | return {__last, errc(0)}; |
394 | | } |
395 | | }; |
396 | | |
397 | | template <> |
398 | | struct _LIBCPP_HIDDEN __integral<8> { |
399 | | template <typename _Tp> |
400 | | _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept { |
401 | | // If value == 0 still need one digit. If the value != this has no |
402 | | // effect since the code scans for the most significat bit set. (Note |
403 | | // that __libcpp_clz doesn't work for 0.) |
404 | | return ((numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1)) + 2) / 3; |
405 | | } |
406 | | |
407 | | template <typename _Tp> |
408 | | _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) { |
409 | | ptrdiff_t __cap = __last - __first; |
410 | | int __n = __width(__value); |
411 | | if (__n > __cap) |
412 | | return {__last, errc::value_too_large}; |
413 | | |
414 | | __last = __first + __n; |
415 | | char* __p = __last; |
416 | | unsigned __divisor = 64; |
417 | | while (__value > __divisor) { |
418 | | unsigned __c = __value % __divisor; |
419 | | __value /= __divisor; |
420 | | __p -= 2; |
421 | | std::copy_n(&__base_8_lut[2 * __c], 2, __p); |
422 | | } |
423 | | do { |
424 | | unsigned __c = __value % 8; |
425 | | __value /= 8; |
426 | | *--__p = "01234567"[__c]; |
427 | | } while (__value != 0); |
428 | | return {__last, errc(0)}; |
429 | | } |
430 | | |
431 | | }; |
432 | | |
433 | | template <> |
434 | | struct _LIBCPP_HIDDEN __integral<16> { |
435 | | template <typename _Tp> |
436 | | _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept { |
437 | | // If value == 0 still need one digit. If the value != this has no |
438 | | // effect since the code scans for the most significat bit set. (Note |
439 | | // that __libcpp_clz doesn't work for 0.) |
440 | | return (numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1) + 3) / 4; |
441 | | } |
442 | | |
443 | | template <typename _Tp> |
444 | | _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) { |
445 | | ptrdiff_t __cap = __last - __first; |
446 | | int __n = __width(__value); |
447 | | if (__n > __cap) |
448 | | return {__last, errc::value_too_large}; |
449 | | |
450 | | __last = __first + __n; |
451 | | char* __p = __last; |
452 | | unsigned __divisor = 256; |
453 | | while (__value > __divisor) { |
454 | | unsigned __c = __value % __divisor; |
455 | | __value /= __divisor; |
456 | | __p -= 2; |
457 | | std::copy_n(&__base_16_lut[2 * __c], 2, __p); |
458 | | } |
459 | | if (__first != __last) |
460 | | do { |
461 | | unsigned __c = __value % 16; |
462 | | __value /= 16; |
463 | | *--__p = "0123456789abcdef"[__c]; |
464 | | } while (__value != 0); |
465 | | return {__last, errc(0)}; |
466 | | } |
467 | | }; |
468 | | |
469 | | } // namespace __itoa |
470 | | |
471 | | template <unsigned _Base, typename _Tp, |
472 | | typename enable_if<(sizeof(_Tp) >= sizeof(unsigned)), int>::type = 0> |
473 | | _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int |
474 | | __to_chars_integral_width(_Tp __value) { |
475 | | return __itoa::__integral<_Base>::__width(__value); |
476 | | } |
477 | | |
478 | | template <unsigned _Base, typename _Tp, |
479 | | typename enable_if<(sizeof(_Tp) < sizeof(unsigned)), int>::type = 0> |
480 | | _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int |
481 | | __to_chars_integral_width(_Tp __value) { |
482 | | return std::__to_chars_integral_width<_Base>(static_cast<unsigned>(__value)); |
483 | | } |
484 | | |
485 | | template <unsigned _Base, typename _Tp, |
486 | | typename enable_if<(sizeof(_Tp) >= sizeof(unsigned)), int>::type = 0> |
487 | | _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result |
488 | | __to_chars_integral(char* __first, char* __last, _Tp __value) { |
489 | | return __itoa::__integral<_Base>::__to_chars(__first, __last, __value); |
490 | | } |
491 | | |
492 | | template <unsigned _Base, typename _Tp, |
493 | | typename enable_if<(sizeof(_Tp) < sizeof(unsigned)), int>::type = 0> |
494 | | _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result |
495 | | __to_chars_integral(char* __first, char* __last, _Tp __value) { |
496 | | return std::__to_chars_integral<_Base>(__first, __last, static_cast<unsigned>(__value)); |
497 | | } |
498 | | |
499 | | template <typename _Tp> |
500 | | _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int |
501 | | __to_chars_integral_width(_Tp __value, unsigned __base) { |
502 | | _LIBCPP_ASSERT(__value >= 0, "The function requires a non-negative value."); |
503 | | |
504 | | unsigned __base_2 = __base * __base; |
505 | | unsigned __base_3 = __base_2 * __base; |
506 | | unsigned __base_4 = __base_2 * __base_2; |
507 | | |
508 | | int __r = 0; |
509 | | while (true) { |
510 | | if (__value < __base) |
511 | | return __r + 1; |
512 | | if (__value < __base_2) |
513 | | return __r + 2; |
514 | | if (__value < __base_3) |
515 | | return __r + 3; |
516 | | if (__value < __base_4) |
517 | | return __r + 4; |
518 | | |
519 | | __value /= __base_4; |
520 | | __r += 4; |
521 | | } |
522 | | |
523 | | __libcpp_unreachable(); |
524 | | } |
525 | | |
526 | | template <typename _Tp> |
527 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result |
528 | | __to_chars_integral(char* __first, char* __last, _Tp __value, int __base, |
529 | | false_type) |
530 | | { |
531 | | if (__base == 10) [[likely]] |
532 | | return std::__to_chars_itoa(__first, __last, __value, false_type()); |
533 | | |
534 | | switch (__base) { |
535 | | case 2: |
536 | | return std::__to_chars_integral<2>(__first, __last, __value); |
537 | | case 8: |
538 | | return std::__to_chars_integral<8>(__first, __last, __value); |
539 | | case 16: |
540 | | return std::__to_chars_integral<16>(__first, __last, __value); |
541 | | } |
542 | | |
543 | | ptrdiff_t __cap = __last - __first; |
544 | | int __n = std::__to_chars_integral_width(__value, __base); |
545 | | if (__n > __cap) |
546 | | return {__last, errc::value_too_large}; |
547 | | |
548 | | __last = __first + __n; |
549 | | char* __p = __last; |
550 | | do { |
551 | | unsigned __c = __value % __base; |
552 | | __value /= __base; |
553 | | *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c]; |
554 | | } while (__value != 0); |
555 | | return {__last, errc(0)}; |
556 | | } |
557 | | |
558 | | template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> |
559 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result |
560 | | to_chars(char* __first, char* __last, _Tp __value) |
561 | 0 | { |
562 | 0 | using _Type = __make_32_64_or_128_bit_t<_Tp>; |
563 | 0 | static_assert(!is_same<_Type, void>::value, "unsupported integral type used in to_chars"); |
564 | 0 | return std::__to_chars_itoa(__first, __last, static_cast<_Type>(__value), is_signed<_Tp>()); |
565 | 0 | } Unexecuted instantiation: std::__1::to_chars_result std::__1::to_chars[abi:v160006]<int, 0>(char*, char*, int) Unexecuted instantiation: std::__1::to_chars_result std::__1::to_chars[abi:v160006]<long, 0>(char*, char*, long) Unexecuted instantiation: std::__1::to_chars_result std::__1::to_chars[abi:v160006]<long long, 0>(char*, char*, long long) Unexecuted instantiation: std::__1::to_chars_result std::__1::to_chars[abi:v160006]<unsigned int, 0>(char*, char*, unsigned int) Unexecuted instantiation: std::__1::to_chars_result std::__1::to_chars[abi:v160006]<unsigned long, 0>(char*, char*, unsigned long) Unexecuted instantiation: std::__1::to_chars_result std::__1::to_chars[abi:v160006]<unsigned long long, 0>(char*, char*, unsigned long long) |
566 | | |
567 | | template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> |
568 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result |
569 | | to_chars(char* __first, char* __last, _Tp __value, int __base) |
570 | | { |
571 | | _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); |
572 | | |
573 | | using _Type = __make_32_64_or_128_bit_t<_Tp>; |
574 | | return std::__to_chars_integral(__first, __last, static_cast<_Type>(__value), __base, is_signed<_Tp>()); |
575 | | } |
576 | | |
577 | | template <typename _It, typename _Tp, typename _Fn, typename... _Ts> |
578 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result |
579 | | __sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) |
580 | | { |
581 | | using __tl = numeric_limits<_Tp>; |
582 | | decltype(std::__to_unsigned_like(__value)) __x; |
583 | | |
584 | | bool __neg = (__first != __last && *__first == '-'); |
585 | | auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...); |
586 | | switch (__r.ec) |
587 | | { |
588 | | case errc::invalid_argument: |
589 | | return {__first, __r.ec}; |
590 | | case errc::result_out_of_range: |
591 | | return __r; |
592 | | default: |
593 | | break; |
594 | | } |
595 | | |
596 | | if (__neg) |
597 | | { |
598 | | if (__x <= std::__complement(std::__to_unsigned_like(__tl::min()))) |
599 | | { |
600 | | __x = std::__complement(__x); |
601 | | std::copy_n(std::addressof(__x), 1, std::addressof(__value)); |
602 | | return __r; |
603 | | } |
604 | | } |
605 | | else |
606 | | { |
607 | | if (__x <= std::__to_unsigned_like(__tl::max())) |
608 | | { |
609 | | __value = __x; |
610 | | return __r; |
611 | | } |
612 | | } |
613 | | |
614 | | return {__r.ptr, errc::result_out_of_range}; |
615 | | } |
616 | | |
617 | | template <typename _Tp> |
618 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool |
619 | | __in_pattern(_Tp __c) |
620 | | { |
621 | | return '0' <= __c && __c <= '9'; |
622 | | } |
623 | | |
624 | | struct _LIBCPP_HIDDEN __in_pattern_result |
625 | | { |
626 | | bool __ok; |
627 | | int __val; |
628 | | |
629 | 0 | explicit _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI operator bool() const { return __ok; } |
630 | | }; |
631 | | |
632 | | template <typename _Tp> |
633 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI __in_pattern_result |
634 | | __in_pattern(_Tp __c, int __base) |
635 | | { |
636 | | if (__base <= 10) |
637 | | return {'0' <= __c && __c < '0' + __base, __c - '0'}; |
638 | | else if (std::__in_pattern(__c)) |
639 | | return {true, __c - '0'}; |
640 | | else if ('a' <= __c && __c < 'a' + __base - 10) |
641 | | return {true, __c - 'a' + 10}; |
642 | | else |
643 | | return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10}; |
644 | | } |
645 | | |
646 | | template <typename _It, typename _Tp, typename _Fn, typename... _Ts> |
647 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result |
648 | | __subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, |
649 | | _Ts... __args) |
650 | | { |
651 | | auto __find_non_zero = [](_It __firstit, _It __lastit) { |
652 | | for (; __firstit != __lastit; ++__firstit) |
653 | | if (*__firstit != '0') |
654 | | break; |
655 | | return __firstit; |
656 | | }; |
657 | | |
658 | | auto __p = __find_non_zero(__first, __last); |
659 | | if (__p == __last || !std::__in_pattern(*__p, __args...)) |
660 | | { |
661 | | if (__p == __first) |
662 | | return {__first, errc::invalid_argument}; |
663 | | else |
664 | | { |
665 | | __value = 0; |
666 | | return {__p, {}}; |
667 | | } |
668 | | } |
669 | | |
670 | | auto __r = __f(__p, __last, __value, __args...); |
671 | | if (__r.ec == errc::result_out_of_range) |
672 | | { |
673 | | for (; __r.ptr != __last; ++__r.ptr) |
674 | | { |
675 | | if (!std::__in_pattern(*__r.ptr, __args...)) |
676 | | break; |
677 | | } |
678 | | } |
679 | | |
680 | | return __r; |
681 | | } |
682 | | |
683 | | template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0> |
684 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result |
685 | | __from_chars_atoi(const char* __first, const char* __last, _Tp& __value) |
686 | | { |
687 | | using __tx = __itoa::__traits<_Tp>; |
688 | | using __output_type = typename __tx::type; |
689 | | |
690 | | return std::__subject_seq_combinator( |
691 | | __first, __last, __value, |
692 | | [](const char* __f, const char* __l, |
693 | | _Tp& __val) -> from_chars_result { |
694 | | __output_type __a, __b; |
695 | | auto __p = __tx::__read(__f, __l, __a, __b); |
696 | | if (__p == __l || !std::__in_pattern(*__p)) |
697 | | { |
698 | | __output_type __m = numeric_limits<_Tp>::max(); |
699 | | if (__m >= __a && __m - __a >= __b) |
700 | | { |
701 | | __val = __a + __b; |
702 | | return {__p, {}}; |
703 | | } |
704 | | } |
705 | | return {__p, errc::result_out_of_range}; |
706 | | }); |
707 | | } |
708 | | |
709 | | template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0> |
710 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result |
711 | | __from_chars_atoi(const char* __first, const char* __last, _Tp& __value) |
712 | | { |
713 | | using __t = decltype(std::__to_unsigned_like(__value)); |
714 | | return std::__sign_combinator(__first, __last, __value, __from_chars_atoi<__t>); |
715 | | } |
716 | | |
717 | | |
718 | | /* |
719 | | // Code used to generate __from_chars_log2f_lut. |
720 | | #include <cmath> |
721 | | #include <iostream> |
722 | | #include <format> |
723 | | |
724 | | int main() { |
725 | | for (int i = 2; i <= 36; ++i) |
726 | | std::cout << std::format("{},\n", log2f(i)); |
727 | | } |
728 | | */ |
729 | | /// log2f table for bases [2, 36]. |
730 | | inline constexpr float __from_chars_log2f_lut[35] = { |
731 | | 1, 1.5849625, 2, 2.321928, 2.5849626, 2.807355, 3, 3.169925, 3.321928, |
732 | | 3.4594316, 3.5849626, 3.7004397, 3.807355, 3.9068906, 4, 4.087463, 4.169925, 4.2479277, |
733 | | 4.321928, 4.3923173, 4.4594316, 4.523562, 4.5849624, 4.643856, 4.70044, 4.7548876, 4.807355, |
734 | | 4.857981, 4.9068904, 4.9541965, 5, 5.044394, 5.087463, 5.129283, 5.169925}; |
735 | | |
736 | | template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0> |
737 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result |
738 | | __from_chars_integral(const char* __first, const char* __last, _Tp& __value, |
739 | | int __base) |
740 | | { |
741 | | if (__base == 10) |
742 | | return std::__from_chars_atoi(__first, __last, __value); |
743 | | |
744 | | return std::__subject_seq_combinator( |
745 | | __first, __last, __value, |
746 | | [](const char* __p, const char* __lastp, _Tp& __val, |
747 | | int __b) -> from_chars_result { |
748 | | using __tl = numeric_limits<_Tp>; |
749 | | // __base is always between 2 and 36 inclusive. |
750 | | auto __digits = __tl::digits / __from_chars_log2f_lut[__b - 2]; |
751 | | _Tp __x = __in_pattern(*__p++, __b).__val, __y = 0; |
752 | | |
753 | | for (int __i = 1; __p != __lastp; ++__i, ++__p) |
754 | | { |
755 | | if (auto __c = __in_pattern(*__p, __b)) |
756 | | { |
757 | | if (__i < __digits - 1) |
758 | | __x = __x * __b + __c.__val; |
759 | | else |
760 | | { |
761 | | if (!__itoa::__mul_overflowed(__x, __b, __x)) |
762 | | ++__p; |
763 | | __y = __c.__val; |
764 | | break; |
765 | | } |
766 | | } |
767 | | else |
768 | | break; |
769 | | } |
770 | | |
771 | | if (__p == __lastp || !__in_pattern(*__p, __b)) |
772 | | { |
773 | | if (__tl::max() - __x >= __y) |
774 | | { |
775 | | __val = __x + __y; |
776 | | return {__p, {}}; |
777 | | } |
778 | | } |
779 | | return {__p, errc::result_out_of_range}; |
780 | | }, |
781 | | __base); |
782 | | } |
783 | | |
784 | | template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0> |
785 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result |
786 | | __from_chars_integral(const char* __first, const char* __last, _Tp& __value, |
787 | | int __base) |
788 | | { |
789 | | using __t = decltype(std::__to_unsigned_like(__value)); |
790 | | return std::__sign_combinator(__first, __last, __value, |
791 | | __from_chars_integral<__t>, __base); |
792 | | } |
793 | | |
794 | | template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> |
795 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result |
796 | | from_chars(const char* __first, const char* __last, _Tp& __value) |
797 | | { |
798 | | return std::__from_chars_atoi(__first, __last, __value); |
799 | | } |
800 | | |
801 | | template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0> |
802 | | inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result |
803 | | from_chars(const char* __first, const char* __last, _Tp& __value, int __base) |
804 | | { |
805 | | _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); |
806 | | return std::__from_chars_integral(__first, __last, __value, __base); |
807 | | } |
808 | | |
809 | | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
810 | | to_chars_result to_chars(char* __first, char* __last, float __value); |
811 | | |
812 | | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
813 | | to_chars_result to_chars(char* __first, char* __last, double __value); |
814 | | |
815 | | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
816 | | to_chars_result to_chars(char* __first, char* __last, long double __value); |
817 | | |
818 | | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
819 | | to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt); |
820 | | |
821 | | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
822 | | to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt); |
823 | | |
824 | | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
825 | | to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt); |
826 | | |
827 | | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
828 | | to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt, int __precision); |
829 | | |
830 | | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
831 | | to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt, int __precision); |
832 | | |
833 | | _LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS |
834 | | to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt, int __precision); |
835 | | |
836 | | #endif // _LIBCPP_STD_VER > 14 |
837 | | |
838 | | _LIBCPP_END_NAMESPACE_STD |
839 | | |
840 | | _LIBCPP_POP_MACROS |
841 | | |
842 | | #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 |
843 | | # include <concepts> |
844 | | # include <iosfwd> |
845 | | #endif |
846 | | |
847 | | #endif // _LIBCPP_CHARCONV |