diff options
author | Seo Sanghyeon <sanxiyn@gmail.com> | 2018-12-20 00:25:34 +0900 |
---|---|---|
committer | Seo Sanghyeon <sanxiyn@gmail.com> | 2018-12-20 02:26:08 +0900 |
commit | 256f41e2d06a01c3e0e0ca62f39fd7eef0907da5 (patch) | |
tree | 974b35115df4f74c80361275f45e94552dffe51f /src/regex_set.rs | |
parent | 7b3038e8bdf5435d2dd56ce2115f06966f59b225 (diff) |
Warn about unused whitelist options
Diffstat (limited to 'src/regex_set.rs')
-rw-r--r-- | src/regex_set.rs | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/regex_set.rs b/src/regex_set.rs index 6e2ca662..d26e95c9 100644 --- a/src/regex_set.rs +++ b/src/regex_set.rs @@ -1,11 +1,13 @@ //! A type that represents the union of a set of regular expressions. use regex::RegexSet as RxSet; +use std::cell::Cell; /// A dynamic set of regular expressions. #[derive(Debug)] pub struct RegexSet { items: Vec<String>, + matched: Vec<Cell<bool>>, set: Option<RxSet>, } @@ -21,6 +23,7 @@ impl RegexSet { S: AsRef<str>, { self.items.push(string.as_ref().to_owned()); + self.matched.push(Cell::new(false)); self.set = None; } @@ -29,6 +32,17 @@ impl RegexSet { &self.items[..] } + /// Returns regexes in the set which didn't match any strings yet + pub fn unmatched_items(&self) -> Vec<String> { + let mut items = vec![]; + for (i, item) in self.items.iter().enumerate() { + if !self.matched[i].get() { + items.push(item.clone()); + } + } + items + } + /// Construct a RegexSet from the set of entries we've accumulated. /// /// Must be called before calling `matches()`, or it will always return @@ -50,9 +64,16 @@ impl RegexSet { S: AsRef<str>, { let s = string.as_ref(); - self.set.as_ref().map(|set| set.is_match(s)).unwrap_or( - false, - ) + if let Some(set) = self.set.as_ref() { + let matches = set.matches(s); + if matches.matched_any() { + for i in matches.iter() { + self.matched[i].set(true); + } + return true; + } + } + false } } @@ -60,6 +81,7 @@ impl Default for RegexSet { fn default() -> Self { RegexSet { items: vec![], + matched: vec![], set: None, } } |