From 5363f350887b1e5b5dd21a86f88c8af9d7fea6da Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:25 +0200 Subject: Merging upstream version 1.67.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/regex-automata-0.1.10/src/transducer.rs | 107 +++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 vendor/regex-automata-0.1.10/src/transducer.rs (limited to 'vendor/regex-automata-0.1.10/src/transducer.rs') diff --git a/vendor/regex-automata-0.1.10/src/transducer.rs b/vendor/regex-automata-0.1.10/src/transducer.rs new file mode 100644 index 000000000..679c75726 --- /dev/null +++ b/vendor/regex-automata-0.1.10/src/transducer.rs @@ -0,0 +1,107 @@ +use fst::Automaton; + +use crate::{StateID, DFA}; + +macro_rules! imp { + ($ty:ty, $id:ty) => { + impl, S: StateID> Automaton for $ty { + type State = S; + + #[inline] + fn start(&self) -> S { + self.start_state() + } + + #[inline] + fn is_match(&self, state: &S) -> bool { + self.is_match_state(*state) + } + + #[inline] + fn accept(&self, state: &S, byte: u8) -> S { + self.next_state(*state, byte) + } + + #[inline] + fn can_match(&self, state: &S) -> bool { + !self.is_dead_state(*state) + } + } + }; +} + +imp!(crate::dense::DenseDFA, S); +imp!(crate::dense::Standard, S); +imp!(crate::dense::ByteClass, S); +imp!(crate::dense::Premultiplied, S); +imp!(crate::dense::PremultipliedByteClass, S); +imp!(crate::sparse::SparseDFA, u8); +imp!(crate::sparse::Standard, u8); +imp!(crate::sparse::ByteClass, u8); + +#[cfg(test)] +mod tests { + use bstr::BString; + use fst::{Automaton, IntoStreamer, Set, Streamer}; + + use crate::dense::{self, DenseDFA}; + use crate::sparse::SparseDFA; + + fn search>( + set: &Set, + aut: A, + ) -> Vec { + let mut stream = set.search(aut).into_stream(); + + let mut results = vec![]; + while let Some(key) = stream.next() { + results.push(BString::from(key)); + } + results + } + + #[test] + fn dense_anywhere() { + let set = + Set::from_iter(&["a", "bar", "baz", "wat", "xba", "xbax", "z"]) + .unwrap(); + let dfa = DenseDFA::new("ba.*").unwrap(); + let got = search(&set, &dfa); + assert_eq!(got, vec!["bar", "baz", "xba", "xbax"]); + } + + #[test] + fn dense_anchored() { + let set = + Set::from_iter(&["a", "bar", "baz", "wat", "xba", "xbax", "z"]) + .unwrap(); + let dfa = dense::Builder::new().anchored(true).build("ba.*").unwrap(); + let got = search(&set, &dfa); + assert_eq!(got, vec!["bar", "baz"]); + } + + #[test] + fn sparse_anywhere() { + let set = + Set::from_iter(&["a", "bar", "baz", "wat", "xba", "xbax", "z"]) + .unwrap(); + let dfa = SparseDFA::new("ba.*").unwrap(); + let got = search(&set, &dfa); + assert_eq!(got, vec!["bar", "baz", "xba", "xbax"]); + } + + #[test] + fn sparse_anchored() { + let set = + Set::from_iter(&["a", "bar", "baz", "wat", "xba", "xbax", "z"]) + .unwrap(); + let dfa = dense::Builder::new() + .anchored(true) + .build("ba.*") + .unwrap() + .to_sparse() + .unwrap(); + let got = search(&set, &dfa); + assert_eq!(got, vec!["bar", "baz"]); + } +} -- cgit v1.2.3