-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsrell.hpp
11932 lines (9907 loc) · 299 KB
/
srell.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
/*****************************************************************************
**
** SRELL (std::regex-like library) version 4.063
**
** Copyright (c) 2012-2024, Nozomu Katoo. All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** 1. Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer.
**
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
** IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
******************************************************************************
*/
#ifndef SRELL_REGEX_TEMPLATE_LIBRARY
#define SRELL_REGEX_TEMPLATE_LIBRARY
#include <stdexcept>
#include <climits>
#include <cwchar>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstddef>
#include <utility>
#include <vector>
#include <iterator>
#include <memory>
#include <algorithm>
#include <limits>
#if !defined(SRELL_NO_UNISTACK) && (defined(__cplusplus) && (__cplusplus >= 201103L)) || (defined(_MSC_VER) && (_MSC_VER >= 1900))
#include <type_traits>
#define SRELL_HAS_TYPE_TRAITS
#endif
#if !defined(SRELL_NO_SIMD)
#if (defined(_M_X64) && !defined(_M_ARM64EC)) || defined(__x86_64__) || defined(_M_IX86) || defined(__i386__)
#if defined(__SSE4_2__)
#define SRELL_HAS_SSE42
#elif defined(_MSC_VER) && (_MSC_VER >= 1500)
#define SRELL_HAS_SSE42
#elif defined(__clang__) && defined(__clang_major__) && ((__clang_major__ >= 4) || ((__clang_major__ == 3) && defined(__clang_minor__) && (__clang_minor__ >= 8)))
#define SRELL_HAS_SSE42
#elif defined(__GNUC__) && ((__GNUC__ >= 5) || ((__GNUC__ == 4) && defined(__GNUC_MINOR__) && (__GNUC_MINOR__ >= 9)))
#define SRELL_HAS_SSE42
#endif // sse 4.2.
#endif // x86/x64.
#endif
#define SRELL_AT_SSE42
#if defined(SRELL_HAS_SSE42)
#if defined(_MSC_VER)
#include <intrin.h>
#else
#include <x86intrin.h>
#if !defined(__SSE4_2__)
#undef SRELL_AT_SSE42
#define SRELL_AT_SSE42 __attribute__((target("sse4.2")))
#endif
#endif
#endif
// The following SRELL_NO_* macros would be useful for reducing the
// size of an executable file by turning off some feature(s).
#ifdef SRELL_NO_UNICODE_DATA
// Prevents Unicode data used for icase (case-insensitive) matching
// from being output into a resulting binary. In this case only the
// ASCII characters are case-folded when icase matching is performed
// (i.e., [A-Z] -> [a-z] only).
#define SRELL_NO_UNICODE_ICASE
// Disables the Unicode property (\p{...} and \P{...}) and prevents
// Unicode property data from being output into a resulting binary.
#define SRELL_NO_UNICODE_PROPERTY
#endif
// Prevents icase matching specific functions into a resulting binary.
// In this case the icase flag is ignored and icase matching becomes
// unavailable.
#ifdef SRELL_NO_ICASE
#ifndef SRELL_NO_UNICODE_ICASE
#define SRELL_NO_UNICODE_ICASE
#endif
#endif
// This macro might be removed in the future.
#ifdef SRELL_V1_COMPATIBLE
#ifndef SRELL_NO_UNICODE_PROPERTY
#define SRELL_NO_UNICODE_PROPERTY
#endif
#define SRELL_NO_NAMEDCAPTURE
#define SRELL_NO_SINGLELINE
//#define SRELL_FIXEDWIDTHLOOKBEHIND
// Since version 4.019, SRELL highly depends on the variable-length
// lookbehind feature. Uncommenting this line is not recommended.
#endif
#if defined(_MSC_VER)
#define SRELL_NO_VCWARNING(n) \
__pragma(warning(push)) \
__pragma(warning(disable:n))
#define SRELL_NO_VCWARNING_END __pragma(warning(pop))
#else
#define SRELL_NO_VCWARNING(x)
#define SRELL_NO_VCWARNING_END
#endif
#if defined(__cpp_rvalue_references) && (!defined(_MSC_VER) || (_MSC_VER >= 1900))
#define SRELL_NOEXCEPT noexcept
#else
#define SRELL_NOEXCEPT
#endif
namespace srell
{
// ["regex_constants.h" ...
namespace regex_constants
{
enum syntax_option_type
{
icase = 1 << 1,
nosubs = 1 << 2,
optimize = 1 << 3,
collate = 0,
ECMAScript = 1 << 0,
basic = 0,
extended = 0,
awk = 0,
grep = 0,
egrep = 0,
multiline = 1 << 4,
// SRELL's extension.
sticky = 1 << 5,
dotall = 1 << 6, // singleline.
unicodesets = 1 << 7,
// For internal use.
back_ = 1 << 8,
pflagsmask_ = (1 << 8) - 1
};
inline syntax_option_type operator&(const syntax_option_type left, const syntax_option_type right)
{
return static_cast<syntax_option_type>(static_cast<int>(left) & static_cast<int>(right));
}
inline syntax_option_type operator|(const syntax_option_type left, const syntax_option_type right)
{
return static_cast<syntax_option_type>(static_cast<int>(left) | static_cast<int>(right));
}
inline syntax_option_type operator^(const syntax_option_type left, const syntax_option_type right)
{
return static_cast<syntax_option_type>(static_cast<int>(left) ^ static_cast<int>(right));
}
inline syntax_option_type operator~(const syntax_option_type b)
{
return static_cast<syntax_option_type>(~static_cast<int>(b));
}
inline syntax_option_type &operator&=(syntax_option_type &left, const syntax_option_type right)
{
left = left & right;
return left;
}
inline syntax_option_type &operator|=(syntax_option_type &left, const syntax_option_type right)
{
left = left | right;
return left;
}
inline syntax_option_type &operator^=(syntax_option_type &left, const syntax_option_type right)
{
left = left ^ right;
return left;
}
}
// namespace regex_constants
namespace regex_constants
{
enum match_flag_type
{
match_default = 0,
match_not_bol = 1 << 0,
match_not_eol = 1 << 1,
match_not_bow = 1 << 2,
match_not_eow = 1 << 3,
match_any = 0,
match_not_null = 1 << 4,
match_continuous = 1 << 5,
match_prev_avail = 1 << 6,
format_default = 0,
format_sed = 0,
format_no_copy = 1 << 7,
format_first_only = 1 << 8,
// For internal use.
match_match_ = 1 << 9
};
inline match_flag_type operator&(const match_flag_type left, const match_flag_type right)
{
return static_cast<match_flag_type>(static_cast<int>(left) & static_cast<int>(right));
}
inline match_flag_type operator|(const match_flag_type left, const match_flag_type right)
{
return static_cast<match_flag_type>(static_cast<int>(left) | static_cast<int>(right));
}
inline match_flag_type operator^(const match_flag_type left, const match_flag_type right)
{
return static_cast<match_flag_type>(static_cast<int>(left) ^ static_cast<int>(right));
}
inline match_flag_type operator~(const match_flag_type b)
{
return static_cast<match_flag_type>(~static_cast<int>(b));
}
inline match_flag_type &operator&=(match_flag_type &left, const match_flag_type right)
{
left = left & right;
return left;
}
inline match_flag_type &operator|=(match_flag_type &left, const match_flag_type right)
{
left = left | right;
return left;
}
inline match_flag_type &operator^=(match_flag_type &left, const match_flag_type right)
{
left = left ^ right;
return left;
}
}
// namespace regex_constants
// 28.5, regex constants:
namespace regex_constants
{
typedef int error_type;
static const error_type error_collate = 100;
static const error_type error_ctype = 101;
static const error_type error_escape = 102;
static const error_type error_backref = 103;
static const error_type error_brack = 104;
static const error_type error_paren = 105;
static const error_type error_brace = 106;
static const error_type error_badbrace = 107;
static const error_type error_range = 108;
static const error_type error_space = 109;
static const error_type error_badrepeat = 110;
static const error_type error_complexity = 111;
static const error_type error_stack = 112;
// SRELL's extensions.
static const error_type error_utf8 = 113;
// The expression contained an invalid UTF-8 sequence.
static const error_type error_property = 114;
// The expression contained an invalid Unicode property name or value.
static const error_type error_noescape = 115;
// (Only in v-mode) ( ) [ ] { } / - \ | need to be escaped in a character class.
static const error_type error_operator = 116;
// (Only in v-mode) A character class contained a reserved double punctuation
// operator or different types of operators at the same level, such as [ab--cd].
static const error_type error_complement = 117;
// (Only in v-mode) \P or a negated character class contained a property of strings.
static const error_type error_modifier = 118;
// A specific flag modifier appeared more then once, or the un-bounded form
// ((?ism-ism)) appeared at a position other than the beginning of the expression.
#if defined(SRELL_FIXEDWIDTHLOOKBEHIND)
static const error_type error_lookbehind = 200;
#endif
static const error_type error_internal = 999;
}
// namespace regex_constants
// ... "regex_constants.h"]
// ["regex_error.hpp" ...
// 28.6, class regex_error:
class regex_error : public std::runtime_error
{
public:
explicit regex_error(const regex_constants::error_type ecode)
: std::runtime_error("regex_error") // added for error C2512.
, ecode_(ecode)
{
}
regex_constants::error_type code() const
{
return ecode_;
}
private:
regex_constants::error_type ecode_;
};
// ... "regex_error.hpp"]
// ["rei_type.h" ...
namespace re_detail
{
#if defined(__cpp_unicode_characters)
typedef char32_t ui_l32; // uint_least32.
#elif defined(UINT_MAX) && UINT_MAX >= 0xFFFFFFFF
typedef unsigned int ui_l32;
#elif defined(ULONG_MAX) && ULONG_MAX >= 0xFFFFFFFF
typedef unsigned long ui_l32;
#else
#error could not find a suitable type for 32-bit Unicode integer values.
#endif // defined(__cpp_unicode_characters)
} // namespace re_detail
// ... "rei_type.h"]
// ["rei_constants.h" ...
namespace re_detail
{
enum re_state_type
{
st_character, // 0x00
st_character_class, // 0x01
st_epsilon, // 0x02
st_check_counter, // 0x03
st_increment_counter, // 0x04
st_decrement_counter, // 0x05
st_save_and_reset_counter, // 0x06
st_restore_counter, // 0x07
st_roundbracket_open, // 0x08
st_roundbracket_pop, // 0x09
st_roundbracket_close, // 0x0a
st_repeat_in_push, // 0x0b
st_repeat_in_pop, // 0x0c
st_check_0_width_repeat, // 0x0d
st_backreference, // 0x0e
st_lookaround_open, // 0x0f
st_lookaround_pop, // 0x10
st_bol, // 0x11
st_eol, // 0x12
st_boundary, // 0x13
st_success, // 0x14
#if defined(SRELLTEST_NEXTPOS_OPT)
st_move_nextpos, // 0x15
#endif
st_lookaround_close = st_success,
st_zero_width_boundary = st_lookaround_open,
};
// re_state_type
namespace constants
{
static const ui_l32 unicode_max_codepoint = 0x10ffff;
static const ui_l32 invalid_u32value = static_cast<ui_l32>(-1);
static const ui_l32 max_u32value = static_cast<ui_l32>(-2);
static const ui_l32 ccstr_empty = static_cast<ui_l32>(-1);
static const ui_l32 infinity = static_cast<ui_l32>(~0);
}
// constants
namespace masks
{
static const ui_l32 asc_icase = 0x20;
static const ui_l32 pos_cf = 0x200000; // 1 << 21.
static const ui_l32 pos_char = 0x1fffff;
static const ui_l32 fcc_simd = 0xffffff00;
static const ui_l32 fcc_simd_num = 0xff;
}
// masks
namespace sflags
{
static const ui_l32 is_not = 1;
static const ui_l32 icase = 1;
static const ui_l32 multiline = 1;
static const ui_l32 backrefno_unresolved = 1 << 1;
static const ui_l32 hooking = 1 << 2;
static const ui_l32 hookedlast = 1 << 3;
static const ui_l32 byn2 = 1 << 4;
static const ui_l32 clrn2 = 1 << 5;
}
// sflags
namespace meta_char
{
static const ui_l32 mc_exclam = 0x21; // '!'
static const ui_l32 mc_sharp = 0x23; // '#'
static const ui_l32 mc_dollar = 0x24; // '$'
static const ui_l32 mc_rbraop = 0x28; // '('
static const ui_l32 mc_rbracl = 0x29; // ')'
static const ui_l32 mc_astrsk = 0x2a; // '*'
static const ui_l32 mc_plus = 0x2b; // '+'
static const ui_l32 mc_comma = 0x2c; // ','
static const ui_l32 mc_minus = 0x2d; // '-'
static const ui_l32 mc_period = 0x2e; // '.'
static const ui_l32 mc_colon = 0x3a; // ':'
static const ui_l32 mc_lt = 0x3c; // '<'
static const ui_l32 mc_eq = 0x3d; // '='
static const ui_l32 mc_gt = 0x3e; // '>'
static const ui_l32 mc_query = 0x3f; // '?'
static const ui_l32 mc_sbraop = 0x5b; // '['
static const ui_l32 mc_escape = 0x5c; // '\\'
static const ui_l32 mc_sbracl = 0x5d; // ']'
static const ui_l32 mc_caret = 0x5e; // '^'
static const ui_l32 mc_cbraop = 0x7b; // '{'
static const ui_l32 mc_bar = 0x7c; // '|'
static const ui_l32 mc_cbracl = 0x7d; // '}'
}
// meta_char
namespace char_ctrl
{
static const ui_l32 cc_nul = 0x00; // '\0' //0x00:NUL
static const ui_l32 cc_bs = 0x08; // '\b' //0x08:BS
static const ui_l32 cc_htab = 0x09; // '\t' //0x09:HT
static const ui_l32 cc_nl = 0x0a; // '\n' //0x0a:LF
static const ui_l32 cc_vtab = 0x0b; // '\v' //0x0b:VT
static const ui_l32 cc_ff = 0x0c; // '\f' //0x0c:FF
static const ui_l32 cc_cr = 0x0d; // '\r' //0x0d:CR
}
// char_ctrl
namespace char_alnum
{
static const ui_l32 ch_0 = 0x30; // '0'
static const ui_l32 ch_1 = 0x31; // '1'
static const ui_l32 ch_7 = 0x37; // '7'
static const ui_l32 ch_8 = 0x38; // '8'
static const ui_l32 ch_9 = 0x39; // '9'
static const ui_l32 ch_A = 0x41; // 'A'
static const ui_l32 ch_B = 0x42; // 'B'
static const ui_l32 ch_D = 0x44; // 'D'
static const ui_l32 ch_F = 0x46; // 'F'
static const ui_l32 ch_P = 0x50; // 'P'
static const ui_l32 ch_S = 0x53; // 'S'
static const ui_l32 ch_W = 0x57; // 'W'
static const ui_l32 ch_Z = 0x5a; // 'Z'
static const ui_l32 ch_a = 0x61; // 'a'
static const ui_l32 ch_b = 0x62; // 'b'
static const ui_l32 ch_c = 0x63; // 'c'
static const ui_l32 ch_d = 0x64; // 'd'
static const ui_l32 ch_f = 0x66; // 'f'
static const ui_l32 ch_i = 0x69; // 'i'
static const ui_l32 ch_k = 0x6b; // 'k'
static const ui_l32 ch_m = 0x6d; // 'm'
static const ui_l32 ch_n = 0x6e; // 'n'
static const ui_l32 ch_p = 0x70; // 'p'
static const ui_l32 ch_q = 0x71; // 'q'
static const ui_l32 ch_r = 0x72; // 'r'
static const ui_l32 ch_s = 0x73; // 's'
static const ui_l32 ch_t = 0x74; // 't'
static const ui_l32 ch_u = 0x75; // 'u'
static const ui_l32 ch_v = 0x76; // 'v'
static const ui_l32 ch_w = 0x77; // 'w'
static const ui_l32 ch_x = 0x78; // 'x'
static const ui_l32 ch_z = 0x7a; // 'z'
}
// char_alnum
namespace char_other
{
static const ui_l32 co_perc = 0x25; // '%'
static const ui_l32 co_amp = 0x26; // '&'
static const ui_l32 co_apos = 0x27; // '\''
static const ui_l32 co_slash = 0x2f; // '/'
static const ui_l32 co_smcln = 0x3b; // ';'
static const ui_l32 co_atmrk = 0x40; // '@'
static const ui_l32 co_ll = 0x5f; // '_'
static const ui_l32 co_grav = 0x60; // '`'
static const ui_l32 co_tilde = 0x7e; // '~'
}
// char_other
namespace epsilon_type // Used only in the pattern compiler.
{
static const ui_l32 et_dfastrsk = 0x40; // '@'
static const ui_l32 et_ccastrsk = 0x2a; // '*'
static const ui_l32 et_alt = 0x7c; // '|'
static const ui_l32 et_ncgopen = 0x3a; // ':'
static const ui_l32 et_ncgclose = 0x3b; // ';'
static const ui_l32 et_jmpinlp = 0x2b; // '+'
static const ui_l32 et_brnchend = 0x2f; // '/'
static const ui_l32 et_fmrbckrf = 0x5c; // '\\'
static const ui_l32 et_bo1fmrbr = 0x31; // '1'
static const ui_l32 et_bo2fmrbr = 0x32; // '2'
static const ui_l32 et_bo2skpd = 0x21; // '!'
static const ui_l32 et_rvfmrcg = 0x28; // '('
static const ui_l32 et_mfrfmrcg = 0x29; // ')'
static const ui_l32 et_aofmrast = 0x78; // 'x'
}
// epsilon_type
}
// namespace re_detail
// ... "rei_constants.h"]
// ["rei_utf_traits.hpp" ...
namespace re_detail
{
#if defined(_MSC_VER)
#define SRELL_FORCEINLINE __forceinline
#elif defined(__GNUC__)
#define SRELL_FORCEINLINE __attribute__((always_inline))
#else
#define SRELL_FORCEINLINE
#endif
template <typename charT>
struct utf_traits_core
{
public:
typedef charT char_type;
enum
{
maxseqlen = 1,
charbit = (sizeof (charT) * CHAR_BIT) > 21 ? 21 : (sizeof (charT) * CHAR_BIT)
};
static const ui_l32 bitsetsize = static_cast<ui_l32>(1) << charbit;
static const ui_l32 bitsetmask = bitsetsize - 1;
static const ui_l32 maxcpvalue = charbit == 21 ? 0x10ffff : bitsetmask;
// *iter++
template <typename ForwardIterator>
static ui_l32 codepoint_inc(ForwardIterator &begin, const ForwardIterator /* end */)
{
return static_cast<ui_l32>(*begin++);
// Caller is responsible for begin != end.
}
// *--iter
template <typename BidirectionalIterator>
static ui_l32 dec_codepoint(BidirectionalIterator &cur, const BidirectionalIterator /* begin */)
{
return static_cast<ui_l32>(*--cur);
// Caller is responsible for cur != begin.
}
template <typename I> // ui_l32 or char_type2.
static bool is_mculeading(const I)
{
return false;
}
template <typename charT2>
static bool is_trailing(const charT2 /* cu */)
{
return false;
}
static ui_l32 to_codeunits(charT out[maxseqlen], ui_l32 cp)
{
out[0] = static_cast<charT>(cp);
return 1;
}
static ui_l32 seqlen(const ui_l32)
{
return 1;
}
static ui_l32 firstcodeunit(const ui_l32 cp)
{
return cp;
}
static ui_l32 nextlengthchange(const ui_l32)
{
return maxcpvalue + 1;
}
};
template <typename charT> const ui_l32 utf_traits_core<charT>::bitsetsize;
template <typename charT> const ui_l32 utf_traits_core<charT>::bitsetmask;
template <typename charT> const ui_l32 utf_traits_core<charT>::maxcpvalue;
// utf_traits_core
// common and utf-32.
template <typename charT>
struct utf_traits : public utf_traits_core<charT>
{
};
// utf_traits
// utf-8 specific.
template <typename charT>
struct utf8_traits : public utf_traits_core<charT>
{
public:
enum
{
maxseqlen = 4,
charbit = 8
};
static const ui_l32 bitsetsize = 0x100;
static const ui_l32 bitsetmask = 0xff;
static const ui_l32 maxcpvalue = 0x10ffff;
template <typename ForwardIterator>
static SRELL_FORCEINLINE ui_l32 codepoint_inc(ForwardIterator &begin, const ForwardIterator end)
{
ui_l32 codepoint = static_cast<ui_l32>(*begin++ & 0xff);
if ((codepoint & 0x80) == 0)
return codepoint;
if (begin != end)
{
// codepoint = static_cast<ui_l32>((codepoint << 6) | _pdep_u32(*begin, 0xc03f));
codepoint = static_cast<ui_l32>((*begin & 0x3f) | ((*begin & 0xc0) << 8) | (codepoint << 6));
++begin;
// 1011 0aaa aabb bbbb?
if ((codepoint - 0xb080) < 0x780)
return static_cast<ui_l32>(codepoint & 0x7ff);
if (begin != end)
{
codepoint = static_cast<ui_l32>((*begin & 0x3f) | ((*begin & 0xc0) << 16) | (codepoint << 6));
++begin;
// 1010 1110 aaaa bbbb bbcc cccc?
if ((codepoint - 0xae0800) < 0xf800)
return static_cast<ui_l32>(codepoint & 0xffff);
if (begin != end)
{
codepoint = static_cast<ui_l32>((*begin & 0x3f) | ((*begin & 0xc0) << 24) | (codepoint << 6));
++begin;
// 1010 1011 110a aabb bbbb cccc ccdd dddd?
if ((codepoint - 0xabc10000) < 0x100000)
return static_cast<ui_l32>(codepoint & 0x1fffff);
}
}
}
return re_detail::constants::invalid_u32value;
}
template <typename BidirectionalIterator>
static SRELL_FORCEINLINE ui_l32 dec_codepoint(BidirectionalIterator &cur, const BidirectionalIterator begin)
{
ui_l32 codepoint = static_cast<ui_l32>(*--cur);
if ((codepoint & 0x80) == 0)
return static_cast<ui_l32>(codepoint & 0xff);
if (cur != begin)
{
codepoint = static_cast<ui_l32>((codepoint & 0x3f) | ((codepoint & 0xc0) << 8) | ((*--cur & 0xff) << 6));
// 1011 0bbb bbaa aaaa?
if ((codepoint - 0xb080) < 0x780)
return static_cast<ui_l32>(codepoint & 0x7ff);
if (cur != begin)
{
codepoint = static_cast<ui_l32>((codepoint & 0xfff) | ((codepoint & 0xf000) << 8) | ((*--cur & 0xff) << 12));
// 1010 1110 cccc bbbb bbaa aaaa?
if ((codepoint - 0xae0800) < 0xf800)
return static_cast<ui_l32>(codepoint & 0xffff);
if (cur != begin)
{
codepoint = static_cast<ui_l32>((codepoint & 0x3ffff) | ((codepoint & 0xfc0000) << 8) | ((*--cur & 0xff) << 18));
// 1010 1011 110d ddcc cccc bbbb bbaa aaaa?
if ((codepoint - 0xabc10000) < 0x100000)
return static_cast<ui_l32>(codepoint & 0x1fffff);
}
}
}
return re_detail::constants::invalid_u32value;
}
template <typename I>
static bool is_mculeading(const I c)
{
return (c & 0x80) ? true : false;
}
template <typename charT2>
static bool is_trailing(const charT2 cu)
{
return (cu & 0xc0) == 0x80;
}
static ui_l32 to_codeunits(charT out[maxseqlen], ui_l32 cp)
{
if (cp < 0x80)
{
out[0] = static_cast<charT>(cp);
return 1;
}
else if (cp < 0x800)
{
out[0] = static_cast<charT>(((cp >> 6) & 0x1f) | 0xc0);
out[1] = static_cast<charT>((cp & 0x3f) | 0x80);
return 2;
}
else if (cp < 0x10000)
{
out[0] = static_cast<charT>(((cp >> 12) & 0x0f) | 0xe0);
out[1] = static_cast<charT>(((cp >> 6) & 0x3f) | 0x80);
out[2] = static_cast<charT>((cp & 0x3f) | 0x80);
return 3;
}
out[0] = static_cast<charT>(((cp >> 18) & 0x07) | 0xf0);
out[1] = static_cast<charT>(((cp >> 12) & 0x3f) | 0x80);
out[2] = static_cast<charT>(((cp >> 6) & 0x3f) | 0x80);
out[3] = static_cast<charT>((cp & 0x3f) | 0x80);
return 4;
}
static ui_l32 seqlen(const ui_l32 cp)
{
return (cp < 0x80) ? 1 : ((cp < 0x800) ? 2 : ((cp < 0x10000) ? 3 : 4));
}
static ui_l32 firstcodeunit(const ui_l32 cp)
{
if (cp < 0x80)
return cp;
if (cp < 0x800)
return static_cast<ui_l32>(((cp >> 6) & 0x1f) | 0xc0);
if (cp < 0x10000)
return static_cast<ui_l32>(((cp >> 12) & 0x0f) | 0xe0);
return static_cast<ui_l32>(((cp >> 18) & 0x07) | 0xf0);
}
static ui_l32 nextlengthchange(const ui_l32 cp)
{
return (cp < 0x80) ? 0x80 : ((cp < 0x800) ? 0x800 : ((cp < 0x10000) ? 0x10000 : 0x110000));
}
};
template <typename charT> const ui_l32 utf8_traits<charT>::bitsetsize;
template <typename charT> const ui_l32 utf8_traits<charT>::bitsetmask;
template <typename charT> const ui_l32 utf8_traits<charT>::maxcpvalue;
// utf8_traits
// utf-16 specific.
template <typename charT>
struct utf16_traits : public utf_traits_core<charT>
{
public:
enum
{
maxseqlen = 2,
charbit = 16
};
static const ui_l32 bitsetsize = 0x10000;
static const ui_l32 bitsetmask = 0xffff;
static const ui_l32 maxcpvalue = 0x10ffff;
template <typename ForwardIterator>
static SRELL_FORCEINLINE ui_l32 codepoint_inc(ForwardIterator &begin, const ForwardIterator end)
{
const ui_l32 codeunit = *begin++;
if ((codeunit & 0xfc00) != 0xd800)
return static_cast<ui_l32>(codeunit & 0xffff);
if (begin != end && (*begin & 0xfc00) == 0xdc00)
return static_cast<ui_l32>((((codeunit & 0x3ff) << 10) | (*begin++ & 0x3ff)) + 0x10000);
return static_cast<ui_l32>(codeunit & 0xffff);
}
template <typename BidirectionalIterator>
static SRELL_FORCEINLINE ui_l32 dec_codepoint(BidirectionalIterator &cur, const BidirectionalIterator begin)
{
const ui_l32 codeunit = *--cur;
if ((codeunit & 0xfc00) != 0xdc00 || cur == begin)
return static_cast<ui_l32>(codeunit & 0xffff);
if ((*--cur & 0xfc00) == 0xd800)
return static_cast<ui_l32>((((*cur & 0x3ff) << 10) | (codeunit & 0x3ff)) + 0x10000);
++cur;
return static_cast<ui_l32>(codeunit & 0xffff);
}
template <typename I>
static bool is_mculeading(const I c)
{
return (c & 0xfc00) == 0xd800;
}
template <typename charT2>
static bool is_trailing(const charT2 cu)
{
return (cu & 0xfc00) == 0xdc00;
}
static ui_l32 to_codeunits(charT out[maxseqlen], ui_l32 cp)
{
if (cp < 0x10000)
{
out[0] = static_cast<charT>(cp);
return 1;
}
cp -= 0x10000;
out[0] = static_cast<charT>(((cp >> 10) & 0x3ff) | 0xd800);
out[1] = static_cast<charT>((cp & 0x3ff) | 0xdc00);
return 2;
}
static ui_l32 seqlen(const ui_l32 cp)
{
return (cp < 0x10000) ? 1 : 2;
}
static ui_l32 firstcodeunit(const ui_l32 cp)
{
if (cp < 0x10000)
return cp;
return static_cast<ui_l32>((cp >> 10) + 0xd7c0);
// aaaaa bbbbcccc ddddeeee -> AA AAbb bbcc/cc dddd eeee where AAAA = aaaaa - 1.
}
static ui_l32 nextlengthchange(const ui_l32 cp)
{
return (cp < 0x10000) ? 0x10000 : 0x110000;
}
};
template <typename charT> const ui_l32 utf16_traits<charT>::bitsetsize;
template <typename charT> const ui_l32 utf16_traits<charT>::bitsetmask;
template <typename charT> const ui_l32 utf16_traits<charT>::maxcpvalue;
// utf16_traits
// specialisation for char.
template <>
struct utf_traits<char> : public utf_traits_core<char>
{
public:
template <typename ForwardIterator>
static ui_l32 codepoint_inc(ForwardIterator &begin, const ForwardIterator /* end */)
{
return static_cast<ui_l32>(static_cast<unsigned char>(*begin++));
}
template <typename BidirectionalIterator>
static ui_l32 dec_codepoint(BidirectionalIterator &cur, const BidirectionalIterator /* begin */)
{
return static_cast<ui_l32>(static_cast<unsigned char>(*--cur));
}
}; // utf_traits<char>
// specialisation for signed char.
template <>
struct utf_traits<signed char> : public utf_traits<char>
{
};
// (signed) short, (signed) int, (signed) long, (signed) long long, ...
#if defined(__cpp_unicode_characters)
template <>
struct utf_traits<char16_t> : public utf16_traits<char16_t>
{
};
#endif
#if defined(__cpp_char8_t)
template <>
struct utf_traits<char8_t> : public utf8_traits<char8_t>
{
};
#endif
} // re_detail
// ... "rei_utf_traits.hpp"]
// ["regex_traits.hpp" ...
// 28.7, class template regex_traits:
template <class charT>
struct regex_traits
{
public:
typedef charT char_type;
typedef std::basic_string<char_type> string_type;
typedef int locale_type;
typedef int char_class_type;
typedef re_detail::utf_traits<charT> utf_traits;
}; // regex_traits
template <class charT>
struct u8regex_traits : public regex_traits<charT>
{
typedef re_detail::utf8_traits<charT> utf_traits;
};
template <class charT>
struct u16regex_traits : public regex_traits<charT>
{
typedef re_detail::utf16_traits<charT> utf_traits;
};
// ... "regex_traits.hpp"]
// ["rei_memory.hpp" ...
namespace re_detail
{
/*
* Similar to std::basic_string, except for:
* a. only allocates memory, does not initialise it.
* b. uses realloc() to avoid moving data as much as possible when
* resizing an allocated buffer.
*/
template <typename ElemT>
class simple_array
{
public:
typedef ElemT value_type;
typedef std::size_t size_type;
typedef ElemT &reference;
typedef const ElemT &const_reference;
typedef ElemT *pointer;
typedef const ElemT *const_pointer;
static const size_type npos = static_cast<size_type>(-1);
public:
simple_array()
: buffer_(NULL)
, size_(0)
, capacity_(0)
{
}
simple_array(const size_type initsize)
: buffer_(NULL)
, size_(0)
, capacity_(0)
{