usecrate::{
automaton::{Automaton, StateID},
Anchored as AcAnchored, Input, MatchError,
};
/// Represents an unanchored Aho-Corasick search of a finite state transducer. /// /// Wrapping an Aho-Corasick automaton in `Unanchored` will fail if the /// underlying automaton does not support unanchored searches. /// /// # Example /// /// This shows how to build an FST of keys and then run an unanchored search on /// those keys using an Aho-Corasick automaton. /// /// ``` /// use aho_corasick::{nfa::contiguous::NFA, transducer::Unanchored}; /// use fst::{Automaton, IntoStreamer, Set, Streamer}; /// /// let set = Set::from_iter(&["abcd", "bc", "bcd", "xyz"]).unwrap(); /// let nfa = NFA::new(&["bcd", "x"]).unwrap(); /// // NFAs always support both unanchored and anchored searches. /// let searcher = Unanchored::new(&nfa).unwrap(); /// /// let mut stream = set.search(searcher).into_stream(); /// let mut results = vec![]; /// while let Some(key) = stream.next() { /// results.push(std::str::from_utf8(key).unwrap().to_string()); /// } /// assert_eq!(vec!["abcd", "bcd", "xyz"], results); /// ``` #[derive(Clone, Debug)] pubstruct Unanchored<A>(A);
impl<A: Automaton> Unanchored<A> { /// Create a new `Unanchored` implementation of the `fst::Automaton` trait. /// /// If the given Aho-Corasick automaton does not support unanchored /// searches, then this returns an error. pubfn new(aut: A) -> Result<Unanchored<A>, MatchError> { let input = Input::new("").anchored(AcAnchored::No); let _ = aut.start_state(&input)?;
Ok(Unanchored(aut))
}
/// Returns a borrow to the underlying automaton. pubfn as_ref(&self) -> &A {
&self.0
}
/// Unwrap this value and return the inner automaton. pubfn into_inner(self) -> A { self.0
}
}
impl<A: Automaton> fst::Automaton for Unanchored<A> { type State = StateID;
#[inline] fn start(&self) -> StateID { let input = Input::new("").anchored(AcAnchored::No); self.0.start_state(&input).expect("support for unanchored searches")
}
/// Represents an anchored Aho-Corasick search of a finite state transducer. /// /// Wrapping an Aho-Corasick automaton in `Unanchored` will fail if the /// underlying automaton does not support unanchored searches. /// /// # Example /// /// This shows how to build an FST of keys and then run an anchored search on /// those keys using an Aho-Corasick automaton. /// /// ``` /// use aho_corasick::{nfa::contiguous::NFA, transducer::Anchored}; /// use fst::{Automaton, IntoStreamer, Set, Streamer}; /// /// let set = Set::from_iter(&["abcd", "bc", "bcd", "xyz"]).unwrap(); /// let nfa = NFA::new(&["bcd", "x"]).unwrap(); /// // NFAs always support both unanchored and anchored searches. /// let searcher = Anchored::new(&nfa).unwrap(); /// /// let mut stream = set.search(searcher).into_stream(); /// let mut results = vec![]; /// while let Some(key) = stream.next() { /// results.push(std::str::from_utf8(key).unwrap().to_string()); /// } /// assert_eq!(vec!["bcd", "xyz"], results); /// ``` /// /// This is like the example above, except we use an Aho-Corasick DFA, which /// requires explicitly configuring it to support anchored searches. (NFAs /// unconditionally support both unanchored and anchored searches.) /// /// ``` /// use aho_corasick::{dfa::DFA, transducer::Anchored, StartKind}; /// use fst::{Automaton, IntoStreamer, Set, Streamer}; /// /// let set = Set::from_iter(&["abcd", "bc", "bcd", "xyz"]).unwrap(); /// let dfa = DFA::builder() /// .start_kind(StartKind::Anchored) /// .build(&["bcd", "x"]) /// .unwrap(); /// // We've explicitly configured our DFA to support anchored searches. /// let searcher = Anchored::new(&dfa).unwrap(); /// /// let mut stream = set.search(searcher).into_stream(); /// let mut results = vec![]; /// while let Some(key) = stream.next() { /// results.push(std::str::from_utf8(key).unwrap().to_string()); /// } /// assert_eq!(vec!["bcd", "xyz"], results); /// ``` #[derive(Clone, Debug)] pubstruct Anchored<A>(A);
impl<A: Automaton> Anchored<A> { /// Create a new `Anchored` implementation of the `fst::Automaton` trait. /// /// If the given Aho-Corasick automaton does not support anchored searches, /// then this returns an error. pubfn new(aut: A) -> Result<Anchored<A>, MatchError> { let input = Input::new("").anchored(AcAnchored::Yes); let _ = aut.start_state(&input)?;
Ok(Anchored(aut))
}
/// Returns a borrow to the underlying automaton. pubfn as_ref(&self) -> &A {
&self.0
}
/// Unwrap this value and return the inner automaton. pubfn into_inner(self) -> A { self.0
}
}
impl<A: Automaton> fst::Automaton for Anchored<A> { type State = StateID;
#[inline] fn start(&self) -> StateID { let input = Input::new("").anchored(AcAnchored::Yes); self.0.start_state(&input).expect("support for unanchored searches")
}
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.