-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathregex
4674 lines (3975 loc) · 170 KB
/
regex
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
// regex standard header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef _REGEX_
#define _REGEX_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cwchar>
#include <iterator>
#include <locale>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#if _HAS_CXX17
#include <xpolymorphic_allocator.h>
#endif // _HAS_CXX17
#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
#ifndef _REGEX_MAX_COMPLEXITY_COUNT
#define _REGEX_MAX_COMPLEXITY_COUNT 10000000L // set to 0 to disable
#endif // !defined(_REGEX_MAX_COMPLEXITY_COUNT)
#ifndef _REGEX_MAX_STACK_COUNT
#ifdef _WIN64
#define _REGEX_MAX_STACK_COUNT 600L // set to 0 to disable
#else // ^^^ defined(_WIN64) / !defined(_WIN64) vvv
#define _REGEX_MAX_STACK_COUNT 1000L // set to 0 to disable
#endif // ^^^ !defined(_WIN64) ^^^
#endif // !defined(_REGEX_MAX_STACK_COUNT)
#ifndef _ENHANCED_REGEX_VISUALIZER
#ifdef _DEBUG
#define _ENHANCED_REGEX_VISUALIZER 1
#else // ^^^ defined(_DEBUG) / !defined(_DEBUG) vvv
#define _ENHANCED_REGEX_VISUALIZER 0
#endif // ^^^ !defined(_DEBUG) ^^^
#endif // !defined(_ENHANCED_REGEX_VISUALIZER)
_STD_BEGIN
enum _Meta_type : int { // meta character representations for parser
_Meta_lpar = '(',
_Meta_rpar = ')',
_Meta_dlr = '$',
_Meta_caret = '^',
_Meta_dot = '.',
_Meta_star = '*',
_Meta_plus = '+',
_Meta_query = '?',
_Meta_lsq = '[',
_Meta_rsq = ']',
_Meta_bar = '|',
_Meta_esc = '\\',
_Meta_dash = '-',
_Meta_lbr = '{',
_Meta_rbr = '}',
_Meta_comma = ',',
_Meta_colon = ':',
_Meta_equal = '=',
_Meta_exc = '!',
_Meta_eos = -1,
_Meta_nl = '\n',
_Meta_cr = '\r',
_Meta_bsp = '\b',
_Meta_chr = 0,
_Esc_bsl = '\\',
_Esc_word = 'b',
_Esc_not_word = 'B',
_Esc_ctrl_a = 'a',
_Esc_ctrl_b = 'b',
_Esc_ctrl_f = 'f',
_Esc_ctrl_n = 'n',
_Esc_ctrl_r = 'r',
_Esc_ctrl_t = 't',
_Esc_ctrl_v = 'v',
_Esc_ctrl = 'c',
_Esc_hex = 'x',
_Esc_uni = 'u'
};
namespace regex_constants {
// constants used in regular expressions
_EXPORT_STD enum syntax_option_type : int { // specify RE syntax rules
ECMAScript = 0x01,
basic = 0x02,
extended = 0x04,
awk = 0x08,
grep = 0x10,
egrep = 0x20,
_Gmask = 0x3F,
icase = 0x0100,
nosubs = 0x0200,
optimize = 0x0400,
collate = 0x0800
};
_BITMASK_OPS(_EXPORT_STD, syntax_option_type)
_EXPORT_STD enum match_flag_type : int { // specify matching and formatting rules
match_default = 0x0000,
match_not_bol = 0x0001,
match_not_eol = 0x0002,
match_not_bow = 0x0004,
match_not_eow = 0x0008,
match_any = 0x0010,
match_not_null = 0x0020,
match_continuous = 0x0040,
match_prev_avail = 0x0100,
format_default = 0x0000,
format_sed = 0x0400,
format_no_copy = 0x0800,
format_first_only = 0x1000,
_Match_not_null = 0x2000,
_Skip_zero_length = 0x4000,
};
_BITMASK_OPS(_EXPORT_STD, match_flag_type)
_EXPORT_STD enum error_type { // identify error
error_collate,
error_ctype,
error_escape,
error_backref,
error_brack,
error_paren,
error_brace,
error_badbrace,
error_range,
error_space,
error_badrepeat,
error_complexity,
error_stack,
error_parse,
error_syntax
};
} // namespace regex_constants
extern "C++" [[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Xregex_error(regex_constants::error_type _Code);
_EXPORT_STD template <class _Elem>
class regex_traits;
struct _Cl_names { // structure to associate class name with mask value
const char* _Narrow;
const wchar_t* _Wide;
unsigned int _Len;
ctype_base::mask _Ctype;
template <class _Elem>
_NODISCARD const _Elem* _Get() const noexcept {
if constexpr (is_same_v<_Elem, char>) {
return _Narrow;
} else {
return _Wide;
}
}
};
template <class _CharT>
struct _Std_char_traits_eq {
_STATIC_CALL_OPERATOR bool operator()(_CharT _Left, _CharT _Right) _CONST_CALL_OPERATOR noexcept {
return char_traits<_CharT>::eq(_Left, _Right);
}
};
template <class _CharT>
struct _Std_char_traits_lt {
_STATIC_CALL_OPERATOR bool operator()(_CharT _Left, _CharT _Right) _CONST_CALL_OPERATOR noexcept {
return char_traits<_CharT>::lt(_Left, _Right);
}
};
// signed char and other unsigned integral types are supported as an extension.
template <class _Ty>
constexpr bool _Is_predefined_char_like_type = _Is_character<_Ty>::value || is_unsigned_v<_Ty>;
// library-provided char_traits::eq behaves like equal_to<_Elem>
template <class _Elem>
constexpr bool _Can_memcmp_elements_with_pred<_Elem, _Elem, _Std_char_traits_eq<_Elem>> =
_Is_predefined_char_like_type<_Elem> && _Can_memcmp_elements<_Elem, _Elem>;
template <class _Elem, bool = _Is_predefined_char_like_type<_Elem>>
struct _Lex_compare_memcmp_classify_pred_for_std_char_traits_lt {
using _UElem = make_unsigned_t<_Elem>;
using _Pred = conditional_t<_Lex_compare_memcmp_classify_elements<_UElem, _UElem>, less<int>, void>;
};
template <class _Elem>
struct _Lex_compare_memcmp_classify_pred_for_std_char_traits_lt<_Elem, false> {
using _Pred = void;
};
// library-provided char_traits::lt behaves like less<make_unsigned_t<_Elem>>
template <class _Elem>
struct _Lex_compare_memcmp_classify_pred<_Elem, _Elem, _Std_char_traits_lt<_Elem>>
: _Lex_compare_memcmp_classify_pred_for_std_char_traits_lt<_Elem> {};
template <class _RxTraits>
struct _Cmp_cs { // functor to compare two character values for equality
using _Elem = typename _RxTraits::char_type;
_STATIC_CALL_OPERATOR bool operator()(_Elem _Ex1, _Elem _Ex2) _CONST_CALL_OPERATOR {
return _Ex1 == _Ex2;
}
};
template <class _RxTraits>
struct _Cmp_icase { // functor to compare for case-insensitive equality
using _Elem = typename _RxTraits::char_type;
explicit _Cmp_icase(const _RxTraits& _Tr) noexcept : _Traits(_Tr) {}
bool operator()(_Elem _Ex1, _Elem _Ex2) const {
return _Traits.translate_nocase(_Ex1) == _Traits.translate_nocase(_Ex2);
}
const _RxTraits& _Traits;
};
template <class _RxTraits>
struct _Cmp_collate { // functor to compare for locale-specific equality
using _Elem = typename _RxTraits::char_type;
explicit _Cmp_collate(const _RxTraits& _Tr) noexcept : _Traits(_Tr) {}
bool operator()(_Elem _Ex1, _Elem _Ex2) const {
return _Traits.translate(_Ex1) == _Traits.translate(_Ex2);
}
const _RxTraits& _Traits;
};
struct _Regex_traits_base { // base of all regular expression traits
enum _Char_class_type {
_Ch_alnum = ctype_base::alnum,
_Ch_alpha = ctype_base::alpha,
_Ch_cntrl = ctype_base::cntrl,
_Ch_digit = ctype_base::digit,
_Ch_graph = ctype_base::graph,
_Ch_lower = ctype_base::lower,
_Ch_print = ctype_base::print,
_Ch_punct = ctype_base::punct,
_Ch_space = ctype_base::space,
_Ch_upper = ctype_base::upper,
_Ch_xdigit = ctype_base::xdigit,
_Ch_blank = ctype_base::blank
};
using char_class_type = ctype_base::mask;
};
template <class _Elem>
class _Regex_traits : public _Regex_traits_base { // base class for regular expression traits
public:
using _Uelem = make_unsigned_t<_Elem>;
using char_type = _Elem;
using size_type = size_t;
using string_type = basic_string<_Elem>;
using locale_type = locale;
static size_type length(const _Elem* _Str) {
return char_traits<_Elem>::length(_Str);
}
_Regex_traits() {
_Cache_locale();
}
_Regex_traits(const _Regex_traits& _Right) : _Loc(_Right._Loc) {
_Cache_locale();
}
_Regex_traits& operator=(const _Regex_traits& _Right) {
_Loc = _Right._Loc;
_Cache_locale();
return *this;
}
_Elem translate(_Elem _Ch) const { // provide locale-sensitive mapping
string_type _Res = _Getcoll()->transform(_STD addressof(_Ch), _STD addressof(_Ch) + 1);
return _Res.size() == 1 ? _Res[0] : _Ch;
}
_Elem translate_nocase(_Elem _Ch) const { // provide case-insensitive mapping
return _Getctype()->tolower(_Ch);
}
template <class _FwdIt>
string_type transform(_FwdIt _First, _FwdIt _Last) const { // apply locale-specific transformation
const string_type _Str(_First, _Last);
return _Getcoll()->transform(_Str.data(), _Str.data() + _Str.size());
}
template <class _FwdIt>
string_type transform_primary(_FwdIt _First, _FwdIt _Last) const {
// apply locale-specific case-insensitive transformation
string_type _Res;
if (_First != _Last) { // non-empty string, transform it
vector<_Elem> _Temp(_First, _Last);
_Getctype()->tolower(_Temp.data(), _Temp.data() + _Temp.size());
_Res = _Getcoll()->transform(_Temp.data(), _Temp.data() + _Temp.size());
}
return _Res;
}
bool isctype(_Elem _Ch, char_class_type _Fx) const {
if (_Fx != static_cast<char_class_type>(-1)) {
return _Getctype()->is(_Fx, _Ch);
} else {
return _Ch == '_' // assumes L'_' == '_'
|| _Getctype()->is(_Ch_alnum, _Ch);
}
}
template <class _Iter>
char_class_type lookup_classname(_Iter _First, _Iter _Last, bool _Icase = false) const {
// map [_First, _Last) to character class mask value
#define _REGEX_CHAR_CLASS_NAME(n, c) \
{ n, L##n, static_cast<unsigned int>(_STD size(n) - 1), c }
static constexpr _Cl_names _Names[] = {
// map class names to numeric constants
_REGEX_CHAR_CLASS_NAME("alnum", _Ch_alnum),
_REGEX_CHAR_CLASS_NAME("alpha", _Ch_alpha),
_REGEX_CHAR_CLASS_NAME("blank", _Ch_blank),
_REGEX_CHAR_CLASS_NAME("cntrl", _Ch_cntrl),
_REGEX_CHAR_CLASS_NAME("d", _Ch_digit),
_REGEX_CHAR_CLASS_NAME("digit", _Ch_digit),
_REGEX_CHAR_CLASS_NAME("graph", _Ch_graph),
_REGEX_CHAR_CLASS_NAME("lower", _Ch_lower),
_REGEX_CHAR_CLASS_NAME("print", _Ch_print),
_REGEX_CHAR_CLASS_NAME("punct", _Ch_punct),
_REGEX_CHAR_CLASS_NAME("space", _Ch_space),
_REGEX_CHAR_CLASS_NAME("s", _Ch_space),
_REGEX_CHAR_CLASS_NAME("upper", _Ch_upper),
_REGEX_CHAR_CLASS_NAME("w", static_cast<ctype_base::mask>(-1)),
_REGEX_CHAR_CLASS_NAME("xdigit", _Ch_xdigit),
{nullptr, nullptr, 0, 0},
};
#undef _REGEX_CHAR_CLASS_NAME
_Adl_verify_range(_First, _Last);
unsigned int _Ix = 0;
for (; _Names[_Ix]._Get<_Elem>(); ++_Ix) {
if (_STD equal(_Get_unwrapped(_First), _Get_unwrapped(_Last), _Names[_Ix]._Get<_Elem>(),
_Names[_Ix]._Get<_Elem>() + _Names[_Ix]._Len, _Cmp_icase<_Regex_traits<_Elem>>{*this})) {
break;
}
}
char_class_type _Mask{};
if (_Names[_Ix]._Get<_Elem>()) {
_Mask = _Names[_Ix]._Ctype;
}
if (_Icase && (_Mask & (_Ch_lower | _Ch_upper))) {
_Mask |= _Ch_lower | _Ch_upper;
}
return _Mask;
}
template <class _FwdIt>
string_type lookup_collatename(_FwdIt _First, _FwdIt _Last) const { // map [_First, _Last) to collation element
return string_type{_First, _Last};
}
locale_type imbue(locale_type _Lx) { // store locale object
locale_type _Tmp = _Loc;
_Loc = _Lx;
_Cache_locale();
return _Tmp;
}
locale_type getloc() const noexcept /* strengthened */ {
return _Loc;
}
const collate<_Elem>* _Getcoll() const noexcept { // get collate facet pointer
return _Pcoll;
}
const ctype<_Elem>* _Getctype() const noexcept { // get ctype facet pointer
return _Pctype;
}
private:
void _Cache_locale() { // populate _Pcoll and _Pctype with _Loc locale
_Pcoll = _STD addressof(_STD use_facet<collate<_Elem>>(_Loc));
_Pctype = _STD addressof(_STD use_facet<ctype<_Elem>>(_Loc));
}
const collate<_Elem>* _Pcoll;
const ctype<_Elem>* _Pctype;
locale_type _Loc;
};
template <>
class regex_traits<char> : public _Regex_traits<char> {
public:
int value(char _Ch, int _Base) const { // map character value to numeric value
if ((_Base != 8 && '0' <= _Ch && _Ch <= '9') || (_Base == 8 && '0' <= _Ch && _Ch <= '7')) {
return _Ch - '0';
}
if (_Base != 16) {
return -1;
}
if ('a' <= _Ch && _Ch <= 'f') {
return _Ch - 'a' + 10;
}
if ('A' <= _Ch && _Ch <= 'F') {
return _Ch - 'A' + 10;
}
return -1;
}
};
template <>
class regex_traits<wchar_t> : public _Regex_traits<wchar_t> {
public:
int value(wchar_t _Ch, int _Base) const { // map character value to numeric value
if ((_Base != 8 && L'0' <= _Ch && _Ch <= L'9') || (_Base == 8 && L'0' <= _Ch && _Ch <= L'7')) {
return _Ch - L'0';
}
if (_Base != 16) {
return -1;
}
if (L'a' <= _Ch && _Ch <= L'f') {
return _Ch - L'a' + 10;
}
if (L'A' <= _Ch && _Ch <= L'F') {
return _Ch - L'A' + 10;
}
return -1;
}
};
_EXPORT_STD class _NODISCARD regex_error : public runtime_error { // type of all regular expression exceptions
public:
explicit regex_error(regex_constants::error_type _Ex) : runtime_error(_Stringify(_Ex)), _Err(_Ex) {}
_NODISCARD regex_constants::error_type code() const {
return _Err;
}
private:
static const char* _Stringify(regex_constants::error_type _Ex) { // map error code to string
switch (_Ex) { // select known error_type message
case regex_constants::error_collate:
return "regex_error(error_collate): The expression "
"contained an invalid collating element name.";
case regex_constants::error_ctype:
return "regex_error(error_ctype): The expression "
"contained an invalid character class name.";
case regex_constants::error_escape:
return "regex_error(error_escape): The expression "
"contained an invalid escaped character, "
"or a trailing escape.";
case regex_constants::error_backref:
return "regex_error(error_backref): The expression "
"contained an invalid back reference.";
case regex_constants::error_brack:
return "regex_error(error_brack): The expression "
"contained mismatched [ and ].";
case regex_constants::error_paren:
return "regex_error(error_paren): The expression "
"contained mismatched ( and ).";
case regex_constants::error_brace:
return "regex_error(error_brace): The expression "
"contained mismatched { and }.";
case regex_constants::error_badbrace:
return "regex_error(error_badbrace): The expression "
"contained an invalid range in a {} expression.";
case regex_constants::error_range:
return "regex_error(error_range): The expression "
"contained an invalid character range, "
"such as [b-a] in most encodings.";
case regex_constants::error_space:
return "regex_error(error_space): There was insufficient "
"memory to convert the expression into "
"a finite state machine.";
case regex_constants::error_badrepeat:
return "regex_error(error_badrepeat): One of *?+{ "
"was not preceded by a valid regular expression.";
case regex_constants::error_complexity:
return "regex_error(error_complexity): The complexity of "
"an attempted match against a regular expression "
"exceeded a pre-set level.";
case regex_constants::error_stack:
return "regex_error(error_stack): There was insufficient "
"memory to determine whether the regular expression "
"could match the specified character sequence.";
case regex_constants::error_parse:
return "regex_error(error_parse)";
case regex_constants::error_syntax:
return "regex_error(error_syntax)";
default:
return "regex_error";
}
}
regex_constants::error_type _Err;
};
inline bool _Is_word(unsigned char _UCh) {
// special casing char to avoid branches for std::regex in this path
static constexpr bool _Is_word_table[_STD _Max_limit<unsigned char>() + 1] = {
// X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 XA XB XC XD XE XF
/* 0X */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 1X */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 2X */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 3X */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 0-9
/* 4X */ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A-Z
/* 5X */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 5F == _
/* 6X */ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // a-z
/* 7X */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
// non-ASCII values initialized to 0
};
return _Is_word_table[_UCh];
}
inline bool _Is_word(char _Ch) {
return _Is_word(static_cast<unsigned char>(_Ch));
}
template <class _Elem>
bool _Is_word(_Elem _Ch) {
// assumes 'x' == L'x' for the ASCII range
using _UElem = make_unsigned_t<_Elem>;
const auto _UCh = static_cast<_UElem>(_Ch);
return _UCh <= static_cast<_UElem>('z') && _Is_word(static_cast<unsigned char>(_UCh));
}
_EXPORT_STD template <class _BidIt>
class sub_match : public pair<_BidIt, _BidIt> { // class to hold contents of a capture group
public:
using _Mybase = pair<_BidIt, _BidIt>;
using value_type = typename iterator_traits<_BidIt>::value_type;
using difference_type = typename iterator_traits<_BidIt>::difference_type;
using iterator = _BidIt;
using string_type = basic_string<value_type>;
constexpr sub_match() = default;
bool matched = false;
_NODISCARD difference_type length() const {
const _Mybase _Range(_Effective_range());
return _STD distance(_Range.first, _Range.second);
}
operator string_type() const { // convert matched text to string
return str();
}
_NODISCARD string_type str() const { // convert matched text to string
const _Mybase _Range(_Effective_range());
return string_type{_Range.first, _Range.second};
}
_NODISCARD int compare(const sub_match& _Right) const { // compare *this to _Right
const _Mybase _LRange(_Effective_range());
const _Mybase _RRange(_Right._Effective_range());
return _Iter_compare(_LRange.first, _LRange.second, _RRange.first, _RRange.second);
}
_NODISCARD int compare(const string_type& _Right) const { // compare *this to _Right
return _Compare(_Right.data(), _Right.size());
}
_NODISCARD int compare(_In_z_ const value_type* _Ptr) const { // compare *this to array pointed to by _Ptr
return _Compare(_Ptr, char_traits<value_type>::length(_Ptr));
}
void swap(sub_match& _Other) noexcept(_Is_nothrow_swappable<_BidIt>::value) {
_Mybase::swap(_Other);
_STD swap(matched, _Other.matched);
}
template <class _FwdIt2>
static int _Iter_compare(_BidIt _First1, _BidIt _Last1, _FwdIt2 _First2, _FwdIt2 _Last2) {
// compare two iterator ranges:
// if [_First1, _Last1) is lexicographically less than [_First2, _Last2), a negative value
// if [_First2, _Last2) is lexicographically less than [_First1, _Last1), a positive value
// otherwise, zero
static_assert(is_same_v<_Iter_value_t<_FwdIt2>, value_type>, "bad _FwdIt2 to _Iter_compare");
_Adl_verify_range(_First1, _Last1);
_Adl_verify_range(_First2, _Last2);
auto _UFirst1 = _Get_unwrapped(_First1);
auto _ULast1 = _Get_unwrapped(_Last1);
auto _UFirst2 = _Get_unwrapped(_First2);
auto _ULast2 = _Get_unwrapped(_Last2);
if constexpr (is_pointer_v<decltype(_UFirst1)> && is_pointer_v<decltype(_UFirst2)>) {
return _Traits_compare<char_traits<value_type>>(
_UFirst1, static_cast<size_t>(_ULast1 - _UFirst1), _UFirst2, static_cast<size_t>(_ULast2 - _UFirst2));
} else {
const auto _Cmp = _STD mismatch(_UFirst1, _ULast1, _UFirst2, _ULast2, _Std_char_traits_eq<value_type>{});
if (_Cmp.first == _ULast1) {
if (_Cmp.second == _ULast2) {
return 0;
} else {
return -1;
}
}
if (_Cmp.second == _ULast2) {
return 1;
}
if (char_traits<value_type>::lt(*_Cmp.first, *_Cmp.second)) {
return -1;
} else {
return 1;
}
}
}
int _Compare(const value_type* const _Ptr, const size_t _Count) const {
// compare *this to array [_Ptr, _Ptr + _Count)
const _Mybase _Range(_Effective_range());
return _Iter_compare(_Range.first, _Range.second, _Ptr, _Ptr + _Count);
}
bool _Match_equal(const sub_match& _Right) const { // check *this to _Right for equality
const _Mybase _LRange(_Effective_range());
const _Mybase _RRange(_Right._Effective_range());
return _STD equal(
_LRange.first, _LRange.second, _RRange.first, _RRange.second, _Std_char_traits_eq<value_type>{});
}
bool _Match_equal(const value_type* const _Ptr, const size_t _Count) const {
// check *this to array [_Ptr, _Ptr + _Count) for equality
const _Mybase _Range(_Effective_range());
return _STD equal(_Range.first, _Range.second, _Ptr, _Ptr + _Count, _Std_char_traits_eq<value_type>{});
}
bool _Match_equal(const value_type* const _Ptr) const { // check *this to C-string _Ptr for equality
return _Match_equal(_Ptr, char_traits<value_type>::length(_Ptr));
}
bool _Less(const sub_match& _Right) const { // check whether *this is less than _Right
const _Mybase _LRange(_Effective_range());
const _Mybase _RRange(_Right._Effective_range());
return _STD lexicographical_compare(
_LRange.first, _LRange.second, _RRange.first, _RRange.second, _Std_char_traits_lt<value_type>{});
}
bool _Less(const value_type* const _Ptr, const size_t _Count) const {
// check whether *this is less than [_Ptr, _Ptr + _Count)
const _Mybase _Range(_Effective_range());
return _STD lexicographical_compare(
_Range.first, _Range.second, _Ptr, _Ptr + _Count, _Std_char_traits_lt<value_type>{});
}
bool _Less(const value_type* const _Ptr) const { // check whether *this is less than C-string _Ptr
return _Less(_Ptr, char_traits<value_type>::length(_Ptr));
}
bool _Greater(const value_type* const _Ptr, const size_t _Count) const {
// check whether *this is greater than [_Ptr, _Ptr + _Count)
const _Mybase _Range(_Effective_range());
return _STD lexicographical_compare(
_Ptr, _Ptr + _Count, _Range.first, _Range.second, _Std_char_traits_lt<value_type>{});
}
bool _Greater(const value_type* const _Ptr) const { // check whether *this is greater than C-string _Ptr
return _Greater(_Ptr, char_traits<value_type>::length(_Ptr));
}
_Mybase _Effective_range() const { // if matched, returns *this; otherwise returns an empty range
if (matched) {
return *this;
} else {
return _Mybase{};
}
}
};
_EXPORT_STD using csub_match = sub_match<const char*>;
_EXPORT_STD using wcsub_match = sub_match<const wchar_t*>;
_EXPORT_STD using ssub_match = sub_match<string::const_iterator>;
_EXPORT_STD using wssub_match = sub_match<wstring::const_iterator>;
_EXPORT_STD template <class _BidIt>
_NODISCARD bool operator==(const sub_match<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return _Left._Match_equal(_Right);
}
#if _HAS_CXX20
_EXPORT_STD template <class _BidIt>
_NODISCARD auto operator<=>(const sub_match<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
using _Comparison_category = _Get_comparison_category_t<char_traits<_Iter_value_t<_BidIt>>>;
return static_cast<_Comparison_category>(_Left.compare(_Right) <=> 0);
}
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
template <class _BidIt>
_NODISCARD bool operator!=(const sub_match<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return !(_Left == _Right);
}
template <class _BidIt>
_NODISCARD bool operator<(const sub_match<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return _Left._Less(_Right);
}
template <class _BidIt>
_NODISCARD bool operator>(const sub_match<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return _Right < _Left;
}
template <class _BidIt>
_NODISCARD bool operator<=(const sub_match<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return !(_Right < _Left);
}
template <class _BidIt>
_NODISCARD bool operator>=(const sub_match<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return !(_Left < _Right);
}
#endif // ^^^ !_HAS_CXX20 ^^^
_EXPORT_STD template <class _BidIt>
_NODISCARD bool operator==(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>* _Right) {
return _Left._Match_equal(_Right);
}
#if _HAS_CXX20
_EXPORT_STD template <class _BidIt>
_NODISCARD auto operator<=>(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>* _Right) {
using _Comparison_category = _Get_comparison_category_t<char_traits<_Iter_value_t<_BidIt>>>;
return static_cast<_Comparison_category>(_Left.compare(_Right) <=> 0);
}
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
template <class _BidIt>
_NODISCARD bool operator==(const _Iter_value_t<_BidIt>* _Left, const sub_match<_BidIt>& _Right) {
return _Right._Match_equal(_Left);
}
template <class _BidIt>
_NODISCARD bool operator!=(const _Iter_value_t<_BidIt>* _Left, const sub_match<_BidIt>& _Right) {
return !(_Left == _Right);
}
template <class _BidIt>
_NODISCARD bool operator<(const _Iter_value_t<_BidIt>* _Left, const sub_match<_BidIt>& _Right) {
return _Right._Greater(_Left);
}
template <class _BidIt>
_NODISCARD bool operator>(const _Iter_value_t<_BidIt>* _Left, const sub_match<_BidIt>& _Right) {
return _Right < _Left;
}
template <class _BidIt>
_NODISCARD bool operator<=(const _Iter_value_t<_BidIt>* _Left, const sub_match<_BidIt>& _Right) {
return !(_Right < _Left);
}
template <class _BidIt>
_NODISCARD bool operator>=(const _Iter_value_t<_BidIt>* _Left, const sub_match<_BidIt>& _Right) {
return !(_Left < _Right);
}
template <class _BidIt>
_NODISCARD bool operator!=(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>* _Right) {
return !(_Left == _Right);
}
template <class _BidIt>
_NODISCARD bool operator<(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>* _Right) {
return _Left._Less(_Right);
}
template <class _BidIt>
_NODISCARD bool operator>(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>* _Right) {
return _Right < _Left;
}
template <class _BidIt>
_NODISCARD bool operator<=(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>* _Right) {
return !(_Right < _Left);
}
template <class _BidIt>
_NODISCARD bool operator>=(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>* _Right) {
return !(_Left < _Right);
}
#endif // ^^^ !_HAS_CXX20 ^^^
_EXPORT_STD template <class _BidIt>
_NODISCARD bool operator==(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>& _Right) {
return _Left._Match_equal(_STD addressof(_Right), 1);
}
#if _HAS_CXX20
_EXPORT_STD template <class _BidIt>
_NODISCARD auto operator<=>(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>& _Right) {
using _Comparison_category = _Get_comparison_category_t<char_traits<_Iter_value_t<_BidIt>>>;
return static_cast<_Comparison_category>(_Left._Compare(_STD addressof(_Right), 1) <=> 0);
}
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
template <class _BidIt>
_NODISCARD bool operator==(const _Iter_value_t<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return _Right._Match_equal(_STD addressof(_Left), 1);
}
template <class _BidIt>
_NODISCARD bool operator!=(const _Iter_value_t<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return !(_Left == _Right);
}
template <class _BidIt>
_NODISCARD bool operator<(const _Iter_value_t<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return _Right._Greater(_STD addressof(_Left), 1);
}
template <class _BidIt>
_NODISCARD bool operator>(const _Iter_value_t<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return _Right < _Left;
}
template <class _BidIt>
_NODISCARD bool operator<=(const _Iter_value_t<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return !(_Right < _Left);
}
template <class _BidIt>
_NODISCARD bool operator>=(const _Iter_value_t<_BidIt>& _Left, const sub_match<_BidIt>& _Right) {
return !(_Left < _Right);
}
template <class _BidIt>
_NODISCARD bool operator!=(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>& _Right) {
return !(_Left == _Right);
}
template <class _BidIt>
_NODISCARD bool operator<(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>& _Right) {
return _Left._Less(_STD addressof(_Right), 1);
}
template <class _BidIt>
_NODISCARD bool operator>(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>& _Right) {
return _Right < _Left;
}
template <class _BidIt>
_NODISCARD bool operator<=(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>& _Right) {
return !(_Right < _Left);
}
template <class _BidIt>
_NODISCARD bool operator>=(const sub_match<_BidIt>& _Left, const _Iter_value_t<_BidIt>& _Right) {
return !(_Left < _Right);
}
#endif // ^^^ !_HAS_CXX20 ^^^
_EXPORT_STD template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator==(
const sub_match<_BidIt>& _Left, const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Right) {
return _Left._Match_equal(_Right.data(), _Right.size());
}
#if _HAS_CXX20
_EXPORT_STD template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD auto operator<=>(
const sub_match<_BidIt>& _Left, const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Right) {
using _Comparison_category = _Get_comparison_category_t<char_traits<_Iter_value_t<_BidIt>>>;
return static_cast<_Comparison_category>(_Left._Compare(_Right.data(), _Right.size()) <=> 0);
}
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator==(
const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Left, const sub_match<_BidIt>& _Right) {
return _Right._Match_equal(_Left.data(), _Left.size());
}
template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator!=(
const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Left, const sub_match<_BidIt>& _Right) {
return !(_Left == _Right);
}
template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator<(
const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Left, const sub_match<_BidIt>& _Right) {
return _Right._Greater(_Left.data(), _Left.size());
}
template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator>(
const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Left, const sub_match<_BidIt>& _Right) {
return _Right < _Left;
}
template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator<=(
const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Left, const sub_match<_BidIt>& _Right) {
return !(_Right < _Left);
}
template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator>=(
const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Left, const sub_match<_BidIt>& _Right) {
return !(_Left < _Right);
}
template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator!=(
const sub_match<_BidIt>& _Left, const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Right) {
return !(_Left == _Right);
}
template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator<(
const sub_match<_BidIt>& _Left, const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Right) {
return _Left._Less(_Right.data(), _Right.size());
}
template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator>(
const sub_match<_BidIt>& _Left, const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Right) {
return _Right < _Left;
}
template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator<=(
const sub_match<_BidIt>& _Left, const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Right) {
return !(_Right < _Left);
}
template <class _BidIt, class _Traits, class _Alloc>
_NODISCARD bool operator>=(
const sub_match<_BidIt>& _Left, const basic_string<_Iter_value_t<_BidIt>, _Traits, _Alloc>& _Right) {
return !(_Left < _Right);
}
#endif // ^^^ !_HAS_CXX20 ^^^
_EXPORT_STD template <class _Elem, class _Traits, class _BidIt>
basic_ostream<_Elem, _Traits>& operator<<(basic_ostream<_Elem, _Traits>& _Ostr, const sub_match<_BidIt>& _Match) {
return _Ostr << _Match.str();
}
_EXPORT_STD template <class _BidIt, class _Alloc = allocator<sub_match<_BidIt>>>
class match_results;
template <class _BidIt, class _Alloc, class _InIt, class _OutIt>
_OutIt _Format_default(const match_results<_BidIt, _Alloc>& _Match, _OutIt _Out, _InIt _First, _InIt _Last,
regex_constants::match_flag_type _Flags = regex_constants::format_default);
template <class _BidIt, class _Alloc, class _InIt, class _OutIt>
_OutIt _Format_sed(const match_results<_BidIt, _Alloc>& _Match, _OutIt _Out, _InIt _First, _InIt _Last,
regex_constants::match_flag_type _Flags = regex_constants::format_default);
_EXPORT_STD template <class _BidIt, class _Alloc>
class match_results { // class to hold contents of all capture groups
public:
using _Elem = sub_match<_BidIt>;
using _MyCont = vector<_Elem, _Alloc>;
using _Mytraits = allocator_traits<_Alloc>;
using value_type = _Elem;
using const_reference = const value_type&;
using reference = value_type&;
using const_iterator = typename _MyCont::const_iterator;
using iterator = const_iterator;
using difference_type = typename iterator_traits<_BidIt>::difference_type;
using size_type = typename _Mytraits::size_type;
using allocator_type = _Alloc;
using char_type = typename iterator_traits<_BidIt>::value_type;