-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathxlocnum
1678 lines (1423 loc) · 69.2 KB
/
xlocnum
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// xlocnum internal header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef _XLOCNUM_
#define _XLOCNUM_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iterator>
#include <streambuf>
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_EXTERN_C_UNLESS_PURE
_CRTIMP2_PURE long __CLRCALL_PURE_OR_CDECL _Stolx(const char*, _Out_opt_ _Deref_post_opt_valid_ char**, int, int*);
_CRTIMP2_PURE unsigned long __CLRCALL_PURE_OR_CDECL _Stoulx(
const char*, _Out_opt_ _Deref_post_opt_valid_ char**, int, int*);
_CRTIMP2_PURE long long __CLRCALL_PURE_OR_CDECL _Stollx(
const char*, _Out_opt_ _Deref_post_opt_valid_ char**, int, int*);
_CRTIMP2_PURE unsigned long long __CLRCALL_PURE_OR_CDECL _Stoullx(
const char*, _Out_opt_ _Deref_post_opt_valid_ char**, int, int*);
_END_EXTERN_C_UNLESS_PURE
_STD_BEGIN
_INLINE_VAR constexpr size_t _Max_int_dig = 32; // integer properties
inline double _Stodx_v3(const char* _Str, char** _Endptr, int* _Perr) noexcept { // convert string to double
int& _Errno_ref = errno; // Nonzero cost, pay it once
const int _Orig = _Errno_ref;
_Errno_ref = 0;
double _Val = _CSTD strtod(_Str, _Endptr);
*_Perr = _Errno_ref;
_Errno_ref = _Orig;
return _Val;
}
inline float _Stofx_v3(const char* _Str, char** _Endptr, int* _Perr) noexcept { // convert string to float
int& _Errno_ref = errno; // Nonzero cost, pay it once
const int _Orig = _Errno_ref;
_Errno_ref = 0;
float _Val = _CSTD strtof(_Str, _Endptr);
*_Perr = _Errno_ref;
_Errno_ref = _Orig;
return _Val;
}
template <class _Elem, size_t _Base_size>
size_t _Find_elem(const _Elem (&_Base)[_Base_size], const _Elem _Ch) {
// lookup _Ch in array storing NUL-terminated string _Base
// pre: _Base contains no nulls except for _Base[_Base_size - 1]
return static_cast<size_t>(_STD _Find_unchecked(_Base, _Base + (_Base_size - 1), _Ch) - _Base);
}
inline wchar_t* _Maklocwcs(const wchar_t* _Ptr) { // copy NTWCS to allocated storage
const size_t _Count = _CSTD wcslen(_Ptr) + 1;
wchar_t* _Ptrdest = static_cast<wchar_t*>(_calloc_dbg(_Count, sizeof(wchar_t), _CRT_BLOCK, __FILE__, __LINE__));
if (!_Ptrdest) {
_Xbad_alloc();
}
_CSTD wmemcpy(_Ptrdest, _Ptr, _Count);
return _Ptrdest;
}
_EXPORT_STD template <class _Elem>
class numpunct : public locale::facet { // facet for defining numeric punctuation text
private:
friend _Tidy_guard<numpunct>;
public:
static_assert(!_ENFORCE_FACET_SPECIALIZATIONS || _Is_any_of_v<_Elem, char, wchar_t>, _FACET_SPECIALIZATION_MESSAGE);
using string_type = basic_string<_Elem, char_traits<_Elem>, allocator<_Elem>>;
using char_type = _Elem;
__PURE_APPDOMAIN_GLOBAL _CRTIMP2_PURE_IMPORT static locale::id id; // unique facet id
_Elem decimal_point() const {
return do_decimal_point();
}
_Elem thousands_sep() const {
return do_thousands_sep();
}
string grouping() const {
return do_grouping();
}
string_type falsename() const {
return do_falsename();
}
string_type truename() const {
return do_truename();
}
explicit numpunct(size_t _Refs = 0) : locale::facet(_Refs) { // construct from current locale
_BEGIN_LOCINFO(_Lobj)
_Init(_Lobj);
if (_Kseparator == 0) {
_Kseparator = // NB: differs from "C" locale
_Maklocchr(',', static_cast<_Elem*>(nullptr), _Lobj._Getcvt());
}
_END_LOCINFO()
}
numpunct(const _Locinfo& _Lobj, size_t _Refs = 0, bool _Isdef = false) : locale::facet(_Refs) {
_Init(_Lobj, _Isdef);
}
static size_t _Getcat(const locale::facet** _Ppf = nullptr, const locale* _Ploc = nullptr) {
// return locale category mask and construct standard facet
if (_Ppf && !*_Ppf) {
*_Ppf = new numpunct<_Elem>(_Locinfo(_Ploc->_C_str()), 0, true);
}
return _X_NUMERIC;
}
protected:
__CLR_OR_THIS_CALL ~numpunct() noexcept override {
_Tidy();
}
numpunct(const char* _Locname, size_t _Refs = 0, bool _Isdef = false) : locale::facet(_Refs) {
_BEGIN_LOCINFO(_Lobj(_Locname))
_Init(_Lobj, _Isdef);
_END_LOCINFO()
}
template <class _Elem2>
void _Getvals(_Elem2, const lconv* _Ptr, _Locinfo::_Cvtvec _Cvt) { // get values
_Dp = _Maklocchr(_Ptr->decimal_point[0], static_cast<_Elem2*>(nullptr), _Cvt);
_Kseparator = _Maklocchr(_Ptr->thousands_sep[0], static_cast<_Elem2*>(nullptr), _Cvt);
}
void _Getvals(wchar_t, const lconv* _Ptr, _Locinfo::_Cvtvec) { // get values
_Dp = static_cast<_Elem>(_Ptr->_W_decimal_point[0]);
_Kseparator = static_cast<_Elem>(_Ptr->_W_thousands_sep[0]);
}
void _Init(const _Locinfo& _Lobj, bool _Isdef = false) { // initialize from _Lobj
const lconv* _Ptr = _Lobj._Getlconv();
_Locinfo::_Cvtvec _Cvt = _Lobj._Getcvt(); // conversion information
_Grouping = nullptr;
_Falsename = nullptr;
_Truename = nullptr;
_Tidy_guard<numpunct> _Guard{this};
_Grouping = _Maklocstr(_Isdef ? "" : _Ptr->grouping, static_cast<char*>(nullptr), _Lobj._Getcvt());
_Falsename = _Maklocstr(_Lobj._Getfalse(), static_cast<_Elem*>(nullptr), _Cvt);
_Truename = _Maklocstr(_Lobj._Gettrue(), static_cast<_Elem*>(nullptr), _Cvt);
_Guard._Target = nullptr;
if (_Isdef) { // apply defaults for required facets
// _Grouping = _Maklocstr("", static_cast<char *>(nullptr), _Cvt);
_Dp = _Maklocchr('.', static_cast<_Elem*>(nullptr), _Cvt);
_Kseparator = _Maklocchr(',', static_cast<_Elem*>(nullptr), _Cvt);
} else {
_Getvals(_Elem{}, _Ptr, _Cvt);
}
}
virtual _Elem __CLR_OR_THIS_CALL do_decimal_point() const {
return _Dp;
}
virtual _Elem __CLR_OR_THIS_CALL do_thousands_sep() const {
return _Kseparator;
}
virtual string __CLR_OR_THIS_CALL do_grouping() const {
return string{_Grouping};
}
virtual string_type __CLR_OR_THIS_CALL do_falsename() const {
return string_type{_Falsename};
}
virtual string_type __CLR_OR_THIS_CALL do_truename() const {
return string_type{_Truename};
}
private:
void _Tidy() noexcept { // free all storage
_CSTD free(const_cast<char*>(_Grouping));
_CSTD free(const_cast<_Elem*>(_Falsename));
_CSTD free(const_cast<_Elem*>(_Truename));
}
const char* _Grouping; // grouping string, "" for "C" locale
_Elem _Dp; // decimal point, '.' for "C" locale
_Elem _Kseparator; // thousands separator, '\0' for "C" locale
const _Elem* _Falsename; // name for false, "false" for "C" locale
const _Elem* _Truename; // name for true, "true" for "C" locale
};
_EXPORT_STD template <class _Elem>
class numpunct_byname : public numpunct<_Elem> { // numpunct for named locale
public:
static_assert(!_ENFORCE_FACET_SPECIALIZATIONS || _Is_any_of_v<_Elem, char, wchar_t>, _FACET_SPECIALIZATION_MESSAGE);
explicit numpunct_byname(const char* _Locname, size_t _Refs = 0)
: numpunct<_Elem>(_Locname, _Refs) {} // construct for named locale
explicit numpunct_byname(const string& _Str, size_t _Refs = 0)
: numpunct<_Elem>(_Str.c_str(), _Refs) {} // construct for named locale
protected:
__CLR_OR_THIS_CALL ~numpunct_byname() noexcept override {}
};
#if !defined(_CRTBLD) || defined(CRTDLL2) || !defined(_DLL) || defined(_M_CEE_PURE) // TRANSITION, VSO-578955
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdllimport-static-field-def"
#endif // defined(__clang__)
template <class _Elem>
__PURE_APPDOMAIN_GLOBAL locale::id numpunct<_Elem>::id;
#ifdef __clang__
#pragma clang diagnostic pop
#endif // defined(__clang__)
#endif // !defined(_CRTBLD) || defined(CRTDLL2) || !defined(_DLL) || defined(_M_CEE_PURE)
struct _Num_get_parse_result {
// For integers: negative values mean parsing failure, while the "actual" base (used by _Getifld) is ~_Base.
// Otherwise, 0, 8, 10, or 16.
// For floating-point numbers: 0 for parsing failure, otherwise 10 or 16.
int8_t _Base;
bool _Bad_grouping;
};
_EXPORT_STD extern "C++" template <class _Elem, class _InIt = istreambuf_iterator<_Elem, char_traits<_Elem>>>
class num_get : public locale::facet { // facet for converting text to encoded numbers
public:
static_assert(!_ENFORCE_FACET_SPECIALIZATIONS || _Is_any_of_v<_Elem, char, wchar_t>, _FACET_SPECIALIZATION_MESSAGE);
static size_t __CLRCALL_OR_CDECL _Getcat(const locale::facet** _Ppf = nullptr, const locale* _Ploc = nullptr) {
// return locale category mask and construct standard facet
if (_Ppf && !*_Ppf) {
*_Ppf = new num_get<_Elem, _InIt>(_Locinfo(_Ploc->_C_str()));
}
return _X_NUMERIC;
}
__PURE_APPDOMAIN_GLOBAL static locale::id id; // unique facet id
protected:
__CLR_OR_THIS_CALL ~num_get() noexcept override {}
void _Init(const _Locinfo&) {} // initialize from _Locinfo object
public:
explicit __CLR_OR_THIS_CALL num_get(size_t _Refs = 0) : locale::facet(_Refs) { // construct from current locale
_BEGIN_LOCINFO(_Lobj)
_Init(_Lobj);
_END_LOCINFO()
}
__CLR_OR_THIS_CALL num_get(const _Locinfo& _Lobj, size_t _Refs = 0) : locale::facet(_Refs) {
_Init(_Lobj);
}
using char_type = _Elem;
using iter_type = _InIt;
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
bool& _Val) const { // get bool from [_First, _Last) into _Val
return do_get(_First, _Last, _Iosbase, _State, _Val);
}
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
unsigned short& _Val) const { // get unsigned short from [_First, _Last) into _Val
return do_get(_First, _Last, _Iosbase, _State, _Val);
}
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
unsigned int& _Val) const { // get unsigned int from [_First, _Last) into _Val
return do_get(_First, _Last, _Iosbase, _State, _Val);
}
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
long& _Val) const { // get long from [_First, _Last) into _Val
return do_get(_First, _Last, _Iosbase, _State, _Val);
}
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
unsigned long& _Val) const { // get unsigned long from [_First, _Last) into _Val
return do_get(_First, _Last, _Iosbase, _State, _Val);
}
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
long long& _Val) const { // get long long from [_First, _Last) into _Val
return do_get(_First, _Last, _Iosbase, _State, _Val);
}
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
unsigned long long& _Val) const { // get unsigned long long from [_First, _Last) into _Val
return do_get(_First, _Last, _Iosbase, _State, _Val);
}
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
float& _Val) const { // get float from [_First, _Last) into _Val
return do_get(_First, _Last, _Iosbase, _State, _Val);
}
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
double& _Val) const { // get double from [_First, _Last) into _Val
return do_get(_First, _Last, _Iosbase, _State, _Val);
}
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
long double& _Val) const { // get long double from [_First, _Last) into _Val
return do_get(_First, _Last, _Iosbase, _State, _Val);
}
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
void*& _Val) const { // get void pointer from [_First, _Last) into _Val
return do_get(_First, _Last, _Iosbase, _State, _Val);
}
protected:
virtual _InIt __CLR_OR_THIS_CALL do_get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
bool& _Val) const { // get bool from [_First, _Last) into _Val
_Adl_verify_range(_First, _Last);
if (_Iosbase.flags() & ios_base::boolalpha) { // get false name or true name
const auto& _Punct_fac = _STD use_facet<numpunct<_Elem>>(_Iosbase.getloc());
basic_string<_Elem> _Str(static_cast<size_t>(1), _Elem{});
_Str += _Punct_fac.falsename();
_Str.push_back(_Elem{});
_Str += _Punct_fac.truename(); // construct "\0false\0true"
switch (_Getloctxt(_First, _Last, 2, _Str.c_str(), _Case_sensitive::_Yes)) {
case 0:
_Val = false;
break;
case 1:
_Val = true;
break;
default:
_Val = false;
_State = ios_base::failbit;
break;
}
} else { // get long value
char _Ac[_Max_int_dig];
const auto _Parse_result =
_Parse_int_with_locale(_Ac, _First, _Last, _Iosbase.flags(), _Iosbase.getloc()); // gather field
if (_Parse_result._Base < 0) {
// N4950 [facet.num.get.virtuals]/3.9:
// "zero, if the conversion function does not convert the entire field."
_Val = false;
_State = ios_base::failbit;
} else {
char* _Ep;
int _Errno;
const long _Ans = _CSTD _Stolx(_Ac, &_Ep, _Parse_result._Base, &_Errno); // convert
if (_Ep == _Ac || _Errno != 0 // N4950 [facet.num.get.virtuals]/3
|| _Parse_result._Bad_grouping) { // N4950 [facet.num.get.virtuals]/4
_Val = true;
_State = ios_base::failbit;
} else {
_Val = _Ans != 0;
if (_Ans != 0 && _Ans != 1) {
_State = ios_base::failbit;
}
}
}
}
if (_First == _Last) {
_State |= ios_base::eofbit;
}
return _First;
}
virtual _InIt __CLR_OR_THIS_CALL do_get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
unsigned short& _Val) const { // get unsigned short from [_First, _Last) into _Val
_Adl_verify_range(_First, _Last);
char _Ac[_Max_int_dig];
const auto _Parse_result =
_Parse_int_with_locale(_Ac, _First, _Last, _Iosbase.flags(), _Iosbase.getloc()); // gather field
if (_Parse_result._Base < 0) { // ditto "fails to convert the entire field"
_State = ios_base::failbit;
_Val = 0;
} else {
const bool _Minus = _Ac[0] == '-';
const char* _Digits = _Ac;
// C11 7.22.1.4/5: the sequence of characters starting with the first digit
// is interpreted as an integer constant according to the rules of 6.4.4.1
if (_Minus) { // skip over minus to start with the first digit
++_Digits;
}
char* _Ep;
int _Errno;
const unsigned long _Tmp = _CSTD _Stoulx(_Digits, &_Ep, _Parse_result._Base, &_Errno); // convert
_Val = static_cast<unsigned short>(_Tmp);
if (_Ep == _Digits || _Errno != 0 || _Tmp > USHRT_MAX) { // N4950 [facet.num.get.virtuals]/3
_State = ios_base::failbit;
_Val = USHRT_MAX;
} else if (_Minus) { // C11 7.22.1.4/5: If the subject sequence begins with a minus sign,
// the value resulting from the conversion is negated (in the return type).
_Val = static_cast<unsigned short>(0 - _Val);
}
if (_Parse_result._Bad_grouping) { // N4950 [facet.num.get.virtuals]/4
_State = ios_base::failbit;
}
}
if (_First == _Last) {
_State |= ios_base::eofbit;
}
return _First;
}
virtual _InIt __CLR_OR_THIS_CALL do_get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
unsigned int& _Val) const { // get unsigned int from [_First, _Last) into _Val
static_assert(sizeof(unsigned int) == sizeof(unsigned long),
"Bad overflow assumptions due to sizeof(unsigned int) != sizeof(unsigned long)");
unsigned long _Tmp;
_First = num_get::do_get(_First, _Last, _Iosbase, _State, _Tmp); // avoid virtual call for perf
_Val = _Tmp;
return _First;
}
virtual _InIt __CLR_OR_THIS_CALL do_get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
long& _Val) const { // get long from [_First, _Last) into _Val
_Adl_verify_range(_First, _Last);
char _Ac[_Max_int_dig];
const auto _Parse_result =
_Parse_int_with_locale(_Ac, _First, _Last, _Iosbase.flags(), _Iosbase.getloc()); // gather field
if (_Parse_result._Base < 0) { // ditto "fails to convert the entire field"
_State = ios_base::failbit;
_Val = 0;
} else {
char* _Ep;
int _Errno;
_Val = _CSTD _Stolx(_Ac, &_Ep, _Parse_result._Base, &_Errno); // convert
if (_Ep == _Ac || _Errno != 0 // N4950 [facet.num.get.virtuals]/3
|| _Parse_result._Bad_grouping) { // N4950 [facet.num.get.virtuals]/4
_State = ios_base::failbit;
}
}
if (_First == _Last) {
_State |= ios_base::eofbit;
}
return _First;
}
virtual _InIt __CLR_OR_THIS_CALL do_get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
unsigned long& _Val) const { // get unsigned long from [_First, _Last) into _Val
_Adl_verify_range(_First, _Last);
char _Ac[_Max_int_dig];
const auto _Parse_result =
_Parse_int_with_locale(_Ac, _First, _Last, _Iosbase.flags(), _Iosbase.getloc()); // gather field
if (_Parse_result._Base < 0) { // ditto "fails to convert the entire field"
_State = ios_base::failbit;
_Val = 0;
} else {
char* _Ep;
int _Errno;
_Val = _CSTD _Stoulx(_Ac, &_Ep, _Parse_result._Base, &_Errno); // convert
if (_Ep == _Ac || _Errno != 0 // N4950 [facet.num.get.virtuals]/3
|| _Parse_result._Bad_grouping) { // N4950 [facet.num.get.virtuals]/4
_State = ios_base::failbit;
}
}
if (_First == _Last) {
_State |= ios_base::eofbit;
}
return _First;
}
virtual _InIt __CLR_OR_THIS_CALL do_get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
long long& _Val) const { // get long long from [_First, _Last) into _Val
_Adl_verify_range(_First, _Last);
char _Ac[_Max_int_dig];
const auto _Parse_result =
_Parse_int_with_locale(_Ac, _First, _Last, _Iosbase.flags(), _Iosbase.getloc()); // gather field
if (_Parse_result._Base < 0) { // ditto "fails to convert the entire field"
_State = ios_base::failbit;
_Val = 0;
} else {
char* _Ep;
int _Errno;
_Val = _CSTD _Stollx(_Ac, &_Ep, _Parse_result._Base, &_Errno); // convert
if (_Ep == _Ac || _Errno != 0 // N4950 [facet.num.get.virtuals]/3
|| _Parse_result._Bad_grouping) { // N4950 [facet.num.get.virtuals]/4
_State = ios_base::failbit;
}
}
if (_First == _Last) {
_State |= ios_base::eofbit;
}
return _First;
}
virtual _InIt __CLR_OR_THIS_CALL do_get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
unsigned long long& _Val) const { // get unsigned long long from [_First, _Last) into _Val
_Adl_verify_range(_First, _Last);
char _Ac[_Max_int_dig];
const auto _Parse_result =
_Parse_int_with_locale(_Ac, _First, _Last, _Iosbase.flags(), _Iosbase.getloc()); // gather field
if (_Parse_result._Base < 0) { // ditto "fails to convert the entire field"
_State = ios_base::failbit;
_Val = 0;
} else {
int _Errno;
char* _Ep;
_Val = _CSTD _Stoullx(_Ac, &_Ep, _Parse_result._Base, &_Errno); // convert
if (_Ep == _Ac || _Errno != 0 // N4950 [facet.num.get.virtuals]/3
|| _Parse_result._Bad_grouping) { // N4950 [facet.num.get.virtuals]/4
_State = ios_base::failbit;
}
}
if (_First == _Last) {
_State |= ios_base::eofbit;
}
return _First;
}
#define _MAX_SIG_DIG_V2 768
#define _MAX_EXP_DIG 8 // for parsing floating-point numbers
// Size of char buffer used by num_get::do_get() for float/double/long double
#define _FLOATING_BUFFER_SIZE (_MAX_EXP_DIG + _MAX_SIG_DIG_V2 + 16)
virtual _InIt __CLR_OR_THIS_CALL do_get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
float& _Val) const { // get float from [_First, _Last) into _Val
_Adl_verify_range(_First, _Last);
char _Ac[_FLOATING_BUFFER_SIZE];
const auto _Parse_result =
_Parse_fp_with_locale(_Ac, _MAX_SIG_DIG_V2, _First, _Last, _Iosbase.getloc()); // gather field
if (_Parse_result._Base == 0) { // ditto "fails to convert the entire field"
_State = ios_base::failbit;
_Val = 0.0f;
} else {
int _Errno;
char* _Ep;
_Val = _STD _Stofx_v3(_Ac, &_Ep, &_Errno); // convert
if (_Ep == _Ac || _Errno != 0 // N4950 [facet.num.get.virtuals]/3
|| _Parse_result._Bad_grouping) { // N4950 [facet.num.get.virtuals]/4
_State = ios_base::failbit;
}
}
if (_First == _Last) {
_State |= ios_base::eofbit;
}
return _First;
}
virtual _InIt __CLR_OR_THIS_CALL do_get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
double& _Val) const { // get double from [_First, _Last) into _Val
_Adl_verify_range(_First, _Last);
char _Ac[_FLOATING_BUFFER_SIZE];
const auto _Parse_result =
_Parse_fp_with_locale(_Ac, _MAX_SIG_DIG_V2, _First, _Last, _Iosbase.getloc()); // gather field
if (_Parse_result._Base == 0) { // ditto "fails to convert the entire field"
_State = ios_base::failbit;
_Val = 0.0;
} else {
int _Errno;
char* _Ep;
_Val = _STD _Stodx_v3(_Ac, &_Ep, &_Errno); // convert
if (_Ep == _Ac || _Errno != 0 // N4950 [facet.num.get.virtuals]/3
|| _Parse_result._Bad_grouping) { // N4950 [facet.num.get.virtuals]/4
_State = ios_base::failbit;
}
}
if (_First == _Last) {
_State |= ios_base::eofbit;
}
return _First;
}
#undef _FLOATING_BUFFER_SIZE
#undef _MAX_EXP_DIG
virtual _InIt __CLR_OR_THIS_CALL do_get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
long double& _Val) const { // get long double from [_First, _Last) into _Val
static_assert(sizeof(double) == sizeof(long double), "Bad assumption: sizeof(double) == sizeof(long double).");
double _Result;
_First = num_get::do_get(_First, _Last, _Iosbase, _State, _Result); // avoid virtual call for perf
_Val = _Result;
return _First;
}
virtual _InIt __CLR_OR_THIS_CALL do_get(_InIt _First, _InIt _Last, ios_base& _Iosbase, ios_base::iostate& _State,
void*& _Val) const { // get void pointer from [_First, _Last) into _Val
_Adl_verify_range(_First, _Last);
char _Ac[_Max_int_dig];
const auto _Parse_result =
_Parse_int_with_locale(_Ac, _First, _Last, ios_base::hex, _Iosbase.getloc()); // gather field
if (_Parse_result._Base < 0) { // ditto "fails to convert the entire field"
_State = ios_base::failbit;
_Val = nullptr;
} else {
int _Errno;
char* _Ep;
#ifdef _WIN64
_Val = reinterpret_cast<void*>(_CSTD _Stoullx(_Ac, &_Ep, _Parse_result._Base, &_Errno));
#else // ^^^ defined(_WIN64) / !defined(_WIN64) vvv
_Val = reinterpret_cast<void*>(_CSTD _Stoulx(_Ac, &_Ep, _Parse_result._Base, &_Errno));
#endif // ^^^ !defined(_WIN64) ^^^
if (_Ep == _Ac || _Errno != 0) { // N4950 [facet.num.get.virtuals]/3
_State = ios_base::failbit;
_Val = nullptr;
}
if (_Parse_result._Bad_grouping) { // N4950 [facet.num.get.virtuals]/4
_State = ios_base::failbit;
}
}
if (_First == _Last) {
_State |= ios_base::eofbit;
}
return _First;
}
private:
template <int = 0> // TRANSITION, ABI
static _Num_get_parse_result _Parse_int_with_locale(
char* const _Ac, _InIt& _First, _InIt& _Last, ios_base::fmtflags _Basefield, const locale& _Loc) {
// get integer field from [_First, _Last) into _Ac
const auto& _Punct_fac = _STD use_facet<numpunct<_Elem>>(_Loc);
const string _Grouping = _Punct_fac.grouping();
const _Elem _Kseparator = _Grouping.empty() ? _Elem{} : _Punct_fac.thousands_sep();
constexpr int _Numget_signoff = 22;
constexpr int _Numget_xoff = 24;
static constexpr char _Src[] = "0123456789ABCDEFabcdef-+Xx";
_Elem _Atoms[sizeof(_Src)];
const ctype<_Elem>& _Ctype_fac = _STD use_facet<ctype<_Elem>>(_Loc);
_Ctype_fac.widen(_STD begin(_Src), _STD end(_Src), _Atoms);
bool _Bad_grouping = false;
// skip leading separators before the sign
if (_Kseparator != _Elem{}) {
while (_First != _Last && *_First == _Kseparator) {
++_First;
_Bad_grouping = true;
}
}
char* _Ptr = _Ac;
if (_First != _Last) {
if (*_First == _Atoms[_Numget_signoff + 1]) { // gather plus sign
*_Ptr++ = '+';
++_First;
} else if (*_First == _Atoms[_Numget_signoff]) { // gather minus sign
*_Ptr++ = '-';
++_First;
}
}
// skip leading separators before digits
if (_Kseparator != _Elem{}) {
while (_First != _Last && *_First == _Kseparator) {
++_First;
_Bad_grouping = true;
}
}
_Basefield &= ios_base::basefield;
int8_t _Base;
if (_Basefield == ios_base::oct) {
_Base = 8;
} else if (_Basefield == ios_base::hex) {
_Base = 16;
} else if (_Basefield == ios_base::_Fmtzero) {
_Base = 0;
} else {
_Base = 10;
}
bool _Seendigit = false; // seen a digit in input
bool _Nonzero = false; // seen a nonzero digit in input
if (_First != _Last && *_First == _Atoms[0]) { // leading zero, look for 0x, 0X
_Seendigit = true;
++_First;
if (_First != _Last && (*_First == _Atoms[_Numget_xoff + 1] || *_First == _Atoms[_Numget_xoff])
&& (_Base == 0 || _Base == 16)) {
_Base = 16;
_Seendigit = false;
++_First;
} else if (_Base == 0) {
_Base = 8;
}
}
const auto _Dlen = static_cast<size_t>(_Base == 0 || _Base == 10 ? 10 : _Base == 8 ? 8 : 16 + 6);
string _Groups(1, static_cast<char>(_Seendigit)); // Groups are detected in the reversed order of _Groups.
size_t _Groups_arr_idx = 0;
for (char* const _Pe = &_Ac[_Max_int_dig - 1]; _First != _Last; ++_First) { // look for digits and separators
size_t _Idx = _STD _Find_elem(_Atoms, *_First);
if (_Idx < _Dlen) { // got a digit, characterize it and add to group size
*_Ptr = _Src[_Idx];
if ((_Nonzero || *_Ptr != '0') && _Ptr < _Pe) {
++_Ptr;
_Nonzero = true;
}
_Seendigit = true;
if (_Groups[_Groups_arr_idx] != CHAR_MAX) {
++_Groups[_Groups_arr_idx];
}
} else if (_Kseparator == _Elem{} || *_First != _Kseparator) {
break; // not a group separator, done
} else if (_Groups[_Groups_arr_idx] == '\0') {
_Bad_grouping = true; // adjacent separators, fail
} else { // add a new group to _Groups string
_Groups.push_back('\0');
++_Groups_arr_idx;
}
}
if (_Groups_arr_idx != 0) {
if (_Groups[_Groups_arr_idx] > '\0') {
++_Groups_arr_idx; // add trailing group to group count
} else {
_Bad_grouping = true; // trailing separator, fail
}
}
// skip trailing separators
if (_Kseparator != _Elem{}) {
while (_First != _Last && *_First == _Kseparator) {
++_First;
_Bad_grouping = true;
}
}
const char* _Grouping_iter = _Grouping.data();
const char* const _Grouping_end = _Grouping_iter + _Grouping.size();
for (char _Current_grouping_count = '\0'; _Seendigit && !_Bad_grouping && _Groups_arr_idx > 0;) {
if (_Grouping_iter != _Grouping_end) { // keep the last value when _Grouping is exhausted
_Current_grouping_count = *_Grouping_iter; // if _Grouping is empty, '\0' is used
++_Grouping_iter;
}
--_Groups_arr_idx;
if ((_Current_grouping_count > '\0' && _Current_grouping_count != CHAR_MAX)
&& ((_Groups_arr_idx > 0 && _Groups[_Groups_arr_idx] != _Current_grouping_count)
|| (_Groups_arr_idx == 0 && _Groups[_Groups_arr_idx] > _Current_grouping_count))) {
_Bad_grouping = true; // bad group size, fail
}
// group size okay, advance to next test
}
if (!_Seendigit) {
return {static_cast<int8_t>(~_Base), false};
}
if (!_Nonzero) {
*_Ptr++ = '0'; // zero field, replace stripped zero(s)
}
*_Ptr = '\0';
return {_Base, _Bad_grouping};
}
template <int = 0> // TRANSITION, ABI
static _Num_get_parse_result _Parse_fp_with_locale(
char* const _Ac, const int _Max_sig_dig, _InIt& _First, _InIt& _Last, const locale& _Loc) {
// get floating-point field from [_First, _Last) into _Ac
char* _Ptr = _Ac;
constexpr size_t _Offset_dec_digit_end = 10;
constexpr size_t _Offset_hex_digit_end = 22;
constexpr size_t _Offset_neg_sign = 22;
constexpr size_t _Offset_pos_sign = 23;
constexpr size_t _Offset_upper_x = 24;
constexpr size_t _Offset_lower_x = 25;
constexpr size_t _Offset_upper_p = 26;
constexpr size_t _Offset_lower_p = 27;
constexpr size_t _Offset_upper_e = 14;
constexpr size_t _Offset_lower_e = 20;
static constexpr char _Src[] = "0123456789ABCDEFabcdef-+XxPp";
_Elem _Atoms[sizeof(_Src)];
const auto& _Ctype_fac = _STD use_facet<ctype<_Elem>>(_Loc);
_Ctype_fac.widen(_STD begin(_Src), _STD end(_Src), _Atoms);
const _Elem _Positive_sign = _Atoms[_Offset_pos_sign];
const _Elem _Negative_sign = _Atoms[_Offset_neg_sign];
const _Elem _Zero_wc = _Atoms[0];
const auto& _Punct_fac = _STD use_facet<numpunct<_Elem>>(_Loc);
const string _Grouping = _Punct_fac.grouping();
const _Elem _Kseparator = _Grouping.empty() ? _Elem{} : _Punct_fac.thousands_sep();
bool _Bad_grouping = false;
// skip leading separators before the sign
if (!_Grouping.empty()) {
while (_First != _Last && *_First == _Kseparator) {
++_First;
_Bad_grouping = true;
}
}
if (_First != _Last) {
if (*_First == _Positive_sign) { // gather plus sign
*_Ptr++ = '+';
++_First;
} else if (*_First == _Negative_sign) { // gather minus sign
*_Ptr++ = '-';
++_First;
}
}
*_Ptr++ = '0'; // backstop carries from sticky bit
bool _Parse_hex = false;
bool _Seendigit = false; // seen a digit in input
char _Initial_dec_leading_zero = '\0';
if (_First != _Last && *_First == _Zero_wc) {
++_First;
if (_First == _Last) { // "0" only
*_Ptr = '\0';
return {10, _Bad_grouping};
}
if (*_First == _Atoms[_Offset_lower_x] || *_First == _Atoms[_Offset_upper_x]) { // 0x or 0X
_Parse_hex = true;
++_First; // discard 0x or 0X for further parsing
*_Ptr++ = 'x';
} else {
_Seendigit = true;
++_Initial_dec_leading_zero;
}
}
bool _Has_unaccumulated_digits = false;
int _Significant = 0; // number of significant digits
ptrdiff_t _Power_of_rep_base = 0; // power of 10 or 16
const size_t _Offset_digit_end = _Parse_hex ? _Offset_hex_digit_end : _Offset_dec_digit_end;
if (_Grouping.empty()) {
for (size_t _Idx; _First != _Last && (_Idx = _STD _Find_elem(_Atoms, *_First)) < _Offset_digit_end;
_Seendigit = true, (void) ++_First) {
if (_Significant >= _Max_sig_dig) {
++_Power_of_rep_base; // just scale by 10 or 16
if (_Idx > 0) {
_Has_unaccumulated_digits = true;
}
} else if (_Idx != 0 || _Significant != 0) { // save a significant digit
*_Ptr++ = _Src[_Idx];
++_Significant;
}
}
} else {
// skip leading separators before digits
while (_First != _Last && *_First == _Kseparator) {
++_First;
_Bad_grouping = true;
}
string _Groups(1, _Initial_dec_leading_zero); // Groups are detected in the reversed order of _Groups.
size_t _Groups_arr_idx = 0;
for (; _First != _Last; ++_First) {
const size_t _Idx = _STD _Find_elem(_Atoms, *_First);
if (_Idx < _Offset_digit_end) { // got a digit, add to group size
_Seendigit = true;
if (_Significant >= _Max_sig_dig) {
++_Power_of_rep_base; // just scale by 10 or 16
if (_Idx > 0) {
_Has_unaccumulated_digits = true;
}
} else if (_Idx != 0 || _Significant != 0) { // save a significant digit
*_Ptr++ = _Src[_Idx];
++_Significant;
}
if (_Groups[_Groups_arr_idx] != CHAR_MAX) {
++_Groups[_Groups_arr_idx];
}
} else if (*_First != _Kseparator) {
break; // not a group separator, done
} else if (_Groups[_Groups_arr_idx] == '\0') {
_Bad_grouping = true; // adjacent separators, fail
} else { // add a new group to _Groups string
_Groups.push_back('\0');
++_Groups_arr_idx;
}
}
if (_Groups_arr_idx != 0) {
if (_Groups[_Groups_arr_idx] > '\0') {
++_Groups_arr_idx; // add trailing group to group count
} else {
_Bad_grouping = true; // trailing separator, fail
}
}
// skip trailing separators
while (_First != _Last && *_First == _Kseparator) {
++_First;
_Bad_grouping = true;
}
const char* _Grouping_iter = _Grouping.data();
const char* const _Grouping_end = _Grouping_iter + _Grouping.size();
char _Current_grouping_count = '\0';
while (!_Bad_grouping && _Groups_arr_idx > 0) {
if (_Grouping_iter != _Grouping_end) { // keep the last value when _Grouping is exhausted
_Current_grouping_count = *_Grouping_iter; // assign the variable at least once
++_Grouping_iter;
}
--_Groups_arr_idx;
if ((_Current_grouping_count > '\0' && _Current_grouping_count != CHAR_MAX)
&& ((_Groups_arr_idx > 0 && _Groups[_Groups_arr_idx] != _Current_grouping_count)
|| (_Groups_arr_idx == 0 && _Groups[_Groups_arr_idx] > _Current_grouping_count))) {
_Bad_grouping = true; // bad group size, fail
}
// group size okay, advance to next test
}
}
if (_Parse_hex && _Seendigit && _Significant == 0) {
// the condition is true when all of the digits after the 'x' and before the decimal point are zero
*_Ptr++ = '0'; // save at least one leading digit for hex
}
const char _Decimal_point = _CSTD localeconv()->decimal_point[0];
if (_First != _Last && *_First == _Punct_fac.decimal_point()) { // add .
*_Ptr++ = _Decimal_point;
++_First;
}
if (_Significant == 0) { // 0000. so far
for (; _First != _Last && *_First == _Zero_wc; _Seendigit = true, (void) ++_First) {
--_Power_of_rep_base; // count leading fraction zeros without storing digits into buffer
}
}
for (size_t _Idx; _First != _Last && (_Idx = _STD _Find_elem(_Atoms, *_First)) < _Offset_digit_end;
_Seendigit = true, (void) ++_First) {
if (_Significant < _Max_sig_dig) { // save a significant fraction digit
*_Ptr++ = _Src[_Idx];
++_Significant;
} else if (_Idx > 0) {
_Has_unaccumulated_digits = true; // just update _Has_unaccumulated_digits
}
}
if (_Has_unaccumulated_digits) { // increment last digit in memory of those lost
char& _Last_got_digit = _Ptr[-1] == _Decimal_point ? _Ptr[-2] : _Ptr[-1];
if (_Last_got_digit == '0' || _Last_got_digit == (_Parse_hex ? '8' : '5')) {
++_Last_got_digit;
}
}
const _Elem _Lower_exp_wc = _Atoms[_Parse_hex ? _Offset_lower_p : _Offset_lower_e]; // 'e' for dec, 'p' for hex
const _Elem _Upper_exp_wc = _Atoms[_Parse_hex ? _Offset_upper_p : _Offset_upper_e]; // 'E' for dec, 'P' for hex