xref: /freebsd/contrib/llvm-project/libcxx/include/charconv (revision 7d91d6b83e74edf278dde375e6049aca833cbebd)
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
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    friend bool operator==(const to_chars_result&, const to_chars_result&) = default; // since C++20
31  };
32
33  to_chars_result to_chars(char* first, char* last, see below value,
34                           int base = 10);
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  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
80#include <__availability>
81#include <__bits>
82#include <__charconv/chars_format.h>
83#include <__charconv/from_chars_result.h>
84#include <__charconv/to_chars_result.h>
85#include <__config>
86#include <__errc>
87#include <cmath> // for log2f
88#include <cstdint>
89#include <cstdlib> // for _LIBCPP_UNREACHABLE
90#include <cstring>
91#include <limits>
92#include <type_traits>
93
94#include <__debug>
95
96#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
97#pragma GCC system_header
98#endif
99
100_LIBCPP_PUSH_MACROS
101#include <__undef_macros>
102
103_LIBCPP_BEGIN_NAMESPACE_STD
104
105namespace __itoa {
106_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;
108} // namespace __itoa
109
110#ifndef _LIBCPP_CXX03_LANG
111
112to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
113from_chars_result from_chars(const char*, const char*, bool, int = 10) = delete;
114
115namespace __itoa
116{
117
118static _LIBCPP_CONSTEXPR uint64_t __pow10_64[] = {
119    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
141static _LIBCPP_CONSTEXPR uint32_t __pow10_32[] = {
142    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
148template <typename _Tp, typename = void>
149struct _LIBCPP_HIDDEN __traits_base
150{
151    using type = uint64_t;
152
153    static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
154    {
155        auto __t = (64 - _VSTD::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
156        return __t - (__v < __pow10_64[__t]) + 1;
157    }
158
159    _LIBCPP_AVAILABILITY_TO_CHARS
160    static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
161    {
162        return __u64toa(__v, __p);
163    }
164
165    static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_64)& __pow() { return __pow10_64; }
166};
167
168template <typename _Tp>
169struct _LIBCPP_HIDDEN
170    __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))>
171{
172    using type = uint32_t;
173
174    static _LIBCPP_INLINE_VISIBILITY int __width(_Tp __v)
175    {
176        auto __t = (32 - _VSTD::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
177        return __t - (__v < __pow10_32[__t]) + 1;
178    }
179
180    _LIBCPP_AVAILABILITY_TO_CHARS
181    static _LIBCPP_INLINE_VISIBILITY char* __convert(_Tp __v, char* __p)
182    {
183        return __u32toa(__v, __p);
184    }
185
186    static _LIBCPP_INLINE_VISIBILITY decltype(__pow10_32)& __pow() { return __pow10_32; }
187};
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;
195    return __c > numeric_limits<unsigned char>::max();
196}
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;
204    return __c > numeric_limits<unsigned short>::max();
205}
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
215    bool __did = __b && (numeric_limits<_Tp>::max() / __b) < __a;
216    __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{
231    static _LIBCPP_CONSTEXPR int digits = numeric_limits<_Tp>::digits10 + 1;
232    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>
277_LIBCPP_AVAILABILITY_TO_CHARS
278inline _LIBCPP_INLINE_VISIBILITY to_chars_result
279__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
280{
281    auto __x = __to_unsigned_like(__value);
282    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>
292_LIBCPP_AVAILABILITY_TO_CHARS
293inline _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
299    if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
300        return {__tx::__convert(__value, __first), errc(0)};
301    else
302        return {__last, errc::value_too_large};
303}
304
305template <typename _Tp>
306_LIBCPP_AVAILABILITY_TO_CHARS
307inline _LIBCPP_INLINE_VISIBILITY to_chars_result
308__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
309                    true_type)
310{
311    auto __x = __to_unsigned_like(__value);
312    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>
322_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>
348_LIBCPP_AVAILABILITY_TO_CHARS
349inline _LIBCPP_INLINE_VISIBILITY to_chars_result
350__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
351                    false_type)
352{
353  if (__base == 10)
354    return __to_chars_itoa(__first, __last, __value, false_type());
355
356  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};
360
361  __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)};
369}
370
371template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
372_LIBCPP_AVAILABILITY_TO_CHARS
373inline _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
379template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
380_LIBCPP_AVAILABILITY_TO_CHARS
381inline _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>;
394    decltype(__to_unsigned_like(__value)) __x;
395
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    {
410        if (__x <= __complement(__to_unsigned_like(__tl::min())))
411        {
412            __x = __complement(__x);
413            _VSTD::memcpy(&__value, &__x, sizeof(__x));
414            return __r;
415        }
416    }
417    else
418    {
419        if (__x <= __tl::max())
420        {
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
495template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
496inline _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            {
510                __output_type __m = numeric_limits<_Tp>::max();
511                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
521template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
522inline _LIBCPP_INLINE_VISIBILITY from_chars_result
523__from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
524{
525    using __t = decltype(__to_unsigned_like(__value));
526    return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
527}
528
529template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
530inline _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,
539        [](const char* __p, const char* __lastx, _Tp& __value,
540           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
545            for (int __i = 1; __p != __lastx; ++__i, ++__p)
546            {
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
563            if (__p == __lastx || !__in_pattern(*__p, __base))
564            {
565                if (__tl::max() - __a >= __b)
566                {
567                    __value = __a + __b;
568                    return {__p, {}};
569                }
570            }
571            return {__p, errc::result_out_of_range};
572        },
573        __base);
574}
575
576template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
577inline _LIBCPP_INLINE_VISIBILITY from_chars_result
578__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
579                      int __base)
580{
581    using __t = decltype(__to_unsigned_like(__value));
582    return __sign_combinator(__first, __last, __value,
583                             __from_chars_integral<__t>, __base);
584}
585
586template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
587inline _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
593template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
594inline _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
601// 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
633#endif // _LIBCPP_CXX03_LANG
634
635_LIBCPP_END_NAMESPACE_STD
636
637_LIBCPP_POP_MACROS
638
639#endif // _LIBCPP_CHARCONV
640