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 <__assert> // all public C++ headers provide the assertion handler
81#include <__availability>
82#include <__bits>
83#include <__charconv/chars_format.h>
84#include <__charconv/from_chars_result.h>
85#include <__charconv/tables.h>
86#include <__charconv/to_chars_base_10.h>
87#include <__charconv/to_chars_result.h>
88#include <__config>
89#include <__debug>
90#include <__errc>
91#include <__utility/unreachable.h>
92#include <cmath> // for log2f
93#include <cstdint>
94#include <cstdlib>
95#include <cstring>
96#include <limits>
97#include <type_traits>
98
99#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
100#  pragma GCC system_header
101#endif
102
103_LIBCPP_PUSH_MACROS
104#include <__undef_macros>
105
106_LIBCPP_BEGIN_NAMESPACE_STD
107
108#ifndef _LIBCPP_CXX03_LANG
109
110to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
111from_chars_result from_chars(const char*, const char*, bool, int = 10) = delete;
112
113namespace __itoa
114{
115
116
117template <typename _Tp, typename = void>
118struct _LIBCPP_HIDDEN __traits_base
119{
120    using type = uint64_t;
121
122    static _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v)
123    {
124        auto __t = (64 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
125        return __t - (__v < __table<>::__pow10_64[__t]) + 1;
126    }
127
128    static _LIBCPP_HIDE_FROM_ABI char* __convert(_Tp __v, char* __p)
129    {
130        return __itoa::__base_10_u64(__v, __p);
131    }
132
133    static _LIBCPP_HIDE_FROM_ABI decltype(__table<>::__pow10_64)& __pow() { return __table<>::__pow10_64; }
134};
135
136template <typename _Tp>
137struct _LIBCPP_HIDDEN
138    __traits_base<_Tp, decltype(void(uint32_t{declval<_Tp>()}))>
139{
140    using type = uint32_t;
141
142    static _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v)
143    {
144        auto __t = (32 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
145        return __t - (__v < __table<>::__pow10_32[__t]) + 1;
146    }
147
148    static _LIBCPP_HIDE_FROM_ABI char* __convert(_Tp __v, char* __p)
149    {
150        return __itoa::__base_10_u32(__v, __p);
151    }
152
153    static _LIBCPP_HIDE_FROM_ABI decltype(__table<>::__pow10_32)& __pow() { return __table<>::__pow10_32; }
154};
155
156template <typename _Tp>
157inline _LIBCPP_HIDE_FROM_ABI bool
158__mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r)
159{
160    auto __c = __a * __b;
161    __r = __c;
162    return __c > numeric_limits<unsigned char>::max();
163}
164
165template <typename _Tp>
166inline _LIBCPP_HIDE_FROM_ABI bool
167__mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r)
168{
169    auto __c = __a * __b;
170    __r = __c;
171    return __c > numeric_limits<unsigned short>::max();
172}
173
174template <typename _Tp>
175inline _LIBCPP_HIDE_FROM_ABI bool
176__mul_overflowed(_Tp __a, _Tp __b, _Tp& __r)
177{
178    static_assert(is_unsigned<_Tp>::value, "");
179#if !defined(_LIBCPP_COMPILER_MSVC)
180    return __builtin_mul_overflow(__a, __b, &__r);
181#else
182    bool __did = __b && (numeric_limits<_Tp>::max() / __b) < __a;
183    __r = __a * __b;
184    return __did;
185#endif
186}
187
188template <typename _Tp, typename _Up>
189inline _LIBCPP_HIDE_FROM_ABI bool
190__mul_overflowed(_Tp __a, _Up __b, _Tp& __r)
191{
192    return __mul_overflowed(__a, static_cast<_Tp>(__b), __r);
193}
194
195template <typename _Tp>
196struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp>
197{
198    static constexpr int digits = numeric_limits<_Tp>::digits10 + 1;
199    using __traits_base<_Tp>::__pow;
200    using typename __traits_base<_Tp>::type;
201
202    // precondition: at least one non-zero character available
203    static _LIBCPP_HIDE_FROM_ABI char const*
204    __read(char const* __p, char const* __ep, type& __a, type& __b)
205    {
206        type __cprod[digits];
207        int __j = digits - 1;
208        int __i = digits;
209        do
210        {
211            if (!('0' <= *__p && *__p <= '9'))
212                break;
213            __cprod[--__i] = *__p++ - '0';
214        } while (__p != __ep && __i != 0);
215
216        __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1,
217                              __cprod[__i]);
218        if (__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))
219            --__p;
220        return __p;
221    }
222
223    template <typename _It1, typename _It2, class _Up>
224    static _LIBCPP_HIDE_FROM_ABI _Up
225    __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init)
226    {
227        for (; __first1 < __last1; ++__first1, ++__first2)
228            __init = __init + *__first1 * *__first2;
229        return __init;
230    }
231};
232
233}  // namespace __itoa
234
235template <typename _Tp>
236inline _LIBCPP_HIDE_FROM_ABI _Tp
237__complement(_Tp __x)
238{
239    static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
240    return _Tp(~__x + 1);
241}
242
243template <typename _Tp>
244inline _LIBCPP_HIDE_FROM_ABI to_chars_result
245__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
246{
247    auto __x = __to_unsigned_like(__value);
248    if (__value < 0 && __first != __last)
249    {
250        *__first++ = '-';
251        __x = __complement(__x);
252    }
253
254    return __to_chars_itoa(__first, __last, __x, false_type());
255}
256
257template <typename _Tp>
258inline _LIBCPP_HIDE_FROM_ABI to_chars_result
259__to_chars_itoa(char* __first, char* __last, _Tp __value, false_type)
260{
261    using __tx = __itoa::__traits<_Tp>;
262    auto __diff = __last - __first;
263
264    if (__tx::digits <= __diff || __tx::__width(__value) <= __diff)
265        return {__tx::__convert(__value, __first), errc(0)};
266    else
267        return {__last, errc::value_too_large};
268}
269
270template <typename _Tp>
271inline _LIBCPP_HIDE_FROM_ABI to_chars_result
272__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
273                    true_type)
274{
275    auto __x = __to_unsigned_like(__value);
276    if (__value < 0 && __first != __last)
277    {
278        *__first++ = '-';
279        __x = __complement(__x);
280    }
281
282    return __to_chars_integral(__first, __last, __x, __base, false_type());
283}
284
285namespace __itoa {
286
287template <unsigned _Base>
288struct _LIBCPP_HIDDEN __integral;
289
290template <>
291struct _LIBCPP_HIDDEN __integral<2> {
292  template <typename _Tp>
293  _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
294    // If value == 0 still need one digit. If the value != this has no
295    // effect since the code scans for the most significant bit set. (Note
296    // that __libcpp_clz doesn't work for 0.)
297    return numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1);
298  }
299
300  template <typename _Tp>
301  _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) {
302    ptrdiff_t __cap = __last - __first;
303    int __n = __width(__value);
304    if (__n > __cap)
305      return {__last, errc::value_too_large};
306
307    __last = __first + __n;
308    char* __p = __last;
309    const unsigned __divisor = 16;
310    while (__value > __divisor) {
311      unsigned __c = __value % __divisor;
312      __value /= __divisor;
313      __p -= 4;
314      std::memcpy(__p, &__table<>::__base_2_lut[4 * __c], 4);
315    }
316    do {
317      unsigned __c = __value % 2;
318      __value /= 2;
319      *--__p = "01"[__c];
320    } while (__value != 0);
321    return {__last, errc(0)};
322  }
323};
324
325template <>
326struct _LIBCPP_HIDDEN __integral<8> {
327  template <typename _Tp>
328  _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
329    // If value == 0 still need one digit. If the value != this has no
330    // effect since the code scans for the most significat bit set. (Note
331    // that __libcpp_clz doesn't work for 0.)
332    return ((numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1)) + 2) / 3;
333  }
334
335  template <typename _Tp>
336  _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) {
337    ptrdiff_t __cap = __last - __first;
338    int __n = __width(__value);
339    if (__n > __cap)
340      return {__last, errc::value_too_large};
341
342    __last = __first + __n;
343    char* __p = __last;
344    unsigned __divisor = 64;
345    while (__value > __divisor) {
346      unsigned __c = __value % __divisor;
347      __value /= __divisor;
348      __p -= 2;
349      std::memcpy(__p, &__table<>::__base_8_lut[2 * __c], 2);
350    }
351    do {
352      unsigned __c = __value % 8;
353      __value /= 8;
354      *--__p = "01234567"[__c];
355    } while (__value != 0);
356    return {__last, errc(0)};
357  }
358
359};
360
361template <>
362struct _LIBCPP_HIDDEN __integral<16> {
363  template <typename _Tp>
364  _LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
365    // If value == 0 still need one digit. If the value != this has no
366    // effect since the code scans for the most significat bit set. (Note
367    // that __libcpp_clz doesn't work for 0.)
368    return (numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1) + 3) / 4;
369  }
370
371  template <typename _Tp>
372  _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) {
373    ptrdiff_t __cap = __last - __first;
374    int __n = __width(__value);
375    if (__n > __cap)
376      return {__last, errc::value_too_large};
377
378    __last = __first + __n;
379    char* __p = __last;
380    unsigned __divisor = 256;
381    while (__value > __divisor) {
382      unsigned __c = __value % __divisor;
383      __value /= __divisor;
384      __p -= 2;
385      std::memcpy(__p, &__table<>::__base_16_lut[2 * __c], 2);
386    }
387    if (__first != __last)
388      do {
389        unsigned __c = __value % 16;
390        __value /= 16;
391        *--__p = "0123456789abcdef"[__c];
392      } while (__value != 0);
393    return {__last, errc(0)};
394  }
395};
396
397} // namespace __itoa
398
399template <unsigned _Base, typename _Tp,
400          typename enable_if<(sizeof(_Tp) >= sizeof(unsigned)), int>::type = 0>
401_LIBCPP_HIDE_FROM_ABI int
402__to_chars_integral_width(_Tp __value) {
403  return __itoa::__integral<_Base>::__width(__value);
404}
405
406template <unsigned _Base, typename _Tp,
407          typename enable_if<(sizeof(_Tp) < sizeof(unsigned)), int>::type = 0>
408_LIBCPP_HIDE_FROM_ABI int
409__to_chars_integral_width(_Tp __value) {
410  return std::__to_chars_integral_width<_Base>(static_cast<unsigned>(__value));
411}
412
413template <unsigned _Base, typename _Tp,
414          typename enable_if<(sizeof(_Tp) >= sizeof(unsigned)), int>::type = 0>
415_LIBCPP_HIDE_FROM_ABI to_chars_result
416__to_chars_integral(char* __first, char* __last, _Tp __value) {
417  return __itoa::__integral<_Base>::__to_chars(__first, __last, __value);
418}
419
420template <unsigned _Base, typename _Tp,
421          typename enable_if<(sizeof(_Tp) < sizeof(unsigned)), int>::type = 0>
422_LIBCPP_HIDE_FROM_ABI to_chars_result
423__to_chars_integral(char* __first, char* __last, _Tp __value) {
424  return std::__to_chars_integral<_Base>(__first, __last, static_cast<unsigned>(__value));
425}
426
427template <typename _Tp>
428_LIBCPP_HIDE_FROM_ABI int
429__to_chars_integral_width(_Tp __value, unsigned __base) {
430  _LIBCPP_ASSERT(__value >= 0, "The function requires a non-negative value.");
431
432  unsigned __base_2 = __base * __base;
433  unsigned __base_3 = __base_2 * __base;
434  unsigned __base_4 = __base_2 * __base_2;
435
436  int __r = 0;
437  while (true) {
438    if (__value < __base)
439      return __r + 1;
440    if (__value < __base_2)
441      return __r + 2;
442    if (__value < __base_3)
443      return __r + 3;
444    if (__value < __base_4)
445      return __r + 4;
446
447    __value /= __base_4;
448    __r += 4;
449  }
450
451  __libcpp_unreachable();
452}
453
454template <typename _Tp>
455inline _LIBCPP_HIDE_FROM_ABI to_chars_result
456__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
457                    false_type)
458{
459  if (__base == 10) [[likely]]
460    return __to_chars_itoa(__first, __last, __value, false_type());
461
462  switch (__base) {
463  case 2:
464    return __to_chars_integral<2>(__first, __last, __value);
465  case 8:
466    return __to_chars_integral<8>(__first, __last, __value);
467  case 16:
468    return __to_chars_integral<16>(__first, __last, __value);
469  }
470
471  ptrdiff_t __cap = __last - __first;
472  int __n = __to_chars_integral_width(__value, __base);
473  if (__n > __cap)
474    return {__last, errc::value_too_large};
475
476  __last = __first + __n;
477  char* __p = __last;
478  do {
479    unsigned __c = __value % __base;
480    __value /= __base;
481    *--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c];
482  } while (__value != 0);
483  return {__last, errc(0)};
484}
485
486template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
487inline _LIBCPP_HIDE_FROM_ABI to_chars_result
488to_chars(char* __first, char* __last, _Tp __value)
489{
490    return __to_chars_itoa(__first, __last, __value, is_signed<_Tp>());
491}
492
493template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
494inline _LIBCPP_HIDE_FROM_ABI to_chars_result
495to_chars(char* __first, char* __last, _Tp __value, int __base)
496{
497    _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
498    return __to_chars_integral(__first, __last, __value, __base,
499                               is_signed<_Tp>());
500}
501
502template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
503inline _LIBCPP_HIDE_FROM_ABI from_chars_result
504__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
505{
506    using __tl = numeric_limits<_Tp>;
507    decltype(__to_unsigned_like(__value)) __x;
508
509    bool __neg = (__first != __last && *__first == '-');
510    auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);
511    switch (__r.ec)
512    {
513    case errc::invalid_argument:
514        return {__first, __r.ec};
515    case errc::result_out_of_range:
516        return __r;
517    default:
518        break;
519    }
520
521    if (__neg)
522    {
523        if (__x <= __complement(__to_unsigned_like(__tl::min())))
524        {
525            __x = __complement(__x);
526            std::memcpy(&__value, &__x, sizeof(__x));
527            return __r;
528        }
529    }
530    else
531    {
532        if (__x <= __to_unsigned_like(__tl::max()))
533        {
534            __value = __x;
535            return __r;
536        }
537    }
538
539    return {__r.ptr, errc::result_out_of_range};
540}
541
542template <typename _Tp>
543inline _LIBCPP_HIDE_FROM_ABI bool
544__in_pattern(_Tp __c)
545{
546    return '0' <= __c && __c <= '9';
547}
548
549struct _LIBCPP_HIDDEN __in_pattern_result
550{
551    bool __ok;
552    int __val;
553
554    explicit _LIBCPP_HIDE_FROM_ABI operator bool() const { return __ok; }
555};
556
557template <typename _Tp>
558inline _LIBCPP_HIDE_FROM_ABI __in_pattern_result
559__in_pattern(_Tp __c, int __base)
560{
561    if (__base <= 10)
562        return {'0' <= __c && __c < '0' + __base, __c - '0'};
563    else if (__in_pattern(__c))
564        return {true, __c - '0'};
565    else if ('a' <= __c && __c < 'a' + __base - 10)
566        return {true, __c - 'a' + 10};
567    else
568        return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10};
569}
570
571template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
572inline _LIBCPP_HIDE_FROM_ABI from_chars_result
573__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f,
574                         _Ts... __args)
575{
576    auto __find_non_zero = [](_It __firstit, _It __lastit) {
577        for (; __firstit != __lastit; ++__firstit)
578            if (*__firstit != '0')
579                break;
580        return __firstit;
581    };
582
583    auto __p = __find_non_zero(__first, __last);
584    if (__p == __last || !__in_pattern(*__p, __args...))
585    {
586        if (__p == __first)
587            return {__first, errc::invalid_argument};
588        else
589        {
590            __value = 0;
591            return {__p, {}};
592        }
593    }
594
595    auto __r = __f(__p, __last, __value, __args...);
596    if (__r.ec == errc::result_out_of_range)
597    {
598        for (; __r.ptr != __last; ++__r.ptr)
599        {
600            if (!__in_pattern(*__r.ptr, __args...))
601                break;
602        }
603    }
604
605    return __r;
606}
607
608template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
609inline _LIBCPP_HIDE_FROM_ABI from_chars_result
610__from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
611{
612    using __tx = __itoa::__traits<_Tp>;
613    using __output_type = typename __tx::type;
614
615    return __subject_seq_combinator(
616        __first, __last, __value,
617        [](const char* _First, const char* _Last,
618           _Tp& __val) -> from_chars_result {
619            __output_type __a, __b;
620            auto __p = __tx::__read(_First, _Last, __a, __b);
621            if (__p == _Last || !__in_pattern(*__p))
622            {
623                __output_type __m = numeric_limits<_Tp>::max();
624                if (__m >= __a && __m - __a >= __b)
625                {
626                    __val = __a + __b;
627                    return {__p, {}};
628                }
629            }
630            return {__p, errc::result_out_of_range};
631        });
632}
633
634template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
635inline _LIBCPP_HIDE_FROM_ABI from_chars_result
636__from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
637{
638    using __t = decltype(__to_unsigned_like(__value));
639    return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
640}
641
642template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
643inline _LIBCPP_HIDE_FROM_ABI from_chars_result
644__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
645                      int __base)
646{
647    if (__base == 10)
648        return __from_chars_atoi(__first, __last, __value);
649
650    return __subject_seq_combinator(
651        __first, __last, __value,
652        [](const char* __p, const char* __lastp, _Tp& __val,
653           int _Base) -> from_chars_result {
654            using __tl = numeric_limits<_Tp>;
655            auto __digits = __tl::digits / log2f(float(_Base));
656            _Tp __a = __in_pattern(*__p++, _Base).__val, __b = 0;
657
658            for (int __i = 1; __p != __lastp; ++__i, ++__p)
659            {
660                if (auto __c = __in_pattern(*__p, _Base))
661                {
662                    if (__i < __digits - 1)
663                        __a = __a * _Base + __c.__val;
664                    else
665                    {
666                        if (!__itoa::__mul_overflowed(__a, _Base, __a))
667                            ++__p;
668                        __b = __c.__val;
669                        break;
670                    }
671                }
672                else
673                    break;
674            }
675
676            if (__p == __lastp || !__in_pattern(*__p, _Base))
677            {
678                if (__tl::max() - __a >= __b)
679                {
680                    __val = __a + __b;
681                    return {__p, {}};
682                }
683            }
684            return {__p, errc::result_out_of_range};
685        },
686        __base);
687}
688
689template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
690inline _LIBCPP_HIDE_FROM_ABI from_chars_result
691__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
692                      int __base)
693{
694    using __t = decltype(__to_unsigned_like(__value));
695    return __sign_combinator(__first, __last, __value,
696                             __from_chars_integral<__t>, __base);
697}
698
699template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
700inline _LIBCPP_HIDE_FROM_ABI from_chars_result
701from_chars(const char* __first, const char* __last, _Tp& __value)
702{
703    return __from_chars_atoi(__first, __last, __value);
704}
705
706template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
707inline _LIBCPP_HIDE_FROM_ABI from_chars_result
708from_chars(const char* __first, const char* __last, _Tp& __value, int __base)
709{
710    _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]");
711    return __from_chars_integral(__first, __last, __value, __base);
712}
713
714// Floating-point implementation starts here.
715// Unlike the other parts of charconv this is only available in C++17 and newer.
716#if _LIBCPP_STD_VER > 14
717
718_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
719to_chars_result to_chars(char* __first, char* __last, float __value);
720
721_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
722to_chars_result to_chars(char* __first, char* __last, double __value);
723
724_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
725to_chars_result to_chars(char* __first, char* __last, long double __value);
726
727_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
728to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt);
729
730_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
731to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt);
732
733_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
734to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt);
735
736_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
737to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt, int __precision);
738
739_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
740to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt, int __precision);
741
742_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS
743to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt, int __precision);
744
745#  endif // _LIBCPP_STD_VER > 14
746#endif // _LIBCPP_CXX03_LANG
747
748_LIBCPP_END_NAMESPACE_STD
749
750_LIBCPP_POP_MACROS
751
752#endif // _LIBCPP_CHARCONV
753