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
use id_arena::{Arena, Id};

type AsciiString = Vec<u8>;
pub type AsciiStringRef<'a> = &'a [u8];

type NFAStateId = Id<NFAState>;
type VisitedNodes = Vec<NFAStateId>;

struct NFAState {
    is_end: bool,
    char_transition: Vec<Option<NFAStateId>>,
    epsilon_transition: Vec<NFAStateId>,
}

impl NFAState {
    fn new() -> Self {
        NFAState {
            is_end: false,
            char_transition: vec![None; 256],
            epsilon_transition: vec![],
        }
    }

    fn add_epsilon(&mut self, to: NFAStateId) {
        self.epsilon_transition.push(to)
    }
}

struct NFAFragment {
    start: NFAStateId,
    out: NFAStateId,
}

impl NFAFragment {
    fn new(arena: &mut Arena<NFAState>) -> Self {
        let start = arena.alloc(NFAState::new());
        let out = arena.alloc(NFAState::new());

        arena[start].is_end = false;
        arena[out].is_end = true;

        NFAFragment { start, out }
    }
}

fn insert_concat_operator(regexp_bytes: AsciiStringRef) -> AsciiString {
    let n = regexp_bytes.len();
    let mut result = Vec::with_capacity(n + n + 1);

    for i in 0..n {
        let at = regexp_bytes[i];
        result.push(at);

        if at == ('(' as u8) || at == ('|' as u8) {
            continue;
        }

        if i < n - 1 {
            let next = regexp_bytes[i + 1];

            if next == ('|' as u8)
                || next == ('+' as u8)
                || next == ('*' as u8)
                || next == (')' as u8)
                || next == ('?' as u8)
            {
                continue;
            }

            result.push('.' as u8);
        }
    }

    result
}

fn operator_precedence(c: u8) -> u8 {
    match c as char {
        '.' => 0,
        '|' => 1,
        '+' => 2,
        '?' => 2,
        '*' => 2,
        _ => 0,
    }
}

fn is_operator(c: u8) -> bool {
    (c == '|' as u8) || (c == '+' as u8) || (c == '.' as u8) || (c == '*' as u8) || (c == '?' as u8)
}

fn regexp_to_postfix(regexp: AsciiStringRef) -> AsciiString {
    let n = regexp.len();
    let mut result: Vec<u8> = Vec::with_capacity(n + n + 1);
    let mut stack: Vec<u8> = Vec::with_capacity(n + n + 1);

    for i in 0..n {
        let token: u8 = regexp[i];

        if is_operator(token) {
            let precedence = operator_precedence(token);

            while let Some(c) = stack.last() {
                if *c != '(' as u8 && operator_precedence(stack[0]) >= precedence {
                    result.push(*c);
                    stack.pop();
                } else {
                    break;
                }
            }

            stack.push(token);
        } else if token == '(' as u8 {
            stack.push(token);
        } else if token == ')' as u8 {
            while let Some(c) = stack.last() {
                if *c != '(' as u8 {
                    result.push(*c);
                    stack.pop();
                } else {
                    break;
                }
            }
            stack.pop();
        } else {
            result.push(token);
        }
    }

    while let Some(c) = stack.last() {
        result.push(*c);
        stack.pop();
    }

    result
}

