-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.hpp
718 lines (543 loc) · 18.1 KB
/
regex.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
#pragma once
#include <vector>
#include <bitset>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <locale>
#include <stdexcept>
#include <stack>
#include <cstdint>
#include <list>
#include <algorithm>
#include "dynamic_bitset.hpp"
namespace rgx
{
template <typename String_T = std::string>
class Regex
{
using Char_T = typename String_T::value_type;
using Traits_T = typename String_T::traits_type;
using Alloc_T = typename String_T::allocator_type;
using Exp_value_T = typename std::pair <Char_T, uint8_t>;
using Exp_T = typename std::vector <Exp_value_T>;
using State_T = typename bit::Bitset <uint64_t>;
using NFA_T = typename std::vector <State_T>;
static constexpr const uint8_t CHAR_VAL = 0;
static constexpr const uint8_t UNION_VAL = 1;
static constexpr const uint8_t STAR_VAL = 2;
static constexpr const uint8_t PLUS_VAL = 3;
static constexpr const uint8_t CONCAT_VAL = 4;
static constexpr const uint8_t OPEN_VAL = 5;
static constexpr const uint8_t CLOSE_VAL = 6;
static constexpr const uint8_t ESCAPE_VAL = 7;
const std::ctype <Char_T> &facet = std::use_facet <std::ctype <Char_T>>(
std::locale()
);
const Char_T UNION = facet.widen('|');
const Char_T STAR = facet.widen('*');
const Char_T PLUS = facet.widen('+');
const Char_T CONCAT = facet.widen('.');
const Char_T OPEN = facet.widen('(');
const Char_T CLOSE = facet.widen(')');
const Char_T ESCAPE = facet.widen('\\');
private:
NFA_T m_NFA;
std::size_t m_size;
Exp_T m_transition_labels;
const Exp_T m_postfix;
Exp_T process_escape(const String_T&) const;
Exp_T process_concat(const Exp_T&) const;
Exp_T process_exp(const String_T&) const;
Exp_T process_postfix(const Exp_T&);
void compute_NFA(const Exp_T&);
//helpers
bool is_operator(const Char_T) const noexcept;
bool is_bracket(const Char_T) const noexcept;
bool is_character(const Char_T) const noexcept;
bool detect_concat(const Char_T, const Char_T) const noexcept;
uint8_t operator_precedence(const Char_T) const noexcept;
static constexpr bool is_operator(const Exp_value_T) noexcept;
static constexpr bool is_bracket(const Exp_value_T) noexcept;
static constexpr bool is_character(const Exp_value_T) noexcept;
static constexpr bool detect_concat(const Exp_value_T, const Exp_value_T) noexcept;
static constexpr uint8_t operator_precedence(const Exp_value_T) noexcept;
uint8_t op_value(const Char_T) const noexcept;
public:
Regex(const String_T &);
bool match(const String_T &word);
bool search(const String_T &sample);
bool search_results(const String_T &sample, const std::size_t max_res) const;
};
template <typename String_T>
Regex <String_T>::Regex(const String_T &exp)
:
m_postfix(
process_postfix(
process_exp(exp)
)
)
{
compute_NFA(m_postfix);
std::for_each(m_transition_labels.begin(), m_transition_labels.end(), [](auto i)->void {std::cout << i.first;});
};
// returns processed exp, ready for postfixing
template <typename String_T>
typename Regex<String_T>::Exp_T
Regex<String_T>::process_exp(const String_T &exp) const
{
// std::cout << exp;
// std::cout << std::endl;
Exp_T processed_exp;
processed_exp.reserve(exp.size() * 2 + 2);
processed_exp.push_back({OPEN, OPEN_VAL});
std::size_t itr = 0;
for(itr; itr < exp.size() - 1; ++itr)
{
// escape characters
const Exp_value_T curr_ch = (exp[itr] == ESCAPE)
? Exp_value_T{exp[++itr], CHAR_VAL}
: Exp_value_T{exp[itr], op_value(exp[itr])};
// Exp_value_T curr_ch;
// if(exp[itr] == ESCAPE)
// {
// ++itr;
// curr_ch = Exp_value_T{exp[itr], CHAR_VA L}
// }
// else
// {
// curr_ch = Exp_value_T{exp[itr], op_value(exp[itr])}
// }
// add concat operators
if(detect_concat(processed_exp.back(), curr_ch))
{
processed_exp.push_back({CONCAT, CONCAT_VAL});
}
processed_exp.push_back(curr_ch);
}
if(itr < exp.size())
{
// add concat operators
if(detect_concat(processed_exp.back().first, exp[itr]))
{
processed_exp.push_back({CONCAT, CONCAT_VAL});
}
processed_exp.push_back({exp[itr], op_value(exp[itr])});
}
processed_exp.push_back({CLOSE, CLOSE_VAL});
// processed_exp.shrink_to_fit();
return std::move(processed_exp);
};
template <typename String_T>
typename Regex<String_T>::Exp_T
Regex<String_T>::process_escape(const String_T &exp) const
{
// std::cout << exp;
// std::cout << std::endl;
Exp_T processed_exp;
processed_exp.reserve(exp.size());
std::size_t itr = 0;
for(itr; itr < exp.size() - 1; ++itr)
{
if(exp[itr] == ESCAPE)
{
processed_exp.push_back({exp[++itr], CHAR_VAL});
}
else
{
processed_exp.push_back({exp[itr], op_value(exp[itr])});
}
}
if(itr < exp.size())
{
processed_exp.push_back({exp[itr], op_value(exp[itr])});
}
processed_exp.shrink_to_fit();
return std::move(processed_exp);
};
template <typename String_T>
typename Regex<String_T>::Exp_T
Regex<String_T>::process_concat(const Exp_T &exp) const
{
// std::for_each(exp.begin(), exp.end(), [](auto i)->void {std::cout << i.first;});
// std::for_each(exp.begin(), exp.end(), [](auto i)->void {std::cout << (unsigned int)i.second << ' ';});
// std::cout << std::endl;
Exp_T processed_exp;
processed_exp.reserve(exp.size() * 2 + 2);
processed_exp.push_back({OPEN, OPEN_VAL});
for(const Exp_value_T chr : exp)
{
if(detect_concat(processed_exp.back(), chr))
{
processed_exp.push_back({CONCAT, CONCAT_VAL});
}
processed_exp.push_back(chr);
}
processed_exp.push_back({CLOSE, CLOSE_VAL});
processed_exp.shrink_to_fit();
return std::move(processed_exp);
};
// calculates nr of states
template <typename String_T>
typename Regex<String_T>::Exp_T
Regex <String_T>::process_postfix(const Exp_T &exp)
{
m_size = 0;
// std::for_each(exp.begin(), exp.end(), [](auto i)->void {std::cout << i.first;});
// std::for_each(exp.begin(), exp.end(), [](auto i)->void {std::cout << (unsigned int)i.second << ' ';});
// std::cout << std::endl;
Exp_T processed_exp;
processed_exp.reserve(exp.size());
// a fixed size stack would help,
// repeated allocations are still expensive
// also leads to caching struggles
std::stack <Exp_value_T> conversion_stack;
for(const Exp_value_T chr : exp)
{
if(is_character(chr))
{
processed_exp.push_back(chr);
++m_size; // increment number of states
continue;
}
if(chr.second == OPEN_VAL)
{
conversion_stack.push(chr);
continue;
}
if(is_operator(chr))
{
while(operator_precedence(conversion_stack.top()) >= operator_precedence(chr))
{
processed_exp.push_back(conversion_stack.top());
conversion_stack.pop();
}
conversion_stack.push(chr);
continue;
}
while(conversion_stack.top().second != OPEN_VAL)
{
processed_exp.push_back(conversion_stack.top());
conversion_stack.pop();
}
conversion_stack.pop();
}
// processed_exp.shrink_to_fit();
return std::move(processed_exp);
};
template <typename String_T>
void Regex <String_T>::compute_NFA(const Exp_T &exp)
{
std::for_each(m_postfix.begin(), m_postfix.end(), [](auto i)->void {std::cout << i.first;});
std::cout << std::endl;
std::for_each(m_postfix.begin(), m_postfix.end(), [](auto i)->void {std::cout << (unsigned int)i.second << ' ';});
std::cout << std::endl;
// std::cout << std::endl << m_size;
m_NFA.reserve(m_size + 1);
m_transition_labels.reserve(m_size + 1);
std::stack <State_T> first_stack;
std::stack <State_T> last_stack;
//indexes that are contained within the stack expression
std::stack <State_T> pos_stack;
m_NFA.push_back(State_T(m_size + 1)); //first
m_transition_labels.push_back({'/', 0});
std::size_t itr = 1;
for(const Exp_value_T chr : exp)
{
if(is_character(chr))
{
// each character has a state
m_transition_labels.push_back(chr);
State_T mask(m_size + 1);
mask.set(itr);
// First(x) = {x}
first_stack.push(mask);
// Last(x) = {x}
last_stack.push(mask);
pos_stack.push(mask);
// we push the empty bit to its follow() since they come in order
m_NFA.push_back(State_T(m_size + 1));
itr++;
}
else
{
if(chr.second == STAR_VAL)
{
// we keep track whether epsilon belongs to the current
// subsequence of RE using the 0th byte
// First(E*) = First(E+) = First(E)
first_stack.top().set(0);
// Last(E*) = Last(E+) = Last(E)
last_stack.top().set(0);
// follow
for(std::size_t itr = 1; itr < m_NFA.size(); ++itr)
if(pos_stack.top().test(itr) && last_stack.top().test(itr))
m_NFA[itr] |= first_stack.top();
}
if(chr.second == UNION_VAL)
{
// First(F | G) = First(F) U First(G)
State_T mask = std::move(first_stack.top());
first_stack.pop();
first_stack.top() |= mask;
// Last(F | G) = Last(F) U Last(G)
mask = std::move(last_stack.top());
last_stack.pop();
last_stack.top() |= mask;
// positions united
mask = std::move(pos_stack.top());
pos_stack.pop();
pos_stack.top() |= mask;
// nothing changes for follow
}
if(chr.second == CONCAT_VAL)
{
// First(F + G) = First(F) if epsilon DOES NOT belong to L(F);
// First(F + G) = First(F) U First(G) if epsilon belongs to L(F)
State_T mask = std::move(first_stack.top());
first_stack.pop();
State_T firstG = mask; // save for follow...
if(first_stack.top().test(0))
{
if(!mask.test(0))
{
first_stack.top().reset(0);
}
first_stack.top() |= mask;
}
// Last(F + G) = Last(F) if epsilon DOES NOT belong to L(F);
// Last(F + G) = Last(F) U Last(G) if epsilon belongs to L(F)
mask = std::move(last_stack.top());
last_stack.pop();
State_T lastF = last_stack.top(); // save for follow...
if(mask.test(0))
{
if(!last_stack.top().test(0))
{
mask.reset(0);
}
last_stack.top() |= mask;
}
else
{
last_stack.emplace(std::move(mask));
}
// positions concatenated (no difference)
mask = std::move(pos_stack.top());
pos_stack.pop();
pos_stack.top() |= mask;
// follow
for(std::size_t itr = 1; itr < m_NFA.size(); ++itr)
{
if(pos_stack.top().test(itr) && lastF.test(itr))
{
m_NFA[itr] |= firstG;
}
}
}
}
}
m_NFA[0] = std::move(first_stack.top());
for(std::size_t itr = 1; itr < m_NFA.size(); ++itr)
{
m_NFA[itr].set(0, last_stack.top().test(itr));
}
};
template <typename String_T>
bool Regex <String_T>::match(const String_T &word)
{
State_T state_simulator = m_NFA[0];
std::cout << word << std::endl;
for(const Char_T chr : word)
{
State_T transition(m_size + 1);
for(std::size_t itr = 1; itr < m_NFA.size(); ++itr)
{
// break if transition is null
if(state_simulator.test(itr) && m_transition_labels[itr].first == chr)
{
transition |= m_NFA[itr];
}
}
state_simulator = std::move(transition);
std::cout << chr << " " << state_simulator << std::endl;
}
return state_simulator.test(0);
};
template <typename String_T>
bool Regex <String_T>::search(const String_T &sample)
{
// empty words will be ignored
// std::cout << "Searching for expression: \"" << infixExp_ <<"\" on given string..." << std::endl;
std::size_t count = 0;
std::list <std::pair <State_T, std::pair <std::size_t, std::size_t>>> matches; // pair(transition, pair(begin, end))
for(std::size_t itr = 0; itr < sample.size(); ++itr)
{
matches.push_back({m_NFA[0], {itr, -1}});
auto match_itr = matches.begin();
while(match_itr != matches.end())
{
State_T transition(m_size + 1);
for(std::size_t jtr = 1; jtr < m_NFA.size(); ++jtr)
{
if(match_itr->first.test(jtr) && m_transition_labels[jtr].first == sample[itr])
{
transition |= m_NFA[jtr];
}
}
if(transition.none())
{
// cant transition (haha transphobic jokes am i right) :/ yikes bro
if(match_itr->second.second != -1)
{
std::cout << "Found: \""<< sample.substr(match_itr->second.first, match_itr->second.second - match_itr->second.first + 1) << "\" at index " << match_itr->second.first << std::endl;
++count;
}
match_itr = matches.erase(match_itr);
}
else
{
match_itr->first = std::move(transition); // new state
if(transition.test(0))
{
match_itr->second.second = itr; // bigger match length
match_itr = matches.erase(std::next(match_itr, 1), matches.end());
break;
}
match_itr = std::next(match_itr);
}
}
}
if(matches.begin() != matches.end() && matches.begin()->second.second != -1)
{
std::cout << "Found: \""<< sample.substr(matches.begin()->second.first, matches.begin()->second.second - matches.begin()->second.first + 1) << "\" at index " << matches.begin()->second.first << std::endl;
++count;
}
if(count == 0)
{
std::cout << "No matches." << std::endl;
return false;
}
else
{
std::cout << "Found a total of " << count << " matches." << std::endl;
return true;
}
};
/*
================================================================================
------------------------------ rgx::Regex HELPERS ------------------------------
================================================================================
*/
template <typename String_T>
constexpr bool
Regex <String_T>::is_operator(const Exp_value_T chr) noexcept
{
return 1 <= chr.second && chr.second <= 4;
};
template <typename String_T>
bool Regex <String_T>::is_operator(const Char_T chr) const noexcept
{
return chr == UNION || chr == STAR || chr == CONCAT;
};
template <typename String_T>
constexpr bool
Regex <String_T>::is_bracket(const Exp_value_T chr) noexcept
{
return chr.second == OPEN_VAL || chr.second == CLOSE_VAL;
};
template <typename String_T>
bool Regex <String_T>::is_bracket(const Char_T chr) const noexcept
{
return chr == OPEN || chr == CLOSE;
};
template <typename String_T>
constexpr bool
Regex <String_T>::is_character(const Exp_value_T chr) noexcept
{
return chr.second == CHAR_VAL;
};
template <typename String_T>
bool Regex <String_T>::is_character(const Char_T chr) const noexcept
{
return !(is_operator(chr) || is_bracket(chr) || chr == ESCAPE);
};
template <typename String_T>
constexpr bool
Regex <String_T>::detect_concat(const Exp_value_T chr1, const Exp_value_T chr2)
noexcept
{
return (
chr1.second == CHAR_VAL || chr1.second == CLOSE_VAL ||
chr1.second == STAR_VAL || chr1.second == PLUS_VAL
) && (
chr2.second == CHAR_VAL || chr2.second == OPEN_VAL
);
};
template <typename String_T>
bool Regex <String_T>::detect_concat(const Char_T chr1, const Char_T chr2)
const noexcept
{
return(
is_character(chr1) || chr1 == CLOSE || chr1 == STAR || chr1 == PLUS
) && (
is_character(chr2) || chr2 == OPEN
);
};
template <typename String_T>
constexpr uint8_t
Regex <String_T>::operator_precedence(const Exp_value_T chr) noexcept
{
if(chr.second == STAR_VAL || chr.second == PLUS_VAL)
{
return 3;
}
if(chr.second == CONCAT_VAL)
{
return 2;
}
if(chr.second == UNION_VAL)
{
return 1;
}
return 0;
};
template <typename String_T>
uint8_t Regex <String_T>::operator_precedence(const Char_T chr)
const noexcept
{
if(chr == STAR || chr == PLUS)
{
return 3;
}
if(chr == CONCAT)
{
return 2;
}
if(chr == UNION)
{
return 1;
}
return 0;
};
template <typename String_T>
uint8_t Regex <String_T>::op_value(const Char_T chr) const noexcept
{
if(chr == UNION)
return UNION_VAL;
if(chr == STAR)
return STAR_VAL;
if(chr == PLUS)
return PLUS_VAL;
if(chr == CONCAT)
return CONCAT_VAL;
if(chr == OPEN)
return OPEN_VAL;
if(chr == CLOSE)
return CLOSE_VAL;
if(chr == ESCAPE)
return ESCAPE_VAL;
return CHAR_VAL;
};
}