//! A type that represents the union of a set of regular expressions. #![deny(clippy::missing_docs_in_private_items)]
use regex::RegexSet as RxSet; use std::cell::Cell;
/// A dynamic set of regular expressions. #[derive(Clone, Debug, Default)] pubstruct RegexSet {
items: Vec<Box<str>>, /// 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 pubfn new() -> RegexSet {
RegexSet::default()
}
/// Is this set empty? pubfn is_empty(&self) -> bool { self.items.is_empty()
}
/// Insert a new regex into this set. pubfn insert<S>(&mutself, string: S) where
S: AsRef<str>,
{ self.items.push(string.as_ref().to_owned().into_boxed_str()); self.matched.push(Cell::new(false)); self.set = None;
}
/// Returns slice of String from its field 'items' pubfn get_items(&self) -> &[Box<str>] {
&self.items
}
/// Returns an iterator over regexes in the set which didn't match any /// strings yet. pubfn unmatched_items(&self) -> impl Iterator<Item = &str> { self.items.iter().enumerate().filter_map(move |(i, item)| { if !self.record_matches || self.matched[i].get() { return None;
}
Some(item.as_ref())
})
}
/// Construct a RegexSet from the set of entries we've accumulated. /// /// Must be called before calling `matches()`, or it will always return /// false. #[inline] pubfn build(&mutself, record_matches: bool) { self.build_inner(record_matches, None)
}
#[cfg(all(feature = "__cli", feature = "experimental"))] /// Construct a RegexSet from the set of entries we've accumulated and emit diagnostics if the /// name of the regex set is passed to it. /// /// Must be called before calling `matches()`, or it will always return /// false. #[inline] pubfn build_with_diagnostics(
&mutself,
record_matches: bool,
name: Option<&'static str>,
) { self.build_inner(record_matches, name)
}
#[cfg(all(not(feature = "__cli"), feature = "experimental"))] /// Construct a RegexSet from the set of entries we've accumulated and emit diagnostics if the /// name of the regex set is passed to it. /// /// Must be called before calling `matches()`, or it will always return /// false. #[inline] pub(crate) fn build_with_diagnostics(
&mutself,
record_matches: bool,
name: Option<&'static str>,
) { self.build_inner(record_matches, name)
}
/// Does the given `string` match any of the regexes in this set? pubfn matches<S>(&self, string: S) -> bool where
S: AsRef<str>,
{ let s = string.as_ref(); let set = matchself.set {
Some(ref set) => set,
None => returnfalse,
};
if !self.record_matches { return set.is_match(s);
}
let matches = set.matches(s); if !matches.matched_any() { returnfalse;
} for i in matches.iter() { self.matched[i].set(true);
}
match err {
regex::Error::Syntax(string) => { if string.starts_with("regex parse error:\n") { letmut source = String::new();
letmut parsing_source = true;
for line in string.lines().skip(1) { if parsing_source { if line.starts_with(' ') {
source.push_str(line);
source.push('\n'); continue;
}
parsing_source = false;
} let error = "error: "; if line.starts_with(error) { let (_, msg) = line.split_at(error.len());
diagnostic.add_annotation(msg.to_owned(), Level::Error);
} else {
diagnostic.add_annotation(line.to_owned(), Level::Info);
}
} letmut slice = Slice::default();
slice.with_source(source);
diagnostic.add_slice(slice);
diagnostic.with_title( "Error while parsing a regular expression.",
Level::Warn,
);
} else {
diagnostic.with_title(string, Level::Warn);
}
}
err => { let err = err.to_string();
diagnostic.with_title(err, Level::Warn);
}
}
diagnostic.add_annotation(
format!("This regular expression was passed via `{}`.", name),
Level::Note,
);
if set.items.iter().any(|item| item.as_ref() == "*") {
diagnostic.add_annotation("Wildcard patterns \"*\" are no longer considered valid. Use \".*\" instead.", Level::Help);
}
diagnostic.display();
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.