-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
basic_regex_creator.hpp
1575 lines (1522 loc) · 52.8 KB
/
basic_regex_creator.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
/*
*
* Copyright (c) 2004
* John Maddock
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
/*
* LOCATION: see http://www.boost.org for most recent version.
* FILE basic_regex_creator.cpp
* VERSION see <boost/version.hpp>
* DESCRIPTION: Declares template class basic_regex_creator which fills in
* the data members of a regex_data object.
*/
#ifndef BOOST_REGEX_V5_BASIC_REGEX_CREATOR_HPP
#define BOOST_REGEX_V5_BASIC_REGEX_CREATOR_HPP
#include <boost/regex/v5/indexed_bit_flag.hpp>
#ifdef BOOST_REGEX_MSVC
# pragma warning(push)
#pragma warning(disable:4459)
#if BOOST_REGEX_MSVC < 1910
#pragma warning(disable:4800)
#endif
#endif
namespace boost{
namespace BOOST_REGEX_DETAIL_NS{
template <class charT>
struct digraph : public std::pair<charT, charT>
{
digraph() : std::pair<charT, charT>(charT(0), charT(0)){}
digraph(charT c1) : std::pair<charT, charT>(c1, charT(0)){}
digraph(charT c1, charT c2) : std::pair<charT, charT>(c1, c2)
{}
digraph(const digraph<charT>& d) : std::pair<charT, charT>(d.first, d.second){}
digraph<charT>& operator=(const digraph<charT>&) = default;
template <class Seq>
digraph(const Seq& s) : std::pair<charT, charT>()
{
BOOST_REGEX_ASSERT(s.size() <= 2);
BOOST_REGEX_ASSERT(s.size());
this->first = s[0];
this->second = (s.size() > 1) ? s[1] : 0;
}
};
template <class charT, class traits>
class basic_char_set
{
public:
typedef digraph<charT> digraph_type;
typedef typename traits::string_type string_type;
typedef typename traits::char_class_type m_type;
basic_char_set()
{
m_negate = false;
m_has_digraphs = false;
m_classes = 0;
m_negated_classes = 0;
m_empty = true;
}
void add_single(const digraph_type& s)
{
m_singles.insert(s);
if(s.second)
m_has_digraphs = true;
m_empty = false;
}
void add_range(const digraph_type& first, const digraph_type& end)
{
m_ranges.push_back(first);
m_ranges.push_back(end);
if(first.second)
{
m_has_digraphs = true;
add_single(first);
}
if(end.second)
{
m_has_digraphs = true;
add_single(end);
}
m_empty = false;
}
void add_class(m_type m)
{
m_classes |= m;
m_empty = false;
}
void add_negated_class(m_type m)
{
m_negated_classes |= m;
m_empty = false;
}
void add_equivalent(const digraph_type& s)
{
m_equivalents.insert(s);
if(s.second)
{
m_has_digraphs = true;
add_single(s);
}
m_empty = false;
}
void negate()
{
m_negate = true;
//m_empty = false;
}
//
// accessor functions:
//
bool has_digraphs()const
{
return m_has_digraphs;
}
bool is_negated()const
{
return m_negate;
}
typedef typename std::vector<digraph_type>::const_iterator list_iterator;
typedef typename std::set<digraph_type>::const_iterator set_iterator;
set_iterator singles_begin()const
{
return m_singles.begin();
}
set_iterator singles_end()const
{
return m_singles.end();
}
list_iterator ranges_begin()const
{
return m_ranges.begin();
}
list_iterator ranges_end()const
{
return m_ranges.end();
}
set_iterator equivalents_begin()const
{
return m_equivalents.begin();
}
set_iterator equivalents_end()const
{
return m_equivalents.end();
}
m_type classes()const
{
return m_classes;
}
m_type negated_classes()const
{
return m_negated_classes;
}
bool empty()const
{
return m_empty;
}
private:
std::set<digraph_type> m_singles; // a list of single characters to match
std::vector<digraph_type> m_ranges; // a list of end points of our ranges
bool m_negate; // true if the set is to be negated
bool m_has_digraphs; // true if we have digraphs present
m_type m_classes; // character classes to match
m_type m_negated_classes; // negated character classes to match
bool m_empty; // whether we've added anything yet
std::set<digraph_type> m_equivalents; // a list of equivalence classes
};
template <class charT, class traits>
class basic_regex_creator
{
public:
basic_regex_creator(regex_data<charT, traits>* data);
std::ptrdiff_t getoffset(void* addr)
{
return getoffset(addr, m_pdata->m_data.data());
}
std::ptrdiff_t getoffset(const void* addr, const void* base)
{
return static_cast<const char*>(addr) - static_cast<const char*>(base);
}
re_syntax_base* getaddress(std::ptrdiff_t off)
{
return getaddress(off, m_pdata->m_data.data());
}
re_syntax_base* getaddress(std::ptrdiff_t off, void* base)
{
return static_cast<re_syntax_base*>(static_cast<void*>(static_cast<char*>(base) + off));
}
void init(unsigned l_flags)
{
m_pdata->m_flags = l_flags;
m_icase = l_flags & regex_constants::icase;
}
regbase::flag_type flags()
{
return m_pdata->m_flags;
}
void flags(regbase::flag_type f)
{
m_pdata->m_flags = f;
if(m_icase != static_cast<bool>(f & regbase::icase))
{
m_icase = static_cast<bool>(f & regbase::icase);
}
}
re_syntax_base* append_state(syntax_element_type t, std::size_t s = sizeof(re_syntax_base));
re_syntax_base* insert_state(std::ptrdiff_t pos, syntax_element_type t, std::size_t s = sizeof(re_syntax_base));
re_literal* append_literal(charT c);
re_syntax_base* append_set(const basic_char_set<charT, traits>& char_set);
re_syntax_base* append_set(const basic_char_set<charT, traits>& char_set, std::integral_constant<bool, false>*);
re_syntax_base* append_set(const basic_char_set<charT, traits>& char_set, std::integral_constant<bool, true>*);
void finalize(const charT* p1, const charT* p2);
protected:
regex_data<charT, traits>* m_pdata; // pointer to the basic_regex_data struct we are filling in
const ::boost::regex_traits_wrapper<traits>&
m_traits; // convenience reference to traits class
re_syntax_base* m_last_state; // the last state we added
bool m_icase; // true for case insensitive matches
unsigned m_repeater_id; // the state_id of the next repeater
bool m_has_backrefs; // true if there are actually any backrefs
indexed_bit_flag m_backrefs; // bitmask of permitted backrefs
std::uintmax_t m_bad_repeats; // bitmask of repeats we can't deduce a startmap for;
bool m_has_recursions; // set when we have recursive expressions to fixup
std::vector<unsigned char> m_recursion_checks; // notes which recursions we've followed while analysing this expression
typename traits::char_class_type m_word_mask; // mask used to determine if a character is a word character
typename traits::char_class_type m_mask_space; // mask used to determine if a character is a word character
typename traits::char_class_type m_lower_mask; // mask used to determine if a character is a lowercase character
typename traits::char_class_type m_upper_mask; // mask used to determine if a character is an uppercase character
typename traits::char_class_type m_alpha_mask; // mask used to determine if a character is an alphabetic character
private:
basic_regex_creator& operator=(const basic_regex_creator&);
basic_regex_creator(const basic_regex_creator&);
void fixup_pointers(re_syntax_base* state);
void fixup_recursions(re_syntax_base* state);
void create_startmaps(re_syntax_base* state);
int calculate_backstep(re_syntax_base* state);
void create_startmap(re_syntax_base* state, unsigned char* l_map, unsigned int* pnull, unsigned char mask);
unsigned get_restart_type(re_syntax_base* state);
void set_all_masks(unsigned char* bits, unsigned char);
bool is_bad_repeat(re_syntax_base* pt);
void set_bad_repeat(re_syntax_base* pt);
syntax_element_type get_repeat_type(re_syntax_base* state);
void probe_leading_repeat(re_syntax_base* state);
};
template <class charT, class traits>
basic_regex_creator<charT, traits>::basic_regex_creator(regex_data<charT, traits>* data)
: m_pdata(data), m_traits(*(data->m_ptraits)), m_last_state(0), m_icase(false), m_repeater_id(0),
m_has_backrefs(false), m_bad_repeats(0), m_has_recursions(false), m_word_mask(0), m_mask_space(0), m_lower_mask(0), m_upper_mask(0), m_alpha_mask(0)
{
m_pdata->m_data.clear();
m_pdata->m_status = ::boost::regex_constants::error_ok;
static const charT w = 'w';
static const charT s = 's';
static const charT l[5] = { 'l', 'o', 'w', 'e', 'r', };
static const charT u[5] = { 'u', 'p', 'p', 'e', 'r', };
static const charT a[5] = { 'a', 'l', 'p', 'h', 'a', };
m_word_mask = m_traits.lookup_classname(&w, &w +1);
m_mask_space = m_traits.lookup_classname(&s, &s +1);
m_lower_mask = m_traits.lookup_classname(l, l + 5);
m_upper_mask = m_traits.lookup_classname(u, u + 5);
m_alpha_mask = m_traits.lookup_classname(a, a + 5);
m_pdata->m_word_mask = m_word_mask;
BOOST_REGEX_ASSERT(m_word_mask != 0);
BOOST_REGEX_ASSERT(m_mask_space != 0);
BOOST_REGEX_ASSERT(m_lower_mask != 0);
BOOST_REGEX_ASSERT(m_upper_mask != 0);
BOOST_REGEX_ASSERT(m_alpha_mask != 0);
}
template <class charT, class traits>
re_syntax_base* basic_regex_creator<charT, traits>::append_state(syntax_element_type t, std::size_t s)
{
// if the state is a backref then make a note of it:
if(t == syntax_element_backref)
this->m_has_backrefs = true;
// append a new state, start by aligning our last one:
m_pdata->m_data.align();
// set the offset to the next state in our last one:
if(m_last_state)
m_last_state->next.i = m_pdata->m_data.size() - getoffset(m_last_state);
// now actually extend our data:
m_last_state = static_cast<re_syntax_base*>(m_pdata->m_data.extend(s));
// fill in boilerplate options in the new state:
m_last_state->next.i = 0;
m_last_state->type = t;
return m_last_state;
}
template <class charT, class traits>
re_syntax_base* basic_regex_creator<charT, traits>::insert_state(std::ptrdiff_t pos, syntax_element_type t, std::size_t s)
{
// append a new state, start by aligning our last one:
m_pdata->m_data.align();
// set the offset to the next state in our last one:
if(m_last_state)
m_last_state->next.i = m_pdata->m_data.size() - getoffset(m_last_state);
// remember the last state position:
std::ptrdiff_t off = getoffset(m_last_state) + s;
// now actually insert our data:
re_syntax_base* new_state = static_cast<re_syntax_base*>(m_pdata->m_data.insert(pos, s));
// fill in boilerplate options in the new state:
new_state->next.i = s;
new_state->type = t;
m_last_state = getaddress(off);
return new_state;
}
template <class charT, class traits>
re_literal* basic_regex_creator<charT, traits>::append_literal(charT c)
{
re_literal* result;
// start by seeing if we have an existing re_literal we can extend:
if((0 == m_last_state) || (m_last_state->type != syntax_element_literal))
{
// no existing re_literal, create a new one:
result = static_cast<re_literal*>(append_state(syntax_element_literal, sizeof(re_literal) + sizeof(charT)));
result->length = 1;
*static_cast<charT*>(static_cast<void*>(result+1)) = m_traits.translate(c, m_icase);
}
else
{
// we have an existing re_literal, extend it:
std::ptrdiff_t off = getoffset(m_last_state);
m_pdata->m_data.extend(sizeof(charT));
m_last_state = result = static_cast<re_literal*>(getaddress(off));
charT* characters = static_cast<charT*>(static_cast<void*>(result+1));
characters[result->length] = m_traits.translate(c, m_icase);
result->length += 1;
}
return result;
}
template <class charT, class traits>
inline re_syntax_base* basic_regex_creator<charT, traits>::append_set(
const basic_char_set<charT, traits>& char_set)
{
typedef std::integral_constant<bool, (sizeof(charT) == 1) > truth_type;
return char_set.has_digraphs()
? append_set(char_set, static_cast<std::integral_constant<bool, false>*>(0))
: append_set(char_set, static_cast<truth_type*>(0));
}
template <class charT, class traits>
re_syntax_base* basic_regex_creator<charT, traits>::append_set(
const basic_char_set<charT, traits>& char_set, std::integral_constant<bool, false>*)
{
typedef typename traits::string_type string_type;
typedef typename basic_char_set<charT, traits>::list_iterator item_iterator;
typedef typename basic_char_set<charT, traits>::set_iterator set_iterator;
typedef typename traits::char_class_type m_type;
re_set_long<m_type>* result = static_cast<re_set_long<m_type>*>(append_state(syntax_element_long_set, sizeof(re_set_long<m_type>)));
//
// fill in the basics:
//
result->csingles = static_cast<unsigned int>(std::distance(char_set.singles_begin(), char_set.singles_end()));
result->cranges = static_cast<unsigned int>(std::distance(char_set.ranges_begin(), char_set.ranges_end())) / 2;
result->cequivalents = static_cast<unsigned int>(std::distance(char_set.equivalents_begin(), char_set.equivalents_end()));
result->cclasses = char_set.classes();
result->cnclasses = char_set.negated_classes();
if(flags() & regbase::icase)
{
// adjust classes as needed:
if(((result->cclasses & m_lower_mask) == m_lower_mask) || ((result->cclasses & m_upper_mask) == m_upper_mask))
result->cclasses |= m_alpha_mask;
if(((result->cnclasses & m_lower_mask) == m_lower_mask) || ((result->cnclasses & m_upper_mask) == m_upper_mask))
result->cnclasses |= m_alpha_mask;
}
result->isnot = char_set.is_negated();
result->singleton = !char_set.has_digraphs();
//
// remember where the state is for later:
//
std::ptrdiff_t offset = getoffset(result);
//
// now extend with all the singles:
//
item_iterator first, last;
set_iterator sfirst, slast;
sfirst = char_set.singles_begin();
slast = char_set.singles_end();
while(sfirst != slast)
{
charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (sfirst->first == static_cast<charT>(0) ? 1 : sfirst->second ? 3 : 2)));
p[0] = m_traits.translate(sfirst->first, m_icase);
if(sfirst->first == static_cast<charT>(0))
{
p[0] = 0;
}
else if(sfirst->second)
{
p[1] = m_traits.translate(sfirst->second, m_icase);
p[2] = 0;
}
else
p[1] = 0;
++sfirst;
}
//
// now extend with all the ranges:
//
first = char_set.ranges_begin();
last = char_set.ranges_end();
while(first != last)
{
// first grab the endpoints of the range:
digraph<charT> c1 = *first;
c1.first = this->m_traits.translate(c1.first, this->m_icase);
c1.second = this->m_traits.translate(c1.second, this->m_icase);
++first;
digraph<charT> c2 = *first;
c2.first = this->m_traits.translate(c2.first, this->m_icase);
c2.second = this->m_traits.translate(c2.second, this->m_icase);
++first;
string_type s1, s2;
// different actions now depending upon whether collation is turned on:
if(flags() & regex_constants::collate)
{
// we need to transform our range into sort keys:
charT a1[3] = { c1.first, c1.second, charT(0), };
charT a2[3] = { c2.first, c2.second, charT(0), };
s1 = this->m_traits.transform(a1, (a1[1] ? a1+2 : a1+1));
s2 = this->m_traits.transform(a2, (a2[1] ? a2+2 : a2+1));
if(s1.empty())
s1 = string_type(1, charT(0));
if(s2.empty())
s2 = string_type(1, charT(0));
}
else
{
if(c1.second)
{
s1.insert(s1.end(), c1.first);
s1.insert(s1.end(), c1.second);
}
else
s1 = string_type(1, c1.first);
if(c2.second)
{
s2.insert(s2.end(), c2.first);
s2.insert(s2.end(), c2.second);
}
else
s2.insert(s2.end(), c2.first);
}
if(s1 > s2)
{
// Oops error:
return 0;
}
charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (s1.size() + s2.size() + 2) ) );
BOOST_REGEX_DETAIL_NS::copy(s1.begin(), s1.end(), p);
p[s1.size()] = charT(0);
p += s1.size() + 1;
BOOST_REGEX_DETAIL_NS::copy(s2.begin(), s2.end(), p);
p[s2.size()] = charT(0);
}
//
// now process the equivalence classes:
//
sfirst = char_set.equivalents_begin();
slast = char_set.equivalents_end();
while(sfirst != slast)
{
string_type s;
if(sfirst->second)
{
charT cs[3] = { sfirst->first, sfirst->second, charT(0), };
s = m_traits.transform_primary(cs, cs+2);
}
else
s = m_traits.transform_primary(&sfirst->first, &sfirst->first+1);
if(s.empty())
return 0; // invalid or unsupported equivalence class
charT* p = static_cast<charT*>(this->m_pdata->m_data.extend(sizeof(charT) * (s.size()+1) ) );
BOOST_REGEX_DETAIL_NS::copy(s.begin(), s.end(), p);
p[s.size()] = charT(0);
++sfirst;
}
//
// finally reset the address of our last state:
//
m_last_state = result = static_cast<re_set_long<m_type>*>(getaddress(offset));
return result;
}
template<class T>
inline bool char_less(T t1, T t2)
{
return t1 < t2;
}
inline bool char_less(char t1, char t2)
{
return static_cast<unsigned char>(t1) < static_cast<unsigned char>(t2);
}
inline bool char_less(signed char t1, signed char t2)
{
return static_cast<unsigned char>(t1) < static_cast<unsigned char>(t2);
}
template <class charT, class traits>
re_syntax_base* basic_regex_creator<charT, traits>::append_set(
const basic_char_set<charT, traits>& char_set, std::integral_constant<bool, true>*)
{
typedef typename traits::string_type string_type;
typedef typename basic_char_set<charT, traits>::list_iterator item_iterator;
typedef typename basic_char_set<charT, traits>::set_iterator set_iterator;
re_set* result = static_cast<re_set*>(append_state(syntax_element_set, sizeof(re_set)));
bool negate = char_set.is_negated();
std::memset(result->_map, 0, sizeof(result->_map));
//
// handle singles first:
//
item_iterator first, last;
set_iterator sfirst, slast;
sfirst = char_set.singles_begin();
slast = char_set.singles_end();
while(sfirst != slast)
{
for(unsigned int i = 0; i < (1 << CHAR_BIT); ++i)
{
if(this->m_traits.translate(static_cast<charT>(i), this->m_icase)
== this->m_traits.translate(sfirst->first, this->m_icase))
result->_map[i] = true;
}
++sfirst;
}
//
// OK now handle ranges:
//
first = char_set.ranges_begin();
last = char_set.ranges_end();
while(first != last)
{
// first grab the endpoints of the range:
charT c1 = this->m_traits.translate(first->first, this->m_icase);
++first;
charT c2 = this->m_traits.translate(first->first, this->m_icase);
++first;
// different actions now depending upon whether collation is turned on:
if(flags() & regex_constants::collate)
{
// we need to transform our range into sort keys:
charT c3[2] = { c1, charT(0), };
string_type s1 = this->m_traits.transform(c3, c3+1);
c3[0] = c2;
string_type s2 = this->m_traits.transform(c3, c3+1);
if(s1 > s2)
{
// Oops error:
return 0;
}
BOOST_REGEX_ASSERT(c3[1] == charT(0));
for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
{
c3[0] = static_cast<charT>(i);
string_type s3 = this->m_traits.transform(c3, c3 +1);
if((s1 <= s3) && (s3 <= s2))
result->_map[i] = true;
}
}
else
{
if(char_less(c2, c1))
{
// Oops error:
return 0;
}
// everything in range matches:
std::memset(result->_map + static_cast<unsigned char>(c1), true, static_cast<unsigned char>(1u) + static_cast<unsigned char>(static_cast<unsigned char>(c2) - static_cast<unsigned char>(c1)));
}
}
//
// and now the classes:
//
typedef typename traits::char_class_type m_type;
m_type m = char_set.classes();
if(flags() & regbase::icase)
{
// adjust m as needed:
if(((m & m_lower_mask) == m_lower_mask) || ((m & m_upper_mask) == m_upper_mask))
m |= m_alpha_mask;
}
if(m != 0)
{
for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
{
if(this->m_traits.isctype(static_cast<charT>(i), m))
result->_map[i] = true;
}
}
//
// and now the negated classes:
//
m = char_set.negated_classes();
if(flags() & regbase::icase)
{
// adjust m as needed:
if(((m & m_lower_mask) == m_lower_mask) || ((m & m_upper_mask) == m_upper_mask))
m |= m_alpha_mask;
}
if(m != 0)
{
for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
{
if(0 == this->m_traits.isctype(static_cast<charT>(i), m))
result->_map[i] = true;
}
}
//
// now process the equivalence classes:
//
sfirst = char_set.equivalents_begin();
slast = char_set.equivalents_end();
while(sfirst != slast)
{
string_type s;
BOOST_REGEX_ASSERT(static_cast<charT>(0) == sfirst->second);
s = m_traits.transform_primary(&sfirst->first, &sfirst->first+1);
if(s.empty())
return 0; // invalid or unsupported equivalence class
for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
{
charT c[2] = { (static_cast<charT>(i)), charT(0), };
string_type s2 = this->m_traits.transform_primary(c, c+1);
if(s == s2)
result->_map[i] = true;
}
++sfirst;
}
if(negate)
{
for(unsigned i = 0; i < (1u << CHAR_BIT); ++i)
{
result->_map[i] = !(result->_map[i]);
}
}
return result;
}
template <class charT, class traits>
void basic_regex_creator<charT, traits>::finalize(const charT* p1, const charT* p2)
{
if(this->m_pdata->m_status)
return;
// we've added all the states we need, now finish things off.
// start by adding a terminating state:
append_state(syntax_element_match);
// extend storage to store original expression:
std::ptrdiff_t len = p2 - p1;
m_pdata->m_expression_len = len;
charT* ps = static_cast<charT*>(m_pdata->m_data.extend(sizeof(charT) * (1 + (p2 - p1))));
m_pdata->m_expression = ps;
BOOST_REGEX_DETAIL_NS::copy(p1, p2, ps);
ps[p2 - p1] = 0;
// fill in our other data...
// successful parsing implies a zero status:
m_pdata->m_status = 0;
// get the first state of the machine:
m_pdata->m_first_state = static_cast<re_syntax_base*>(m_pdata->m_data.data());
// fixup pointers in the machine:
fixup_pointers(m_pdata->m_first_state);
if(m_has_recursions)
{
m_pdata->m_has_recursions = true;
fixup_recursions(m_pdata->m_first_state);
if(this->m_pdata->m_status)
return;
}
else
m_pdata->m_has_recursions = false;
// create nested startmaps:
create_startmaps(m_pdata->m_first_state);
// create main startmap:
std::memset(m_pdata->m_startmap, 0, sizeof(m_pdata->m_startmap));
m_pdata->m_can_be_null = 0;
m_bad_repeats = 0;
if(m_has_recursions)
m_recursion_checks.assign(1 + m_pdata->m_mark_count, 0u);
create_startmap(m_pdata->m_first_state, m_pdata->m_startmap, &(m_pdata->m_can_be_null), mask_all);
// get the restart type:
m_pdata->m_restart_type = get_restart_type(m_pdata->m_first_state);
// optimise a leading repeat if there is one:
probe_leading_repeat(m_pdata->m_first_state);
}
template <class charT, class traits>
void basic_regex_creator<charT, traits>::fixup_pointers(re_syntax_base* state)
{
while(state)
{
switch(state->type)
{
case syntax_element_recurse:
m_has_recursions = true;
if(state->next.i)
state->next.p = getaddress(state->next.i, state);
else
state->next.p = 0;
break;
case syntax_element_rep:
case syntax_element_dot_rep:
case syntax_element_char_rep:
case syntax_element_short_set_rep:
case syntax_element_long_set_rep:
// set the state_id of this repeat:
static_cast<re_repeat*>(state)->state_id = m_repeater_id++;
BOOST_REGEX_FALLTHROUGH;
case syntax_element_alt:
std::memset(static_cast<re_alt*>(state)->_map, 0, sizeof(static_cast<re_alt*>(state)->_map));
static_cast<re_alt*>(state)->can_be_null = 0;
BOOST_REGEX_FALLTHROUGH;
case syntax_element_jump:
static_cast<re_jump*>(state)->alt.p = getaddress(static_cast<re_jump*>(state)->alt.i, state);
BOOST_REGEX_FALLTHROUGH;
default:
if(state->next.i)
state->next.p = getaddress(state->next.i, state);
else
state->next.p = 0;
}
state = state->next.p;
}
}
template <class charT, class traits>
void basic_regex_creator<charT, traits>::fixup_recursions(re_syntax_base* state)
{
re_syntax_base* base = state;
while(state)
{
switch(state->type)
{
case syntax_element_assert_backref:
{
// just check that the index is valid:
int idx = static_cast<const re_brace*>(state)->index;
if(idx < 0)
{
idx = -idx-1;
if(idx >= hash_value_mask)
{
idx = m_pdata->get_id(idx);
if(idx <= 0)
{
// check of sub-expression that doesn't exist:
if(0 == this->m_pdata->m_status) // update the error code if not already set
this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
//
// clear the expression, we should be empty:
//
this->m_pdata->m_expression = 0;
this->m_pdata->m_expression_len = 0;
//
// and throw if required:
//
if(0 == (this->flags() & regex_constants::no_except))
{
std::string message = "Encountered a forward reference to a marked sub-expression that does not exist.";
boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
e.raise();
}
}
}
}
}
break;
case syntax_element_recurse:
{
bool ok = false;
re_syntax_base* p = base;
std::ptrdiff_t idx = static_cast<re_jump*>(state)->alt.i;
if(idx >= hash_value_mask)
{
//
// There may be more than one capture group with this hash, just do what Perl
// does and recurse to the leftmost:
//
idx = m_pdata->get_id(static_cast<int>(idx));
}
if(idx < 0)
{
ok = false;
}
else
{
while(p)
{
if((p->type == syntax_element_startmark) && (static_cast<re_brace*>(p)->index == idx))
{
//
// We've found the target of the recursion, set the jump target:
//
static_cast<re_jump*>(state)->alt.p = p;
ok = true;
//
// Now scan the target for nested repeats:
//
p = p->next.p;
int next_rep_id = 0;
while(p)
{
switch(p->type)
{
case syntax_element_rep:
case syntax_element_dot_rep:
case syntax_element_char_rep:
case syntax_element_short_set_rep:
case syntax_element_long_set_rep:
next_rep_id = static_cast<re_repeat*>(p)->state_id;
break;
case syntax_element_endmark:
if(static_cast<const re_brace*>(p)->index == idx)
next_rep_id = -1;
break;
default:
break;
}
if(next_rep_id)
break;
p = p->next.p;
}
if(next_rep_id > 0)
{
static_cast<re_recurse*>(state)->state_id = next_rep_id - 1;
}
break;
}
p = p->next.p;
}
}
if(!ok)
{
// recursion to sub-expression that doesn't exist:
if(0 == this->m_pdata->m_status) // update the error code if not already set
this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
//
// clear the expression, we should be empty:
//
this->m_pdata->m_expression = 0;
this->m_pdata->m_expression_len = 0;
//
// and throw if required:
//
if(0 == (this->flags() & regex_constants::no_except))
{
std::string message = "Encountered a forward reference to a recursive sub-expression that does not exist.";
boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
e.raise();
}
}
}
break;
default:
break;
}
state = state->next.p;
}
}
template <class charT, class traits>
void basic_regex_creator<charT, traits>::create_startmaps(re_syntax_base* state)
{
// non-recursive implementation:
// create the last map in the machine first, so that earlier maps
// can make use of the result...
//
// This was originally a recursive implementation, but that caused stack
// overflows with complex expressions on small stacks (think COM+).
// start by saving the case setting:
bool l_icase = m_icase;
std::vector<std::pair<bool, re_syntax_base*> > v;
while(state)
{
switch(state->type)
{
case syntax_element_toggle_case:
// we need to track case changes here:
m_icase = static_cast<re_case*>(state)->icase;
state = state->next.p;
continue;
case syntax_element_alt:
case syntax_element_rep:
case syntax_element_dot_rep:
case syntax_element_char_rep:
case syntax_element_short_set_rep:
case syntax_element_long_set_rep:
// just push the state onto our stack for now:
v.push_back(std::pair<bool, re_syntax_base*>(m_icase, state));
state = state->next.p;
break;
case syntax_element_backstep:
// we need to calculate how big the backstep is:
static_cast<re_brace*>(state)->index
= this->calculate_backstep(state->next.p);
if(static_cast<re_brace*>(state)->index < 0)
{
// Oops error:
if(0 == this->m_pdata->m_status) // update the error code if not already set
this->m_pdata->m_status = boost::regex_constants::error_bad_pattern;
//
// clear the expression, we should be empty:
//
this->m_pdata->m_expression = 0;
this->m_pdata->m_expression_len = 0;
//
// and throw if required:
//
if(0 == (this->flags() & regex_constants::no_except))
{
std::string message = "Invalid lookbehind assertion encountered in the regular expression.";
boost::regex_error e(message, boost::regex_constants::error_bad_pattern, 0);
e.raise();
}
}
BOOST_REGEX_FALLTHROUGH;
default:
state = state->next.p;
}
}
// now work through our list, building all the maps as we go:
while(!v.empty())
{
// Initialize m_recursion_checks if we need it:
if(m_has_recursions)
m_recursion_checks.assign(1 + m_pdata->m_mark_count, 0u);
const std::pair<bool, re_syntax_base*>& p = v.back();
m_icase = p.first;
state = p.second;
v.pop_back();
// Build maps:
m_bad_repeats = 0;
create_startmap(state->next.p, static_cast<re_alt*>(state)->_map, &static_cast<re_alt*>(state)->can_be_null, mask_take);
m_bad_repeats = 0;
if(m_has_recursions)
m_recursion_checks.assign(1 + m_pdata->m_mark_count, 0u);
create_startmap(static_cast<re_alt*>(state)->alt.p, static_cast<re_alt*>(state)->_map, &static_cast<re_alt*>(state)->can_be_null, mask_skip);
// adjust the type of the state to allow for faster matching:
state->type = this->get_repeat_type(state);
}
// restore case sensitivity:
m_icase = l_icase;
}
template <class charT, class traits>
int basic_regex_creator<charT, traits>::calculate_backstep(re_syntax_base* state)
{
typedef typename traits::char_class_type m_type;
int result = 0;
while(state)
{
switch(state->type)
{
case syntax_element_startmark:
if((static_cast<re_brace*>(state)->index == -1)
|| (static_cast<re_brace*>(state)->index == -2))
{
state = static_cast<re_jump*>(state->next.p)->alt.p->next.p;
continue;
}
else if(static_cast<re_brace*>(state)->index == -3)
{
state = state->next.p->next.p;
continue;
}
break;
case syntax_element_endmark:
if((static_cast<re_brace*>(state)->index == -1)
|| (static_cast<re_brace*>(state)->index == -2))
return result;
break;
case syntax_element_literal:
result += static_cast<re_literal*>(state)->length;
break;