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
pub use vec_reg_common::{CompiledRegex, Regex};
pub use vec_reg_macro::vec_reg;
#[cfg(test)]
mod test {
use super::*;
#[test]
fn match_without_macro() {
let is_fizz = |x: &i32| x % 3 == 0;
let is_buzz = |x: &i32| x % 5 == 0;
let is_fizz_buzz = |x: &i32| x % 15 == 0;
let reg = Regex::concat(
Regex::satisfy(is_fizz),
Regex::repeat1(Regex::concat(
Regex::satisfy(is_buzz),
Regex::satisfy(is_fizz_buzz),
)),
)
.compile();
assert!(!reg.is_match(&[1, 2, 3]));
assert!(reg.is_match(&[3, 5, 15]));
assert!(reg.is_match(&[6, 10, 15, 10, 30]));
}
#[test]
fn match_with_macro() {
let is_fizz = |x: &i32| x % 3 == 0;
let is_buzz = |x: &i32| x % 5 == 0;
let reg = vec_reg!([is_fizz]([is_buzz][|x| x % 15 == 0])+).compile();
assert!(!reg.is_match(&[1, 2, 3]));
assert!(reg.is_match(&[3, 5, 15]));
assert!(reg.is_match(&[6, 10, 15, 10, 30]));
}
#[test]
fn match_repeat_n_macro() {
let is_even = |x: &i32| x % 2 == 0;
let is_odd = |x: &i32| x % 2 == 1;
let reg1 = vec_reg!([is_even]{2}).compile();
let reg2 = vec_reg!([is_even]{2,}).compile();
let reg3 = vec_reg!([is_even]{2,3}).compile();
let reg4 = vec_reg!(([is_even]|[is_odd]){2,3}).compile();
assert!(!reg1.is_match(&[2]));
assert!(reg1.is_match(&[2, 4]));
assert!(!reg1.is_match(&[2, 4, 6]));
assert!(!reg2.is_match(&[2]));
assert!(reg2.is_match(&[2, 4]));
assert!(reg2.is_match(&[2, 4, 6]));
assert!(!reg3.is_match(&[2]));
assert!(reg3.is_match(&[2, 4]));
assert!(reg3.is_match(&[2, 4, 6]));
assert!(!reg3.is_match(&[2, 4, 6, 8]));
assert!(!reg4.is_match(&[2]));
assert!(reg4.is_match(&[1, 2]));
assert!(reg4.is_match(&[1, 2, 3]));
assert!(!reg4.is_match(&[1, 2, 3, 4]));
}
}