-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
xlocale
3291 lines (2671 loc) · 123 KB
/
xlocale
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
// xlocale internal header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once
#ifndef _XLOCALE_
#define _XLOCALE_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <climits>
#include <cstring>
#include <memory>
#include <typeinfo>
#include <xfacet>
#include <xlocinfo>
#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
_STD_BEGIN
template <class _Dummy>
class _Locbase { // define templatized category constants, instantiate on demand
public:
_PGLOBAL static const int collate = _M_COLLATE;
_PGLOBAL static const int ctype = _M_CTYPE;
_PGLOBAL static const int monetary = _M_MONETARY;
_PGLOBAL static const int numeric = _M_NUMERIC;
_PGLOBAL static const int time = _M_TIME;
_PGLOBAL static const int messages = _M_MESSAGES;
_PGLOBAL static const int all = _M_ALL;
_PGLOBAL static const int none = 0;
};
template <class _Dummy>
const int _Locbase<_Dummy>::collate;
template <class _Dummy>
const int _Locbase<_Dummy>::ctype;
template <class _Dummy>
const int _Locbase<_Dummy>::monetary;
template <class _Dummy>
const int _Locbase<_Dummy>::numeric;
template <class _Dummy>
const int _Locbase<_Dummy>::time;
template <class _Dummy>
const int _Locbase<_Dummy>::messages;
template <class _Dummy>
const int _Locbase<_Dummy>::all;
template <class _Dummy>
const int _Locbase<_Dummy>::none;
_EXPORT_STD template <class _Elem>
class collate;
extern "C++" struct _CRTIMP2_PURE_IMPORT _Crt_new_delete { // base class for marking allocations as CRT blocks
#ifdef _DEBUG
void* __CLRCALL_OR_CDECL operator new(size_t _Size) { // replace operator new
void* _Ptr = operator new(_Size, nothrow);
if (!_Ptr) {
_Xbad_alloc();
}
return _Ptr;
}
void* __CLRCALL_OR_CDECL operator new(size_t _Size, const nothrow_t&) noexcept { // replace nothrow operator new
return _malloc_dbg(_Size > 0 ? _Size : 1, _CRT_BLOCK, __FILE__, __LINE__);
}
void __CLRCALL_OR_CDECL operator delete(void* _Ptr) noexcept { // replace operator delete
_CSTD free(_Ptr);
}
void __CLRCALL_OR_CDECL operator delete(void* _Ptr, const nothrow_t&) noexcept { // replace nothrow operator delete
operator delete(_Ptr);
}
void* __CLRCALL_OR_CDECL operator new(size_t, void* _Ptr) noexcept { // imitate True Placement New
return _Ptr;
}
void __CLRCALL_OR_CDECL operator delete(void*, void*) noexcept {} // imitate True Placement Delete
#endif // _DEBUG
};
_EXPORT_STD extern "C++" class locale : public _Locbase<int>, public _Crt_new_delete {
public:
using category = int;
class _CRTIMP2_PURE_IMPORT id { // identifier stamp, unique for each distinct kind of facet
public:
__CLR_OR_THIS_CALL id(size_t _Val = 0) : _Id(_Val) {}
id(const id&) = delete;
id& operator=(const id&) = delete;
__CLR_OR_THIS_CALL operator size_t() { // get stamp, with lazy allocation
if (_Id == 0) { // still zero, allocate stamp
_BEGIN_LOCK(_LOCK_LOCALE)
if (_Id == 0) {
_Id = static_cast<size_t>(++_Id_cnt);
}
_END_LOCK()
}
return _Id;
}
private:
size_t _Id; // the identifier stamp
__PURE_APPDOMAIN_GLOBAL static int _Id_cnt;
};
class _Locimp;
class _CRTIMP2_PURE_IMPORT facet : public _Facet_base, public _Crt_new_delete {
// base class for all locale facets, performs reference counting
private:
friend struct _Facet_guard;
public:
static size_t __CLRCALL_OR_CDECL _Getcat(const facet** = nullptr, const locale* = nullptr) {
// get category value, or -1 if no corresponding C category
return static_cast<size_t>(-1);
}
void __CLR_OR_THIS_CALL _Incref() noexcept override { // increment use count
_MT_INCR(_Myrefs);
}
_Facet_base* __CLR_OR_THIS_CALL _Decref() noexcept override { // decrement use count
if (_MT_DECR(_Myrefs) == 0) {
return this;
}
return nullptr;
}
private:
_Atomic_counter_t _Myrefs; // the reference count
protected:
explicit __CLR_OR_THIS_CALL facet(size_t _Initrefs = 0)
: _Myrefs(static_cast<_Atomic_counter_t>(_Initrefs)) // non-atomic initialization
{}
__CLR_OR_THIS_CALL ~facet() noexcept override {}
public:
__CLR_OR_THIS_CALL facet(const facet&) = delete;
facet& __CLR_OR_THIS_CALL operator=(const facet&) = delete;
};
struct _NODISCARD _Facet_guard {
facet* _Target;
~_Facet_guard() {
if (_Target) {
delete _Target->_Decref();
}
}
};
class _CRTIMP2_PURE_IMPORT _Locimp : public facet { // reference-counted actual implementation of a locale
protected:
__CLR_OR_THIS_CALL ~_Locimp() noexcept {
_Locimp_dtor(this);
}
private:
static _Locimp* __CLRCALL_PURE_OR_CDECL _New_Locimp(bool _Transparent = false);
static _Locimp* __CLRCALL_PURE_OR_CDECL _New_Locimp(const _Locimp& _Right);
static void __CLRCALL_PURE_OR_CDECL _Locimp_dtor(_Locimp*);
static void __CLRCALL_PURE_OR_CDECL _Locimp_Addfac(_Locimp*, facet*, size_t); // add a facet
static void __CLRCALL_PURE_OR_CDECL _Locimp_ctor(_Locimp*, const _Locimp&);
friend locale;
__CLR_OR_THIS_CALL _Locimp(bool _Transparent)
: locale::facet(1), _Facetvec(nullptr), _Facetcount(0), _Catmask(none), _Xparent(_Transparent), _Name("*") {
}
__CLR_OR_THIS_CALL _Locimp(const _Locimp& _Right)
: locale::facet(1), _Facetvec(nullptr), _Facetcount(_Right._Facetcount), _Catmask(_Right._Catmask),
_Xparent(_Right._Xparent), _Name(_Right._Name.c_str()) {
_Locimp_ctor(this, _Right);
}
void __CLR_OR_THIS_CALL _Addfac(facet* _Pfacet, size_t _Id) { // add a facet
_Locimp_Addfac(this, _Pfacet, _Id);
}
static _Locimp* __CLRCALL_OR_CDECL _Makeloc(
const _Locinfo&, category, _Locimp*, const locale*); // make essential facets
static void __CLRCALL_OR_CDECL _Makewloc(
const _Locinfo&, category, _Locimp*, const locale*); // make wchar_t facets
#if defined(_NATIVE_WCHAR_T_DEFINED) && !_ENFORCE_FACET_SPECIALIZATIONS
static void __CLRCALL_OR_CDECL _Makeushloc(
const _Locinfo&, category, _Locimp*, const locale*); // make ushort facets
#endif // defined(_NATIVE_WCHAR_T_DEFINED) && !_ENFORCE_FACET_SPECIALIZATIONS
static void __CLRCALL_OR_CDECL _Makexloc(
const _Locinfo&, category, _Locimp*, const locale*); // make remaining facets
facet** _Facetvec; // pointer to vector of facets
size_t _Facetcount; // size of vector of facets
category _Catmask; // mask describing implemented categories
bool _Xparent; // true if locale is transparent
_Yarn<char> _Name; // locale name, or "*" if not known
__PURE_APPDOMAIN_GLOBAL static _Locimp* _Clocptr;
public:
_Locimp& __CLR_OR_THIS_CALL operator=(const _Locimp&) = delete;
};
template <class _Elem, class _Traits, class _Alloc>
bool operator()(
const basic_string<_Elem, _Traits, _Alloc>& _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) const {
// compare _Left and _Right strings using collate facet in locale
const auto& _Coll_fac = _STD use_facet<_STD collate<_Elem>>(*this);
const _Elem* const _Left_data = _Left.data();
const _Elem* const _Right_data = _Right.data();
return _Coll_fac.compare(_Left_data, _Left_data + _Left.size(), _Right_data, _Right_data + _Right.size()) < 0;
}
template <class _Facet>
locale combine(const locale& _Loc) const { // combine two locales
_Facet* _Facptr;
_TRY_BEGIN
_Facptr = const_cast<_Facet*>(_STD addressof(_STD use_facet<_Facet>(_Loc)));
_CATCH_ALL
_Xruntime_error("locale::combine facet missing");
_CATCH_END
_Locimp* _Newimp = _Locimp::_New_Locimp(*_Ptr);
_Newimp->_Addfac(_Facptr, _Facet::id);
_Newimp->_Catmask = none;
_Newimp->_Name = "*";
return locale(_Newimp);
}
template <class _Facet>
locale(const locale& _Loc, const _Facet* _Facptr) : _Ptr(_Locimp::_New_Locimp(*_Loc._Ptr)) {
if (_Facptr) { // replace facet
_Ptr->_Addfac(const_cast<_Facet*>(_Facptr), _Facet::id);
_Ptr->_Catmask = none;
_Ptr->_Name = "*";
}
}
locale(_Uninitialized) {} // defer construction
locale(const locale& _Right) noexcept : _Ptr(_Right._Ptr) {
_Ptr->_Incref();
}
locale() noexcept : _Ptr(_Init(true)) {}
#if !defined(MRTDLL) || !defined(_CRTBLD)
locale(const locale& _Loc, const locale& _Other, category _Cat) : _Ptr(_Locimp::_New_Locimp(*_Loc._Ptr)) {
// construct a locale by copying named facets
if (_Cat != none) { // worth adding, do it
_Facet_guard _Guard{_Ptr};
_BEGIN_LOCINFO(_Lobj)
_Locimp::_Makeloc(_Lobj, _Cat, _Ptr, &_Other);
_Lobj._Addcats(_Loc._Ptr->_Catmask, _Loc.name().c_str());
_Lobj._Addcats(_Other._Ptr->_Catmask, _Other.name().c_str());
_Ptr->_Catmask = _Loc._Ptr->_Catmask | _Other._Ptr->_Catmask;
_Ptr->_Name = _Lobj._Getname();
_END_LOCINFO()
_Guard._Target = nullptr;
}
}
private:
void _Construct(const string& _Str, category _Cat) {
// construct a locale with named facets
bool _Bad = false;
_Init();
if (_Cat != none) { // worth adding, do it
_Facet_guard _Guard{_Ptr};
_BEGIN_LOCINFO(_Lobj(_Cat, _Str.c_str()))
if (_Badname(_Lobj)) {
_Bad = true;
} else { // name okay, build the locale
_Locimp::_Makeloc(_Lobj, _Cat, _Ptr, nullptr);
_Ptr->_Catmask = _Cat;
_Ptr->_Name = _Str.c_str();
}
_END_LOCINFO()
_Guard._Target = nullptr;
}
if (_Bad) { // Don't throw within _BEGIN_LOCINFO if we can avoid it
delete _Ptr->_Decref();
_Xruntime_error("bad locale name");
}
}
public:
explicit locale(const char* _Locname, category _Cat = all) : _Ptr(_Locimp::_New_Locimp()) {
// construct a locale with named facets
// _Locname might have been returned from setlocale().
// Therefore, _Construct() takes const string&.
if (_Locname) {
_Construct(_Locname, _Cat);
return;
}
_Xruntime_error("bad locale name");
}
locale(const locale& _Loc, const char* _Locname, category _Cat) : _Ptr(_Locimp::_New_Locimp(*_Loc._Ptr)) {
// construct a locale by copying, replacing named facets
// _Locname might have been returned from setlocale().
// Therefore, _Construct() takes const string&.
if (_Locname) {
_Construct(_Locname, _Cat);
return;
}
_Xruntime_error("bad locale name");
}
explicit locale(const string& _Str, category _Cat = all) : _Ptr(_Locimp::_New_Locimp()) {
// construct a locale with named facets
_Construct(_Str, _Cat);
}
locale(const locale& _Loc, const string& _Str, category _Cat) : _Ptr(_Locimp::_New_Locimp(*_Loc._Ptr)) {
// construct a locale by copying, replacing named facets
_Construct(_Str, _Cat);
}
#endif // !MRTDLL || !_CRTBLD
~locale() noexcept {
if (_Ptr) {
delete _Ptr->_Decref();
}
}
const locale& operator=(const locale& _Right) noexcept {
if (_Ptr != _Right._Ptr) { // different implementation, point at new one
delete _Ptr->_Decref();
_Ptr = _Right._Ptr;
_Ptr->_Incref();
}
return *this;
}
string name() const {
return _Ptr ? _Ptr->_Name.c_str() : string{};
}
_Ret_z_ const char* _C_str() const noexcept {
return _Ptr ? _Ptr->_Name.c_str() : "";
}
const facet* _Getfacet(size_t _Id) const { // look up a facet in locale object
const facet* _Facptr = _Id < _Ptr->_Facetcount ? _Ptr->_Facetvec[_Id] : nullptr; // null if id off end
if (_Facptr || !_Ptr->_Xparent) {
return _Facptr; // found facet or not transparent
}
// look in current locale
locale::_Locimp* _Ptr0 = _Getgloballocale();
if (_Id < _Ptr0->_Facetcount) {
return _Ptr0->_Facetvec[_Id]; // get from current locale
}
return nullptr; // no entry in current locale
}
_NODISCARD bool operator==(const locale& _Loc) const { // compare locales for equality
return _Ptr == _Loc._Ptr || (name().compare("*") != 0 && name().compare(_Loc.name()) == 0);
}
#if !_HAS_CXX20
_NODISCARD bool operator!=(const locale& _Right) const {
return !(*this == _Right);
}
#endif // !_HAS_CXX20
static _MRTIMP2_PURE const locale& __CLRCALL_PURE_OR_CDECL classic(); // classic "C" locale
static _MRTIMP2_PURE locale __CLRCALL_PURE_OR_CDECL global(const locale&); // current locale
static _MRTIMP2_PURE locale __CLRCALL_PURE_OR_CDECL empty(); // empty (transparent) locale
private:
locale(_Locimp* _Ptrimp) : _Ptr(_Ptrimp) {}
static _MRTIMP2_PURE _Locimp* __CLRCALL_PURE_OR_CDECL _Init(bool _Do_incref = false); // initialize locale
static _MRTIMP2_PURE _Locimp* __CLRCALL_PURE_OR_CDECL _Getgloballocale();
static _MRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Setgloballocale(void*);
bool _Badname(const _Locinfo& _Lobj) { // test if name is "*"
return _CSTD strcmp(_Lobj._Getname(), "*") == 0;
}
_Locimp* _Ptr; // pointer to locale implementation object
};
template <class _Facet>
struct _Facetptr { // store pointer to lazy facet for use_facet
__PURE_APPDOMAIN_GLOBAL static const locale::facet* _Psave;
};
template <class _Facet>
__PURE_APPDOMAIN_GLOBAL const locale::facet* _Facetptr<_Facet>::_Psave = nullptr;
_EXPORT_STD template <class _Facet>
const _Facet& __CRTDECL use_facet(const locale& _Loc) { // get facet reference from locale
_BEGIN_LOCK(_LOCK_LOCALE) // the thread lock, make get atomic
const locale::facet* _Psave = _Facetptr<_Facet>::_Psave; // static pointer to lazy facet
const size_t _Id = _Facet::id;
const locale::facet* _Pf = _Loc._Getfacet(_Id);
if (!_Pf) {
if (_Psave) {
_Pf = _Psave; // lazy facet already allocated
} else if (_Facet::_Getcat(&_Psave, &_Loc) == static_cast<size_t>(-1)) {
#if _HAS_EXCEPTIONS
_Throw_bad_cast(); // lazy disallowed
#else // _HAS_EXCEPTIONS
_CSTD abort(); // lazy disallowed
#endif // _HAS_EXCEPTIONS
} else { // queue up lazy facet for destruction
auto _Pfmod = const_cast<locale::facet*>(_Psave);
unique_ptr<_Facet_base> _Psave_guard(static_cast<_Facet_base*>(_Pfmod));
#if defined(_M_CEE)
_Facet_Register_m(_Pfmod);
#else // defined(_M_CEE)
_Facet_Register(_Pfmod);
#endif // defined(_M_CEE)
_Pfmod->_Incref();
_Facetptr<_Facet>::_Psave = _Psave;
_Pf = _Psave;
(void) _Psave_guard.release();
}
}
return static_cast<const _Facet&>(*_Pf); // should be dynamic_cast
_END_LOCK()
} // end of use_facet body
template <class _Elem>
char __CRTDECL _Maklocbyte(_Elem _Char, const _Locinfo::_Cvtvec&) {
// convert _Elem to char using _Cvtvec
return static_cast<char>(static_cast<unsigned char>(_Char));
}
template <>
inline char __CRTDECL _Maklocbyte(wchar_t _Char, const _Locinfo::_Cvtvec& _Cvt) {
// convert wchar_t to char using _Cvtvec
char _Byte = '\0';
mbstate_t _Mbst1 = {};
_Wcrtomb(&_Byte, _Char, &_Mbst1, &_Cvt);
return _Byte;
}
#if defined(_NATIVE_WCHAR_T_DEFINED) && !_ENFORCE_FACET_SPECIALIZATIONS
template <>
inline char __CRTDECL _Maklocbyte(unsigned short _Char, const _Locinfo::_Cvtvec& _Cvt) {
// convert unsigned short to char using _Cvtvec
char _Byte = '\0';
mbstate_t _Mbst1 = {};
_Wcrtomb(&_Byte, static_cast<wchar_t>(_Char), &_Mbst1, &_Cvt);
return _Byte;
}
#endif // defined(_NATIVE_WCHAR_T_DEFINED) && !_ENFORCE_FACET_SPECIALIZATIONS
template <class _Elem>
_Elem __CRTDECL _Maklocchr(char _Byte, _Elem*, const _Locinfo::_Cvtvec&) {
// convert char to _Elem using _Cvtvec
return static_cast<_Elem>(static_cast<unsigned char>(_Byte));
}
template <>
inline wchar_t __CRTDECL _Maklocchr(char _Byte, wchar_t*, const _Locinfo::_Cvtvec& _Cvt) {
// convert char to wchar_t using _Cvtvec
wchar_t _Wc = L'\0';
mbstate_t _Mbst1 = {};
_Mbrtowc(&_Wc, &_Byte, 1, &_Mbst1, &_Cvt);
return _Wc;
}
#if defined(_NATIVE_WCHAR_T_DEFINED) && !_ENFORCE_FACET_SPECIALIZATIONS
template <>
inline unsigned short __CRTDECL _Maklocchr(char _Byte, unsigned short*, const _Locinfo::_Cvtvec& _Cvt) {
// convert char to unsigned short using _Cvtvec
unsigned short _Wc = 0;
mbstate_t _Mbst1 = {};
_Mbrtowc(reinterpret_cast<wchar_t*>(&_Wc), &_Byte, 1, &_Mbst1, &_Cvt);
return _Wc;
}
#endif // defined(_NATIVE_WCHAR_T_DEFINED) && !_ENFORCE_FACET_SPECIALIZATIONS
template <class _Elem>
_Elem* __CRTDECL _Maklocstr(const char* _Ptr, _Elem*, const _Locinfo::_Cvtvec&) {
// convert C string to _Elem sequence using _Cvtvec
size_t _Count = _CSTD strlen(_Ptr) + 1;
_Elem* _Ptrdest = static_cast<_Elem*>(_calloc_dbg(_Count, sizeof(_Elem), _CRT_BLOCK, __FILE__, __LINE__));
if (!_Ptrdest) {
_Xbad_alloc();
}
for (_Elem* _Ptrnext = _Ptrdest; 0 < _Count; --_Count, ++_Ptrnext, ++_Ptr) {
*_Ptrnext = static_cast<_Elem>(static_cast<unsigned char>(*_Ptr));
}
return _Ptrdest;
}
template <>
inline wchar_t* __CRTDECL _Maklocstr(const char* _Ptr, wchar_t*, const _Locinfo::_Cvtvec& _Cvt) {
// convert C string to wchar_t sequence using _Cvtvec
size_t _Count;
size_t _Count1;
size_t _Wchars;
const char* _Ptr1;
int _Bytes;
wchar_t _Wc;
mbstate_t _Mbst1 = {};
_Count1 = _CSTD strlen(_Ptr) + 1;
for (_Count = _Count1, _Wchars = 0, _Ptr1 = _Ptr; 0 < _Count; _Count -= _Bytes, _Ptr1 += _Bytes, ++_Wchars) {
if ((_Bytes = _Mbrtowc(&_Wc, _Ptr1, _Count, &_Mbst1, &_Cvt)) <= 0) {
break;
}
}
++_Wchars; // count terminating nul
wchar_t* _Ptrdest = static_cast<wchar_t*>(_calloc_dbg(_Wchars, sizeof(wchar_t), _CRT_BLOCK, __FILE__, __LINE__));
if (!_Ptrdest) {
_Xbad_alloc();
}
wchar_t* _Ptrnext = _Ptrdest;
mbstate_t _Mbst2 = {};
for (; 0 < _Wchars; _Count -= _Bytes, _Ptr += _Bytes, --_Wchars, ++_Ptrnext) {
if ((_Bytes = _Mbrtowc(_Ptrnext, _Ptr, _Count1, &_Mbst2, &_Cvt)) <= 0) {
break;
}
}
*_Ptrnext = L'\0';
return _Ptrdest;
}
#if defined(_NATIVE_WCHAR_T_DEFINED) && !_ENFORCE_FACET_SPECIALIZATIONS
template <>
inline unsigned short* __CRTDECL _Maklocstr(const char* _Ptr, unsigned short*, const _Locinfo::_Cvtvec& _Cvt) {
// convert C string to unsigned short sequence using _Cvtvec
size_t _Count;
size_t _Count1;
size_t _Wchars;
const char* _Ptr1;
int _Bytes;
unsigned short _Wc;
mbstate_t _Mbst1 = {};
_Count1 = _CSTD strlen(_Ptr) + 1;
for (_Count = _Count1, _Wchars = 0, _Ptr1 = _Ptr; 0 < _Count; _Count -= _Bytes, _Ptr1 += _Bytes, ++_Wchars) {
if ((_Bytes = _Mbrtowc(reinterpret_cast<wchar_t*>(&_Wc), _Ptr1, _Count, &_Mbst1, &_Cvt)) <= 0) {
break;
}
}
++_Wchars; // count terminating nul
wchar_t* _Ptrdest = static_cast<wchar_t*>(_calloc_dbg(_Wchars, sizeof(wchar_t), _CRT_BLOCK, __FILE__, __LINE__));
if (!_Ptrdest) {
_Xbad_alloc();
}
wchar_t* _Ptrnext = _Ptrdest;
mbstate_t _Mbst2 = {};
for (; 0 < _Wchars; _Count -= _Bytes, _Ptr += _Bytes, --_Wchars, ++_Ptrnext) {
if ((_Bytes = _Mbrtowc(_Ptrnext, _Ptr, _Count1, &_Mbst2, &_Cvt)) <= 0) {
break;
}
}
*_Ptrnext = L'\0';
return reinterpret_cast<unsigned short*>(_Ptrdest);
}
#endif // defined(_NATIVE_WCHAR_T_DEFINED) && !_ENFORCE_FACET_SPECIALIZATIONS
_EXPORT_STD extern "C++" class _CRTIMP2_PURE_IMPORT codecvt_base : public locale::facet { // base class for codecvt
public:
enum { // constants for different parse states
ok,
partial,
error,
noconv
};
using result = int;
__CLR_OR_THIS_CALL codecvt_base(size_t _Refs = 0) : locale::facet(_Refs) {}
bool __CLR_OR_THIS_CALL always_noconv() const noexcept {
// return true if conversions never change input (from codecvt)
return do_always_noconv();
}
int __CLR_OR_THIS_CALL max_length() const noexcept {
// return maximum length required for a conversion (from codecvt)
return do_max_length();
}
int __CLR_OR_THIS_CALL encoding() const noexcept {
return do_encoding();
}
__CLR_OR_THIS_CALL ~codecvt_base() noexcept {}
protected:
virtual bool __CLR_OR_THIS_CALL do_always_noconv() const noexcept {
// return true if conversions never change input (from codecvt)
return false;
}
virtual int __CLR_OR_THIS_CALL do_max_length() const noexcept {
// return maximum length required for a conversion (from codecvt)
return 1;
}
virtual int __CLR_OR_THIS_CALL do_encoding() const noexcept {
return 1; // -1 ==> state dependent, 0 ==> varying length
}
};
template <class _Ty>
_INLINE_VAR constexpr bool _Is_one_byte_char_like_v = _Is_any_of_v<remove_cv_t<_Ty>, signed char, unsigned char,
#ifdef __cpp_lib_byte
byte,
#endif // __cpp_lib_byte
#ifdef __cpp_char8_t
char8_t,
#endif // __cpp_char8_t
char>;
template <class _Elem, class _Byte>
_INLINE_VAR constexpr bool _Is_codecvt_do_always_noconv_v =
is_same_v<_Byte, _Elem> || (_Is_one_byte_char_like_v<_Byte> && _Is_one_byte_char_like_v<_Elem>);
_EXPORT_STD extern "C++" template <class _Elem, class _Byte, class _Statype>
class codecvt : public codecvt_base { // facet for converting between _Elem and _Byte sequences
public:
// Explicitly specialized below:
// codecvt<wchar_t, char, mbstate_t>
// codecvt<unsigned short, char, mbstate_t> (extension)
// codecvt<char16_t, char, mbstate_t>
// codecvt<char32_t, char, mbstate_t>
// codecvt<char16_t, char8_t, mbstate_t>
// codecvt<char32_t, char8_t, mbstate_t>
static_assert(!_ENFORCE_FACET_SPECIALIZATIONS || is_same_v<codecvt, codecvt<char, char, mbstate_t>>,
_FACET_SPECIALIZATION_MESSAGE);
using intern_type = _Elem;
using extern_type = _Byte;
using state_type = _Statype;
result __CLR_OR_THIS_CALL in(_Statype& _State, const _Byte* _First1, const _Byte* _Last1, const _Byte*& _Mid1,
_Elem* _First2, _Elem* _Last2, _Elem*& _Mid2) const { // convert bytes [_First1, _Last1) to [_First2, _Last2)
return do_in(_State, _First1, _Last1, _Mid1, _First2, _Last2, _Mid2);
}
result __CLR_OR_THIS_CALL out(_Statype& _State, const _Elem* _First1, const _Elem* _Last1, const _Elem*& _Mid1,
_Byte* _First2, _Byte* _Last2, _Byte*& _Mid2) const { // convert [_First1, _Last1) to bytes [_First2, _Last2)
return do_out(_State, _First1, _Last1, _Mid1, _First2, _Last2, _Mid2);
}
result __CLR_OR_THIS_CALL unshift(_Statype& _State, _Byte* _First2, _Byte* _Last2, _Byte*& _Mid2) const {
// generate bytes to return to default shift state
return do_unshift(_State, _First2, _Last2, _Mid2);
}
int __CLR_OR_THIS_CALL length(_Statype& _State, const _Byte* _First1, const _Byte* _Last1, size_t _Count) const {
// return p - _First1, for the largest value p in [_First1, _Last1] such that [_First1, p) successfully
// converts to at most _Count _Elems
return do_length(_State, _First1, _Last1, _Count);
}
__PURE_APPDOMAIN_GLOBAL static locale::id id; // unique facet id
explicit __CLR_OR_THIS_CALL codecvt(size_t _Refs = 0) : codecvt_base(_Refs) {
_Init(_Locinfo());
}
explicit __CLR_OR_THIS_CALL codecvt(const _Locinfo& _Lobj, size_t _Refs = 0) : codecvt_base(_Refs) {
_Init(_Lobj);
}
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 codecvt(_Locinfo(_Ploc->_C_str()));
}
return _X_CTYPE;
}
protected:
__CLR_OR_THIS_CALL ~codecvt() noexcept override {}
void __CLR_OR_THIS_CALL _Init(const _Locinfo&) {} // initialize from _Locinfo object
bool __CLR_OR_THIS_CALL do_always_noconv() const noexcept override {
// return true if conversions never change input (from codecvt)
return _Is_codecvt_do_always_noconv_v<_Elem, _Byte>;
}
virtual result __CLR_OR_THIS_CALL do_in(_Statype&, const _Byte* _First1, const _Byte* _Last1, const _Byte*& _Mid1,
_Elem* _First2, _Elem* _Last2, _Elem*& _Mid2) const { // convert bytes [_First1, _Last1) to [_First2, _Last2)
_Mid1 = _First1;
_Mid2 = _First2;
if constexpr (_Is_codecvt_do_always_noconv_v<_Elem, _Byte>) {
return noconv; // convert nothing
} else {
// types differ, copy one for one
for (; _Mid1 != _Last1; ++_Mid1, ++_Mid2) {
if (_Mid2 == _Last2) {
return partial;
}
*_Mid2 = static_cast<_Elem>(*_Mid1);
}
return ok;
}
}
virtual result __CLR_OR_THIS_CALL do_out(_Statype&, const _Elem* _First1, const _Elem* _Last1, const _Elem*& _Mid1,
_Byte* _First2, _Byte* _Last2, _Byte*& _Mid2) const { // convert [_First1, _Last1) to bytes [_First2, _Last2)
_Mid1 = _First1;
_Mid2 = _First2;
if constexpr (_Is_codecvt_do_always_noconv_v<_Elem, _Byte>) {
return noconv; // convert nothing
} else {
// types differ, copy one for one
for (; _Mid1 != _Last1; ++_Mid1, ++_Mid2) {
if (_Mid2 == _Last2) {
return partial;
}
*_Mid2 = static_cast<_Byte>(*_Mid1);
}
return ok;
}
}
virtual result __CLR_OR_THIS_CALL do_unshift(_Statype&, _Byte* _First2, _Byte*, _Byte*& _Mid2) const {
// generate bytes to return to default shift state
_Mid2 = _First2;
return noconv; // no termination required
}
virtual int __CLR_OR_THIS_CALL do_length(
_Statype&, const _Byte* _First1, const _Byte* _Last1, size_t _Count) const {
// return p - _First1, for the largest value p in [_First1, _Last1] such that [_First1, p) successfully
// converts to at most _Count _Elems
// assumes 1:1 conversion
const auto _Dist = static_cast<size_t>((_STD min)(_Last1 - _First1, ptrdiff_t{INT_MAX}));
return static_cast<int>((_STD min)(_Count, _Dist));
}
};
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdllimport-static-field-def"
#endif // __clang__
template <class _Elem, class _Byte, class _Statype>
__PURE_APPDOMAIN_GLOBAL locale::id codecvt<_Elem, _Byte, _Statype>::id;
#ifdef __clang__
#pragma clang diagnostic pop
#endif // __clang__
_EXPORT_STD /* TRANSITION, VSO-1538698 */ template <class _CvtTy, class _Byte, class _Statype>
_NODISCARD int _Codecvt_do_length(
const _CvtTy& _Cvt, _Statype& _State, const _Byte* _First1, const _Byte* _Last1, size_t _Count) {
// return p - _First1, for the largest value p in [_First1, _Last1] such that _Cvt will successfully convert
// [_First1, p) to at most _Count wide characters
_STL_DISABLE_DEPRECATED_WARNING
using _Elem = typename _CvtTy::intern_type;
_STL_RESTORE_DEPRECATED_WARNING
_Adl_verify_range(_First1, _Last1);
const auto _Old_first1 = _First1;
while (_Count > 0 && _First1 != _Last1) { // convert another wide character
const _Byte* _Mid1;
_Elem* _Mid2;
_Elem _Ch;
// test result of single wide-char conversion
_STL_DISABLE_DEPRECATED_WARNING
const auto _Result = _Cvt._CvtTy::do_in(_State, _First1, _Last1, _Mid1, &_Ch, &_Ch + 1, _Mid2);
_STL_RESTORE_DEPRECATED_WARNING
if (_Result != codecvt_base::ok) {
if (_Result == codecvt_base::noconv) {
_First1 += (_STD min)(static_cast<size_t>(_Last1 - _First1), _Count);
}
break; // error, noconv, or partial
}
if (_Mid2 == &_Ch + 1) {
--_Count; // do_in converted an output character
}
_First1 = _Mid1;
}
return static_cast<int>((_STD min)(_First1 - _Old_first1, ptrdiff_t{INT_MAX}));
}
enum _Codecvt_mode { _Consume_header = 4, _Generate_header = 2 };
template <>
class _CRTIMP2_PURE_IMPORT_UNLESS_CODECVT_ID_SATELLITE _CXX20_DEPRECATE_CODECVT_FACETS
codecvt<char16_t, char, mbstate_t> : public codecvt_base {
// facet for converting between char16_t and UTF-8 byte sequences
public:
using intern_type = char16_t;
using extern_type = char;
using state_type = mbstate_t;
result __CLR_OR_THIS_CALL in(mbstate_t& _State, const char* _First1, const char* _Last1, const char*& _Mid1,
char16_t* _First2, char16_t* _Last2, char16_t*& _Mid2) const {
// convert bytes [_First1, _Last1) to [_First2, _Last2)
return do_in(_State, _First1, _Last1, _Mid1, _First2, _Last2, _Mid2);
}
result __CLR_OR_THIS_CALL out(mbstate_t& _State, const char16_t* _First1, const char16_t* _Last1,
const char16_t*& _Mid1, char* _First2, char* _Last2, char*& _Mid2) const {
// convert [_First1, _Last1) to bytes [_First2, _Last2)
return do_out(_State, _First1, _Last1, _Mid1, _First2, _Last2, _Mid2);
}
result __CLR_OR_THIS_CALL unshift(mbstate_t& _State, char* _First2, char* _Last2, char*& _Mid2) const {
// generate bytes to return to default shift state
return do_unshift(_State, _First2, _Last2, _Mid2);
}
int __CLR_OR_THIS_CALL length(mbstate_t& _State, const char* _First1, const char* _Last1, size_t _Count) const {
// return p - _First1, for the largest value p in [_First1, _Last1] such that [_First1, p) successfully
// converts to at most _Count UTF-16 code units
return do_length(_State, _First1, _Last1, _Count);
}
_CRT_SATELLITE_CODECVT_IDS_NOIMPORT static locale::id id;
explicit __CLR_OR_THIS_CALL codecvt(size_t _Refs = 0)
: codecvt_base(_Refs), _Maxcode(0x10ffff), _Mode(_Consume_header) {
_BEGIN_LOCINFO(_Lobj)
_Init(_Lobj);
_END_LOCINFO()
}
explicit __CLR_OR_THIS_CALL codecvt(const _Locinfo& _Lobj, size_t _Refs = 0)
: codecvt_base(_Refs), _Maxcode(0x10ffff), _Mode(_Consume_header) {
_Init(_Lobj);
}
__CLR_OR_THIS_CALL codecvt(
const _Locinfo& _Lobj, unsigned long _Maxcode_arg, _Codecvt_mode _Mode_arg, size_t _Refs = 0)
: codecvt_base(_Refs), _Maxcode(_Maxcode_arg), _Mode(_Mode_arg) {
_Init(_Lobj);
}
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) {
_STL_DISABLE_DEPRECATED_WARNING
*_Ppf = new codecvt(_Locinfo(_Ploc->_C_str()));
_STL_RESTORE_DEPRECATED_WARNING
}
return _X_CTYPE;
}
protected:
__CLR_OR_THIS_CALL ~codecvt() noexcept override {}
void __CLR_OR_THIS_CALL _Init(const _Locinfo&) {} // initialize
virtual result __CLR_OR_THIS_CALL do_in(mbstate_t& _State, const char* _First1, const char* _Last1,
const char*& _Mid1, char16_t* _First2, char16_t* _Last2, char16_t*& _Mid2) const {
// convert bytes [_First1, _Last1) to [_First2, _Last2)
unsigned short* _Pstate = reinterpret_cast<unsigned short*>(&_State);
_Mid1 = _First1;
_Mid2 = _First2;
while (_Mid1 != _Last1 && _Mid2 != _Last2) { // convert a multibyte sequence
unsigned char _By = static_cast<unsigned char>(*_Mid1);
unsigned long _Ch;
int _Nextra;
int _Nskip;
if (*_Pstate > 1) {
if (_By < 0x80 || 0xc0 <= _By) {
return codecvt::error; // not continuation byte
}
// deliver second half of two-word value
++_Mid1;
*_Mid2++ = static_cast<char16_t>(*_Pstate | (_By & 0x3f));
*_Pstate = 1;
continue;
}
if (_By < 0x80u) {
_Ch = _By;
_Nextra = 0;
} else if (_By < 0xc0u) { // 0x80-0xbf not first byte
++_Mid1;
return codecvt::error;
} else if (_By < 0xe0u) {
_Ch = static_cast<unsigned long>(_By & 0x1f);
_Nextra = 1;
} else if (_By < 0xf0u) {
_Ch = static_cast<unsigned long>(_By & 0x0f);
_Nextra = 2;
} else if (_By < 0xf8u) {
_Ch = static_cast<unsigned long>(_By & 0x07);
_Nextra = 3;
} else {
_Ch = static_cast<unsigned long>(_By & 0x03);
_Nextra = _By < 0xfc ? 4 : 5;
}
_Nskip = _Nextra < 3 ? 0 : 1; // leave a byte for 2nd word
_First1 = _Mid1; // roll back point
if (_Nextra == 0) {
++_Mid1;
} else if (_Last1 - _Mid1 < _Nextra + 1 - _Nskip) {
break; // not enough input
} else {
for (++_Mid1; _Nskip < _Nextra; --_Nextra, ++_Mid1) {
if ((_By = static_cast<unsigned char>(*_Mid1)) < 0x80u || 0xc0u <= _By) {
return codecvt::error; // not continuation byte
} else {
_Ch = _Ch << 6 | (_By & 0x3f);
}
}
}
if (0 < _Nskip) {
_Ch <<= 6; // get last byte on next call
}
if ((_Maxcode < 0x10ffffu ? _Maxcode : 0x10ffffu) < _Ch) {
return codecvt::error; // value too large
}
if (0xffffu < _Ch) { // deliver first half of two-word value, save second word
unsigned short _Ch0 = static_cast<unsigned short>(0xd800 | (_Ch >> 10) - 0x0040);
*_Mid2++ = static_cast<char16_t>(_Ch0);
*_Pstate = static_cast<unsigned short>(0xdc00 | (_Ch & 0x03ff));
continue;
}
if (_Nskip != 0) {
if (_Mid1 == _Last1) { // not enough bytes, noncanonical value
_Mid1 = _First1;
break;
}
if ((_By = static_cast<unsigned char>(*_Mid1++)) < 0x80u || 0xc0u <= _By) {
return codecvt::error; // not continuation byte
}