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