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