Capstone: Quarry's Text Index
In short: one text_index function routes each query to whichever
linear-time tool fits it best — KMP for an exact phrase, a rolling hash's
fingerprint set for many same-length phrases at once, and two Z-arrays
(forwards and backwards) for a query that allows one typo. None of the tools
is faster than the others; what differs is what each one remembers between
characters: a border, a fingerprint, or a matched box.

Three lessons have given Quarry three linear-time ways to look at text. This one
puts them behind a single function, text_index, that takes a set of documents
and a batch of queries and returns every place each query occurs. A query may
also ask for one typo of slack. By the end, the piece you build is the one a
user actually types into.
Picking the tool
Each technique answers a slightly different question, and the index uses each one where it fits best:
| question | best tool | why |
|---|---|---|
| where does this one phrase occur? | KMP (or Z) | exact, worst-case O(n + m), no false alarms |
| does this phrase occur in a live stream? | KMP | the only state is j, so the text is read once |
| which of many same-length phrases occur? | rolling hash | one pass, and a set lookup per window covers every phrase |
| which passages repeat across documents? | rolling hash | fingerprint every window, group by fingerprint |
| where does it occur with at most one typo? | Z, forwards and backwards | front match plus back match covers all but one character |
None of them is "the fast one". They are all linear. What differs is what they remember: a fingerprint, a border, or a box of known matches.