-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
__msvc_string_view.hpp
1833 lines (1531 loc) · 73.8 KB
/
__msvc_string_view.hpp
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
// __msvc_string_view.hpp internal header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// (<string_view> without emitting non-C++17 warnings)
#ifndef __MSVC_STRING_VIEW_HPP
#define __MSVC_STRING_VIEW_HPP
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <iosfwd>
#include <xutility>
#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
#ifdef __clang__
#define _HAS_MEMCPY_MEMMOVE_INTRINSICS 1
#else // ^^^ use __builtin_memcpy and __builtin_memmove / use workaround vvv
#define _HAS_MEMCPY_MEMMOVE_INTRINSICS 0 // TRANSITION, DevCom-1046483 (MSVC) and VSO-1129974 (EDG)
#endif // ^^^ use workaround ^^^
template <class _Elem, class _Int_type>
struct _Char_traits { // properties of a string or stream element
using char_type = _Elem;
using int_type = _Int_type;
using pos_type = streampos;
using off_type = streamoff;
using state_type = _Mbstatet;
#if _HAS_CXX20
using comparison_category = strong_ordering;
#endif // _HAS_CXX20
// For copy/move, we can uniformly call memcpy/memmove (or their builtin versions) for all element types.
static _CONSTEXPR20 _Elem* copy(_Out_writes_all_(_Count) _Elem* const _First1,
_In_reads_(_Count) const _Elem* const _First2, const size_t _Count) noexcept /* strengthened */ {
// copy [_First2, _First2 + _Count) to [_First1, ...)
#if _HAS_MEMCPY_MEMMOVE_INTRINSICS
__builtin_memcpy(_First1, _First2, _Count * sizeof(_Elem));
#else // ^^^ _HAS_MEMCPY_MEMMOVE_INTRINSICS / !_HAS_MEMCPY_MEMMOVE_INTRINSICS vvv
#if _HAS_CXX20
if (_STD is_constant_evaluated()) {
// pre: [_First1, _First1 + _Count) and [_First2, _First2 + _Count) do not overlap
for (size_t _Idx = 0; _Idx != _Count; ++_Idx) {
_First1[_Idx] = _First2[_Idx];
}
return _First1;
}
#endif // _HAS_CXX20
_CSTD memcpy(_First1, _First2, _Count * sizeof(_Elem));
#endif // ^^^ !_HAS_MEMCPY_MEMMOVE_INTRINSICS ^^^
return _First1;
}
_Pre_satisfies_(_Dest_size >= _Count) static _CONSTEXPR20 _Elem* _Copy_s(_Out_writes_all_(_Dest_size)
_Elem* const _First1,
const size_t _Dest_size, _In_reads_(_Count) const _Elem* const _First2, const size_t _Count) noexcept {
// copy [_First2, _First2 + _Count) to [_First1, _First1 + _Dest_size)
_STL_VERIFY(_Count <= _Dest_size, "invalid argument");
return copy(_First1, _First2, _Count);
}
static _CONSTEXPR20 _Elem* move(_Out_writes_all_(_Count) _Elem* const _First1,
_In_reads_(_Count) const _Elem* const _First2, const size_t _Count) noexcept /* strengthened */ {
// copy [_First2, _First2 + _Count) to [_First1, ...), allowing overlap
#if _HAS_MEMCPY_MEMMOVE_INTRINSICS
__builtin_memmove(_First1, _First2, _Count * sizeof(_Elem));
#else // ^^^ _HAS_MEMCPY_MEMMOVE_INTRINSICS / !_HAS_MEMCPY_MEMMOVE_INTRINSICS vvv
#if _HAS_CXX20
if (_STD is_constant_evaluated()) {
// dest: [_First1, _First1 + _Count)
// src: [_First2, _First2 + _Count)
// We need to handle overlapping ranges.
// If _First1 is in the src range, we need a backward loop.
// Otherwise, the forward loop works (even if the back of dest overlaps the front of src).
// Usually, we would compare pointers with less-than, even though they could belong to different arrays.
// However, we're not allowed to do that during constant evaluation, so we need a linear scan for equality.
bool _Loop_forward = true;
for (const _Elem* _Src = _First2; _Src != _First2 + _Count; ++_Src) {
if (_First1 == _Src) {
_Loop_forward = false;
break;
}
}
if (_Loop_forward) {
for (size_t _Idx = 0; _Idx != _Count; ++_Idx) {
_First1[_Idx] = _First2[_Idx];
}
} else {
for (size_t _Idx = _Count; _Idx != 0; --_Idx) {
_First1[_Idx - 1] = _First2[_Idx - 1];
}
}
return _First1;
}
#endif // _HAS_CXX20
_CSTD memmove(_First1, _First2, _Count * sizeof(_Elem));
#endif // ^^^ !_HAS_MEMCPY_MEMMOVE_INTRINSICS ^^^
return _First1;
}
// For compare/length/find/assign, we can't uniformly call CRT functions (or their builtin versions).
// 8-bit: memcmp/strlen/memchr/memset; 16-bit: wmemcmp/wcslen/wmemchr/wmemset; 32-bit: nonexistent
_NODISCARD static _CONSTEXPR17 int compare(_In_reads_(_Count) const _Elem* _First1,
_In_reads_(_Count) const _Elem* _First2, size_t _Count) noexcept /* strengthened */ {
// compare [_First1, _First1 + _Count) with [_First2, ...)
for (; 0 < _Count; --_Count, ++_First1, ++_First2) {
if (*_First1 != *_First2) {
return *_First1 < *_First2 ? -1 : +1;
}
}
return 0;
}
_NODISCARD static _CONSTEXPR17 size_t length(_In_z_ const _Elem* _First) noexcept /* strengthened */ {
// find length of null-terminated sequence
size_t _Count = 0;
while (*_First != _Elem()) {
++_Count;
++_First;
}
return _Count;
}
_NODISCARD static _CONSTEXPR17 const _Elem* find(
_In_reads_(_Count) const _Elem* _First, size_t _Count, const _Elem& _Ch) noexcept /* strengthened */ {
// look for _Ch in [_First, _First + _Count)
for (; 0 < _Count; --_Count, ++_First) {
if (*_First == _Ch) {
return _First;
}
}
return nullptr;
}
static _CONSTEXPR20 _Elem* assign(
_Out_writes_all_(_Count) _Elem* const _First, size_t _Count, const _Elem _Ch) noexcept /* strengthened */ {
// assign _Count * _Ch to [_First, ...)
for (_Elem* _Next = _First; _Count > 0; --_Count, ++_Next) {
*_Next = _Ch;
}
return _First;
}
static _CONSTEXPR17 void assign(_Elem& _Left, const _Elem& _Right) noexcept {
_Left = _Right;
}
_NODISCARD static constexpr bool eq(const _Elem _Left, const _Elem _Right) noexcept {
return _Left == _Right;
}
_NODISCARD static constexpr bool lt(const _Elem _Left, const _Elem _Right) noexcept {
return _Left < _Right;
}
_NODISCARD static constexpr _Elem to_char_type(const int_type _Meta) noexcept {
return static_cast<_Elem>(_Meta);
}
_NODISCARD static constexpr int_type to_int_type(const _Elem _Ch) noexcept {
return static_cast<int_type>(_Ch);
}
_NODISCARD static constexpr bool eq_int_type(const int_type _Left, const int_type _Right) noexcept {
return _Left == _Right;
}
_NODISCARD static constexpr int_type not_eof(const int_type _Meta) noexcept {
return _Meta != eof() ? _Meta : !eof();
}
_NODISCARD static constexpr int_type eof() noexcept {
return static_cast<int_type>(EOF);
}
};
template <class _Elem>
struct _WChar_traits : private _Char_traits<_Elem, unsigned short> {
// char_traits for the char16_t-likes: char16_t, wchar_t, unsigned short
private:
using _Primary_char_traits = _Char_traits<_Elem, unsigned short>;
public:
using char_type = _Elem;
using int_type = unsigned short;
using pos_type = streampos;
using off_type = streamoff;
using state_type = mbstate_t;
#if _HAS_CXX20
using comparison_category = strong_ordering;
#endif // _HAS_CXX20
using _Primary_char_traits::_Copy_s;
using _Primary_char_traits::copy;
using _Primary_char_traits::move;
_NODISCARD static _CONSTEXPR17 int compare(_In_reads_(_Count) const _Elem* const _First1,
_In_reads_(_Count) const _Elem* const _First2, const size_t _Count) noexcept /* strengthened */ {
// compare [_First1, _First1 + _Count) with [_First2, ...)
#if _HAS_CXX17
if (_STD _Is_constant_evaluated()) {
if constexpr (is_same_v<_Elem, wchar_t>) {
return __builtin_wmemcmp(_First1, _First2, _Count);
} else {
return _Primary_char_traits::compare(_First1, _First2, _Count);
}
}
#endif // _HAS_CXX17
return _CSTD wmemcmp(
reinterpret_cast<const wchar_t*>(_First1), reinterpret_cast<const wchar_t*>(_First2), _Count);
}
_NODISCARD static _CONSTEXPR17 size_t length(_In_z_ const _Elem* _First) noexcept /* strengthened */ {
// find length of null-terminated sequence
#if _HAS_CXX17
if (_STD _Is_constant_evaluated()) {
if constexpr (is_same_v<_Elem, wchar_t>) {
return __builtin_wcslen(_First);
} else {
return _Primary_char_traits::length(_First);
}
}
#endif // _HAS_CXX17
return _CSTD wcslen(reinterpret_cast<const wchar_t*>(_First));
}
_NODISCARD static _CONSTEXPR17 const _Elem* find(
_In_reads_(_Count) const _Elem* _First, const size_t _Count, const _Elem& _Ch) noexcept /* strengthened */ {
// look for _Ch in [_First, _First + _Count)
#if _HAS_CXX17
if (_STD _Is_constant_evaluated()) {
if constexpr (is_same_v<_Elem, wchar_t>) {
return __builtin_wmemchr(_First, _Ch, _Count);
} else {
return _Primary_char_traits::find(_First, _Count, _Ch);
}
}
#endif // _HAS_CXX17
return reinterpret_cast<const _Elem*>(_CSTD wmemchr(reinterpret_cast<const wchar_t*>(_First), _Ch, _Count));
}
static _CONSTEXPR20 _Elem* assign(
_Out_writes_all_(_Count) _Elem* const _First, size_t _Count, const _Elem _Ch) noexcept /* strengthened */ {
// assign _Count * _Ch to [_First, ...)
#if _HAS_CXX20
if (_STD is_constant_evaluated()) {
return _Primary_char_traits::assign(_First, _Count, _Ch);
}
#endif // _HAS_CXX20
return reinterpret_cast<_Elem*>(_CSTD wmemset(reinterpret_cast<wchar_t*>(_First), _Ch, _Count));
}
static _CONSTEXPR17 void assign(_Elem& _Left, const _Elem& _Right) noexcept {
#if _HAS_CXX20
if (_STD is_constant_evaluated()) {
return _Primary_char_traits::assign(_Left, _Right);
}
#endif // _HAS_CXX20
_Left = _Right;
}
_NODISCARD static constexpr bool eq(const _Elem _Left, const _Elem _Right) noexcept {
return _Left == _Right;
}
_NODISCARD static constexpr bool lt(const _Elem _Left, const _Elem _Right) noexcept {
return _Left < _Right;
}
_NODISCARD static constexpr _Elem to_char_type(const int_type _Meta) noexcept {
return _Meta;
}
_NODISCARD static constexpr int_type to_int_type(const _Elem _Ch) noexcept {
return _Ch;
}
_NODISCARD static constexpr bool eq_int_type(const int_type _Left, const int_type _Right) noexcept {
return _Left == _Right;
}
_NODISCARD static constexpr int_type not_eof(const int_type _Meta) noexcept {
return _Meta != eof() ? _Meta : static_cast<int_type>(!eof());
}
_NODISCARD static constexpr int_type eof() noexcept {
return WEOF;
}
};
_EXPORT_STD template <class _Elem>
struct char_traits : _Char_traits<_Elem, long> {}; // properties of a string or stream unknown element
template <>
struct char_traits<char16_t> : _WChar_traits<char16_t> {};
template <>
struct char_traits<char32_t> : _Char_traits<char32_t, unsigned int> {};
template <>
struct char_traits<wchar_t> : _WChar_traits<wchar_t> {};
#ifdef _CRTBLD
template <>
struct char_traits<unsigned short> : _WChar_traits<unsigned short> {};
#endif // defined(_CRTBLD)
#if defined(__cpp_char8_t) && !defined(__clang__) && !defined(__EDG__)
#define _HAS_U8_INTRINSICS 1
#else // ^^^ Use intrinsics for char8_t / don't use said intrinsics vvv
#define _HAS_U8_INTRINSICS 0
#endif // Detect u8 intrinsics
template <class _Elem, class _Int_type>
struct _Narrow_char_traits : private _Char_traits<_Elem, _Int_type> {
// Implement char_traits for narrow character types char and char8_t
private:
using _Primary_char_traits = _Char_traits<_Elem, _Int_type>;
public:
using char_type = _Elem;
using int_type = _Int_type;
using pos_type = streampos;
using off_type = streamoff;
using state_type = mbstate_t;
#if _HAS_CXX20
using comparison_category = strong_ordering;
#endif // _HAS_CXX20
using _Primary_char_traits::_Copy_s;
using _Primary_char_traits::copy;
using _Primary_char_traits::move;
_NODISCARD static _CONSTEXPR17 int compare(_In_reads_(_Count) const _Elem* const _First1,
_In_reads_(_Count) const _Elem* const _First2, const size_t _Count) noexcept /* strengthened */ {
// compare [_First1, _First1 + _Count) with [_First2, ...)
#if _HAS_CXX17
return __builtin_memcmp(_First1, _First2, _Count);
#else // ^^^ _HAS_CXX17 / !_HAS_CXX17 vvv
return _CSTD memcmp(_First1, _First2, _Count);
#endif // ^^^ !_HAS_CXX17 ^^^
}
_NODISCARD static _CONSTEXPR17 size_t length(_In_z_ const _Elem* const _First) noexcept /* strengthened */ {
// find length of null-terminated string
#if _HAS_CXX17
#ifdef __cpp_char8_t
if constexpr (is_same_v<_Elem, char8_t>) {
#if _HAS_U8_INTRINSICS
return __builtin_u8strlen(_First);
#else // ^^^ use u8 intrinsics / no u8 intrinsics vvv
return _Primary_char_traits::length(_First);
#endif // ^^^ no u8 intrinsics ^^^
} else
#endif // defined(__cpp_char8_t)
{
return __builtin_strlen(_First);
}
#else // ^^^ _HAS_CXX17 / !_HAS_CXX17 vvv
return _CSTD strlen(reinterpret_cast<const char*>(_First));
#endif // ^^^ !_HAS_CXX17 ^^^
}
_NODISCARD static _CONSTEXPR17 const _Elem* find(_In_reads_(_Count) const _Elem* const _First, const size_t _Count,
const _Elem& _Ch) noexcept /* strengthened */ {
// look for _Ch in [_First, _First + _Count)
#if _HAS_CXX17
#ifdef __cpp_char8_t
if constexpr (is_same_v<_Elem, char8_t>) {
#if _HAS_U8_INTRINSICS
return __builtin_u8memchr(_First, _Ch, _Count);
#else // ^^^ use u8 intrinsics / no u8 intrinsics vvv
return _Primary_char_traits::find(_First, _Count, _Ch);
#endif // ^^^ no u8 intrinsics ^^^
} else
#endif // defined(__cpp_char8_t)
{
return __builtin_char_memchr(_First, _Ch, _Count);
}
#else // ^^^ _HAS_CXX17 / !_HAS_CXX17 vvv
return static_cast<const _Elem*>(_CSTD memchr(_First, _Ch, _Count));
#endif // ^^^ !_HAS_CXX17 ^^^
}
static _CONSTEXPR20 _Elem* assign(
_Out_writes_all_(_Count) _Elem* const _First, size_t _Count, const _Elem _Ch) noexcept /* strengthened */ {
// assign _Count * _Ch to [_First, ...)
#if _HAS_CXX20
if (_STD is_constant_evaluated()) {
return _Primary_char_traits::assign(_First, _Count, _Ch);
}
#endif // _HAS_CXX20
return static_cast<_Elem*>(_CSTD memset(_First, _Ch, _Count));
}
static _CONSTEXPR17 void assign(_Elem& _Left, const _Elem& _Right) noexcept {
#if _HAS_CXX20
if (_STD is_constant_evaluated()) {
return _Primary_char_traits::assign(_Left, _Right);
}
#endif // _HAS_CXX20
_Left = _Right;
}
_NODISCARD static constexpr bool eq(const _Elem _Left, const _Elem _Right) noexcept {
return _Left == _Right;
}
_NODISCARD static constexpr bool lt(const _Elem _Left, const _Elem _Right) noexcept {
return static_cast<unsigned char>(_Left) < static_cast<unsigned char>(_Right);
}
_NODISCARD static constexpr _Elem to_char_type(const int_type _Meta) noexcept {
return static_cast<_Elem>(_Meta);
}
_NODISCARD static constexpr int_type to_int_type(const _Elem _Ch) noexcept {
return static_cast<unsigned char>(_Ch);
}
_NODISCARD static constexpr bool eq_int_type(const int_type _Left, const int_type _Right) noexcept {
return _Left == _Right;
}
_NODISCARD static constexpr int_type not_eof(const int_type _Meta) noexcept {
return _Meta != eof() ? _Meta : !eof();
}
_NODISCARD static constexpr int_type eof() noexcept {
return static_cast<int_type>(EOF);
}
};
#undef _HAS_U8_INTRINSICS
#undef _HAS_MEMCPY_MEMMOVE_INTRINSICS
template <>
struct char_traits<char> : _Narrow_char_traits<char, int> {}; // properties of a string or stream char element
#ifdef __cpp_char8_t
template <>
struct char_traits<char8_t> : _Narrow_char_traits<char8_t, unsigned int> {};
#endif // defined(__cpp_char8_t)
template <class _Elem, class _Traits, class _SizeT>
basic_ostream<_Elem, _Traits>& _Insert_string(
basic_ostream<_Elem, _Traits>& _Ostr, const _Elem* const _Data, const _SizeT _Size) {
// insert a character-type sequence into _Ostr as if through a basic_string copy
using _Ostr_t = basic_ostream<_Elem, _Traits>;
typename _Ostr_t::iostate _State = _Ostr_t::goodbit;
_SizeT _Pad;
if (_Ostr.width() <= 0 || static_cast<_SizeT>(_Ostr.width()) <= _Size) {
_Pad = 0;
} else {
_Pad = static_cast<_SizeT>(_Ostr.width()) - _Size;
}
const typename _Ostr_t::sentry _Ok(_Ostr);
if (!_Ok) {
_State |= _Ostr_t::badbit;
} else { // state okay, insert characters
_TRY_IO_BEGIN
if ((_Ostr.flags() & _Ostr_t::adjustfield) != _Ostr_t::left) {
for (; 0 < _Pad; --_Pad) { // pad on left
if (_Traits::eq_int_type(_Traits::eof(), _Ostr.rdbuf()->sputc(_Ostr.fill()))) {
_State |= _Ostr_t::badbit; // insertion failed, quit
break;
}
}
}
if (_State == _Ostr_t::goodbit
&& _Ostr.rdbuf()->sputn(_Data, static_cast<streamsize>(_Size)) != static_cast<streamsize>(_Size)) {
_State |= _Ostr_t::badbit;
} else {
for (; 0 < _Pad; --_Pad) { // pad on right
if (_Traits::eq_int_type(_Traits::eof(), _Ostr.rdbuf()->sputc(_Ostr.fill()))) {
_State |= _Ostr_t::badbit; // insertion failed, quit
break;
}
}
}
_Ostr.width(0);
_CATCH_IO_(_Ostr_t, _Ostr)
}
_Ostr.setstate(_State);
return _Ostr;
}
template <class _Traits>
using _Traits_ch_t = typename _Traits::char_type;
template <class _Traits>
using _Traits_ptr_t = const typename _Traits::char_type*;
template <class _Traits>
constexpr bool _Traits_equal(_In_reads_(_Left_size) const _Traits_ptr_t<_Traits> _Left, const size_t _Left_size,
_In_reads_(_Right_size) const _Traits_ptr_t<_Traits> _Right, const size_t _Right_size) noexcept {
// compare [_Left, _Left + _Left_size) to [_Right, _Right + _Right_size) for equality using _Traits
if (_Left_size != _Right_size) {
return false;
}
if (_Left_size == 0u) {
return true;
}
return _Traits::compare(_Left, _Right, _Left_size) == 0;
}
template <class _Traits>
constexpr int _Traits_compare(_In_reads_(_Left_size) const _Traits_ptr_t<_Traits> _Left, const size_t _Left_size,
_In_reads_(_Right_size) const _Traits_ptr_t<_Traits> _Right, const size_t _Right_size) noexcept {
// compare [_Left, _Left + _Left_size) to [_Right, _Right + _Right_size) using _Traits
const int _Ans = _Traits::compare(_Left, _Right, (_STD min)(_Left_size, _Right_size));
if (_Ans != 0) {
return _Ans;
}
if (_Left_size < _Right_size) {
return -1;
}
if (_Left_size > _Right_size) {
return 1;
}
return 0;
}
template <class _Traits>
constexpr size_t _Traits_find(_In_reads_(_Hay_size) const _Traits_ptr_t<_Traits> _Haystack, const size_t _Hay_size,
const size_t _Start_at, _In_reads_(_Needle_size) const _Traits_ptr_t<_Traits> _Needle,
const size_t _Needle_size) noexcept {
// search [_Haystack, _Haystack + _Hay_size) for [_Needle, _Needle + _Needle_size), at/after _Start_at
if (_Needle_size > _Hay_size || _Start_at > _Hay_size - _Needle_size) {
// xpos cannot exist, report failure
// N4950 [string.view.find]/3 says:
// 1. _Start_at <= xpos
// 2. xpos + _Needle_size <= _Hay_size;
// therefore:
// 3. _Needle_size <= _Hay_size (by 2) (checked above)
// 4. _Start_at + _Needle_size <= _Hay_size (substitute 1 into 2)
// 5. _Start_at <= _Hay_size - _Needle_size (4, move _Needle_size to other side) (also checked above)
return static_cast<size_t>(-1);
}
if (_Needle_size == 0) { // empty string always matches if xpos is possible
return _Start_at;
}
const auto _Possible_matches_end = _Haystack + (_Hay_size - _Needle_size) + 1;
for (auto _Match_try = _Haystack + _Start_at;; ++_Match_try) {
_Match_try = _Traits::find(_Match_try, static_cast<size_t>(_Possible_matches_end - _Match_try), *_Needle);
if (!_Match_try) { // didn't find first character; report failure
return static_cast<size_t>(-1);
}
if (_Traits::compare(_Match_try, _Needle, _Needle_size) == 0) { // found match
return static_cast<size_t>(_Match_try - _Haystack);
}
}
}
template <class _Traits>
constexpr size_t _Traits_find_ch(_In_reads_(_Hay_size) const _Traits_ptr_t<_Traits> _Haystack, const size_t _Hay_size,
const size_t _Start_at, const _Traits_ch_t<_Traits> _Ch) noexcept {
// search [_Haystack, _Haystack + _Hay_size) for _Ch, at/after _Start_at
if (_Start_at < _Hay_size) {
const auto _Found_at = _Traits::find(_Haystack + _Start_at, _Hay_size - _Start_at, _Ch);
if (_Found_at) {
return static_cast<size_t>(_Found_at - _Haystack);
}
}
return static_cast<size_t>(-1); // (npos) no match
}
template <class _Traits>
constexpr size_t _Traits_rfind(_In_reads_(_Hay_size) const _Traits_ptr_t<_Traits> _Haystack, const size_t _Hay_size,
const size_t _Start_at, _In_reads_(_Needle_size) const _Traits_ptr_t<_Traits> _Needle,
const size_t _Needle_size) noexcept {
// search [_Haystack, _Haystack + _Hay_size) for [_Needle, _Needle + _Needle_size) beginning before _Start_at
if (_Needle_size == 0) {
return (_STD min)(_Start_at, _Hay_size); // empty string always matches
}
if (_Needle_size <= _Hay_size) { // room for match, look for it
for (auto _Match_try = _Haystack + (_STD min)(_Start_at, _Hay_size - _Needle_size);; --_Match_try) {
if (_Traits::eq(*_Match_try, *_Needle) && _Traits::compare(_Match_try, _Needle, _Needle_size) == 0) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
if (_Match_try == _Haystack) {
break; // at beginning, no more chance for match
}
}
}
return static_cast<size_t>(-1); // no match
}
template <class _Traits>
constexpr size_t _Traits_rfind_ch(_In_reads_(_Hay_size) const _Traits_ptr_t<_Traits> _Haystack, const size_t _Hay_size,
const size_t _Start_at, const _Traits_ch_t<_Traits> _Ch) noexcept {
// search [_Haystack, _Haystack + _Hay_size) for _Ch before _Start_at
if (_Hay_size != 0) { // room for match, look for it
for (auto _Match_try = _Haystack + (_STD min)(_Start_at, _Hay_size - 1);; --_Match_try) {
if (_Traits::eq(*_Match_try, _Ch)) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
if (_Match_try == _Haystack) {
break; // at beginning, no more chance for match
}
}
}
return static_cast<size_t>(-1); // no match
}
template <class _Elem, bool = _Is_character<_Elem>::value>
class _String_bitmap { // _String_bitmap for character types
public:
constexpr bool _Mark(const _Elem* _First, const _Elem* const _Last) noexcept {
// mark this bitmap such that the characters in [_First, _Last) are intended to match
// returns whether all inputs can be placed in the bitmap
for (; _First != _Last; ++_First) {
_Matches[static_cast<unsigned char>(*_First)] = true;
}
return true;
}
constexpr bool _Match(const _Elem _Ch) const noexcept { // test if _Ch is in the bitmap
return _Matches[static_cast<unsigned char>(_Ch)];
}
private:
bool _Matches[256] = {};
};
template <class _Elem>
class _String_bitmap<_Elem, false> { // _String_bitmap for wchar_t/unsigned short/char16_t/char32_t/etc. types
public:
static_assert(is_unsigned_v<_Elem>,
"Standard char_traits is only provided for char, wchar_t, char16_t, and char32_t. See N4950 [char.traits]. "
"Visual C++ accepts other unsigned integral types as an extension.");
constexpr bool _Mark(const _Elem* _First, const _Elem* const _Last) noexcept {
// mark this bitmap such that the characters in [_First, _Last) are intended to match
// returns whether all inputs can be placed in the bitmap
for (; _First != _Last; ++_First) {
const auto _Ch = *_First;
if (_Ch >= 256U) {
return false;
}
_Matches[static_cast<unsigned char>(_Ch)] = true;
}
return true;
}
constexpr bool _Match(const _Elem _Ch) const noexcept { // test if _Ch is in the bitmap
return _Ch < 256U && _Matches[_Ch];
}
private:
bool _Matches[256] = {};
};
template <class _Traits, bool _Special = _Is_specialization_v<_Traits, char_traits>>
constexpr size_t _Traits_find_first_of(_In_reads_(_Hay_size) const _Traits_ptr_t<_Traits> _Haystack,
const size_t _Hay_size, const size_t _Start_at, _In_reads_(_Needle_size) const _Traits_ptr_t<_Traits> _Needle,
const size_t _Needle_size) noexcept {
// in [_Haystack, _Haystack + _Hay_size), look for one of [_Needle, _Needle + _Needle_size), at/after _Start_at
if (_Needle_size != 0 && _Start_at < _Hay_size) { // room for match, look for it
if constexpr (_Special) {
_String_bitmap<typename _Traits::char_type> _Matches;
if (!_Matches._Mark(_Needle, _Needle + _Needle_size)) { // couldn't put one of the characters into the
// bitmap, fall back to the serial algorithm
return _Traits_find_first_of<_Traits, false>(_Haystack, _Hay_size, _Start_at, _Needle, _Needle_size);
}
const auto _End = _Haystack + _Hay_size;
for (auto _Match_try = _Haystack + _Start_at; _Match_try < _End; ++_Match_try) {
if (_Matches._Match(*_Match_try)) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
}
} else {
const auto _End = _Haystack + _Hay_size;
for (auto _Match_try = _Haystack + _Start_at; _Match_try < _End; ++_Match_try) {
if (_Traits::find(_Needle, _Needle_size, *_Match_try)) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
}
}
}
return static_cast<size_t>(-1); // no match
}
template <class _Traits, bool _Special = _Is_specialization_v<_Traits, char_traits>>
constexpr size_t _Traits_find_last_of(_In_reads_(_Hay_size) const _Traits_ptr_t<_Traits> _Haystack,
const size_t _Hay_size, const size_t _Start_at, _In_reads_(_Needle_size) const _Traits_ptr_t<_Traits> _Needle,
const size_t _Needle_size) noexcept {
// in [_Haystack, _Haystack + _Hay_size), look for last of [_Needle, _Needle + _Needle_size), before _Start_at
if (_Needle_size != 0 && _Hay_size != 0) { // worth searching, do it
if constexpr (_Special) {
_String_bitmap<typename _Traits::char_type> _Matches;
if (!_Matches._Mark(_Needle, _Needle + _Needle_size)) { // couldn't put one of the characters into the
// bitmap, fall back to the serial algorithm
return _Traits_find_last_of<_Traits, false>(_Haystack, _Hay_size, _Start_at, _Needle, _Needle_size);
}
for (auto _Match_try = _Haystack + (_STD min)(_Start_at, _Hay_size - 1);; --_Match_try) {
if (_Matches._Match(*_Match_try)) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
if (_Match_try == _Haystack) {
break; // at beginning, no more chance for match
}
}
} else {
for (auto _Match_try = _Haystack + (_STD min)(_Start_at, _Hay_size - 1);; --_Match_try) {
if (_Traits::find(_Needle, _Needle_size, *_Match_try)) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
if (_Match_try == _Haystack) {
break; // at beginning, no more chance for match
}
}
}
}
return static_cast<size_t>(-1); // no match
}
template <class _Traits, bool _Special = _Is_specialization_v<_Traits, char_traits>>
constexpr size_t _Traits_find_first_not_of(_In_reads_(_Hay_size) const _Traits_ptr_t<_Traits> _Haystack,
const size_t _Hay_size, const size_t _Start_at, _In_reads_(_Needle_size) const _Traits_ptr_t<_Traits> _Needle,
const size_t _Needle_size) noexcept {
// in [_Haystack, _Haystack + _Hay_size), look for none of [_Needle, _Needle + _Needle_size), at/after _Start_at
if (_Start_at < _Hay_size) { // room for match, look for it
if constexpr (_Special) {
_String_bitmap<typename _Traits::char_type> _Matches;
if (!_Matches._Mark(_Needle, _Needle + _Needle_size)) { // couldn't put one of the characters into the
// bitmap, fall back to the serial algorithm
return _Traits_find_first_not_of<_Traits, false>(
_Haystack, _Hay_size, _Start_at, _Needle, _Needle_size);
}
const auto _End = _Haystack + _Hay_size;
for (auto _Match_try = _Haystack + _Start_at; _Match_try < _End; ++_Match_try) {
if (!_Matches._Match(*_Match_try)) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
}
} else {
const auto _End = _Haystack + _Hay_size;
for (auto _Match_try = _Haystack + _Start_at; _Match_try < _End; ++_Match_try) {
if (!_Traits::find(_Needle, _Needle_size, *_Match_try)) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
}
}
}
return static_cast<size_t>(-1); // no match
}
template <class _Traits>
constexpr size_t _Traits_find_not_ch(_In_reads_(_Hay_size) const _Traits_ptr_t<_Traits> _Haystack,
const size_t _Hay_size, const size_t _Start_at, const _Traits_ch_t<_Traits> _Ch) noexcept {
// search [_Haystack, _Haystack + _Hay_size) for any value other than _Ch, at/after _Start_at
if (_Start_at < _Hay_size) { // room for match, look for it
const auto _End = _Haystack + _Hay_size;
for (auto _Match_try = _Haystack + _Start_at; _Match_try < _End; ++_Match_try) {
if (!_Traits::eq(*_Match_try, _Ch)) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
}
}
return static_cast<size_t>(-1); // no match
}
template <class _Traits, bool _Special = _Is_specialization_v<_Traits, char_traits>>
constexpr size_t _Traits_find_last_not_of(_In_reads_(_Hay_size) const _Traits_ptr_t<_Traits> _Haystack,
const size_t _Hay_size, const size_t _Start_at, _In_reads_(_Needle_size) const _Traits_ptr_t<_Traits> _Needle,
const size_t _Needle_size) noexcept {
// in [_Haystack, _Haystack + _Hay_size), look for none of [_Needle, _Needle + _Needle_size), before _Start_at
if (_Hay_size != 0) { // worth searching, do it
if constexpr (_Special) {
_String_bitmap<typename _Traits::char_type> _Matches;
if (!_Matches._Mark(_Needle, _Needle + _Needle_size)) { // couldn't put one of the characters into the
// bitmap, fall back to the serial algorithm
return _Traits_find_last_not_of<_Traits, false>(_Haystack, _Hay_size, _Start_at, _Needle, _Needle_size);
}
for (auto _Match_try = _Haystack + (_STD min)(_Start_at, _Hay_size - 1);; --_Match_try) {
if (!_Matches._Match(*_Match_try)) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
if (_Match_try == _Haystack) {
break; // at beginning, no more chance for match
}
}
} else {
for (auto _Match_try = _Haystack + (_STD min)(_Start_at, _Hay_size - 1);; --_Match_try) {
if (!_Traits::find(_Needle, _Needle_size, *_Match_try)) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
if (_Match_try == _Haystack) {
break; // at beginning, no more chance for match
}
}
}
}
return static_cast<size_t>(-1); // no match
}
template <class _Traits>
constexpr size_t _Traits_rfind_not_ch(_In_reads_(_Hay_size) const _Traits_ptr_t<_Traits> _Haystack,
const size_t _Hay_size, const size_t _Start_at, const _Traits_ch_t<_Traits> _Ch) noexcept {
// search [_Haystack, _Haystack + _Hay_size) for any value other than _Ch before _Start_at
if (_Hay_size != 0) { // room for match, look for it
for (auto _Match_try = _Haystack + (_STD min)(_Start_at, _Hay_size - 1);; --_Match_try) {
if (!_Traits::eq(*_Match_try, _Ch)) {
return static_cast<size_t>(_Match_try - _Haystack); // found a match
}
if (_Match_try == _Haystack) {
break; // at beginning, no more chance for match
}
}
}
return static_cast<size_t>(-1); // no match
}
template <class _Ty>
constexpr bool _Is_EcharT = _Is_any_of_v<_Ty, char, wchar_t,
#ifdef __cpp_char8_t
char8_t,
#endif // defined(__cpp_char8_t)
char16_t, char32_t>;
#if _HAS_CXX17
_EXPORT_STD template <class _Elem, class _Traits = char_traits<_Elem>>
class basic_string_view;
template <class _Traits>
class _String_view_iterator {
public:
#if _HAS_CXX20
using iterator_concept = contiguous_iterator_tag;
#endif // _HAS_CXX20
using iterator_category = random_access_iterator_tag;
using value_type = typename _Traits::char_type;
using difference_type = ptrdiff_t;
using pointer = const value_type*;
using reference = const value_type&;
constexpr _String_view_iterator() noexcept = default;
private:
friend basic_string_view<value_type, _Traits>;
#if _ITERATOR_DEBUG_LEVEL >= 1
constexpr _String_view_iterator(const pointer _Data, const size_t _Size, const size_t _Off) noexcept
: _Mydata(_Data), _Mysize(_Size), _Myoff(_Off) {}
#else // ^^^ _ITERATOR_DEBUG_LEVEL >= 1 / _ITERATOR_DEBUG_LEVEL == 0 vvv
constexpr explicit _String_view_iterator(const pointer _Ptr) noexcept : _Myptr(_Ptr) {}
#endif // ^^^ _ITERATOR_DEBUG_LEVEL == 0 ^^^
public:
_NODISCARD constexpr reference operator*() const noexcept {
#if _ITERATOR_DEBUG_LEVEL >= 1
_STL_VERIFY(_Mydata, "cannot dereference value-initialized string_view iterator");
_STL_VERIFY(_Myoff < _Mysize, "cannot dereference end string_view iterator");
return _Mydata[_Myoff];
#else // ^^^ _ITERATOR_DEBUG_LEVEL >= 1 / _ITERATOR_DEBUG_LEVEL == 0 vvv
return *_Myptr;
#endif // ^^^ _ITERATOR_DEBUG_LEVEL == 0 ^^^
}
_NODISCARD constexpr pointer operator->() const noexcept {
#if _ITERATOR_DEBUG_LEVEL >= 1
_STL_VERIFY(_Mydata, "cannot dereference value-initialized string_view iterator");
_STL_VERIFY(_Myoff < _Mysize, "cannot dereference end string_view iterator");
return _Mydata + _Myoff;
#else // ^^^ _ITERATOR_DEBUG_LEVEL >= 1 / _ITERATOR_DEBUG_LEVEL == 0 vvv
return _Myptr;
#endif // ^^^ _ITERATOR_DEBUG_LEVEL == 0 ^^^
}
constexpr _String_view_iterator& operator++() noexcept {
#if _ITERATOR_DEBUG_LEVEL >= 1
_STL_VERIFY(_Mydata, "cannot increment value-initialized string_view iterator");
_STL_VERIFY(_Myoff < _Mysize, "cannot increment string_view iterator past end");
++_Myoff;
#else // ^^^ _ITERATOR_DEBUG_LEVEL >= 1 / _ITERATOR_DEBUG_LEVEL == 0 vvv
++_Myptr;
#endif // ^^^ _ITERATOR_DEBUG_LEVEL == 0 ^^^
return *this;
}
constexpr _String_view_iterator operator++(int) noexcept {
_String_view_iterator _Tmp{*this};
++*this;
return _Tmp;
}
constexpr _String_view_iterator& operator--() noexcept {
#if _ITERATOR_DEBUG_LEVEL >= 1
_STL_VERIFY(_Mydata, "cannot decrement value-initialized string_view iterator");
_STL_VERIFY(_Myoff != 0, "cannot decrement string_view iterator before begin");
--_Myoff;
#else // ^^^ _ITERATOR_DEBUG_LEVEL >= 1 / _ITERATOR_DEBUG_LEVEL == 0 vvv
--_Myptr;
#endif // ^^^ _ITERATOR_DEBUG_LEVEL == 0 ^^^
return *this;
}
constexpr _String_view_iterator operator--(int) noexcept {
_String_view_iterator _Tmp{*this};
--*this;
return _Tmp;
}
constexpr void _Verify_offset(const difference_type _Off) const noexcept {
#if _ITERATOR_DEBUG_LEVEL >= 1
if (_Off != 0) {
_STL_VERIFY(_Mydata, "cannot seek value-initialized string_view iterator");
}
if (_Off < 0) {
_STL_VERIFY(
_Myoff >= size_t{0} - static_cast<size_t>(_Off), "cannot seek string_view iterator before begin");
}
if (_Off > 0) {
_STL_VERIFY(_Mysize - _Myoff >= static_cast<size_t>(_Off), "cannot seek string_view iterator after end");
}
#else // ^^^ _ITERATOR_DEBUG_LEVEL >= 1 / _ITERATOR_DEBUG_LEVEL == 0 vvv
(void) _Off;
#endif // ^^^ _ITERATOR_DEBUG_LEVEL == 0 ^^^
}
constexpr _String_view_iterator& operator+=(const difference_type _Off) noexcept {
#if _ITERATOR_DEBUG_LEVEL >= 1
_Verify_offset(_Off);
_Myoff += static_cast<size_t>(_Off);
#else // ^^^ _ITERATOR_DEBUG_LEVEL >= 1 / _ITERATOR_DEBUG_LEVEL == 0 vvv
_Myptr += _Off;
#endif // ^^^ _ITERATOR_DEBUG_LEVEL == 0 ^^^
return *this;
}