gecko-dev/third_party/rust/aho-corasick
Nathan Froyd 65e7b0bbd1 Bug 1485409 - update crates to dispense with winapi 0.2.8 requirement; r=glandium
winapi 0.2.8 is used by a number of crates in our dependency graph.  The
newest version of winapi is 0.3.4, which is a significant restructuring
and also more amenable to further development, e.g. adding AArch64
support.  This patch does the easy work of updating as many things as
possible to winapi 0.3.4 via a simple `cargo update`:

cargo update -p atty:0.2.2 -p fs2 -p msdos_time -p parking_lot_core -p aho-corasick

and then vendoring the results of those changes.
2018-08-28 21:37:30 -04:00
..
benches
ci
examples
src
.cargo-checksum.json
.travis.yml
COPYING
Cargo.toml
LICENSE-MIT
Makefile
README.md
UNLICENSE
ctags.rust
session.vim

README.md

This crate provides an implementation of the Aho-Corasick algorithm. Its intended use case is for fast substring matching, particularly when matching multiple substrings in a search text. This is achieved by compiling the substrings into a finite state machine.

This implementation provides optimal algorithmic time complexity. Construction of the finite state machine is O(p) where p is the length of the substrings concatenated. Matching against search text is O(n + p + m), where n is the length of the search text and m is the number of matches.

Build status

Dual-licensed under MIT or the UNLICENSE.

Documentation

https://docs.rs/aho-corasick/.

Example

The documentation contains several examples, and there is a more complete example as a full program in examples/dict-search.rs.

Here is a quick example showing simple substring matching:

use aho_corasick::{Automaton, AcAutomaton, Match};

let aut = AcAutomaton::new(vec!["apple", "maple"]);
let mut it = aut.find("I like maple apples.");
assert_eq!(it.next(), Some(Match {
    pati: 1,
    start: 7,
    end: 12,
}));
assert_eq!(it.next(), Some(Match {
    pati: 0,
    start: 13,
    end: 18,
}));
assert_eq!(it.next(), None);

Alternatives

Aho-Corasick is useful for matching multiple substrings against many long strings. If your long string is fixed, then you might consider building a suffix array of the search text (which takes O(n) time). Matches can then be found in O(plogn) time.