blob: c0cb9c52aa9d9e4a683c1f8ae4b7b755ea2fc804 [file] [log] [blame]
Zhihao Yuan7bf19052018-08-01 02:38:30 +00001// -*- C++ -*-
2//===------------------------------ charconv ------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_CHARCONV
12#define _LIBCPP_CHARCONV
13
14/*
15 charconv synopsis
16
17namespace std {
18
19 // floating-point format for primitive numerical conversion
20 enum class chars_format {
21 scientific = unspecified,
22 fixed = unspecified,
23 hex = unspecified,
24 general = fixed | scientific
25 };
26
27 // 23.20.2, primitive numerical output conversion
28 struct to_chars_result {
29 char* ptr;
30 errc ec;
31 };
32
33 to_chars_result to_chars(char* first, char* last, see below value,
34 int base = 10);
35
36 to_chars_result to_chars(char* first, char* last, float value);
37 to_chars_result to_chars(char* first, char* last, double value);
38 to_chars_result to_chars(char* first, char* last, long double value);
39
40 to_chars_result to_chars(char* first, char* last, float value,
41 chars_format fmt);
42 to_chars_result to_chars(char* first, char* last, double value,
43 chars_format fmt);
44 to_chars_result to_chars(char* first, char* last, long double value,
45 chars_format fmt);
46
47 to_chars_result to_chars(char* first, char* last, float value,
48 chars_format fmt, int precision);
49 to_chars_result to_chars(char* first, char* last, double value,
50 chars_format fmt, int precision);
51 to_chars_result to_chars(char* first, char* last, long double value,
52 chars_format fmt, int precision);
53
54 // 23.20.3, primitive numerical input conversion
55 struct from_chars_result {
56 const char* ptr;
57 errc ec;
58 };
59
60 from_chars_result from_chars(const char* first, const char* last,
61 see below& value, int base = 10);
62
63 from_chars_result from_chars(const char* first, const char* last,
64 float& value,
65 chars_format fmt = chars_format::general);
66 from_chars_result from_chars(const char* first, const char* last,
67 double& value,
68 chars_format fmt = chars_format::general);
69 from_chars_result from_chars(const char* first, const char* last,
70 long double& value,
71 chars_format fmt = chars_format::general);
72
73} // namespace std
74
75*/
76
77#include <__errc>
78#include <type_traits>
79#include <limits>
80#include <stdint.h>
81#include <string.h>
82#include <math.h>
83
84#include <__debug>
85
86#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
87#pragma GCC system_header
88#endif
89
90_LIBCPP_BEGIN_NAMESPACE_STD
91
Louis Dionne5254b372018-10-25 12:13:43 +000092namespace __itoa {
93_LIBCPP_FUNC_VIS char* __u64toa(uint64_t __value, char* __buffer);
94_LIBCPP_FUNC_VIS char* __u32toa(uint32_t __value, char* __buffer);
95}
96
Zhihao Yuan7bf19052018-08-01 02:38:30 +000097#if _LIBCPP_STD_VER > 11
98
99enum class _LIBCPP_ENUM_VIS chars_format
100{
101 scientific = 0x1,
102 fixed = 0x2,
103 hex = 0x4,
104 general = fixed | scientific
105};
106
107struct _LIBCPP_TYPE_VIS to_chars_result
108{
109 char* ptr;
110 errc ec;
111};
112
113struct _LIBCPP_TYPE_VIS from_chars_result
114{
115 const char* ptr;
116 errc ec;
117};
118
119void to_chars(char*, char*, bool, int = 10) = delete;
120void from_chars(const char*, const char*, bool, int = 10) = delete;
121
122namespace __itoa
123{
124
125static constexpr uint64_t __pow10_64[] = {
126 UINT64_C(0),
127 UINT64_C(10),
128 UINT64_C(100),
129 UINT64_C(1000),
130 UINT64_C(10000),
131 UINT64_C(100000),
132 UINT64_C(1000000),
133 UINT64_C(10000000),
134 UINT64_C(100000000),
135 UINT64_C(1000000000),
136 UINT64_C(10000000000),
137 UINT64_C(100000000000),
138 UINT64_C(1000000000000),
139 UINT64_C(10000000000000),
140 UINT64_C(100000000000000),
141 UINT64_C(1000000000000000),
142 UINT64_C(10000000000000000),
143 UINT64_C(100000000000000000),
144 UINT64_C(1000000000000000000),
145 UINT64_C(10000000000000000000),
146};
147
148static constexpr uint32_t __pow10_32[] = {
149 UINT32_C(0), UINT32_C(10), UINT32_C(100),
150 UINT32_C(1000), UINT32_C(10000), UINT32_C(100000),
151 UINT32_C(1000000), UINT32_C(10000000), UINT32_C(100000000),
152 UINT32_C(1000000000),
153};
154
Zhihao Yuan7bf19052018-08-01 02:38:30 +0000155template <typename _Tp, typename = void>
156struct _LIBCPP_HIDDEN __traits_base
157{
158 using type = uint64_t;
159
160#if !defined(_LIBCPP_COMPILER_MSVC)
161 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
162 {
163 auto __t = (64 - __builtin_clzll(__v | 1)) * 1233 >> 12;
164 return __t - (__v < __pow10_64[__t]) + 1;
165 }
166#endif
167
168 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
169 {
170 return __u64toa(__v, __p);
171 }
172
173 static _LIBCPP_INLINE_VISIBILITY auto& __pow() { return __pow10_64; }
174};
175
176template <typename _Tp>
177struct _LIBCPP_HIDDEN
178 __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))>
179{
180 using type = uint32_t;
181
182#if !defined(_LIBCPP_COMPILER_MSVC)
183 static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
184 {
185 auto __t = (32 - __builtin_clz(__v | 1)) * 1233 >> 12;
186 return __t - (__v < __pow10_32[__t]) + 1;
187 }
188#endif
189
190 static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
191 {
192 return __u32toa(__v, __p);
193 }
194
195 static _LIBCPP_INLINE_VISIBILITY auto& __pow() { return __pow10_32; }
196};
197
198template <typename _Tp>
199inline _LIBCPP_INLINE_VISIBILITY bool
200__mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r)
201{
202 auto __c = __a * __b;
203 __r = __c;
204 return __c > (numeric_limits<unsigned char>::max)();
205}
206
207template <typename _Tp>
208inline _LIBCPP_INLINE_VISIBILITY bool
209__mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r)
210{
211 auto __c = __a * __b;
212 __r = __c;
213 return __c > (numeric_limits<unsigned short>::max)();
214}
215
216template <typename _Tp>
217inline _LIBCPP_INLINE_VISIBILITY bool
218__mul_overflowed(_Tp __a, _Tp __b, _Tp& __r)
219{
220 static_assert(is_unsigned<_Tp>::value, "");
221#if !defined(_LIBCPP_COMPILER_MSVC)
222 return __builtin_mul_overflow(__a, __b, &__r);
223#else
224 bool __did = __b && ((numeric_limits<_Tp>::max)() / __b) < __a;
225 __r = __a * __b;
226 return __did;
227#endif
228}
229
230template <typename _Tp, typename _Up>
231inline _LIBCPP_INLINE_VISIBILITY bool
232__mul_overflowed(_Tp __a, _Up __b, _Tp& __r)
233{
234 return __mul_overflowed(__a, static_cast<_Tp>(__b), __r);
235}
236
237template <typename _Tp>
238struct _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_INLINE_VISIBILITY 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 (!('0' <= *__p && *__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 (__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_INLINE_VISIBILITY _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
277template <typename _Tp>
278inline _LIBCPP_INLINE_VISIBILITY _Tp
279__complement(_Tp __x)
280{
281 static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
282 return _Tp(~__x + 1);
283}
284
285template <typename _Tp>
286inline _LIBCPP_INLINE_VISIBILITY auto
287__to_unsigned(_Tp __x)
288{
289 return static_cast<make_unsigned_t<_Tp>>(__x);
290}
291
292template <typename _Tp>
293inline _LIBCPP_INLINE_VISIBILITY to_chars_result
294__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
295{
296 auto __x = __to_unsigned(__value);
297 if (__value < 0 && __first != __last)
298 {
299 *__first++ = '-';
300 __x = __complement(__x);
301 }
302
303 return __to_chars_itoa(__first, __last, __x, false_type());
304}
305
306template <typename _Tp>
307inline _LIBCPP_INLINE_VISIBILITY to_chars_result
308__to_chars_itoa(char* __first, char* __last, _Tp __value, false_type)
309{
310 using __tx = __itoa::__traits<_Tp>;
311 auto __diff = __last - __first;
312
313#if !defined(_LIBCPP_COMPILER_MSVC)
314 if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
315 return {__tx::__convert(__value, __first), {}};
316 else
317 return {__last, errc::value_too_large};
318#else
319 if (__tx::digits <= __diff)
320 return {__tx::__convert(__value, __first), {}};
321 else
322 {
323 char __buf[__tx::digits];
324 auto __p = __tx::__convert(__value, __buf);
325 auto __len = __p - __buf;
326 if (__len <= __diff)
327 {
328 memcpy(__first, __buf, __len);
329 return {__first + __len, {}};
330 }
331 else
332 return {__last, errc::value_too_large};
333 }
334#endif
335}
336
337template <typename _Tp>
338inline _LIBCPP_INLINE_VISIBILITY to_chars_result
339__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
340 true_type)
341{
342 auto __x = __to_unsigned(__value);
343 if (__value < 0 && __first != __last)
344 {
345 *__first++ = '-';
346 __x = __complement(__x);
347 }
348
349 return __to_chars_integral(__first, __last, __x, __base, false_type());
350}
351
352template <typename _Tp>
353inline _LIBCPP_INLINE_VISIBILITY to_chars_result
354__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
355 false_type)
356{
357 if (__base == 10)
358 return __to_chars_itoa(__first, __last, __value, false_type());
359
360 auto __p = __last;
361 while (__p != __first)
362 {
363 auto __c = __value % __base;
364 __value /= __base;
365 *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c];
366 if (__value == 0)
367 break;
368 }
369
370 auto __len = __last - __p;
371 if (__value != 0 || !__len)
372 return {__last, errc::value_too_large};
373 else
374 {
375 memmove(__first, __p, __len);
376 return {__first + __len, {}};
377 }
378}
379
380template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
381inline _LIBCPP_INLINE_VISIBILITY to_chars_result
382to_chars(char* __first, char* __last, _Tp __value)
383{
384 return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>());
385}
386
387template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
388inline _LIBCPP_INLINE_VISIBILITY to_chars_result
389to_chars(char* __first, char* __last, _Tp __value, int __base)
390{
391 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
392 return __to_chars_integral(__first, __last, __value, __base,
393 is_signed<_Tp>());
394}
395
396template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
397inline _LIBCPP_INLINE_VISIBILITY from_chars_result
398__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
399{
400 using __tl = numeric_limits<_Tp>;
401 decltype(__to_unsigned(__value)) __x;
402
403 bool __neg = (__first != __last && *__first == '-');
404 auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);
405 switch (__r.ec)
406 {
407 case errc::invalid_argument:
408 return {__first, __r.ec};
409 case errc::result_out_of_range:
410 return __r;
411 default:
412 break;
413 }
414
415 if (__neg)
416 {
417 if (__x <= __complement(__to_unsigned(__tl::min())))
418 {
419 __x = __complement(__x);
420 memcpy(&__value, &__x, sizeof(__x));
421 return __r;
422 }
423 }
424 else
425 {
426 if (__x <= (__tl::max)())
427 {
428 __value = __x;
429 return __r;
430 }
431 }
432
433 return {__r.ptr, errc::result_out_of_range};
434}
435
436template <typename _Tp>
437inline _LIBCPP_INLINE_VISIBILITY bool
438__in_pattern(_Tp __c)
439{
440 return '0' <= __c && __c <= '9';
441}
442
443struct _LIBCPP_HIDDEN __in_pattern_result
444{
445 bool __ok;
446 int __val;
447
448 explicit _LIBCPP_INLINE_VISIBILITY operator bool() const { return __ok; }
449};
450
451template <typename _Tp>
452inline _LIBCPP_INLINE_VISIBILITY __in_pattern_result
453__in_pattern(_Tp __c, int __base)
454{
455 if (__base <= 10)
456 return {'0' <= __c && __c < '0' + __base, __c - '0'};
457 else if (__in_pattern(__c))
458 return {true, __c - '0'};
459 else if ('a' <= __c && __c < 'a' + __base - 10)
460 return {true, __c - 'a' + 10};
461 else
462 return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10};
463}
464
465template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
466inline _LIBCPP_INLINE_VISIBILITY from_chars_result
467__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f,
468 _Ts... __args)
469{
470 auto __find_non_zero = [](_It __first, _It __last) {
471 for (; __first != __last; ++__first)
472 if (*__first != '0')
473 break;
474 return __first;
475 };
476
477 auto __p = __find_non_zero(__first, __last);
478 if (__p == __last || !__in_pattern(*__p, __args...))
479 {
480 if (__p == __first)
481 return {__first, errc::invalid_argument};
482 else
483 {
484 __value = 0;
485 return {__p, {}};
486 }
487 }
488
489 auto __r = __f(__p, __last, __value, __args...);
490 if (__r.ec == errc::result_out_of_range)
491 {
492 for (; __r.ptr != __last; ++__r.ptr)
493 {
494 if (!__in_pattern(*__r.ptr, __args...))
495 break;
496 }
497 }
498
499 return __r;
500}
501
502template <typename _Tp, enable_if_t<is_unsigned<_Tp>::value, int> = 0>
503inline _LIBCPP_INLINE_VISIBILITY from_chars_result
504__from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
505{
506 using __tx = __itoa::__traits<_Tp>;
507 using __output_type = typename __tx::type;
508
509 return __subject_seq_combinator(
510 __first, __last, __value,
511 [](const char* __first, const char* __last,
512 _Tp& __value) -> from_chars_result {
513 __output_type __a, __b;
514 auto __p = __tx::__read(__first, __last, __a, __b);
515 if (__p == __last || !__in_pattern(*__p))
516 {
517 __output_type __m = (numeric_limits<_Tp>::max)();
518 if (__m >= __a && __m - __a >= __b)
519 {
520 __value = __a + __b;
521 return {__p, {}};
522 }
523 }
524 return {__p, errc::result_out_of_range};
525 });
526}
527
528template <typename _Tp, enable_if_t<is_signed<_Tp>::value, int> = 0>
529inline _LIBCPP_INLINE_VISIBILITY from_chars_result
530__from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
531{
532 using __t = decltype(__to_unsigned(__value));
533 return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
534}
535
536template <typename _Tp, enable_if_t<is_unsigned<_Tp>::value, int> = 0>
537inline _LIBCPP_INLINE_VISIBILITY from_chars_result
538__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
539 int __base)
540{
541 if (__base == 10)
542 return __from_chars_atoi(__first, __last, __value);
543
544 return __subject_seq_combinator(
545 __first, __last, __value,
546 [](const char* __p, const char* __last, _Tp& __value,
547 int __base) -> from_chars_result {
548 using __tl = numeric_limits<_Tp>;
549 auto __digits = __tl::digits / log2f(float(__base));
550 _Tp __a = __in_pattern(*__p++, __base).__val, __b = 0;
551
552 for (int __i = 1; __p != __last; ++__i, ++__p)
553 {
554 if (auto __c = __in_pattern(*__p, __base))
555 {
556 if (__i < __digits - 1)
557 __a = __a * __base + __c.__val;
558 else
559 {
560 if (!__itoa::__mul_overflowed(__a, __base, __a))
561 ++__p;
562 __b = __c.__val;
563 break;
564 }
565 }
566 else
567 break;
568 }
569
570 if (__p == __last || !__in_pattern(*__p, __base))
571 {
572 if ((__tl::max)() - __a >= __b)
573 {
574 __value = __a + __b;
575 return {__p, {}};
576 }
577 }
578 return {__p, errc::result_out_of_range};
579 },
580 __base);
581}
582
583template <typename _Tp, enable_if_t<is_signed<_Tp>::value, int> = 0>
584inline _LIBCPP_INLINE_VISIBILITY from_chars_result
585__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
586 int __base)
587{
588 using __t = decltype(__to_unsigned(__value));
589 return __sign_combinator(__first, __last, __value,
590 __from_chars_integral<__t>, __base);
591}
592
593template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
594inline _LIBCPP_INLINE_VISIBILITY from_chars_result
595from_chars(const char* __first, const char* __last, _Tp& __value)
596{
597 return __from_chars_atoi(__first, __last, __value);
598}
599
600template <typename _Tp, enable_if_t<is_integral<_Tp>::value, int> = 0>
601inline _LIBCPP_INLINE_VISIBILITY from_chars_result
602from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
603{
604 _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
605 return __from_chars_integral(__first, __last, __value, __base);
606}
607
608#endif // _LIBCPP_STD_VER > 11
609
610_LIBCPP_END_NAMESPACE_STD
611
612#endif // _LIBCPP_CHARCONV