1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
// Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
// Copyright (C) 2017-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file include/charconv
* This is a Standard C++ Library header.
*/
#ifndef _GLIBCXX_CHARCONV
#define _GLIBCXX_CHARCONV 1
#pragma GCC system_header
#if __cplusplus >= 201402L
#include <type_traits>
#include <limits>
#include <bit> // for __log2p1
#include <cctype> // for isdigit
#include <bits/charconv.h> // for __to_chars_len, __to_chars_10_impl
#include <bits/error_constants.h> // for std::errc
// Define when floating point is supported: #define __cpp_lib_to_chars 201611L
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/// Result type of std::to_chars
struct to_chars_result
{
char* ptr;
errc ec;
};
/// Result type of std::from_chars
struct from_chars_result
{
const char* ptr;
errc ec;
};
namespace __detail
{
template<typename _Tp>
using __integer_to_chars_result_type
= enable_if_t<__or_v<__is_signed_integer<_Tp>,
__is_unsigned_integer<_Tp>,
is_same<char, remove_cv_t<_Tp>>>,
to_chars_result>;
// Pick an unsigned type of suitable size. This is used to reduce the
// number of specializations of __to_chars_len, __to_chars etc. that
// get instantiated. For example, to_chars<char> and to_chars<short>
// and to_chars<unsigned> will all use the same code, and so will
// to_chars<long> when sizeof(int) == sizeof(long).
template<typename _Tp>
struct __to_chars_unsigned_type : __make_unsigned_selector_base
{
using _UInts = _List<unsigned int, unsigned long, unsigned long long
#if _GLIBCXX_USE_INT128
, unsigned __int128
#endif
>;
using type = typename __select<sizeof(_Tp), _UInts>::__type;
};
template<typename _Tp>
using __unsigned_least_t = typename __to_chars_unsigned_type<_Tp>::type;
// Generic implementation for arbitrary bases.
// Defined in <bits/charconv.h>.
template<typename _Tp>
constexpr unsigned
__to_chars_len(_Tp __value, int __base /* = 10 */) noexcept;
template<typename _Tp>
constexpr unsigned
__to_chars_len_2(_Tp __value) noexcept
{ return std::__log2p1(__value); }
// Generic implementation for arbitrary bases.
template<typename _Tp>
to_chars_result
__to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
{
static_assert(is_integral<_Tp>::value, "implementation bug");
static_assert(is_unsigned<_Tp>::value, "implementation bug");
to_chars_result __res;
const unsigned __len = __to_chars_len(__val, __base);
if (__builtin_expect((__last - __first) < __len, 0))
{
__res.ptr = __last;
__res.ec = errc::value_too_large;
return __res;
}
unsigned __pos = __len - 1;
static constexpr char __digits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z'
};
while (__val >= __base)
{
auto const __quo = __val / __base;
auto const __rem = __val % __base;
__first[__pos--] = __digits[__rem];
__val = __quo;
}
*__first = __digits[__val];
__res.ptr = __first + __len;
__res.ec = {};
return __res;
}
template<typename _Tp>
__integer_to_chars_result_type<_Tp>
__to_chars_16(char* __first, char* __last, _Tp __val) noexcept
{
static_assert(is_integral<_Tp>::value, "implementation bug");
static_assert(is_unsigned<_Tp>::value, "implementation bug");
to_chars_result __res;
const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
if (__builtin_expect((__last - __first) < __len, 0))
{
__res.ptr = __last;
__res.ec = errc::value_too_large;
return __res;
}
static constexpr char __digits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'
};
unsigned __pos = __len - 1;
while (__val >= 0x100)
{
auto __num = __val & 0xF;
__val >>= 4;
__first[__pos] = __digits[__num];
__num = __val & 0xF;
__val >>= 4;
__first[__pos - 1] = __digits[__num];
__pos -= 2;
}
if (__val >= 0x10)
{
const auto __num = __val & 0xF;
__val >>= 4;
__first[1] = __digits[__num];
__first[0] = __digits[__val];
}
else
__first[0] = __digits[__val];
__res.ptr = __first + __len;
__res.ec = {};
return __res;
}
template<typename _Tp>
inline __integer_to_chars_result_type<_Tp>
__to_chars_10(char* __first, char* __last, _Tp __val) noexcept
{
static_assert(is_integral<_Tp>::value, "implementation bug");
static_assert(is_unsigned<_Tp>::value, "implementation bug");
to_chars_result __res;
const unsigned __len = __to_chars_len(__val, 10);
if (__builtin_expect((__last - __first) < __len, 0))
{
__res.ptr = __last;
__res.ec = errc::value_too_large;
return __res;
}
__detail::__to_chars_10_impl(__first, __len, __val);
__res.ptr = __first + __len;
__res.ec = {};
return __res;
}
template<typename _Tp>
__integer_to_chars_result_type<_Tp>
__to_chars_8(char* __first, char* __last, _Tp __val) noexcept
{
static_assert(is_integral<_Tp>::value, "implementation bug");
static_assert(is_unsigned<_Tp>::value, "implementation bug");
to_chars_result __res;
unsigned __len;
if _GLIBCXX17_CONSTEXPR (numeric_limits<_Tp>::digits <= 16)
{
__len = __val > 077777u ? 6u
: __val > 07777u ? 5u
: __val > 0777u ? 4u
: __val > 077u ? 3u
: __val > 07u ? 2u
: 1u;
}
else
__len = (__to_chars_len_2(__val) + 2) / 3;
if (__builtin_expect((__last - __first) < __len, 0))
{
__res.ptr = __last;
__res.ec = errc::value_too_large;
return __res;
}
unsigned __pos = __len - 1;
while (__val >= 0100)
{
auto __num = __val & 7;
__val >>= 3;
__first[__pos] = '0' + __num;
__num = __val & 7;
__val >>= 3;
__first[__pos - 1] = '0' + __num;
__pos -= 2;
}
if (__val >= 010)
{
auto const __num = __val & 7;
__val >>= 3;
__first[1] = '0' + __num;
__first[0] = '0' + __val;
}
else
__first[0] = '0' + __val;
__res.ptr = __first + __len;
__res.ec = {};
return __res;
}
template<typename _Tp>
__integer_to_chars_result_type<_Tp>
__to_chars_2(char* __first, char* __last, _Tp __val) noexcept
{
static_assert(is_integral<_Tp>::value, "implementation bug");
static_assert(is_unsigned<_Tp>::value, "implementation bug");
to_chars_result __res;
const unsigned __len = __to_chars_len_2(__val);
if (__builtin_expect((__last - __first) < __len, 0))
{
__res.ptr = __last;
__res.ec = errc::value_too_large;
return __res;
}
unsigned __pos = __len - 1;
while (__pos)
{
__first[__pos--] = '0' + (__val & 1);
__val >>= 1;
}
// First digit is always '1' because __to_chars_len_2 skips
// leading zero bits and std::to_chars handles zero values
// directly.
__first[0] = '1';
__res.ptr = __first + __len;
__res.ec = {};
return __res;
}
} // namespace __detail
template<typename _Tp>
__detail::__integer_to_chars_result_type<_Tp>
__to_chars_i(char* __first, char* __last, _Tp __value, int __base = 10)
{
__glibcxx_assert(2 <= __base && __base <= 36);
using _Up = __detail::__unsigned_least_t<_Tp>;
_Up __unsigned_val = __value;
if (__value == 0 && __first != __last)
{
*__first = '0';
return { __first + 1, errc{} };
}
if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
if (__value < 0)
{
if (__builtin_expect(__first != __last, 1))
*__first++ = '-';
__unsigned_val = _Up(~__value) + _Up(1);
}
switch (__base)
{
case 16:
return __detail::__to_chars_16(__first, __last, __unsigned_val);
case 10:
return __detail::__to_chars_10(__first, __last, __unsigned_val);
case 8:
return __detail::__to_chars_8(__first, __last, __unsigned_val);
case 2:
return __detail::__to_chars_2(__first, __last, __unsigned_val);
default:
return __detail::__to_chars(__first, __last, __unsigned_val, __base);
}
}
#define _GLIBCXX_TO_CHARS(T) \
inline to_chars_result \
to_chars(char* __first, char* __last, T __value, int __base = 10) \
{ return std::__to_chars_i<T>(__first, __last, __value, __base); }
_GLIBCXX_TO_CHARS(char)
_GLIBCXX_TO_CHARS(signed char)
_GLIBCXX_TO_CHARS(unsigned char)
_GLIBCXX_TO_CHARS(signed short)
_GLIBCXX_TO_CHARS(unsigned short)
_GLIBCXX_TO_CHARS(signed int)
_GLIBCXX_TO_CHARS(unsigned int)
_GLIBCXX_TO_CHARS(signed long)
_GLIBCXX_TO_CHARS(unsigned long)
_GLIBCXX_TO_CHARS(signed long long)
_GLIBCXX_TO_CHARS(unsigned long long)
#if defined(__GLIBCXX_TYPE_INT_N_0)
_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_0)
_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_0)
#endif
#if defined(__GLIBCXX_TYPE_INT_N_1)
_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_1)
_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_1)
#endif
#if defined(__GLIBCXX_TYPE_INT_N_2)
_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_2)
_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_2)
#endif
#if defined(__GLIBCXX_TYPE_INT_N_3)
_GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_3)
_GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_3)
#endif
#undef _GLIBCXX_TO_CHARS
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3266. to_chars(bool) should be deleted
to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
namespace __detail
{
template<typename _Tp>
bool
__raise_and_add(_Tp& __val, int __base, unsigned char __c)
{
if (__builtin_mul_overflow(__val, __base, &__val)
|| __builtin_add_overflow(__val, __c, &__val))
return false;
return true;
}
/// std::from_chars implementation for integers in base 2.
template<typename _Tp>
bool
__from_chars_binary(const char*& __first, const char* __last, _Tp& __val)
{
static_assert(is_integral<_Tp>::value, "implementation bug");
static_assert(is_unsigned<_Tp>::value, "implementation bug");
const ptrdiff_t __len = __last - __first;
int __i = 0;
while (__i < __len)
{
const unsigned char __c = (unsigned)__first[__i] - '0';
if (__c < 2)
__val = (__val << 1) | __c;
else
break;
__i++;
}
__first += __i;
return __i <= numeric_limits<_Tp>::digits;
}
/// std::from_chars implementation for integers in bases 3 to 10.
template<typename _Tp>
bool
__from_chars_digit(const char*& __first, const char* __last, _Tp& __val,
int __base)
{
static_assert(is_integral<_Tp>::value, "implementation bug");
static_assert(is_unsigned<_Tp>::value, "implementation bug");
auto __matches = [__base](char __c) {
return '0' <= __c && __c <= ('0' + (__base - 1));
};
while (__first != __last)
{
const char __c = *__first;
if (__matches(__c))
{
if (!__raise_and_add(__val, __base, __c - '0'))
{
while (++__first != __last && __matches(*__first))
;
return false;
}
__first++;
}
else
return true;
}
return true;
}
constexpr unsigned char
__from_chars_alpha_to_num(char __c)
{
switch (__c)
{
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
case 'f':
case 'F':
return 15;
case 'g':
case 'G':
return 16;
case 'h':
case 'H':
return 17;
case 'i':
case 'I':
return 18;
case 'j':
case 'J':
return 19;
case 'k':
case 'K':
return 20;
case 'l':
case 'L':
return 21;
case 'm':
case 'M':
return 22;
case 'n':
case 'N':
return 23;
case 'o':
case 'O':
return 24;
case 'p':
case 'P':
return 25;
case 'q':
case 'Q':
return 26;
case 'r':
case 'R':
return 27;
case 's':
case 'S':
return 28;
case 't':
case 'T':
return 29;
case 'u':
case 'U':
return 30;
case 'v':
case 'V':
return 31;
case 'w':
case 'W':
return 32;
case 'x':
case 'X':
return 33;
case 'y':
case 'Y':
return 34;
case 'z':
case 'Z':
return 35;
}
return std::numeric_limits<unsigned char>::max();
}
/// std::from_chars implementation for integers in bases 11 to 26.
template<typename _Tp>
bool
__from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
int __base)
{
bool __valid = true;
while (__first != __last)
{
unsigned char __c = *__first;
if (std::isdigit(__c))
__c -= '0';
else
{
__c = __from_chars_alpha_to_num(__c);
if (__c >= __base)
break;
}
if (__builtin_expect(__valid, 1))
__valid = __raise_and_add(__val, __base, __c);
__first++;
}
return __valid;
}
template<typename _Tp>
using __integer_from_chars_result_type
= enable_if_t<__or_v<__is_signed_integer<_Tp>,
__is_unsigned_integer<_Tp>,
is_same<char, remove_cv_t<_Tp>>>,
from_chars_result>;
} // namespace __detail
/// std::from_chars for integral types.
template<typename _Tp>
__detail::__integer_from_chars_result_type<_Tp>
from_chars(const char* __first, const char* __last, _Tp& __value,
int __base = 10)
{
__glibcxx_assert(2 <= __base && __base <= 36);
from_chars_result __res{__first, {}};
int __sign = 1;
if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
if (__first != __last && *__first == '-')
{
__sign = -1;
++__first;
}
using _Up = __detail::__unsigned_least_t<_Tp>;
_Up __val = 0;
const auto __start = __first;
bool __valid;
if (__base == 2)
__valid = __detail::__from_chars_binary(__first, __last, __val);
else if (__base <= 10)
__valid = __detail::__from_chars_digit(__first, __last, __val, __base);
else
__valid = __detail::__from_chars_alnum(__first, __last, __val, __base);
if (__builtin_expect(__first == __start, 0))
__res.ec = errc::invalid_argument;
else
{
__res.ptr = __first;
if (!__valid)
__res.ec = errc::result_out_of_range;
else
{
if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
{
_Tp __tmp;
if (__builtin_mul_overflow(__val, __sign, &__tmp))
__res.ec = errc::result_out_of_range;
else
__value = __tmp;
}
else
{
if _GLIBCXX17_CONSTEXPR
(numeric_limits<_Up>::max() > numeric_limits<_Tp>::max())
{
if (__val > numeric_limits<_Tp>::max())
__res.ec = errc::result_out_of_range;
else
__value = __val;
}
else
__value = __val;
}
}
}
return __res;
}
/// floating-point format for primitive numerical conversion
enum class chars_format
{
scientific = 1, fixed = 2, hex = 4, general = fixed | scientific
};
constexpr chars_format
operator|(chars_format __lhs, chars_format __rhs) noexcept
{ return (chars_format)((unsigned)__lhs | (unsigned)__rhs); }
constexpr chars_format
operator&(chars_format __lhs, chars_format __rhs) noexcept
{ return (chars_format)((unsigned)__lhs & (unsigned)__rhs); }
constexpr chars_format
operator^(chars_format __lhs, chars_format __rhs) noexcept
{ return (chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
constexpr chars_format
operator~(chars_format __fmt) noexcept
{ return (chars_format)~(unsigned)__fmt; }
constexpr chars_format&
operator|=(chars_format& __lhs, chars_format __rhs) noexcept
{ return __lhs = __lhs | __rhs; }
constexpr chars_format&
operator&=(chars_format& __lhs, chars_format __rhs) noexcept
{ return __lhs = __lhs & __rhs; }
constexpr chars_format&
operator^=(chars_format& __lhs, chars_format __rhs) noexcept
{ return __lhs = __lhs ^ __rhs; }
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // C++14
#endif // _GLIBCXX_CHARCONV
|