diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/bindgen/regex_set.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-upstream.tar.xz firefox-esr-upstream.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/bindgen/regex_set.rs')
-rw-r--r-- | third_party/rust/bindgen/regex_set.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/third_party/rust/bindgen/regex_set.rs b/third_party/rust/bindgen/regex_set.rs new file mode 100644 index 0000000000..6246dd255b --- /dev/null +++ b/third_party/rust/bindgen/regex_set.rs @@ -0,0 +1,103 @@ +//! 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(Clone, Debug, Default)] +pub struct RegexSet { + items: Vec<String>, + /// Whether any of the items in the set was ever matched. The length of this + /// vector is exactly the length of `items`. + matched: Vec<Cell<bool>>, + set: Option<RxSet>, + /// Whether we should record matching items in the `matched` vector or not. + record_matches: bool, +} + +impl RegexSet { + /// Create a new RegexSet + pub fn new() -> RegexSet { + RegexSet { + ..Default::default() + } + } + + /// Is this set empty? + pub fn is_empty(&self) -> bool { + self.items.is_empty() + } + + /// Insert a new regex into this set. + pub fn insert<S>(&mut self, string: S) + where + S: AsRef<str>, + { + let string = string.as_ref().to_owned(); + if string == "*" { + warn!("using wildcard patterns (`*`) is no longer considered valid. Use `.*` instead"); + } + self.items.push(string); + self.matched.push(Cell::new(false)); + self.set = None; + } + + /// Returns slice of String from its field 'items' + pub fn get_items(&self) -> &[String] { + &self.items[..] + } + + /// Returns an iterator over regexes in the set which didn't match any + /// strings yet. + pub fn unmatched_items(&self) -> impl Iterator<Item = &String> { + self.items.iter().enumerate().filter_map(move |(i, item)| { + if !self.record_matches || self.matched[i].get() { + return None; + } + + Some(item) + }) + } + + /// Construct a RegexSet from the set of entries we've accumulated. + /// + /// Must be called before calling `matches()`, or it will always return + /// false. + pub fn build(&mut self, record_matches: bool) { + let items = self.items.iter().map(|item| format!("^({})$", item)); + self.record_matches = record_matches; + self.set = match RxSet::new(items) { + Ok(x) => Some(x), + Err(e) => { + warn!("Invalid regex in {:?}: {:?}", self.items, e); + None + } + } + } + + /// Does the given `string` match any of the regexes in this set? + pub fn matches<S>(&self, string: S) -> bool + where + S: AsRef<str>, + { + let s = string.as_ref(); + let set = match self.set { + Some(ref set) => set, + None => return false, + }; + + if !self.record_matches { + return set.is_match(s); + } + + let matches = set.matches(s); + if !matches.matched_any() { + return false; + } + for i in matches.iter() { + self.matched[i].set(true); + } + + true + } +} |