fn postfix_to_nfa(arena: &mut Arena<NFAState>, postfix_regexp: AsciiStringRef) -> NFAFragment {
    let n = postfix_regexp.len();
    let mut stack: Vec<NFAFragment> = Vec::with_capacity(n);

    for i in 0..n {
        let at = postfix_regexp[i];
        match at as char {
            '.' => {
                let right = stack.pop().unwrap();
                let left = stack.pop().unwrap();

                arena[left.out].is_end = false;
                arena[left.out].add_epsilon(right.start);

                let mut frag = NFAFragment::new(arena);
                frag.start = left.start;
                frag.out = right.out;
                stack.push(frag);
            }
            '|' => {
                let right = stack.pop().unwrap();
                let left = stack.pop().unwrap();

                let frag = NFAFragment::new(arena);
                arena[frag.start].add_epsilon(right.start);
                arena[frag.start].add_epsilon(left.start);

                arena[left.out].is_end = false;
                arena[left.out].add_epsilon(frag.out);

                arena[right.out].is_end = false;
                arena[right.out].add_epsilon(frag.out);

                stack.push(frag);
            }
            '?' => {
                let op = stack.pop().unwrap();

                let frag = NFAFragment::new(arena);
                arena[frag.start].add_epsilon(frag.out);
                arena[frag.start].add_epsilon(op.start);
                arena[op.out].add_epsilon(frag.out);
                arena[op.out].is_end = false;

                stack.push(frag);
            }
            '+' => {
                let op = stack.pop().unwrap();

                let frag = NFAFragment::new(arena);
                arena[frag.start].add_epsilon(op.start);
                arena[op.out].add_epsilon(op.start);
                arena[op.out].add_epsilon(frag.out);
                arena[op.out].is_end = false;

                stack.push(frag);
            }
            '*' => {
                let op = stack.pop().unwrap();

                let frag = NFAFragment::new(arena);
                arena[frag.start].add_epsilon(op.start);
                arena[frag.start].add_epsilon(frag.out);
                arena[op.out].add_epsilon(op.start);
                arena[op.out].add_epsilon(frag.out);
                arena[op.out].is_end = false;

                stack.push(frag);
            }
            _ => {
                let frag = NFAFragment::new(arena);
                arena[frag.start].char_transition[at as usize] = Some(frag.out);
                stack.push(frag);
            }
        }
    }

    let result = stack.pop().unwrap();
    result
}

fn already_visited(v: &VisitedNodes, node: NFAStateId) -> bool {
    for e in v {
        if *e == node {
            return true;
        }
    }

    false
}

fn dfs(
    arena: &Arena<NFAState>,
    root: NFAStateId,
    word: AsciiStringRef,
    matched_num: usize,
    visited: &mut VisitedNodes,
) -> bool {
    if already_visited(visited, root) {
        return false;
    }

    visited.push(root);
    if word.len() == matched_num {
        if arena[root].is_end {
            return true;
        }

        for v in &arena[root].epsilon_transition {
            if dfs(arena, *v, word, matched_num, visited) {
                return true;
            }
        }
    } else {
        if let Some(transition) = arena[root].char_transition[word[matched_num] as usize % 128] {
            let mut visited = VisitedNodes::with_capacity(256);
            if dfs(arena, transition, word, matched_num + 1, &mut visited) {
                return true;
            }
        } else {
            for v in &arena[root].epsilon_transition {
                if dfs(arena, *v, word, matched_num, visited) {
                    return true;
                }
            }
        }
    }

    false
}

pub struct Regex {
    fragment: NFAFragment,
    arena: Arena<NFAState>,
}

impl Regex {
    pub fn new(regexp: AsciiStringRef) -> Option<Self> {
        let mut arena: Arena<NFAState> = Arena::new();
        let concatted = insert_concat_operator(regexp);
        let postfix = regexp_to_postfix(&concatted);
        let result = postfix_to_nfa(&mut arena, &postfix);
        Some(Regex {
            fragment: result,
            arena,
        })
    }

    pub fn is_match(&self, search: AsciiStringRef) -> bool {
        let mut visited = vec![];
        dfs(&self.arena, self.fragment.start, search, 0, &mut visited)
    }
}

pub fn regex_match(regexp: &str, search: &str) -> bool {
    let r = Regex::new(regexp.as_bytes()).unwrap();
    r.is_match(search.as_bytes())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_regex_match() {
        assert_eq!(true, regex_match("(zz)+", "zz"));
        assert_eq!(true, regex_match("(x|y)*z", "xyxyyyxxxz"));
        assert_eq!(false, regex_match("(x|y)*z+", "xy"));
        assert_eq!(true, regex_match("(x|y)*z+", "xyzzz"));
        assert_eq!(true, regex_match("(1|2|3|4|5|6|7|8|9)+", "1423"));
        assert_eq!(false, regex_match("(1|2|3|4|5|6|7|8|9)+", "123abc"));
        assert_eq!(true, regex_match("a?", ""));
        assert_eq!(true, regex_match("a?", "a"));
        assert_eq!(false, regex_match("a?", "aa"));
        assert_eq!(true, regex_match("hell(a|o)?", "hello"));
        assert_eq!(true, regex_match("(a|b)?", "a"));
    }
}