ocaml-aho-corasick
Find many literal patterns in one pass over a byte string. Pure OCaml, with no runtime dependencies beyond the standard library.
v0.1.1 · OCaml ≥ 4.14 · Dune ≥ 3.14 · MIT
Install
Until v0.1.1 is published to opam, install from a checkout:
git clone https://github.com/thevilledev/ocaml-aho-corasick.git
cd ocaml-aho-corasick
opam install .
After publication, use opam install aho-corasick.
Add (libraries aho-corasick) to your Dune stanza.
Quick start
let matcher = Aho_corasick.build [ "he"; "she"; "his"; "hers" ]
let matches = Aho_corasick.find_all matcher "ushers"
(* she: 1..4, he: 2..4, hers: 2..6 *)
let words = Aho_corasick.build [ "cat"; "dog" ]
let replaced =
Aho_corasick.replace_all words ~f:(fun _ -> "[pet]") "cat and dog"
(* "[pet] and [pet]" *)
Build once and reuse the immutable matcher. Each match contains a
pattern index and byte offsets [start, stop).
Choose a search
find_all/find_iter: every match, including overlaps.find_leftmost_longest/replace_all: non-overlapping matches, longest at the earliest position.mem: stop at the first match.Stream: all three match modes and replacement across chunk boundaries.
Matching uses bytes. Case folding is ASCII only. Empty pattern strings are rejected; an empty pattern list matches nothing.
Continue with usage and streaming, match rules, cross-library checks, benchmarks, or the full API reference.