<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><metaname="generator"content="rustdoc"><metaname="description"content="A library for finding occurrences of many patterns at once. This library provides multiple pattern search principally through an implementation of the Aho-Corasick algorithm, which builds a fast finite state machine for executing searches in linear time."><title>aho_corasick - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><linkrel="stylesheet"href="../static.files/normalize-76eba96aa4d2e634.css"><linkrel="stylesheet"href="../static.files/rustdoc-dd39b87e5fcfba68.css"><metaname="rustdoc-vars"data-root-path="../"data-static-root-path="../static.files/"data-current-crate="aho_corasick"data-themes=""data-resource-suffix=""data-rustdoc-version="1.80.0 (051478957 2024-07-21)"data-channel="1.80.0"data-search-js="search-d52510db62a78183.js"data-settings-js="settings-4313503d2e1961c2.js"><scriptsrc="../static.files/storage-118b08c4c78b968e.js"></script><scriptdefersrc="../crates.js"></script><scriptdefersrc="../static.files/main-20a3ad099b048cf2.js"></script><noscript><linkrel="stylesheet"href="../static.files/noscript-df360f571f6edeae.css"></noscript><linkrel="alternate icon"type="image/png"href="../static.files/favicon-32x32-422f7d1d52889060.png"><linkrel="icon"type="image/svg+xml"href="../static.files/favicon-2c020d218678b618.svg"></head><bodyclass="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><navclass="mobile-topbar"><buttonclass="sidebar-menu-toggle"title="show sidebar"></button></nav><navclass="sidebar"><divclass="sidebar-crate"><h2><ahref="../aho_corasick/index.html">aho_corasick</a><spanclass="version">1.1.3</span></h2></div><divclass="sidebar-elems"><ulclass="block"><li><aid="all-types"href="all.html">All Items</a></li></ul><section><ulclass="block"><li><ahref="#modules">Modules</a></li><li><ahref="#structs">Structs</a></li><li><ahref="#enums">Enums</a></li></ul></section></div></nav><divclass="sidebar-resizer"></div><main><divclass="width-limiter"><rustdoc-search></rustdoc-search><sectionid="main-content"class="content"><divclass="main-heading"><h1>Crate <aclass="mod"href="#">aho_corasick</a><buttonid="copy-path"title="Copy item path to clipboard">Copy item path</button></h1><spanclass="out-of-band"><aclass="src"href="../src/aho_corasick/lib.rs.html#1-326">source</a> · <buttonid="toggle-all-docs"title="collapse all docs">[<span>−</span>]</button></span></div><detailsclass="toggle top-doc"open><summaryclass="hideme"><span>Expand description</span></summary><divclass="docblock"><p>A library for finding occurrences of many patterns at once. This library
which builds a fast finite state machine for executing searches in linear time.</p>
<p>Additionally, this library provides a number of configuration options for
building the automaton that permit controlling the space versus time trade
off. Other features include simple ASCII case insensitive matching, finding
overlapping matches, replacements, searching streams and even searching and
replacing text in streams.</p>
<p>Finally, unlike most other Aho-Corasick implementations, this one
supports enabling <ahref="enum.MatchKind.html#variant.LeftmostFirst"title="variant aho_corasick::MatchKind::LeftmostFirst">leftmost-first</a> or
<ahref="enum.MatchKind.html#variant.LeftmostLongest"title="variant aho_corasick::MatchKind::LeftmostLongest">leftmost-longest</a> match semantics, using a
(seemingly) novel alternative construction algorithm. For more details on what
match semantics means, see the <ahref="enum.MatchKind.html"title="enum aho_corasick::MatchKind"><code>MatchKind</code></a> type.</p>
<p>This section gives a brief overview of the primary types in this crate:</p>
<ul>
<li><ahref="struct.AhoCorasick.html"title="struct aho_corasick::AhoCorasick"><code>AhoCorasick</code></a> is the primary type and represents an Aho-Corasick automaton.
This is the type you use to execute searches.</li>
<li><ahref="struct.AhoCorasickBuilder.html"title="struct aho_corasick::AhoCorasickBuilder"><code>AhoCorasickBuilder</code></a> can be used to build an Aho-Corasick automaton, and
supports configuring a number of options.</li>
<li><ahref="struct.Match.html"title="struct aho_corasick::Match"><code>Match</code></a> represents a single match reported by an Aho-Corasick automaton.
Each match has two pieces of information: the pattern that matched and the
start and end byte offsets corresponding to the position in the haystack at
<h2id="example-replacing-matches-in-a-stream"><aclass="doc-anchor"href="#example-replacing-matches-in-a-stream">§</a>Example: replacing matches in a stream</h2>
<h2id="example-finding-the-leftmost-first-match"><aclass="doc-anchor"href="#example-finding-the-leftmost-first-match">§</a>Example: finding the leftmost first match</h2>
<p>While an Aho-Corasick automaton can perform admirably when compared to more
naive solutions, it is generally slower than more specialized algorithms that
are accelerated using vector instructions such as SIMD.</p>
<p>For that reason, this library will internally use a “prefilter” to attempt
to accelerate searches when possible. Currently, this library has several
different algorithms it might use depending on the patterns provided. Once the
number of patterns gets too big, prefilters are no longer used.</p>
<p>While a prefilter is generally good to have on by default since it works
well in the common case, it can lead to less predictable or even sub-optimal
performance in some cases. For that reason, prefilters can be explicitly
disabled via <ahref="struct.AhoCorasickBuilder.html#method.prefilter"title="method aho_corasick::AhoCorasickBuilder::prefilter"><code>AhoCorasickBuilder::prefilter</code></a>.</p>
<p>This crate also provides several sub-modules that collectively expose many of
the implementation details of the main <ahref="struct.AhoCorasick.html"title="struct aho_corasick::AhoCorasick"><code>AhoCorasick</code></a> type. Most users of this
library can completely ignore the submodules and their contents, but if you
needed finer grained control, some parts of them may be useful to you. Here is
a brief overview of each and why you might want to use them:</p>
<ul>
<li>The <ahref="packed/index.html"title="mod aho_corasick::packed"><code>packed</code></a> sub-module contains a lower level API for using fast
vectorized routines for finding a small number of patterns in a haystack.
You might want to use this API when you want to completely side-step using
Aho-Corasick automata. Otherwise, the fast vectorized routines are used
automatically as prefilters for <code>AhoCorasick</code> searches whenever possible.</li>
<li>The <ahref="automaton/index.html"title="mod aho_corasick::automaton"><code>automaton</code></a> sub-module provides a lower level finite state
machine interface that the various Aho-Corasick implementations in
this crate implement. This sub-module’s main contribution is the
<ahref="automaton/trait.Automaton.html"title="trait aho_corasick::automaton::Automaton"><code>Automaton</code></a> trait, which permits manually walking the
state transitions of an Aho-Corasick automaton.</li>
<li>The <ahref="dfa/index.html"title="mod aho_corasick::dfa"><code>dfa</code></a> and <ahref="nfa/index.html"title="mod aho_corasick::nfa"><code>nfa</code></a> sub-modules provide DFA and NFA implementations of
the aforementioned <code>Automaton</code> trait. The main reason one might want to use
these sub-modules is to get access to a type that implements the <code>Automaton</code>
trait. (The top-level <code>AhoCorasick</code> type does not implement the <code>Automaton</code>
trait.)</li>
</ul>
<p>As mentioned above, if you aren’t sure whether you need these sub-modules,
you should be able to safely ignore them and just focus on the <ahref="struct.AhoCorasick.html"title="struct aho_corasick::AhoCorasick"><code>AhoCorasick</code></a>
<p>This crate exposes a few features for controlling dependency usage and whether
this crate can be used without the standard library.</p>
<ul>
<li><strong>std</strong> -
Enables support for the standard library. This feature is enabled by
default. When disabled, only <code>core</code> and <code>alloc</code> are used. At an API
level, enabling <code>std</code> enables <code>std::error::Error</code> trait impls for the
various error types, and higher level stream search routines such as
<ahref="struct.AhoCorasick.html#method.try_stream_find_iter"title="method aho_corasick::AhoCorasick::try_stream_find_iter"><code>AhoCorasick::try_stream_find_iter</code></a>. But the <code>std</code> feature is also required
to enable vectorized prefilters. Prefilters can greatly accelerate searches,
but generally only apply when the number of patterns is small (less than
~100).</li>
<li><strong>perf-literal</strong> -
Enables support for literal prefilters that use vectorized routines from
external crates. This feature is enabled by default. If you’re only using
Aho-Corasick for large numbers of patterns or otherwise can abide lower
throughput when searching with a small number of patterns, then it is
reasonable to disable this feature.</li>
<li><strong>logging</strong> -
Enables a dependency on the <code>log</code> crate and emits messages to aide in
diagnostics. This feature is disabled by default.</li>
</div></details><h2id="modules"class="section-header">Modules<ahref="#modules"class="anchor">§</a></h2><ulclass="item-table"><li><divclass="item-name"><aclass="mod"href="automaton/index.html"title="mod aho_corasick::automaton">automaton</a></div><divclass="desc docblock-short">Provides <ahref="automaton/trait.Automaton.html"title="trait aho_corasick::automaton::Automaton"><code>Automaton</code></a> trait for abstracting over Aho-Corasick automata.</div></li><li><divclass="item-name"><aclass="mod"href="dfa/index.html"title="mod aho_corasick::dfa">dfa</a></div><divclass="desc docblock-short">Provides direct access to a DFA implementation of Aho-Corasick.</div></li><li><divclass="item-name"><aclass="mod"href="nfa/index.html"title="mod aho_corasick::nfa">nfa</a></div><divclass="desc docblock-short">Provides direct access to NFA implementations of Aho-Corasick.</div></li><li><divclass="item-name"><aclass="mod"href="packed/index.html"title="mod aho_corasick::packed">packed</a></div><divclass="desc docblock-short">Provides packed multiple substring search, principally for a small number of
patterns.</div></li></ul><h2id="structs"class="section-header">Structs<ahref="#structs"class="anchor">§</a></h2><ulclass="item-table"><li><divclass="item-name"><aclass="struct"href="struct.AhoCorasick.html"title="struct aho_corasick::AhoCorasick">AhoCorasick</a></div><divclass="desc docblock-short">An automaton for searching multiple strings in linear time.</div></li><li><divclass="item-name"><aclass="struct"href="struct.AhoCorasickBuilder.html"title="struct aho_corasick::AhoCorasickBuilder">AhoCorasickBuilder</a></div><divclass="desc docblock-short">A builder for configuring an Aho-Corasick automaton.</div></li><li><divclass="item-name"><aclass="struct"href="struct.BuildError.html"title="struct aho_corasick::BuildError">BuildError</a></div><divclass="desc docblock-short">An error that occurred during the construction of an Aho-Corasick
automaton.</div></li><li><divclass="item-name"><aclass="struct"href="struct.FindIter.html"title="struct aho_corasick::FindIter">FindIter</a></div><divclass="desc docblock-short">An iterator of non-overlapping matches in a particular haystack.</div></li><li><divclass="item-name"><aclass="struct"href="struct.FindOverlappingIter.html"title="struct aho_corasick::FindOverlappingIter">FindOverlappingIter</a></div><divclass="desc docblock-short">An iterator of overlapping matches in a particular haystack.</div></li><li><divclass="item-name"><aclass="struct"href="struct.Input.html"title="struct aho_corasick::Input">Input</a></div><divclass="desc docblock-short">The configuration and the haystack to use for an Aho-Corasick search.</div></li><li><divclass="item-name"><aclass="struct"href="struct.Match.html"title="struct aho_corasick::Match">Match</a></div><divclass="desc docblock-short">A representation of a match reported by an Aho-Corasick searcher.</div></li><li><divclass="item-name"><aclass="struct"href="struct.MatchError.html"title="struct aho_corasick::MatchError">MatchError</a></div><divclass="desc docblock-short">An error that occurred during an Aho-Corasick search.</div></li><li><divclass="item-name"><aclass="struct"href="struct.PatternID.html"title="struct aho_corasick::PatternID">PatternID</a></div><divclass="desc docblock-short">The identifier of a pattern in an Aho-Corasick automaton.</div></li><li><divclass="item-name"><aclass="struct"href="struct.PatternIDError.html"title="struct aho_corasick::PatternIDError">PatternIDError</a></div><divclass="desc docblock-short">This error occurs when an ID could not be constructed.</div></li><li><divclass="item-name"><aclass="struct"href="struct.Span.html"title="struct aho_corasick::Span">Span</a></div><divclass="desc docblock-short">A representation of a range in a haystack.</div></li><li><divclass="item-name"><aclass="struct"href="struct.StreamFindIter.html"title="struct aho_corasick::StreamFindIter">StreamFindIter</a></div><divclass="desc docblock-short">An iterator that reports Aho-Corasick matches in a stream.</div></li></ul><h2id="enums"class="section-header">Enums<ahref="#enums"class="anchor">§</a></h2><ulclass="item-table"><li><divclass="item-name"><aclass="enum"href="enum.AhoCorasickKind.html"title="enum aho_corasick::AhoCorasickKind">AhoCorasickKind</a></div><divclass="desc docblock-short">The type of Aho-Corasick implementation to use in an <ahref="struct.AhoCorasick.html"title="struct aho_corasick::AhoCorasick"><code>AhoCorasick</code></a>
searcher.</div></li><li><divclass="item-name"><aclass="enum"href="enum.Anchored.html"title="enum aho_corasick::Anchored">Anchored</a></div><divclass="desc docblock-short">The type of anchored search to perform.</div></li><li><divclass="item-name"><aclass="enum"href="enum.MatchErrorKind.html"title="enum aho_corasick::MatchErrorKind">MatchErrorKind</a></div><divclass="desc docblock-short">The underlying kind of a <ahref="struct.MatchError.html"title="struct aho_corasick::MatchError"><code>MatchError</code></a>.</div></li><li><divclass="item-name"><aclass="enum"href="enum.MatchKind.html"title="enum aho_corasick::MatchKind">MatchKind</a></div><divclass="desc docblock-short">A knob for controlling the match semantics of an Aho-Corasick automaton.</div></li><li><divclass="item-name"><aclass="enum"href="enum.StartKind.html"title="enum aho_corasick::StartKind">StartKind</a></div><divclass="desc docblock-short">The kind of anchored starting configurations to support in an Aho-Corasick