Module Aho_corasick.Stream

Streaming

Scan input arriving in chunks (sockets, files) without concatenating: matches spanning chunk boundaries are found, and offsets are absolute across everything fed so far.

Always use the same automaton that created a state. Empty chunks are allowed. Returned states are immutable; keep the new state to advance the stream. Offsets must fit in an OCaml int.

Three match modes are available:

type state

Immutable scanning position; keep the value returned by feed.

val start : t -> state
val feed : t -> state -> string -> state * match_ list

feed t st chunk scans chunk, returning the advanced state and the matches whose last byte lies in chunk (offsets are absolute). Every match is reported, overlapping ones included, as soon as its final byte is seen; there is never anything to flush.

val feed_nonoverlapping : t -> state -> string -> state * match_ list

Like feed, but non-overlapping, preferring the match that ends first: whenever one or more matches end, the longest of them is reported and scanning restarts immediately after it, so no later match overlaps it. Zero latency and nothing to flush, like feed. Equal-length ties use the lowest pattern index. With patterns ["Samwise"; "Sam"] and input "Samwise", the one match is Sam.

Feed any given stream exclusively with feed or exclusively with feed_nonoverlapping: both advance the same state type, but the restart-after-match discipline only makes sense applied to the whole stream.

val pos : state -> int

Total bytes fed so far.

module Leftmost_longest : sig ... end

Non-overlapping leftmost-longest matches — the find_leftmost_longest selection, streamed.

module Replace : sig ... end

replace_all, streamed: each Leftmost_longest match m is replaced by f m, everything else passes through unchanged.