-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
162 lines (140 loc) · 4.48 KB
/
main.rs
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
mod fsm {
const FSM_COLUMN_SIZE: usize = 130;
const END_OF_LINE: usize = 129;
type Index = usize;
#[derive(Default, Clone, Copy)]
struct Action {
pub next: Index,
pub offset: i32,
}
#[derive(Clone)]
struct Column {
pub ts: [Action; FSM_COLUMN_SIZE],
}
impl Column {
fn new() -> Self {
Self {
ts: [Default::default(); FSM_COLUMN_SIZE],
}
}
}
pub struct Regex {
cs: Vec<Column>,
}
impl Regex {
pub fn compile(src: &str) -> Self {
// Failed state
let mut fsm = Self { cs: Vec::new() };
fsm.cs.push(Column::new());
for c in src.chars() {
let mut col = Column::new();
match c {
'$' => {
col.ts[END_OF_LINE] = Action {
next: fsm.cs.len() + 1,
offset: 1,
};
fsm.cs.push(col);
}
'.' => {
for i in 32..127 {
col.ts[i] = Action {
next: fsm.cs.len() + 1,
offset: 1,
};
}
fsm.cs.push(col);
}
'*' => {
let n = fsm.cs.len();
for t in fsm.cs.last_mut().unwrap().ts.iter_mut() {
if t.next == n {
t.next = n - 1;
} else if t.next == 0 {
t.next = n;
t.offset = 0;
} else {
unreachable!();
}
}
}
'+' => {
let n = fsm.cs.len();
fsm.cs.push(fsm.cs.last().unwrap().clone());
for t in fsm.cs.last_mut().unwrap().ts.iter_mut() {
if t.next == n {
// just leave it as it is. it's already looped.
} else if t.next == 0 {
t.next = n + 1;
t.offset = 0;
} else {
unreachable!();
}
}
}
_ => {
col.ts[c as usize] = Action {
next: fsm.cs.len() + 1,
offset: 1,
};
fsm.cs.push(col);
}
}
}
fsm
}
pub fn match_str(&self, input: &str) -> bool {
let mut state = 1;
let mut head: usize = 0;
let chars = input.chars().collect::<Vec<_>>();
let n = chars.len();
while 0 < state && state < self.cs.len() && head < n {
let action = self.cs[state].ts[chars[head] as usize];
state = action.next;
head = (head as i32 + action.offset) as usize;
}
if state == 0 {
return false;
}
if state < self.cs.len() {
let action = self.cs[state].ts[END_OF_LINE];
state = action.next;
}
return state >= self.cs.len();
}
pub fn dump(&mut self) {
for symbol in 0..FSM_COLUMN_SIZE {
print!("{:03} => ", symbol);
for column in self.cs.iter() {
print!(
"({}, {}) ",
column.ts[symbol].next, column.ts[symbol].offset
);
}
println!("");
}
}
}
}
fn main() {
let src = "a+bc$";
let mut regex = fsm::Regex::compile(src);
regex.dump();
println!("----------------------------");
let inputs = vec![
"Hello, World!",
"bc",
"abc",
"aabc",
"aaabc",
"abcd",
"bbc",
"cbc",
"cbd",
"cbt",
];
println!("Regex: {}", src);
for input in inputs.iter() {
println!("{:?} => {:?}", input, regex.match_str(input));
}
}