1
1
//! A type that represents the union of a set of regular expressions.
2
2
3
3
use regex:: RegexSet as RxSet ;
4
+ use std:: cell:: Cell ;
4
5
5
6
/// A dynamic set of regular expressions.
6
7
#[ derive( Debug ) ]
7
8
pub struct RegexSet {
8
9
items : Vec < String > ,
10
+ matched : Vec < Cell < bool > > ,
9
11
set : Option < RxSet > ,
10
12
}
11
13
@@ -21,6 +23,7 @@ impl RegexSet {
21
23
S : AsRef < str > ,
22
24
{
23
25
self . items . push ( string. as_ref ( ) . to_owned ( ) ) ;
26
+ self . matched . push ( Cell :: new ( false ) ) ;
24
27
self . set = None ;
25
28
}
26
29
@@ -29,6 +32,17 @@ impl RegexSet {
29
32
& self . items [ ..]
30
33
}
31
34
35
+ /// Returns regexes in the set which didn't match any strings yet
36
+ pub fn unmatched_items ( & self ) -> Vec < String > {
37
+ let mut items = vec ! [ ] ;
38
+ for ( i, item) in self . items . iter ( ) . enumerate ( ) {
39
+ if !self . matched [ i] . get ( ) {
40
+ items. push ( item. clone ( ) ) ;
41
+ }
42
+ }
43
+ items
44
+ }
45
+
32
46
/// Construct a RegexSet from the set of entries we've accumulated.
33
47
///
34
48
/// Must be called before calling `matches()`, or it will always return
@@ -50,16 +64,24 @@ impl RegexSet {
50
64
S : AsRef < str > ,
51
65
{
52
66
let s = string. as_ref ( ) ;
53
- self . set . as_ref ( ) . map ( |set| set. is_match ( s) ) . unwrap_or (
54
- false ,
55
- )
67
+ if let Some ( set) = self . set . as_ref ( ) {
68
+ let matches = set. matches ( s) ;
69
+ if matches. matched_any ( ) {
70
+ for i in matches. iter ( ) {
71
+ self . matched [ i] . set ( true ) ;
72
+ }
73
+ return true ;
74
+ }
75
+ }
76
+ false
56
77
}
57
78
}
58
79
59
80
impl Default for RegexSet {
60
81
fn default ( ) -> Self {
61
82
RegexSet {
62
83
items : vec ! [ ] ,
84
+ matched : vec ! [ ] ,
63
85
set : None ,
64
86
}
65
87
}
0 commit comments