blob: 06634fe7bc39257051de42512644e70b04947f70 [file] [log] [blame]
Zhihao Yuan7bf19052018-08-01 02:38:30 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Zhihao Yuan7bf19052018-08-01 02:38:30 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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 Yuan7bf19052018-08-01 02:38:30 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_CHARCONV
11#define _LIBCPP_CHARCONV
12
13/*
14 charconv synopsis
15
16namespace std {
17
18 // floating-point format for primitive numerical conversion
19 enum class chars_format {
20 scientific = unspecified,
21 fixed = unspecified,
22 hex = unspecified,
23 general = fixed | scientific
24 };
25
26 // 23.20.2, primitive numerical output conversion
27 struct to_chars_result {
28 char* ptr;
29 errc ec;
Mark de Wever8b9aad92021-10-23 18:28:31 +020030 friend bool operator==(const to_chars_result&, const to_chars_result&) = default; // since C++20
Zhihao Yuan7bf19052018-08-01 02:38:30 +000031 };
32
33 to_chars_result to_chars(char* first, char* last, see below value,
34 int base = 10);
Konstantin Varlamov1071d352021-10-15 17:40:42 +020035 to_chars_result to_chars(char* first, char* last, bool value,
36 int base = 10) = delete;
Zhihao Yuan7bf19052018-08-01 02:38:30 +000037
38 to_chars_result to_chars(char* first, char* last, float value);
39 to_chars_result to_chars(char* first, char* last, double value);
40 to_chars_result to_chars(char* first, char* last, long double value);
41
42 to_chars_result to_chars(char* first, char* last, float value,
43 chars_format fmt);
44 to_chars_result to_chars(char* first, char* last, double value,
45 chars_format fmt);
46 to_chars_result to_chars(char* first, char* last, long double value,
47 chars_format fmt);
48
49 to_chars_result to_chars(char* first, char* last, float value,
50 chars_format fmt, int precision);
51 to_chars_result to_chars(char* first, char* last, double value,
52 chars_format fmt, int precision);
53 to_chars_result to_chars(char* first, char* last, long double value,
54 chars_format fmt, int precision);
55
56 // 23.20.3, primitive numerical input conversion
57 struct from_chars_result {
58 const char* ptr;
59 errc ec;
Mark de Wever8b9aad92021-10-23 18:28:31 +020060 friend bool operator==(const from_chars_result&, const from_chars_result&) = default; // since C++20
Zhihao Yuan7bf19052018-08-01 02:38:30 +000061 };
62
63 from_chars_result from_chars(const char* first, const char* last,
64 see below& value, int base = 10);
65
66 from_chars_result from_chars(const char* first, const char* last,
67 float& value,
68 chars_format fmt = chars_format::general);
69 from_chars_result from_chars(const char* first, const char* last,
70 double& value,
71 chars_format fmt = chars_format::general);
72 from_chars_result from_chars(const char* first, const char* last,
73 long double& value,
74 chars_format fmt = chars_format::general);
75
76} // namespace std
77
78*/
79
Louis Dionne73912b22020-11-04 15:01:25 -050080#include <__availability>
Mark de Weverde2667c2021-08-08 11:02:07 +020081#include <__bits>
Mark de Weverfd2375f2021-08-30 19:47:55 +020082#include <__charconv/chars_format.h>
83#include <__charconv/from_chars_result.h>
84#include <__charconv/to_chars_result.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040085#include <__config>
Zhihao Yuan7bf19052018-08-01 02:38:30 +000086#include <__errc>
Arthur O'Dwyer79a4afc2020-12-07 21:45:29 -050087#include <cmath> // for log2f
88#include <cstdint>
Mark de Wever3ae91192021-02-27 16:52:39 +010089#include <cstdlib> // for _LIBCPP_UNREACHABLE
Arthur O'Dwyer79a4afc2020-12-07 21:45:29 -050090#include <cstring>
Zhihao Yuan7bf19052018-08-01 02:38:30 +000091#include <limits>
Arthur O'Dwyer79a4afc2020-12-07 21:45:29 -050092#include <type_traits>
Zhihao Yuan7bf19052018-08-01 02:38:30 +000093
94#include <__debug>
95
96#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
97#pragma GCC system_header
98#endif
99
Marshall Clow5598be72018-10-26 01:00:56 +0000100_LIBCPP_PUSH_MACROS
101#include <__undef_macros>
102
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000103_LIBCPP_BEGIN_NAMESPACE_STD
104
Louis Dionne5254b372018-10-25 12:13:43 +0000105namespace __itoa {
Louis Dionne4c7d4f12020-05-21 10:25:15 -0400106_LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_FUNC_VIS char* __u64toa(uint64_t __value, char* __buffer) _NOEXCEPT;
107_LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_FUNC_VIS char* __u32toa(uint32_t __value, char* __buffer) _NOEXCEPT;
Nikolas Klauserd26407a2021-12-02 14:12:51 +0100108} // namespace __itoa
Louis Dionne5254b372018-10-25 12:13:43 +0000109
Marshall Clowa8f29b92019-03-20 18:13:23 +0000110#ifndef _LIBCPP_CXX03_LANG
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000111
Mark de Wever10abe8a2021-11-16 16:04:29 +0100112to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
113from_chars_result from_chars(const char*, const char*, bool, int = 10) = delete;
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000114
115namespace __itoa
116{
117
Marshall Clowa8f29b92019-03-20 18:13:23 +0000118static _LIBCPP_CONSTEXPR uint64_t __pow10_64[] = {
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000119 UINT64_C(0),
120 UINT64_C(10),
121 UINT64_C(100),
122 UINT64_C(1000),
123 UINT64_C(10000),
124 UINT64_C(100000),
125 UINT64_C(1000000),
126 UINT64_C(10000000),
127 UINT64_C(100000000),
128 UINT64_C(1000000000),
129 UINT64_C(10000000000),
130 UINT64_C(100000000000),
131 UINT64_C(1000000000000),
132 UINT64_C(10000000000000),
133 UINT64_C(100000000000000),
134 UINT64_C(1000000000000000),
135 UINT64_C(10000000000000000),
136 UINT64_C(100000000000000000),
137 UINT64_C(1000000000000000000),
138 UINT64_C(10000000000000000000),
139};
140
Marshall Clowa8f29b92019-03-20 18:13:23 +0000141static _LIBCPP_CONSTEXPR uint32_t __pow10_32[] = {
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000142 UINT32_C(0), UINT32_C(10), UINT32_C(100),
143 UINT32_C(1000), UINT32_C(10000), UINT32_C(100000),
144 UINT32_C(1000000), UINT32_C(10000000), UINT32_C(100000000),
145 UINT32_C(1000000000),
146};
147
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000148template <typename _Tp, typename = void>
149struct _LIBCPP_HIDDEN __traits_base
150{
151 using type = uint64_t;
152
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000153 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
154 {
Mark de Weverde2667c2021-08-08 11:02:07 +0200155 auto __t = (64 - _VSTD::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000156 return __t - (__v < __pow10_64[__t]) + 1;
157 }
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000158
Louis Dionnec78d7352020-02-14 15:19:47 +0100159 _LIBCPP_AVAILABILITY_TO_CHARS
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000160 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
161 {
162 return __u64toa(__v, __p);
163 }
164
Marshall Clowa8f29b92019-03-20 18:13:23 +0000165 static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_64)& __pow() { return __pow10_64; }
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000166};
167
168template <typename _Tp>
169struct _LIBCPP_HIDDEN
170 __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))>
171{
172 using type = uint32_t;
173
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000174 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
175 {
Mark de Weverde2667c2021-08-08 11:02:07 +0200176 auto __t = (32 - _VSTD::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000177 return __t - (__v < __pow10_32[__t]) + 1;
178 }
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000179
Louis Dionnec78d7352020-02-14 15:19:47 +0100180 _LIBCPP_AVAILABILITY_TO_CHARS
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000181 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
182 {
183 return __u32toa(__v, __p);
184 }
185
Marshall Clowa8f29b92019-03-20 18:13:23 +0000186 static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_32)& __pow() { return __pow10_32; }
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000187};
188
189template <typename _Tp>
190inline _LIBCPP_INLINE_VISIBILITY bool
191__mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r)
192{
193 auto __c = __a * __b;
194 __r = __c;
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500195 return __c > numeric_limits<unsigned char>::max();
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000196}
197
198template <typename _Tp>
199inline _LIBCPP_INLINE_VISIBILITY bool
200__mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r)
201{
202 auto __c = __a * __b;
203 __r = __c;
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500204 return __c > numeric_limits<unsigned short>::max();
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000205}
206
207template <typename _Tp>
208inline _LIBCPP_INLINE_VISIBILITY bool
209__mul_overflowed(_Tp __a, _Tp __b, _Tp& __r)
210{
211 static_assert(is_unsigned<_Tp>::value, "");
212#if !defined(_LIBCPP_COMPILER_MSVC)
213 return __builtin_mul_overflow(__a, __b, &__r);
214#else
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500215 bool __did = __b && (numeric_limits<_Tp>::max() / __b) < __a;
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000216 __r = __a * __b;
217 return __did;
218#endif
219}
220
221template <typename _Tp, typename _Up>
222inline _LIBCPP_INLINE_VISIBILITY bool
223__mul_overflowed(_Tp __a, _Up __b, _Tp& __r)
224{
225 return __mul_overflowed(__a, static_cast<_Tp>(__b), __r);
226}
227
228template <typename _Tp>
229struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp>
230{
Marshall Clowa8f29b92019-03-20 18:13:23 +0000231 static _LIBCPP_CONSTEXPR int digits = numeric_limits<_Tp>::digits10 + 1;
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000232 using __traits_base<_Tp>::__pow;
233 using typename __traits_base<_Tp>::type;
234
235 // precondition: at least one non-zero character available
236 static _LIBCPP_INLINE_VISIBILITY char const*
237 __read(char const* __p, char const* __ep, type& __a, type& __b)
238 {
239 type __cprod[digits];
240 int __j = digits - 1;
241 int __i = digits;
242 do
243 {
244 if (!('0' <= *__p && *__p <= '9'))
245 break;
246 __cprod[--__i] = *__p++ - '0';
247 } while (__p != __ep && __i != 0);
248
249 __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1,
250 __cprod[__i]);
251 if (__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))
252 --__p;
253 return __p;
254 }
255
256 template <typename _It1, typename _It2, class _Up>
257 static _LIBCPP_INLINE_VISIBILITY _Up
258 __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init)
259 {
260 for (; __first1 < __last1; ++__first1, ++__first2)
261 __init = __init + *__first1 * *__first2;
262 return __init;
263 }
264};
265
266} // namespace __itoa
267
268template <typename _Tp>
269inline _LIBCPP_INLINE_VISIBILITY _Tp
270__complement(_Tp __x)
271{
272 static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
273 return _Tp(~__x + 1);
274}
275
276template <typename _Tp>
Louis Dionnec78d7352020-02-14 15:19:47 +0100277_LIBCPP_AVAILABILITY_TO_CHARS
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000278inline _LIBCPP_INLINE_VISIBILITY to_chars_result
279__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
280{
Mark de Wever5f639762021-05-12 17:46:24 +0200281 auto __x = __to_unsigned_like(__value);
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000282 if (__value < 0 && __first != __last)
283 {
284 *__first++ = '-';
285 __x = __complement(__x);
286 }
287
288 return __to_chars_itoa(__first, __last, __x, false_type());
289}
290
291template <typename _Tp>
Louis Dionnec78d7352020-02-14 15:19:47 +0100292_LIBCPP_AVAILABILITY_TO_CHARS
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000293inline _LIBCPP_INLINE_VISIBILITY to_chars_result
294__to_chars_itoa(char* __first, char* __last, _Tp __value, false_type)
295{
296 using __tx = __itoa::__traits<_Tp>;
297 auto __diff = __last - __first;
298
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000299 if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
Thomas Andersonfe733322019-06-13 22:27:24 +0000300 return {__tx::__convert(__value, __first), errc(0)};
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000301 else
302 return {__last, errc::value_too_large};
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000303}
304
305template <typename _Tp>
Louis Dionnec78d7352020-02-14 15:19:47 +0100306_LIBCPP_AVAILABILITY_TO_CHARS
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000307inline _LIBCPP_INLINE_VISIBILITY to_chars_result
308__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
309 true_type)
310{
Mark de Wever5f639762021-05-12 17:46:24 +0200311 auto __x = __to_unsigned_like(__value);
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000312 if (__value < 0 && __first != __last)
313 {
314 *__first++ = '-';
315 __x = __complement(__x);
316 }
317
318 return __to_chars_integral(__first, __last, __x, __base, false_type());
319}
320
321template <typename _Tp>
Mark de Wever3ae91192021-02-27 16:52:39 +0100322_LIBCPP_AVAILABILITY_TO_CHARS _LIBCPP_INLINE_VISIBILITY int __to_chars_integral_width(_Tp __value, unsigned __base) {
323 _LIBCPP_ASSERT(__value >= 0, "The function requires a non-negative value.");
324
325 unsigned __base_2 = __base * __base;
326 unsigned __base_3 = __base_2 * __base;
327 unsigned __base_4 = __base_2 * __base_2;
328
329 int __r = 0;
330 while (true) {
331 if (__value < __base)
332 return __r + 1;
333 if (__value < __base_2)
334 return __r + 2;
335 if (__value < __base_3)
336 return __r + 3;
337 if (__value < __base_4)
338 return __r + 4;
339
340 __value /= __base_4;
341 __r += 4;
342 }
343
344 _LIBCPP_UNREACHABLE();
345}
346
347template <typename _Tp>
Louis Dionnec78d7352020-02-14 15:19:47 +0100348_LIBCPP_AVAILABILITY_TO_CHARS
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000349inline _LIBCPP_INLINE_VISIBILITY to_chars_result
350__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
351 false_type)
352{
Mark de Wever3ae91192021-02-27 16:52:39 +0100353 if (__base == 10)
354 return __to_chars_itoa(__first, __last, __value, false_type());
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000355
Mark de Wever3ae91192021-02-27 16:52:39 +0100356 ptrdiff_t __cap = __last - __first;
357 int __n = __to_chars_integral_width(__value, __base);
358 if (__n > __cap)
359 return {__last, errc::value_too_large};
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000360
Mark de Wever3ae91192021-02-27 16:52:39 +0100361 __last = __first + __n;
362 char* __p = __last;
363 do {
364 unsigned __c = __value % __base;
365 __value /= __base;
366 *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c];
367 } while (__value != 0);
368 return {__last, errc(0)};
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000369}
370
Marshall Clowa8f29b92019-03-20 18:13:23 +0000371template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
Louis Dionnec78d7352020-02-14 15:19:47 +0100372_LIBCPP_AVAILABILITY_TO_CHARS
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000373inline _LIBCPP_INLINE_VISIBILITY to_chars_result
374to_chars(char* __first, char* __last, _Tp __value)
375{
376 return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>());
377}
378
Marshall Clowa8f29b92019-03-20 18:13:23 +0000379template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
Louis Dionnec78d7352020-02-14 15:19:47 +0100380_LIBCPP_AVAILABILITY_TO_CHARS
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000381inline _LIBCPP_INLINE_VISIBILITY to_chars_result
382to_chars(char* __first, char* __last, _Tp __value, int __base)
383{
384 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
385 return __to_chars_integral(__first, __last, __value, __base,
386 is_signed<_Tp>());
387}
388
389template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
390inline _LIBCPP_INLINE_VISIBILITY from_chars_result
391__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
392{
393 using __tl = numeric_limits<_Tp>;
Mark de Wever5f639762021-05-12 17:46:24 +0200394 decltype(__to_unsigned_like(__value)) __x;
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000395
396 bool __neg = (__first != __last && *__first == '-');
397 auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);
398 switch (__r.ec)
399 {
400 case errc::invalid_argument:
401 return {__first, __r.ec};
402 case errc::result_out_of_range:
403 return __r;
404 default:
405 break;
406 }
407
408 if (__neg)
409 {
Mark de Wever5f639762021-05-12 17:46:24 +0200410 if (__x <= __complement(__to_unsigned_like(__tl::min())))
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000411 {
412 __x = __complement(__x);
Arthur O'Dwyer79a4afc2020-12-07 21:45:29 -0500413 _VSTD::memcpy(&__value, &__x, sizeof(__x));
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000414 return __r;
415 }
416 }
417 else
418 {
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500419 if (__x <= __tl::max())
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000420 {
421 __value = __x;
422 return __r;
423 }
424 }
425
426 return {__r.ptr, errc::result_out_of_range};
427}
428
429template <typename _Tp>
430inline _LIBCPP_INLINE_VISIBILITY bool
431__in_pattern(_Tp __c)
432{
433 return '0' <= __c && __c <= '9';
434}
435
436struct _LIBCPP_HIDDEN __in_pattern_result
437{
438 bool __ok;
439 int __val;
440
441 explicit _LIBCPP_INLINE_VISIBILITY operator bool() const { return __ok; }
442};
443
444template <typename _Tp>
445inline _LIBCPP_INLINE_VISIBILITY __in_pattern_result
446__in_pattern(_Tp __c, int __base)
447{
448 if (__base <= 10)
449 return {'0' <= __c && __c < '0' + __base, __c - '0'};
450 else if (__in_pattern(__c))
451 return {true, __c - '0'};
452 else if ('a' <= __c && __c < 'a' + __base - 10)
453 return {true, __c - 'a' + 10};
454 else
455 return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10};
456}
457
458template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
459inline _LIBCPP_INLINE_VISIBILITY from_chars_result
460__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f,
461 _Ts... __args)
462{
463 auto __find_non_zero = [](_It __first, _It __last) {
464 for (; __first != __last; ++__first)
465 if (*__first != '0')
466 break;
467 return __first;
468 };
469
470 auto __p = __find_non_zero(__first, __last);
471 if (__p == __last || !__in_pattern(*__p, __args...))
472 {
473 if (__p == __first)
474 return {__first, errc::invalid_argument};
475 else
476 {
477 __value = 0;
478 return {__p, {}};
479 }
480 }
481
482 auto __r = __f(__p, __last, __value, __args...);
483 if (__r.ec == errc::result_out_of_range)
484 {
485 for (; __r.ptr != __last; ++__r.ptr)
486 {
487 if (!__in_pattern(*__r.ptr, __args...))
488 break;
489 }
490 }
491
492 return __r;
493}
494
Marshall Clowa8f29b92019-03-20 18:13:23 +0000495template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000496inline _LIBCPP_INLINE_VISIBILITY from_chars_result
497__from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
498{
499 using __tx = __itoa::__traits<_Tp>;
500 using __output_type = typename __tx::type;
501
502 return __subject_seq_combinator(
503 __first, __last, __value,
504 [](const char* __first, const char* __last,
505 _Tp& __value) -> from_chars_result {
506 __output_type __a, __b;
507 auto __p = __tx::__read(__first, __last, __a, __b);
508 if (__p == __last || !__in_pattern(*__p))
509 {
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500510 __output_type __m = numeric_limits<_Tp>::max();
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000511 if (__m >= __a && __m - __a >= __b)
512 {
513 __value = __a + __b;
514 return {__p, {}};
515 }
516 }
517 return {__p, errc::result_out_of_range};
518 });
519}
520
Marshall Clowa8f29b92019-03-20 18:13:23 +0000521template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000522inline _LIBCPP_INLINE_VISIBILITY from_chars_result
523__from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
524{
Mark de Wever5f639762021-05-12 17:46:24 +0200525 using __t = decltype(__to_unsigned_like(__value));
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000526 return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
527}
528
Marshall Clowa8f29b92019-03-20 18:13:23 +0000529template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000530inline _LIBCPP_INLINE_VISIBILITY from_chars_result
531__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
532 int __base)
533{
534 if (__base == 10)
535 return __from_chars_atoi(__first, __last, __value);
536
537 return __subject_seq_combinator(
538 __first, __last, __value,
Louis Dionneaf6be622021-07-27 17:30:47 -0400539 [](const char* __p, const char* __lastx, _Tp& __value,
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000540 int __base) -> from_chars_result {
541 using __tl = numeric_limits<_Tp>;
542 auto __digits = __tl::digits / log2f(float(__base));
543 _Tp __a = __in_pattern(*__p++, __base).__val, __b = 0;
544
Louis Dionneaf6be622021-07-27 17:30:47 -0400545 for (int __i = 1; __p != __lastx; ++__i, ++__p)
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000546 {
547 if (auto __c = __in_pattern(*__p, __base))
548 {
549 if (__i < __digits - 1)
550 __a = __a * __base + __c.__val;
551 else
552 {
553 if (!__itoa::__mul_overflowed(__a, __base, __a))
554 ++__p;
555 __b = __c.__val;
556 break;
557 }
558 }
559 else
560 break;
561 }
562
Louis Dionneaf6be622021-07-27 17:30:47 -0400563 if (__p == __lastx || !__in_pattern(*__p, __base))
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000564 {
Arthur O'Dwyer518c1842020-11-27 14:13:05 -0500565 if (__tl::max() - __a >= __b)
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000566 {
567 __value = __a + __b;
568 return {__p, {}};
569 }
570 }
571 return {__p, errc::result_out_of_range};
572 },
573 __base);
574}
575
Marshall Clowa8f29b92019-03-20 18:13:23 +0000576template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000577inline _LIBCPP_INLINE_VISIBILITY from_chars_result
578__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
579 int __base)
580{
Mark de Wever5f639762021-05-12 17:46:24 +0200581 using __t = decltype(__to_unsigned_like(__value));
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000582 return __sign_combinator(__first, __last, __value,
583 __from_chars_integral<__t>, __base);
584}
585
Marshall Clowa8f29b92019-03-20 18:13:23 +0000586template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000587inline _LIBCPP_INLINE_VISIBILITY from_chars_result
588from_chars(const char* __first, const char* __last, _Tp& __value)
589{
590 return __from_chars_atoi(__first, __last, __value);
591}
592
Marshall Clowa8f29b92019-03-20 18:13:23 +0000593template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000594inline _LIBCPP_INLINE_VISIBILITY from_chars_result
595from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
596{
597 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
598 return __from_chars_integral(__first, __last, __value, __base);
599}
600
Mark de Weverfa36ec72021-02-09 17:52:41 +0100601// Floating-point implementation starts here.
602// Unlike the other parts of charconv this is only available in C++17 and newer.
603#if _LIBCPP_STD_VER > 14
604
605_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
606to_chars_result to_chars(char* __first, char* __last, float __value);
607
608_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
609to_chars_result to_chars(char* __first, char* __last, double __value);
610
611_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
612to_chars_result to_chars(char* __first, char* __last, long double __value);
613
614_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
615to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt);
616
617_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
618to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt);
619
620_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
621to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt);
622
623_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
624to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt, int __precision);
625
626_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
627to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt, int __precision);
628
629_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
630to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt, int __precision);
631
632# endif // _LIBCPP_STD_VER > 14
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400633#endif // _LIBCPP_CXX03_LANG
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000634
635_LIBCPP_END_NAMESPACE_STD
636
Marshall Clow5598be72018-10-26 01:00:56 +0000637_LIBCPP_POP_MACROS
638
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400639#endif // _LIBCPP_CHARCONV