Use DoubleArrayAhoCorasickBuilder to construct an efficient Aho-Corasick automaton. You can build it using a simple list of patterns (where the index is automatically used as the value) or by providing explicit pattern-value pairs.
Basic usage with patterns
When passing a list of patterns, the builder automatically associates each pattern with its index i in the list. The type V must be able to be created from a usize via TryFrom.
use daachorse::DoubleArrayAhoCorasickBuilder;
let patterns = vec!["bcd", "ab", "a"];
let pma = DoubleArrayAhoCorasickBuilder::new()
.build(patterns)
.unwrap();
Usage with explicit values
If you need to associate specific data (like IDs or custom structs) with your patterns, use build_with_values. This allows you to map each pattern to a custom type V.
use daachorse::DoubleArrayAhoCorasickBuilder;
let patvals = vec![("bcd", 0), ("ab", 1), ("a", 2), ("e", 1)];
let pma = DoubleArrayAhoCorasickBuilder::new()
.build_with_values(patvals)
.unwrap();
use daachorse::DoubleArrayAhoCorasickBuilder;
let patterns = vec!["bcd", "ab", "a"];
let pma = DoubleArrayAhoCorasickBuilder::new()
.build(patterns)
.unwrap();
let mut it = pma.find_iter("abcd");
let m = it.next().unwrap();
assert_eq!((m.start(), m.end(), m.value()), (0, 1, 2));
let m = it.next().unwrap();
assert_eq!((m.start(), m.end(), m.value()), (1, 4, 0));
assert_eq!(None, it.next());