-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.h
More file actions
77 lines (69 loc) · 1.51 KB
/
regex.h
File metadata and controls
77 lines (69 loc) · 1.51 KB
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
#ifndef _REGEX_H_
#define _REGEX_H_
#include <iostream>
const size_t MAX_OR = 16;
const size_t MAX_STATE = 512;
/*
flag:
0-127: single character
1000: characters
1001: .
1002: \d
1003: \D
1004: \s
1005: \S
1006: \w
1007: \W
1100: e
777: ACCEPT
*/
struct State
{
int flag;
char* chs;
State* next1;
State* next2;
int visit;
State() : flag(1100), chs(NULL), next1(NULL), next2(NULL), visit(0) {}
State(int f) : flag(f), chs(NULL), next1(NULL), next2(NULL), visit(0) {}
State(int f, char* c) : flag(f), chs(c), next1(NULL), next2(NULL), visit(0) {}
~State()
{
if(chs != NULL)
delete [] chs;
}
};
class Regex
{
public:
Regex();
Regex(const char* r);
bool match(const char* str);
bool slow_match(const char* str);
bool is_success() { return re_compile; };
~Regex();
private:
const char* regex;
State* DFA;
State** current;
size_t index;
bool re_compile;
void traversal(State* s);
void traversal(State* s, State** a, size_t& i);
void traversal(State* s, const char* str, size_t i, bool& r);
//
void compile();
void speDFA();
void charsDFA();
void groupDFA();
void splitDFA(State** head, State*** end, size_t& end_count);
void singleCharDFA();
void questionDFA(State** t);
void starDFA(State** t);
void plusDFA(State** t);
bool is_escape(char ch);
//
void addState(State* s, State** p, size_t& i, int r);
bool is_match(char c, State* s);
};
#endif