Coverage Report

Created: 2020-02-25 14:32

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