| <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="An automaton for searching multiple strings in linear time."><meta name="keywords" content="rust, rustlang, rust-lang, AhoCorasick"><title>AhoCorasick in aho_corasick - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc struct"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../aho_corasick/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../aho_corasick/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">AhoCorasick</a></h2><div class="sidebar-elems"><section><h3><a href="#implementations">Methods</a></h3><ul class="block"><li><a href="#method.builder">builder</a></li><li><a href="#method.find">find</a></li><li><a href="#method.find_iter">find_iter</a></li><li><a href="#method.find_overlapping">find_overlapping</a></li><li><a href="#method.find_overlapping_iter">find_overlapping_iter</a></li><li><a href="#method.is_match">is_match</a></li><li><a href="#method.kind">kind</a></li><li><a href="#method.match_kind">match_kind</a></li><li><a href="#method.max_pattern_len">max_pattern_len</a></li><li><a href="#method.memory_usage">memory_usage</a></li><li><a href="#method.min_pattern_len">min_pattern_len</a></li><li><a href="#method.new">new</a></li><li><a href="#method.patterns_len">patterns_len</a></li><li><a href="#method.replace_all">replace_all</a></li><li><a href="#method.replace_all_bytes">replace_all_bytes</a></li><li><a href="#method.replace_all_with">replace_all_with</a></li><li><a href="#method.replace_all_with_bytes">replace_all_with_bytes</a></li><li><a href="#method.start_kind">start_kind</a></li><li><a href="#method.stream_find_iter">stream_find_iter</a></li><li><a href="#method.try_find">try_find</a></li><li><a href="#method.try_find_iter">try_find_iter</a></li><li><a href="#method.try_find_overlapping">try_find_overlapping</a></li><li><a href="#method.try_find_overlapping_iter">try_find_overlapping_iter</a></li><li><a href="#method.try_replace_all">try_replace_all</a></li><li><a href="#method.try_replace_all_bytes">try_replace_all_bytes</a></li><li><a href="#method.try_replace_all_with">try_replace_all_with</a></li><li><a href="#method.try_replace_all_with_bytes">try_replace_all_with_bytes</a></li><li><a href="#method.try_stream_find_iter">try_stream_find_iter</a></li><li><a href="#method.try_stream_replace_all">try_stream_replace_all</a></li><li><a href="#method.try_stream_replace_all_with">try_stream_replace_all_with</a></li></ul><h3><a href="#trait-implementations">Trait Implementations</a></h3><ul class="block"><li><a href="#impl-Clone-for-AhoCorasick">Clone</a></li><li><a href="#impl-Debug-for-AhoCorasick">Debug</a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block"><li><a href="#impl-RefUnwindSafe-for-AhoCorasick">RefUnwindSafe</a></li><li><a href="#impl-Send-for-AhoCorasick">Send</a></li><li><a href="#impl-Sync-for-AhoCorasick">Sync</a></li><li><a href="#impl-Unpin-for-AhoCorasick">Unpin</a></li><li><a href="#impl-UnwindSafe-for-AhoCorasick">UnwindSafe</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block"><li><a href="#impl-Any-for-AhoCorasick">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-AhoCorasick">Borrow<T></a></li><li><a href="#impl-BorrowMut%3CT%3E-for-AhoCorasick">BorrowMut<T></a></li><li><a href="#impl-From%3CT%3E-for-AhoCorasick">From<T></a></li><li><a href="#impl-Into%3CU%3E-for-AhoCorasick">Into<U></a></li><li><a href="#impl-ToOwned-for-AhoCorasick">ToOwned</a></li><li><a href="#impl-TryFrom%3CU%3E-for-AhoCorasick">TryFrom<U></a></li><li><a href="#impl-TryInto%3CU%3E-for-AhoCorasick">TryInto<U></a></li></ul></section><h2><a href="index.html">In aho_corasick</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Struct <a href="index.html">aho_corasick</a>::<wbr><a class="struct" href="#">AhoCorasick</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/aho_corasick/ahocorasick.rs.html#177-214">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="item-decl"><pre class="rust struct"><code>pub struct AhoCorasick { /* private fields */ }</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>An automaton for searching multiple strings in linear time.</p> |
| <p>The <code>AhoCorasick</code> type supports a few basic ways of constructing an |
| automaton, with the default being <a href="struct.AhoCorasick.html#method.new" title="AhoCorasick::new"><code>AhoCorasick::new</code></a>. However, there |
| are a fair number of configurable options that can be set by using |
| <a href="struct.AhoCorasickBuilder.html" title="AhoCorasickBuilder"><code>AhoCorasickBuilder</code></a> instead. Such options include, but are not limited |
| to, how matches are determined, simple case insensitivity, whether to use a |
| DFA or not and various knobs for controlling the space-vs-time trade offs |
| taken when building the automaton.</p> |
| <h2 id="resource-usage"><a href="#resource-usage">Resource usage</a></h2> |
| <p>Aho-Corasick automatons are always constructed in <code>O(p)</code> time, where |
| <code>p</code> is the combined length of all patterns being searched. With that |
| said, building an automaton can be fairly costly because of high constant |
| factors, particularly when enabling the <a href="enum.AhoCorasickKind.html#variant.DFA">DFA</a> option |
| with <a href="struct.AhoCorasickBuilder.html#method.kind" title="AhoCorasickBuilder::kind"><code>AhoCorasickBuilder::kind</code></a>. For this reason, it’s generally a good |
| idea to build an automaton once and reuse it as much as possible.</p> |
| <p>Aho-Corasick automatons can also use a fair bit of memory. To get |
| a concrete idea of how much memory is being used, try using the |
| <a href="struct.AhoCorasick.html#method.memory_usage" title="AhoCorasick::memory_usage"><code>AhoCorasick::memory_usage</code></a> method.</p> |
| <p>To give a quick idea of the differences between Aho-Corasick |
| implementations and their resource usage, here’s a sample of construction |
| times and heap memory used after building an automaton from 100,000 |
| randomly selected titles from Wikipedia:</p> |
| <ul> |
| <li>99MB for a <a href="nfa/noncontiguous/struct.NFA.html" title="noncontiguous::NFA"><code>noncontiguous::NFA</code></a> in 240ms.</li> |
| <li>21MB for a <a href="nfa/contiguous/struct.NFA.html" title="contiguous::NFA"><code>contiguous::NFA</code></a> in 275ms.</li> |
| <li>1.6GB for a <a href="dfa/struct.DFA.html" title="dfa::DFA"><code>dfa::DFA</code></a> in 1.88s.</li> |
| </ul> |
| <p>(Note that the memory usage above reflects the size of each automaton and |
| not peak memory usage. For example, building a contiguous NFA requires |
| first building a noncontiguous NFA. Once the contiguous NFA is built, the |
| noncontiguous NFA is freed.)</p> |
| <p>This experiment very strongly argues that a contiguous NFA is often the |
| best balance in terms of resource usage. It takes a little longer to build, |
| but its memory usage is quite small. Its search speed (not listed) is |
| also often faster than a noncontiguous NFA, but a little slower than a |
| DFA. Indeed, when no specific <a href="enum.AhoCorasickKind.html" title="AhoCorasickKind"><code>AhoCorasickKind</code></a> is used (which is the |
| default), a contiguous NFA is used in most cases.</p> |
| <p>The only “catch” to using a contiguous NFA is that, because of its variety |
| of compression tricks, it may not be able to support automatons as large as |
| what the noncontiguous NFA supports. In which case, building a contiguous |
| NFA will fail and (by default) <code>AhoCorasick</code> will automatically fall |
| back to a noncontiguous NFA. (This typically only happens when building |
| automatons from millions of patterns.) Otherwise, the small additional time |
| for building a contiguous NFA is almost certainly worth it.</p> |
| <h2 id="cloning"><a href="#cloning">Cloning</a></h2> |
| <p>The <code>AhoCorasick</code> type uses thread safe reference counting internally. It |
| is guaranteed that it is cheap to clone.</p> |
| <h2 id="search-configuration"><a href="#search-configuration">Search configuration</a></h2> |
| <p>Most of the search routines accept anything that can be cheaply converted |
| to an <a href="struct.Input.html" title="Input"><code>Input</code></a>. This includes <code>&[u8]</code>, <code>&str</code> and <code>Input</code> itself.</p> |
| <h2 id="construction-failure"><a href="#construction-failure">Construction failure</a></h2> |
| <p>It is generally possible for building an Aho-Corasick automaton to fail. |
| Construction can fail in generally one way: when the inputs provided are |
| too big. Whether that’s a pattern that is too long, too many patterns |
| or some combination of both. A first approximation for the scale at which |
| construction can fail is somewhere around “millions of patterns.”</p> |
| <p>For that reason, if you’re building an Aho-Corasick automaton from |
| untrusted input (or input that doesn’t have any reasonable bounds on its |
| size), then it is strongly recommended to handle the possibility of an |
| error.</p> |
| <p>If you’re constructing an Aho-Corasick automaton from static or trusted |
| data, then it is likely acceptable to panic (by calling <code>unwrap()</code> or |
| <code>expect()</code>) if construction fails.</p> |
| <h2 id="fallibility"><a href="#fallibility">Fallibility</a></h2> |
| <p>The <code>AhoCorasick</code> type provides a number of methods for searching, as one |
| might expect. Depending on how the Aho-Corasick automaton was built and |
| depending on the search configuration, it is possible for a search to |
| return an error. Since an error is <em>never</em> dependent on the actual contents |
| of the haystack, this type provides both infallible and fallible methods |
| for searching. The infallible methods panic if an error occurs, and can be |
| used for convenience and when you know the search will never return an |
| error.</p> |
| <p>For example, the <a href="struct.AhoCorasick.html#method.find_iter" title="AhoCorasick::find_iter"><code>AhoCorasick::find_iter</code></a> method is the infallible |
| version of the <a href="struct.AhoCorasick.html#method.try_find_iter" title="AhoCorasick::try_find_iter"><code>AhoCorasick::try_find_iter</code></a> method.</p> |
| <p>Examples of errors that can occur:</p> |
| <ul> |
| <li>Running a search that requires <a href="enum.MatchKind.html#variant.Standard" title="MatchKind::Standard"><code>MatchKind::Standard</code></a> semantics (such |
| as a stream or overlapping search) with an automaton that was built with |
| <a href="enum.MatchKind.html#variant.LeftmostFirst" title="MatchKind::LeftmostFirst"><code>MatchKind::LeftmostFirst</code></a> or <a href="enum.MatchKind.html#variant.LeftmostLongest" title="MatchKind::LeftmostLongest"><code>MatchKind::LeftmostLongest</code></a> semantics.</li> |
| <li>Running an anchored search with an automaton that only supports |
| unanchored searches. (By default, <code>AhoCorasick</code> only supports unanchored |
| searches. But this can be toggled with <a href="struct.AhoCorasickBuilder.html#method.start_kind" title="AhoCorasickBuilder::start_kind"><code>AhoCorasickBuilder::start_kind</code></a>.)</li> |
| <li>Running an unanchored search with an automaton that only supports |
| anchored searches.</li> |
| </ul> |
| <p>The common thread between the different types of errors is that they are |
| all rooted in the automaton construction and search configurations. If |
| those configurations are a static property of your program, then it is |
| reasonable to call infallible routines since you know an error will never |
| occur. And if one <em>does</em> occur, then it’s a bug in your program.</p> |
| <p>To re-iterate, if the patterns, build or search configuration come from |
| user or untrusted data, then you should handle errors at build or search |
| time. If only the haystack comes from user or untrusted data, then there |
| should be no need to handle errors anywhere and it is generally encouraged |
| to <code>unwrap()</code> (or <code>expect()</code>) both build and search time calls.</p> |
| <h2 id="examples"><a href="#examples">Examples</a></h2> |
| <p>This example shows how to search for occurrences of multiple patterns |
| simultaneously in a case insensitive fashion. Each match includes the |
| pattern that matched along with the byte offsets of the match.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, PatternID}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"apple"</span>, <span class="string">"maple"</span>, <span class="string">"snapple"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"Nobody likes maple in their apple flavored Snapple."</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .ascii_case_insensitive(<span class="bool-val">true</span>) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>matches = <span class="macro">vec!</span>[]; |
| <span class="kw">for </span>mat <span class="kw">in </span>ac.find_iter(haystack) { |
| matches.push((mat.pattern(), mat.start(), mat.end())); |
| } |
| <span class="macro">assert_eq!</span>(matches, <span class="macro">vec!</span>[ |
| (PatternID::must(<span class="number">1</span>), <span class="number">13</span>, <span class="number">18</span>), |
| (PatternID::must(<span class="number">0</span>), <span class="number">28</span>, <span class="number">33</span>), |
| (PatternID::must(<span class="number">2</span>), <span class="number">43</span>, <span class="number">50</span>), |
| ]);</code></pre></div> |
| <p>This example shows how to replace matches with some other string:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::AhoCorasick; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"fox"</span>, <span class="string">"brown"</span>, <span class="string">"quick"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"The quick brown fox."</span>; |
| <span class="kw">let </span>replace_with = <span class="kw-2">&</span>[<span class="string">"sloth"</span>, <span class="string">"grey"</span>, <span class="string">"slow"</span>]; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(patterns).unwrap(); |
| <span class="kw">let </span>result = ac.replace_all(haystack, replace_with); |
| <span class="macro">assert_eq!</span>(result, <span class="string">"The slow grey sloth."</span>);</code></pre></div> |
| </div></details><h2 id="implementations" class="small-section-header">Implementations<a href="#implementations" class="anchor"></a></h2><div id="implementations-list"><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AhoCorasick" class="impl has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#218-271">source</a><a href="#impl-AhoCorasick" class="anchor"></a><h3 class="code-header">impl <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h3></section></summary><div class="docblock"><p>Convenience constructors for an Aho-Corasick searcher. To configure the |
| searcher, use an <a href="struct.AhoCorasickBuilder.html" title="AhoCorasickBuilder"><code>AhoCorasickBuilder</code></a> instead.</p> |
| </div><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.new" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#243-249">source</a><h4 class="code-header">pub fn <a href="#method.new" class="fnname">new</a><I, P>(patterns: I) -> Result<<a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a>, <a class="struct" href="struct.BuildError.html" title="struct aho_corasick::BuildError">BuildError</a>><span class="where fmt-newline">where<br> I: IntoIterator<Item = P>,<br> P: AsRef<[u8]>,</span></h4></section></summary><div class="docblock"><p>Create a new Aho-Corasick automaton using the default configuration.</p> |
| <p>The default configuration optimizes for less space usage, but at the |
| expense of longer search times. To change the configuration, use |
| <a href="struct.AhoCorasickBuilder.html" title="AhoCorasickBuilder"><code>AhoCorasickBuilder</code></a>.</p> |
| <p>This uses the default <a href="enum.MatchKind.html#variant.Standard" title="MatchKind::Standard"><code>MatchKind::Standard</code></a> match semantics, which |
| reports a match as soon as it is found. This corresponds to the |
| standard match semantics supported by textbook descriptions of the |
| Aho-Corasick algorithm.</p> |
| <h5 id="examples-1"><a href="#examples-1">Examples</a></h5> |
| <p>Basic usage:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, PatternID}; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(<span class="kw-2">&</span>[<span class="string">"foo"</span>, <span class="string">"bar"</span>, <span class="string">"baz"</span>]).unwrap(); |
| <span class="macro">assert_eq!</span>( |
| <span class="prelude-val">Some</span>(PatternID::must(<span class="number">1</span>)), |
| ac.find(<span class="string">"xxx bar xxx"</span>).map(|m| m.pattern()), |
| );</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.builder" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#268-270">source</a><h4 class="code-header">pub fn <a href="#method.builder" class="fnname">builder</a>() -> <a class="struct" href="struct.AhoCorasickBuilder.html" title="struct aho_corasick::AhoCorasickBuilder">AhoCorasickBuilder</a></h4></section></summary><div class="docblock"><p>A convenience method for returning a new Aho-Corasick builder.</p> |
| <p>This usually permits one to just import the <code>AhoCorasick</code> type.</p> |
| <h5 id="examples-2"><a href="#examples-2">Examples</a></h5> |
| <p>Basic usage:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, Match, MatchKind}; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(<span class="kw-2">&</span>[<span class="string">"samwise"</span>, <span class="string">"sam"</span>]) |
| .unwrap(); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">0</span>, <span class="number">0</span>..<span class="number">7</span>)), ac.find(<span class="string">"samwise"</span>));</code></pre></div> |
| </div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AhoCorasick-1" class="impl has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#280-913">source</a><a href="#impl-AhoCorasick-1" class="anchor"></a><h3 class="code-header">impl <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h3></section></summary><div class="docblock"><p>Infallible search routines. These APIs panic when the underlying search |
| would otherwise fail. Infallible routines are useful because the errors are |
| a result of both search-time configuration and what configuration is used |
| to build the Aho-Corasick searcher. Both of these things are not usually |
| the result of user input, and thus, an error is typically indicative of a |
| programmer error. In cases where callers want errors instead of panics, use |
| the corresponding <code>try</code> method in the section below.</p> |
| </div><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.is_match" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#311-316">source</a><h4 class="code-header">pub fn <a href="#method.is_match" class="fnname">is_match</a><'h, I: Into<<a class="struct" href="struct.Input.html" title="struct aho_corasick::Input">Input</a><'h>>>(&self, input: I) -> bool</h4></section></summary><div class="docblock"><p>Returns true if and only if this automaton matches the haystack at any |
| position.</p> |
| <p><code>input</code> may be any type that is cheaply convertible to an <code>Input</code>. This |
| includes, but is not limited to, <code>&str</code> and <code>&[u8]</code>.</p> |
| <p>Aside from convenience, when <code>AhoCorasick</code> was built with |
| leftmost-first or leftmost-longest semantics, this might result in a |
| search that visits less of the haystack than <a href="struct.AhoCorasick.html#method.find" title="AhoCorasick::find"><code>AhoCorasick::find</code></a> |
| would otherwise. (For standard semantics, matches are always |
| immediately returned once they are seen, so there is no way for this to |
| do less work in that case.)</p> |
| <p>Note that there is no corresponding fallible routine for this method. |
| If you need a fallible version of this, then <a href="struct.AhoCorasick.html#method.try_find" title="AhoCorasick::try_find"><code>AhoCorasick::try_find</code></a> |
| can be used with <a href="struct.Input.html#method.earliest" title="Input::earliest"><code>Input::earliest</code></a> enabled.</p> |
| <h5 id="examples-3"><a href="#examples-3">Examples</a></h5> |
| <p>Basic usage:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::AhoCorasick; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(<span class="kw-2">&</span>[ |
| <span class="string">"foo"</span>, <span class="string">"bar"</span>, <span class="string">"quux"</span>, <span class="string">"baz"</span>, |
| ]).unwrap(); |
| <span class="macro">assert!</span>(ac.is_match(<span class="string">"xxx bar xxx"</span>)); |
| <span class="macro">assert!</span>(!ac.is_match(<span class="string">"xxx qux xxx"</span>));</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.find" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#404-407">source</a><h4 class="code-header">pub fn <a href="#method.find" class="fnname">find</a><'h, I: Into<<a class="struct" href="struct.Input.html" title="struct aho_corasick::Input">Input</a><'h>>>(&self, input: I) -> Option<<a class="struct" href="struct.Match.html" title="struct aho_corasick::Match">Match</a>></h4></section></summary><div class="docblock"><p>Returns the location of the first match according to the match |
| semantics that this automaton was constructed with.</p> |
| <p><code>input</code> may be any type that is cheaply convertible to an <code>Input</code>. This |
| includes, but is not limited to, <code>&str</code> and <code>&[u8]</code>.</p> |
| <p>This is the infallible version of <a href="struct.AhoCorasick.html#method.try_find" title="AhoCorasick::try_find"><code>AhoCorasick::try_find</code></a>.</p> |
| <h5 id="panics"><a href="#panics">Panics</a></h5> |
| <p>This panics when <a href="struct.AhoCorasick.html#method.try_find" title="AhoCorasick::try_find"><code>AhoCorasick::try_find</code></a> would return an error.</p> |
| <h5 id="examples-4"><a href="#examples-4">Examples</a></h5> |
| <p>Basic usage, with standard semantics:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"b"</span>, <span class="string">"abc"</span>, <span class="string">"abcd"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"abcd"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::Standard) <span class="comment">// default, not necessary |
| </span>.build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>mat = ac.find(haystack).expect(<span class="string">"should have a match"</span>); |
| <span class="macro">assert_eq!</span>(<span class="string">"b"</span>, <span class="kw-2">&</span>haystack[mat.start()..mat.end()]);</code></pre></div> |
| <p>Now with leftmost-first semantics:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"b"</span>, <span class="string">"abc"</span>, <span class="string">"abcd"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"abcd"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>mat = ac.find(haystack).expect(<span class="string">"should have a match"</span>); |
| <span class="macro">assert_eq!</span>(<span class="string">"abc"</span>, <span class="kw-2">&</span>haystack[mat.start()..mat.end()]);</code></pre></div> |
| <p>And finally, leftmost-longest semantics:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"b"</span>, <span class="string">"abc"</span>, <span class="string">"abcd"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"abcd"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostLongest) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>mat = ac.find(haystack).expect(<span class="string">"should have a match"</span>);</code></pre></div> |
| <h5 id="example-configuring-a-search"><a href="#example-configuring-a-search">Example: configuring a search</a></h5> |
| <p>Because this method accepts anything that can be turned into an |
| <a href="struct.Input.html" title="Input"><code>Input</code></a>, it’s possible to provide an <code>Input</code> directly in order to |
| configure the search. In this example, we show how to use the |
| <code>earliest</code> option to force the search to return as soon as it knows |
| a match has occurred.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, Input, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"b"</span>, <span class="string">"abc"</span>, <span class="string">"abcd"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"abcd"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostLongest) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>mat = ac.find(Input::new(haystack).earliest(<span class="bool-val">true</span>)) |
| .expect(<span class="string">"should have a match"</span>); |
| <span class="comment">// The correct leftmost-longest match here is 'abcd', but since we |
| // told the search to quit as soon as it knows a match has occurred, |
| // we get a different match back. |
| </span><span class="macro">assert_eq!</span>(<span class="string">"b"</span>, <span class="kw-2">&</span>haystack[mat.start()..mat.end()]);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.find_overlapping" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#470-478">source</a><h4 class="code-header">pub fn <a href="#method.find_overlapping" class="fnname">find_overlapping</a><'h, I: Into<<a class="struct" href="struct.Input.html" title="struct aho_corasick::Input">Input</a><'h>>>(<br> &self,<br> input: I,<br> state: &mut <a class="struct" href="automaton/struct.OverlappingState.html" title="struct aho_corasick::automaton::OverlappingState">OverlappingState</a><br>)</h4></section></summary><div class="docblock"><p>Returns the location of the first overlapping match in the given |
| input with respect to the current state of the underlying searcher.</p> |
| <p><code>input</code> may be any type that is cheaply convertible to an <code>Input</code>. This |
| includes, but is not limited to, <code>&str</code> and <code>&[u8]</code>.</p> |
| <p>Overlapping searches do not report matches in their return value. |
| Instead, matches can be accessed via <a href="automaton/struct.OverlappingState.html#method.get_match" title="OverlappingState::get_match"><code>OverlappingState::get_match</code></a> |
| after a search call.</p> |
| <p>This is the infallible version of |
| <a href="struct.AhoCorasick.html#method.try_find_overlapping" title="AhoCorasick::try_find_overlapping"><code>AhoCorasick::try_find_overlapping</code></a>.</p> |
| <h5 id="panics-1"><a href="#panics-1">Panics</a></h5> |
| <p>This panics when <a href="struct.AhoCorasick.html#method.try_find_overlapping" title="AhoCorasick::try_find_overlapping"><code>AhoCorasick::try_find_overlapping</code></a> would |
| return an error. For example, when the Aho-Corasick searcher |
| doesn’t support overlapping searches. (Only searchers built with |
| <a href="enum.MatchKind.html#variant.Standard" title="MatchKind::Standard"><code>MatchKind::Standard</code></a> semantics support overlapping searches.)</p> |
| <h5 id="example"><a href="#example">Example</a></h5> |
| <p>This shows how we can repeatedly call an overlapping search without |
| ever needing to explicitly re-slice the haystack. Overlapping search |
| works this way because searches depend on state saved during the |
| previous search.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{ |
| automaton::OverlappingState, |
| AhoCorasick, Input, Match, |
| }; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(patterns).unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>state = OverlappingState::start(); |
| |
| ac.find_overlapping(haystack, <span class="kw-2">&mut </span>state); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">2</span>, <span class="number">0</span>..<span class="number">3</span>)), state.get_match()); |
| |
| ac.find_overlapping(haystack, <span class="kw-2">&mut </span>state); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">0</span>, <span class="number">0</span>..<span class="number">6</span>)), state.get_match()); |
| |
| ac.find_overlapping(haystack, <span class="kw-2">&mut </span>state); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">2</span>, <span class="number">11</span>..<span class="number">14</span>)), state.get_match()); |
| |
| ac.find_overlapping(haystack, <span class="kw-2">&mut </span>state); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">2</span>, <span class="number">22</span>..<span class="number">25</span>)), state.get_match()); |
| |
| ac.find_overlapping(haystack, <span class="kw-2">&mut </span>state); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">0</span>, <span class="number">22</span>..<span class="number">28</span>)), state.get_match()); |
| |
| ac.find_overlapping(haystack, <span class="kw-2">&mut </span>state); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">1</span>, <span class="number">22</span>..<span class="number">31</span>)), state.get_match()); |
| |
| <span class="comment">// No more match matches to be found. |
| </span>ac.find_overlapping(haystack, <span class="kw-2">&mut </span>state); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">None</span>, state.get_match());</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.find_iter" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#562-568">source</a><h4 class="code-header">pub fn <a href="#method.find_iter" class="fnname">find_iter</a><'a, 'h, I: Into<<a class="struct" href="struct.Input.html" title="struct aho_corasick::Input">Input</a><'h>>>(<br> &'a self,<br> input: I<br>) -> <a class="struct" href="struct.FindIter.html" title="struct aho_corasick::FindIter">FindIter</a><'a, 'h><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.FindIter.html" title="struct aho_corasick::FindIter">FindIter</a><'a, 'h></span><code class="content"><span class="where fmt-newline">impl<'a, 'h> Iterator for <a class="struct" href="struct.FindIter.html" title="struct aho_corasick::FindIter">FindIter</a><'a, 'h></span><span class="where fmt-newline"> type <a class="associatedtype">Item</a> = <a class="struct" href="struct.Match.html" title="struct aho_corasick::Match">Match</a>;</span></code></span></span></span></span></h4></section></summary><div class="docblock"><p>Returns an iterator of non-overlapping matches, using the match |
| semantics that this automaton was constructed with.</p> |
| <p><code>input</code> may be any type that is cheaply convertible to an <code>Input</code>. This |
| includes, but is not limited to, <code>&str</code> and <code>&[u8]</code>.</p> |
| <p>This is the infallible version of <a href="struct.AhoCorasick.html#method.try_find_iter" title="AhoCorasick::try_find_iter"><code>AhoCorasick::try_find_iter</code></a>.</p> |
| <h5 id="panics-2"><a href="#panics-2">Panics</a></h5> |
| <p>This panics when <a href="struct.AhoCorasick.html#method.try_find_iter" title="AhoCorasick::try_find_iter"><code>AhoCorasick::try_find_iter</code></a> would return an error.</p> |
| <h5 id="examples-5"><a href="#examples-5">Examples</a></h5> |
| <p>Basic usage, with standard semantics:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind, PatternID}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::Standard) <span class="comment">// default, not necessary |
| </span>.build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>matches: Vec<PatternID> = ac |
| .find_iter(haystack) |
| .map(|mat| mat.pattern()) |
| .collect(); |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[ |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">2</span>), |
| ], matches);</code></pre></div> |
| <p>Now with leftmost-first semantics:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind, PatternID}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>matches: Vec<PatternID> = ac |
| .find_iter(haystack) |
| .map(|mat| mat.pattern()) |
| .collect(); |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[ |
| PatternID::must(<span class="number">0</span>), |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">0</span>), |
| ], matches);</code></pre></div> |
| <p>And finally, leftmost-longest semantics:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind, PatternID}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostLongest) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>matches: Vec<PatternID> = ac |
| .find_iter(haystack) |
| .map(|mat| mat.pattern()) |
| .collect(); |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[ |
| PatternID::must(<span class="number">0</span>), |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">1</span>), |
| ], matches);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.find_overlapping_iter" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#609-616">source</a><h4 class="code-header">pub fn <a href="#method.find_overlapping_iter" class="fnname">find_overlapping_iter</a><'a, 'h, I: Into<<a class="struct" href="struct.Input.html" title="struct aho_corasick::Input">Input</a><'h>>>(<br> &'a self,<br> input: I<br>) -> <a class="struct" href="struct.FindOverlappingIter.html" title="struct aho_corasick::FindOverlappingIter">FindOverlappingIter</a><'a, 'h><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.FindOverlappingIter.html" title="struct aho_corasick::FindOverlappingIter">FindOverlappingIter</a><'a, 'h></span><code class="content"><span class="where fmt-newline">impl<'a, 'h> Iterator for <a class="struct" href="struct.FindOverlappingIter.html" title="struct aho_corasick::FindOverlappingIter">FindOverlappingIter</a><'a, 'h></span><span class="where fmt-newline"> type <a class="associatedtype">Item</a> = <a class="struct" href="struct.Match.html" title="struct aho_corasick::Match">Match</a>;</span></code></span></span></span></span></h4></section></summary><div class="docblock"><p>Returns an iterator of overlapping matches. Stated differently, this |
| returns an iterator of all possible matches at every position.</p> |
| <p><code>input</code> may be any type that is cheaply convertible to an <code>Input</code>. This |
| includes, but is not limited to, <code>&str</code> and <code>&[u8]</code>.</p> |
| <p>This is the infallible version of |
| <a href="struct.AhoCorasick.html#method.try_find_overlapping_iter" title="AhoCorasick::try_find_overlapping_iter"><code>AhoCorasick::try_find_overlapping_iter</code></a>.</p> |
| <h5 id="panics-3"><a href="#panics-3">Panics</a></h5> |
| <p>This panics when <code>AhoCorasick::try_find_overlapping_iter</code> would return |
| an error. For example, when the Aho-Corasick searcher is built with |
| either leftmost-first or leftmost-longest match semantics. Stated |
| differently, overlapping searches require one to build the searcher |
| with <a href="enum.MatchKind.html#variant.Standard" title="MatchKind::Standard"><code>MatchKind::Standard</code></a> (it is the default).</p> |
| <h5 id="example-basic-usage"><a href="#example-basic-usage">Example: basic usage</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, PatternID}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(patterns).unwrap(); |
| <span class="kw">let </span>matches: Vec<PatternID> = ac |
| .find_overlapping_iter(haystack) |
| .map(|mat| mat.pattern()) |
| .collect(); |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[ |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">0</span>), |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">0</span>), |
| PatternID::must(<span class="number">1</span>), |
| ], matches);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.replace_all" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#651-657">source</a><h4 class="code-header">pub fn <a href="#method.replace_all" class="fnname">replace_all</a><B>(&self, haystack: &str, replace_with: &[B]) -> String<span class="where fmt-newline">where<br> B: AsRef<str>,</span></h4></section></summary><div class="docblock"><p>Replace all matches with a corresponding value in the <code>replace_with</code> |
| slice given. Matches correspond to the same matches as reported by |
| <a href="struct.AhoCorasick.html#method.find_iter" title="AhoCorasick::find_iter"><code>AhoCorasick::find_iter</code></a>.</p> |
| <p>Replacements are determined by the index of the matching pattern. |
| For example, if the pattern with index <code>2</code> is found, then it is |
| replaced by <code>replace_with[2]</code>.</p> |
| <p>This is the infallible version of <a href="struct.AhoCorasick.html#method.try_replace_all" title="AhoCorasick::try_replace_all"><code>AhoCorasick::try_replace_all</code></a>.</p> |
| <h5 id="panics-4"><a href="#panics-4">Panics</a></h5> |
| <p>This panics when <a href="struct.AhoCorasick.html#method.try_replace_all" title="AhoCorasick::try_replace_all"><code>AhoCorasick::try_replace_all</code></a> would return an |
| error.</p> |
| <p>This also panics when <code>replace_with.len()</code> does not equal |
| <a href="struct.AhoCorasick.html#method.patterns_len" title="AhoCorasick::patterns_len"><code>AhoCorasick::patterns_len</code></a>.</p> |
| <h5 id="example-basic-usage-1"><a href="#example-basic-usage-1">Example: basic usage</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>result = ac.replace_all(haystack, <span class="kw-2">&</span>[<span class="string">"x"</span>, <span class="string">"y"</span>, <span class="string">"z"</span>]); |
| <span class="macro">assert_eq!</span>(<span class="string">"x the z to the xage"</span>, result);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.replace_all_bytes" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#693-703">source</a><h4 class="code-header">pub fn <a href="#method.replace_all_bytes" class="fnname">replace_all_bytes</a><B>(&self, haystack: &[u8], replace_with: &[B]) -> Vec<u8><span class="where fmt-newline">where<br> B: AsRef<[u8]>,</span></h4></section></summary><div class="docblock"><p>Replace all matches using raw bytes with a corresponding value in the |
| <code>replace_with</code> slice given. Matches correspond to the same matches as |
| reported by <a href="struct.AhoCorasick.html#method.find_iter" title="AhoCorasick::find_iter"><code>AhoCorasick::find_iter</code></a>.</p> |
| <p>Replacements are determined by the index of the matching pattern. |
| For example, if the pattern with index <code>2</code> is found, then it is |
| replaced by <code>replace_with[2]</code>.</p> |
| <p>This is the infallible version of |
| <a href="struct.AhoCorasick.html#method.try_replace_all_bytes" title="AhoCorasick::try_replace_all_bytes"><code>AhoCorasick::try_replace_all_bytes</code></a>.</p> |
| <h5 id="panics-5"><a href="#panics-5">Panics</a></h5> |
| <p>This panics when <a href="struct.AhoCorasick.html#method.try_replace_all_bytes" title="AhoCorasick::try_replace_all_bytes"><code>AhoCorasick::try_replace_all_bytes</code></a> would return an |
| error.</p> |
| <p>This also panics when <code>replace_with.len()</code> does not equal |
| <a href="struct.AhoCorasick.html#method.patterns_len" title="AhoCorasick::patterns_len"><code>AhoCorasick::patterns_len</code></a>.</p> |
| <h5 id="example-basic-usage-2"><a href="#example-basic-usage-2">Example: basic usage</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">b"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>result = ac.replace_all_bytes(haystack, <span class="kw-2">&</span>[<span class="string">"x"</span>, <span class="string">"y"</span>, <span class="string">"z"</span>]); |
| <span class="macro">assert_eq!</span>(<span class="string">b"x the z to the xage"</span>.to_vec(), result);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.replace_all_with" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#765-775">source</a><h4 class="code-header">pub fn <a href="#method.replace_all_with" class="fnname">replace_all_with</a><F>(<br> &self,<br> haystack: &str,<br> dst: &mut String,<br> replace_with: F<br>)<span class="where fmt-newline">where<br> F: FnMut(&<a class="struct" href="struct.Match.html" title="struct aho_corasick::Match">Match</a>, &str, &mut String) -> bool,</span></h4></section></summary><div class="docblock"><p>Replace all matches using a closure called on each match. |
| Matches correspond to the same matches as reported by |
| <a href="struct.AhoCorasick.html#method.find_iter" title="AhoCorasick::find_iter"><code>AhoCorasick::find_iter</code></a>.</p> |
| <p>The closure accepts three parameters: the match found, the text of |
| the match and a string buffer with which to write the replaced text |
| (if any). If the closure returns <code>true</code>, then it continues to the next |
| match. If the closure returns <code>false</code>, then searching is stopped.</p> |
| <p>Note that any matches with boundaries that don’t fall on a valid UTF-8 |
| boundary are silently skipped.</p> |
| <p>This is the infallible version of |
| <a href="struct.AhoCorasick.html#method.try_replace_all_with" title="AhoCorasick::try_replace_all_with"><code>AhoCorasick::try_replace_all_with</code></a>.</p> |
| <h5 id="panics-6"><a href="#panics-6">Panics</a></h5> |
| <p>This panics when <a href="struct.AhoCorasick.html#method.try_replace_all_with" title="AhoCorasick::try_replace_all_with"><code>AhoCorasick::try_replace_all_with</code></a> would return an |
| error.</p> |
| <h5 id="examples-6"><a href="#examples-6">Examples</a></h5> |
| <p>Basic usage:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>result = String::new(); |
| ac.replace_all_with(haystack, <span class="kw-2">&mut </span>result, |mat, <span class="kw">_</span>, dst| { |
| dst.push_str(<span class="kw-2">&</span>mat.pattern().as_usize().to_string()); |
| <span class="bool-val">true |
| </span>}); |
| <span class="macro">assert_eq!</span>(<span class="string">"0 the 2 to the 0age"</span>, result);</code></pre></div> |
| <p>Stopping the replacement by returning <code>false</code> (continued from the |
| example above):</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>result = String::new(); |
| ac.replace_all_with(haystack, <span class="kw-2">&mut </span>result, |mat, <span class="kw">_</span>, dst| { |
| dst.push_str(<span class="kw-2">&</span>mat.pattern().as_usize().to_string()); |
| mat.pattern() != PatternID::must(<span class="number">2</span>) |
| }); |
| <span class="macro">assert_eq!</span>(<span class="string">"0 the 2 to the appendage"</span>, result);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.replace_all_with_bytes" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#834-844">source</a><h4 class="code-header">pub fn <a href="#method.replace_all_with_bytes" class="fnname">replace_all_with_bytes</a><F>(<br> &self,<br> haystack: &[u8],<br> dst: &mut Vec<u8>,<br> replace_with: F<br>)<span class="where fmt-newline">where<br> F: FnMut(&<a class="struct" href="struct.Match.html" title="struct aho_corasick::Match">Match</a>, &[u8], &mut Vec<u8>) -> bool,</span></h4></section></summary><div class="docblock"><p>Replace all matches using raw bytes with a closure called on each |
| match. Matches correspond to the same matches as reported by |
| <a href="struct.AhoCorasick.html#method.find_iter" title="AhoCorasick::find_iter"><code>AhoCorasick::find_iter</code></a>.</p> |
| <p>The closure accepts three parameters: the match found, the text of |
| the match and a byte buffer with which to write the replaced text |
| (if any). If the closure returns <code>true</code>, then it continues to the next |
| match. If the closure returns <code>false</code>, then searching is stopped.</p> |
| <p>This is the infallible version of |
| <a href="struct.AhoCorasick.html#method.try_replace_all_with_bytes" title="AhoCorasick::try_replace_all_with_bytes"><code>AhoCorasick::try_replace_all_with_bytes</code></a>.</p> |
| <h5 id="panics-7"><a href="#panics-7">Panics</a></h5> |
| <p>This panics when <a href="struct.AhoCorasick.html#method.try_replace_all_with_bytes" title="AhoCorasick::try_replace_all_with_bytes"><code>AhoCorasick::try_replace_all_with_bytes</code></a> would |
| return an error.</p> |
| <h5 id="examples-7"><a href="#examples-7">Examples</a></h5> |
| <p>Basic usage:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">b"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>result = <span class="macro">vec!</span>[]; |
| ac.replace_all_with_bytes(haystack, <span class="kw-2">&mut </span>result, |mat, <span class="kw">_</span>, dst| { |
| dst.extend(mat.pattern().as_usize().to_string().bytes()); |
| <span class="bool-val">true |
| </span>}); |
| <span class="macro">assert_eq!</span>(<span class="string">b"0 the 2 to the 0age"</span>.to_vec(), result);</code></pre></div> |
| <p>Stopping the replacement by returning <code>false</code> (continued from the |
| example above):</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>result = <span class="macro">vec!</span>[]; |
| ac.replace_all_with_bytes(haystack, <span class="kw-2">&mut </span>result, |mat, <span class="kw">_</span>, dst| { |
| dst.extend(mat.pattern().as_usize().to_string().bytes()); |
| mat.pattern() != PatternID::must(<span class="number">2</span>) |
| }); |
| <span class="macro">assert_eq!</span>(<span class="string">b"0 the 2 to the appendage"</span>.to_vec(), result);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.stream_find_iter" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#906-912">source</a><h4 class="code-header">pub fn <a href="#method.stream_find_iter" class="fnname">stream_find_iter</a><'a, R: Read>(&'a self, rdr: R) -> <a class="struct" href="struct.StreamFindIter.html" title="struct aho_corasick::StreamFindIter">StreamFindIter</a><'a, R><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.StreamFindIter.html" title="struct aho_corasick::StreamFindIter">StreamFindIter</a><'a, R></span><code class="content"><span class="where fmt-newline">impl<'a, R: Read> Iterator for <a class="struct" href="struct.StreamFindIter.html" title="struct aho_corasick::StreamFindIter">StreamFindIter</a><'a, R></span><span class="where fmt-newline"> type <a class="associatedtype">Item</a> = Result<<a class="struct" href="struct.Match.html" title="struct aho_corasick::Match">Match</a>, Error>;</span></code></span></span></span></span></h4></section></summary><div class="docblock"><p>Returns an iterator of non-overlapping matches in the given |
| stream. Matches correspond to the same matches as reported by |
| <a href="struct.AhoCorasick.html#method.find_iter" title="AhoCorasick::find_iter"><code>AhoCorasick::find_iter</code></a>.</p> |
| <p>The matches yielded by this iterator use absolute position offsets in |
| the stream given, where the first byte has index <code>0</code>. Matches are |
| yieled until the stream is exhausted.</p> |
| <p>Each item yielded by the iterator is an <code>Result<Match, std::io::Error></code>, where an error is yielded if there was a problem |
| reading from the reader given.</p> |
| <p>When searching a stream, an internal buffer is used. Therefore, callers |
| should avoiding providing a buffered reader, if possible.</p> |
| <p>This is the infallible version of |
| <a href="struct.AhoCorasick.html#method.try_stream_find_iter" title="AhoCorasick::try_stream_find_iter"><code>AhoCorasick::try_stream_find_iter</code></a>. Note that both methods return |
| iterators that produce <code>Result</code> values. The difference is that this |
| routine panics if <em>construction</em> of the iterator failed. The <code>Result</code> |
| values yield by the iterator come from whether the given reader returns |
| an error or not during the search.</p> |
| <h5 id="memory-usage"><a href="#memory-usage">Memory usage</a></h5> |
| <p>In general, searching streams will use a constant amount of memory for |
| its internal buffer. The one requirement is that the internal buffer |
| must be at least the size of the longest possible match. In most use |
| cases, the default buffer size will be much larger than any individual |
| match.</p> |
| <h5 id="panics-8"><a href="#panics-8">Panics</a></h5> |
| <p>This panics when <a href="struct.AhoCorasick.html#method.try_stream_find_iter" title="AhoCorasick::try_stream_find_iter"><code>AhoCorasick::try_stream_find_iter</code></a> would return |
| an error. For example, when the Aho-Corasick searcher doesn’t support |
| stream searches. (Only searchers built with <a href="enum.MatchKind.html#variant.Standard" title="MatchKind::Standard"><code>MatchKind::Standard</code></a> |
| semantics support stream searches.)</p> |
| <h5 id="example-basic-usage-3"><a href="#example-basic-usage-3">Example: basic usage</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, PatternID}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(patterns).unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>matches = <span class="macro">vec!</span>[]; |
| <span class="kw">for </span>result <span class="kw">in </span>ac.stream_find_iter(haystack.as_bytes()) { |
| <span class="kw">let </span>mat = result<span class="question-mark">?</span>; |
| matches.push(mat.pattern()); |
| } |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[ |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">2</span>), |
| ], matches); |
| </code></pre></div> |
| </div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AhoCorasick-2" class="impl has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#917-1844">source</a><a href="#impl-AhoCorasick-2" class="anchor"></a><h3 class="code-header">impl <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h3></section></summary><div class="docblock"><p>Fallible search routines. These APIs return an error in cases where the |
| infallible routines would panic.</p> |
| </div><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_find" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1021-1028">source</a><h4 class="code-header">pub fn <a href="#method.try_find" class="fnname">try_find</a><'h, I: Into<<a class="struct" href="struct.Input.html" title="struct aho_corasick::Input">Input</a><'h>>>(<br> &self,<br> input: I<br>) -> Result<Option<<a class="struct" href="struct.Match.html" title="struct aho_corasick::Match">Match</a>>, <a class="struct" href="struct.MatchError.html" title="struct aho_corasick::MatchError">MatchError</a>></h4></section></summary><div class="docblock"><p>Returns the location of the first match according to the match |
| semantics that this automaton was constructed with, and according |
| to the given <code>Input</code> configuration.</p> |
| <p>This is the fallible version of <a href="struct.AhoCorasick.html#method.find" title="AhoCorasick::find"><code>AhoCorasick::find</code></a>.</p> |
| <h5 id="errors"><a href="#errors">Errors</a></h5> |
| <p>This returns an error when this Aho-Corasick searcher does not support |
| the given <code>Input</code> configuration.</p> |
| <p>For example, if the Aho-Corasick searcher only supports anchored |
| searches or only supports unanchored searches, then providing an |
| <code>Input</code> that requests an anchored (or unanchored) search when it isn’t |
| supported would result in an error.</p> |
| <h5 id="example-leftmost-first-searching"><a href="#example-leftmost-first-searching">Example: leftmost-first searching</a></h5> |
| <p>Basic usage with leftmost-first semantics:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind, Input}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"b"</span>, <span class="string">"abc"</span>, <span class="string">"abcd"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"foo abcd"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>mat = ac.try_find(haystack)<span class="question-mark">?</span>.expect(<span class="string">"should have a match"</span>); |
| <span class="macro">assert_eq!</span>(<span class="string">"abc"</span>, <span class="kw-2">&</span>haystack[mat.span()]); |
| </code></pre></div> |
| <h5 id="example-anchored-leftmost-first-searching"><a href="#example-anchored-leftmost-first-searching">Example: anchored leftmost-first searching</a></h5> |
| <p>This shows how to anchor the search, so that even if the haystack |
| contains a match somewhere, a match won’t be reported unless one can |
| be found that starts at the beginning of the search:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, Anchored, Input, MatchKind, StartKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"b"</span>, <span class="string">"abc"</span>, <span class="string">"abcd"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"foo abcd"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .start_kind(StartKind::Anchored) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>input = Input::new(haystack).anchored(Anchored::Yes); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">None</span>, ac.try_find(input)<span class="question-mark">?</span>); |
| </code></pre></div> |
| <p>If the beginning of the search is changed to where a match begins, then |
| it will be found:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, Anchored, Input, MatchKind, StartKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"b"</span>, <span class="string">"abc"</span>, <span class="string">"abcd"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"foo abcd"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .start_kind(StartKind::Anchored) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>input = Input::new(haystack).range(<span class="number">4</span>..).anchored(Anchored::Yes); |
| <span class="kw">let </span>mat = ac.try_find(input)<span class="question-mark">?</span>.expect(<span class="string">"should have a match"</span>); |
| <span class="macro">assert_eq!</span>(<span class="string">"abc"</span>, <span class="kw-2">&</span>haystack[mat.span()]); |
| </code></pre></div> |
| <h5 id="example-earliest-leftmost-first-searching"><a href="#example-earliest-leftmost-first-searching">Example: earliest leftmost-first searching</a></h5> |
| <p>This shows how to run an “earliest” search even when the Aho-Corasick |
| searcher was compiled with leftmost-first match semantics. In this |
| case, the search is stopped as soon as it is known that a match has |
| occurred, even if it doesn’t correspond to the leftmost-first match.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, Input, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"b"</span>, <span class="string">"abc"</span>, <span class="string">"abcd"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"foo abcd"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>input = Input::new(haystack).earliest(<span class="bool-val">true</span>); |
| <span class="kw">let </span>mat = ac.try_find(input)<span class="question-mark">?</span>.expect(<span class="string">"should have a match"</span>); |
| <span class="macro">assert_eq!</span>(<span class="string">"b"</span>, <span class="kw-2">&</span>haystack[mat.span()]); |
| </code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_find_overlapping" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1184-1192">source</a><h4 class="code-header">pub fn <a href="#method.try_find_overlapping" class="fnname">try_find_overlapping</a><'h, I: Into<<a class="struct" href="struct.Input.html" title="struct aho_corasick::Input">Input</a><'h>>>(<br> &self,<br> input: I,<br> state: &mut <a class="struct" href="automaton/struct.OverlappingState.html" title="struct aho_corasick::automaton::OverlappingState">OverlappingState</a><br>) -> Result<(), <a class="struct" href="struct.MatchError.html" title="struct aho_corasick::MatchError">MatchError</a>></h4></section></summary><div class="docblock"><p>Returns the location of the first overlapping match in the given |
| input with respect to the current state of the underlying searcher.</p> |
| <p>Overlapping searches do not report matches in their return value. |
| Instead, matches can be accessed via <a href="automaton/struct.OverlappingState.html#method.get_match" title="OverlappingState::get_match"><code>OverlappingState::get_match</code></a> |
| after a search call.</p> |
| <p>This is the fallible version of <a href="struct.AhoCorasick.html#method.find_overlapping" title="AhoCorasick::find_overlapping"><code>AhoCorasick::find_overlapping</code></a>.</p> |
| <h5 id="errors-1"><a href="#errors-1">Errors</a></h5> |
| <p>This returns an error when this Aho-Corasick searcher does not support |
| the given <code>Input</code> configuration or if overlapping search is not |
| supported.</p> |
| <p>One example is that only Aho-Corasicker searchers built with |
| <a href="enum.MatchKind.html#variant.Standard" title="MatchKind::Standard"><code>MatchKind::Standard</code></a> semantics support overlapping searches. Using |
| any other match semantics will result in this returning an error.</p> |
| <h5 id="example-basic-usage-4"><a href="#example-basic-usage-4">Example: basic usage</a></h5> |
| <p>This shows how we can repeatedly call an overlapping search without |
| ever needing to explicitly re-slice the haystack. Overlapping search |
| works this way because searches depend on state saved during the |
| previous search.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{ |
| automaton::OverlappingState, |
| AhoCorasick, Input, Match, |
| }; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(patterns).unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>state = OverlappingState::start(); |
| |
| ac.try_find_overlapping(haystack, <span class="kw-2">&mut </span>state)<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">2</span>, <span class="number">0</span>..<span class="number">3</span>)), state.get_match()); |
| |
| ac.try_find_overlapping(haystack, <span class="kw-2">&mut </span>state)<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">0</span>, <span class="number">0</span>..<span class="number">6</span>)), state.get_match()); |
| |
| ac.try_find_overlapping(haystack, <span class="kw-2">&mut </span>state)<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">2</span>, <span class="number">11</span>..<span class="number">14</span>)), state.get_match()); |
| |
| ac.try_find_overlapping(haystack, <span class="kw-2">&mut </span>state)<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">2</span>, <span class="number">22</span>..<span class="number">25</span>)), state.get_match()); |
| |
| ac.try_find_overlapping(haystack, <span class="kw-2">&mut </span>state)<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">0</span>, <span class="number">22</span>..<span class="number">28</span>)), state.get_match()); |
| |
| ac.try_find_overlapping(haystack, <span class="kw-2">&mut </span>state)<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(Match::must(<span class="number">1</span>, <span class="number">22</span>..<span class="number">31</span>)), state.get_match()); |
| |
| <span class="comment">// No more match matches to be found. |
| </span>ac.try_find_overlapping(haystack, <span class="kw-2">&mut </span>state)<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">None</span>, state.get_match()); |
| </code></pre></div> |
| <h5 id="example-implementing-your-own-overlapping-iteration"><a href="#example-implementing-your-own-overlapping-iteration">Example: implementing your own overlapping iteration</a></h5> |
| <p>The previous example can be easily adapted to implement your own |
| iteration by repeatedly calling <code>try_find_overlapping</code> until either |
| an error occurs or no more matches are reported.</p> |
| <p>This is effectively equivalent to the iterator returned by |
| <a href="struct.AhoCorasick.html#method.try_find_overlapping_iter" title="AhoCorasick::try_find_overlapping_iter"><code>AhoCorasick::try_find_overlapping_iter</code></a>, with the only difference |
| being that the iterator checks for errors before construction and |
| absolves the caller of needing to check for errors on every search |
| call. (Indeed, if the first <code>try_find_overlapping</code> call succeeds and |
| the same <code>Input</code> is given to subsequent calls, then all subsequent |
| calls are guaranteed to succeed.)</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{ |
| automaton::OverlappingState, |
| AhoCorasick, Input, Match, |
| }; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(patterns).unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>state = OverlappingState::start(); |
| <span class="kw">let </span><span class="kw-2">mut </span>matches = <span class="macro">vec!</span>[]; |
| |
| <span class="kw">loop </span>{ |
| ac.try_find_overlapping(haystack, <span class="kw-2">&mut </span>state)<span class="question-mark">?</span>; |
| <span class="kw">let </span>mat = <span class="kw">match </span>state.get_match() { |
| <span class="prelude-val">None </span>=> <span class="kw">break</span>, |
| <span class="prelude-val">Some</span>(mat) => mat, |
| }; |
| matches.push(mat); |
| } |
| <span class="kw">let </span>expected = <span class="macro">vec!</span>[ |
| Match::must(<span class="number">2</span>, <span class="number">0</span>..<span class="number">3</span>), |
| Match::must(<span class="number">0</span>, <span class="number">0</span>..<span class="number">6</span>), |
| Match::must(<span class="number">2</span>, <span class="number">11</span>..<span class="number">14</span>), |
| Match::must(<span class="number">2</span>, <span class="number">22</span>..<span class="number">25</span>), |
| Match::must(<span class="number">0</span>, <span class="number">22</span>..<span class="number">28</span>), |
| Match::must(<span class="number">1</span>, <span class="number">22</span>..<span class="number">31</span>), |
| ]; |
| <span class="macro">assert_eq!</span>(expected, matches); |
| </code></pre></div> |
| <h5 id="example-anchored-iteration"><a href="#example-anchored-iteration">Example: anchored iteration</a></h5> |
| <p>The previous example can also be adapted to implement |
| iteration over all anchored matches. In particular, |
| <a href="struct.AhoCorasick.html#method.try_find_overlapping_iter" title="AhoCorasick::try_find_overlapping_iter"><code>AhoCorasick::try_find_overlapping_iter</code></a> does not support this |
| because it isn’t totally clear what the match semantics ought to be.</p> |
| <p>In this example, we will find all overlapping matches that start at |
| the beginning of our search.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{ |
| automaton::OverlappingState, |
| AhoCorasick, Anchored, Input, Match, StartKind, |
| }; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .start_kind(StartKind::Anchored) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>input = Input::new(haystack).anchored(Anchored::Yes); |
| <span class="kw">let </span><span class="kw-2">mut </span>state = OverlappingState::start(); |
| <span class="kw">let </span><span class="kw-2">mut </span>matches = <span class="macro">vec!</span>[]; |
| |
| <span class="kw">loop </span>{ |
| ac.try_find_overlapping(input.clone(), <span class="kw-2">&mut </span>state)<span class="question-mark">?</span>; |
| <span class="kw">let </span>mat = <span class="kw">match </span>state.get_match() { |
| <span class="prelude-val">None </span>=> <span class="kw">break</span>, |
| <span class="prelude-val">Some</span>(mat) => mat, |
| }; |
| matches.push(mat); |
| } |
| <span class="kw">let </span>expected = <span class="macro">vec!</span>[ |
| Match::must(<span class="number">2</span>, <span class="number">0</span>..<span class="number">3</span>), |
| Match::must(<span class="number">0</span>, <span class="number">0</span>..<span class="number">6</span>), |
| ]; |
| <span class="macro">assert_eq!</span>(expected, matches); |
| </code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_find_iter" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1275-1282">source</a><h4 class="code-header">pub fn <a href="#method.try_find_iter" class="fnname">try_find_iter</a><'a, 'h, I: Into<<a class="struct" href="struct.Input.html" title="struct aho_corasick::Input">Input</a><'h>>>(<br> &'a self,<br> input: I<br>) -> Result<<a class="struct" href="struct.FindIter.html" title="struct aho_corasick::FindIter">FindIter</a><'a, 'h>, <a class="struct" href="struct.MatchError.html" title="struct aho_corasick::MatchError">MatchError</a>></h4></section></summary><div class="docblock"><p>Returns an iterator of non-overlapping matches, using the match |
| semantics that this automaton was constructed with.</p> |
| <p>This is the fallible version of <a href="struct.AhoCorasick.html#method.find_iter" title="AhoCorasick::find_iter"><code>AhoCorasick::find_iter</code></a>.</p> |
| <p>Note that the error returned by this method occurs during construction |
| of the iterator. The iterator itself yields <code>Match</code> values. That is, |
| once the iterator is constructed, the iteration itself will never |
| report an error.</p> |
| <h5 id="errors-2"><a href="#errors-2">Errors</a></h5> |
| <p>This returns an error when this Aho-Corasick searcher does not support |
| the given <code>Input</code> configuration.</p> |
| <p>For example, if the Aho-Corasick searcher only supports anchored |
| searches or only supports unanchored searches, then providing an |
| <code>Input</code> that requests an anchored (or unanchored) search when it isn’t |
| supported would result in an error.</p> |
| <h5 id="example-leftmost-first-searching-1"><a href="#example-leftmost-first-searching-1">Example: leftmost-first searching</a></h5> |
| <p>Basic usage with leftmost-first semantics:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, Input, MatchKind, PatternID}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>matches: Vec<PatternID> = ac |
| .try_find_iter(Input::new(haystack))<span class="question-mark">? |
| </span>.map(|mat| mat.pattern()) |
| .collect(); |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[ |
| PatternID::must(<span class="number">0</span>), |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">0</span>), |
| ], matches); |
| </code></pre></div> |
| <h5 id="example-anchored-leftmost-first-searching-1"><a href="#example-anchored-leftmost-first-searching-1">Example: anchored leftmost-first searching</a></h5> |
| <p>This shows how to anchor the search, such that all matches must begin |
| at the starting location of the search. For an iterator, an anchored |
| search implies that all matches are adjacent.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{ |
| AhoCorasick, Anchored, Input, MatchKind, PatternID, StartKind, |
| }; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"foo"</span>, <span class="string">"bar"</span>, <span class="string">"quux"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"fooquuxbar foo"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .start_kind(StartKind::Anchored) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>matches: Vec<PatternID> = ac |
| .try_find_iter(Input::new(haystack).anchored(Anchored::Yes))<span class="question-mark">? |
| </span>.map(|mat| mat.pattern()) |
| .collect(); |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[ |
| PatternID::must(<span class="number">0</span>), |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">1</span>), |
| <span class="comment">// The final 'foo' is not found because it is not adjacent to the |
| // 'bar' match. It needs to be adjacent because our search is |
| // anchored. |
| </span>], matches); |
| </code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_find_overlapping_iter" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1350-1357">source</a><h4 class="code-header">pub fn <a href="#method.try_find_overlapping_iter" class="fnname">try_find_overlapping_iter</a><'a, 'h, I: Into<<a class="struct" href="struct.Input.html" title="struct aho_corasick::Input">Input</a><'h>>>(<br> &'a self,<br> input: I<br>) -> Result<<a class="struct" href="struct.FindOverlappingIter.html" title="struct aho_corasick::FindOverlappingIter">FindOverlappingIter</a><'a, 'h>, <a class="struct" href="struct.MatchError.html" title="struct aho_corasick::MatchError">MatchError</a>></h4></section></summary><div class="docblock"><p>Returns an iterator of overlapping matches.</p> |
| <p>This is the fallible version of <a href="struct.AhoCorasick.html#method.find_overlapping_iter" title="AhoCorasick::find_overlapping_iter"><code>AhoCorasick::find_overlapping_iter</code></a>.</p> |
| <p>Note that the error returned by this method occurs during construction |
| of the iterator. The iterator itself yields <code>Match</code> values. That is, |
| once the iterator is constructed, the iteration itself will never |
| report an error.</p> |
| <h5 id="errors-3"><a href="#errors-3">Errors</a></h5> |
| <p>This returns an error when this Aho-Corasick searcher does not support |
| the given <code>Input</code> configuration or does not support overlapping |
| searches.</p> |
| <p>One example is that only Aho-Corasicker searchers built with |
| <a href="enum.MatchKind.html#variant.Standard" title="MatchKind::Standard"><code>MatchKind::Standard</code></a> semantics support overlapping searches. Using |
| any other match semantics will result in this returning an error.</p> |
| <h5 id="example-basic-usage-5"><a href="#example-basic-usage-5">Example: basic usage</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, Input, PatternID}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(patterns).unwrap(); |
| <span class="kw">let </span>matches: Vec<PatternID> = ac |
| .try_find_overlapping_iter(Input::new(haystack))<span class="question-mark">? |
| </span>.map(|mat| mat.pattern()) |
| .collect(); |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[ |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">0</span>), |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">0</span>), |
| PatternID::must(<span class="number">1</span>), |
| ], matches); |
| </code></pre></div> |
| <h5 id="example-anchored-overlapping-search-returns-an-error"><a href="#example-anchored-overlapping-search-returns-an-error">Example: anchored overlapping search returns an error</a></h5> |
| <p>It isn’t clear what the match semantics for anchored overlapping |
| iterators <em>ought</em> to be, so currently an error is returned. Callers |
| may use <a href="struct.AhoCorasick.html#method.try_find_overlapping" title="AhoCorasick::try_find_overlapping"><code>AhoCorasick::try_find_overlapping</code></a> to implement their own |
| semantics if desired.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, Anchored, Input, StartKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"appendappendage app"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .start_kind(StartKind::Anchored) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>input = Input::new(haystack).anchored(Anchored::Yes); |
| <span class="macro">assert!</span>(ac.try_find_overlapping_iter(input).is_err()); |
| </code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_replace_all" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1396-1406">source</a><h4 class="code-header">pub fn <a href="#method.try_replace_all" class="fnname">try_replace_all</a><B>(<br> &self,<br> haystack: &str,<br> replace_with: &[B]<br>) -> Result<String, <a class="struct" href="struct.MatchError.html" title="struct aho_corasick::MatchError">MatchError</a>><span class="where fmt-newline">where<br> B: AsRef<str>,</span></h4></section></summary><div class="docblock"><p>Replace all matches with a corresponding value in the <code>replace_with</code> |
| slice given. Matches correspond to the same matches as reported by |
| <a href="struct.AhoCorasick.html#method.try_find_iter" title="AhoCorasick::try_find_iter"><code>AhoCorasick::try_find_iter</code></a>.</p> |
| <p>Replacements are determined by the index of the matching pattern. |
| For example, if the pattern with index <code>2</code> is found, then it is |
| replaced by <code>replace_with[2]</code>.</p> |
| <h5 id="panics-9"><a href="#panics-9">Panics</a></h5> |
| <p>This panics when <code>replace_with.len()</code> does not equal |
| <a href="struct.AhoCorasick.html#method.patterns_len" title="AhoCorasick::patterns_len"><code>AhoCorasick::patterns_len</code></a>.</p> |
| <h5 id="errors-4"><a href="#errors-4">Errors</a></h5> |
| <p>This returns an error when this Aho-Corasick searcher does not support |
| the default <code>Input</code> configuration. More specifically, this occurs only |
| when the Aho-Corasick searcher does not support unanchored searches |
| since this replacement routine always does an unanchored search.</p> |
| <h5 id="example-basic-usage-6"><a href="#example-basic-usage-6">Example: basic usage</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>result = ac.try_replace_all(haystack, <span class="kw-2">&</span>[<span class="string">"x"</span>, <span class="string">"y"</span>, <span class="string">"z"</span>])<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="string">"x the z to the xage"</span>, result); |
| </code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_replace_all_bytes" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1447-1457">source</a><h4 class="code-header">pub fn <a href="#method.try_replace_all_bytes" class="fnname">try_replace_all_bytes</a><B>(<br> &self,<br> haystack: &[u8],<br> replace_with: &[B]<br>) -> Result<Vec<u8>, <a class="struct" href="struct.MatchError.html" title="struct aho_corasick::MatchError">MatchError</a>><span class="where fmt-newline">where<br> B: AsRef<[u8]>,</span></h4></section></summary><div class="docblock"><p>Replace all matches using raw bytes with a corresponding value in the |
| <code>replace_with</code> slice given. Matches correspond to the same matches as |
| reported by <a href="struct.AhoCorasick.html#method.try_find_iter" title="AhoCorasick::try_find_iter"><code>AhoCorasick::try_find_iter</code></a>.</p> |
| <p>Replacements are determined by the index of the matching pattern. |
| For example, if the pattern with index <code>2</code> is found, then it is |
| replaced by <code>replace_with[2]</code>.</p> |
| <p>This is the fallible version of <a href="struct.AhoCorasick.html#method.replace_all_bytes" title="AhoCorasick::replace_all_bytes"><code>AhoCorasick::replace_all_bytes</code></a>.</p> |
| <h5 id="panics-10"><a href="#panics-10">Panics</a></h5> |
| <p>This panics when <code>replace_with.len()</code> does not equal |
| <a href="struct.AhoCorasick.html#method.patterns_len" title="AhoCorasick::patterns_len"><code>AhoCorasick::patterns_len</code></a>.</p> |
| <h5 id="errors-5"><a href="#errors-5">Errors</a></h5> |
| <p>This returns an error when this Aho-Corasick searcher does not support |
| the default <code>Input</code> configuration. More specifically, this occurs only |
| when the Aho-Corasick searcher does not support unanchored searches |
| since this replacement routine always does an unanchored search.</p> |
| <h5 id="example-basic-usage-7"><a href="#example-basic-usage-7">Example: basic usage</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">b"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span>result = ac.try_replace_all_bytes(haystack, <span class="kw-2">&</span>[<span class="string">"x"</span>, <span class="string">"y"</span>, <span class="string">"z"</span>])<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="string">b"x the z to the xage"</span>.to_vec(), result); |
| </code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_replace_all_with" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1524-1535">source</a><h4 class="code-header">pub fn <a href="#method.try_replace_all_with" class="fnname">try_replace_all_with</a><F>(<br> &self,<br> haystack: &str,<br> dst: &mut String,<br> replace_with: F<br>) -> Result<(), <a class="struct" href="struct.MatchError.html" title="struct aho_corasick::MatchError">MatchError</a>><span class="where fmt-newline">where<br> F: FnMut(&<a class="struct" href="struct.Match.html" title="struct aho_corasick::Match">Match</a>, &str, &mut String) -> bool,</span></h4></section></summary><div class="docblock"><p>Replace all matches using a closure called on each match. |
| Matches correspond to the same matches as reported by |
| <a href="struct.AhoCorasick.html#method.try_find_iter" title="AhoCorasick::try_find_iter"><code>AhoCorasick::try_find_iter</code></a>.</p> |
| <p>The closure accepts three parameters: the match found, the text of |
| the match and a string buffer with which to write the replaced text |
| (if any). If the closure returns <code>true</code>, then it continues to the next |
| match. If the closure returns <code>false</code>, then searching is stopped.</p> |
| <p>Note that any matches with boundaries that don’t fall on a valid UTF-8 |
| boundary are silently skipped.</p> |
| <p>This is the fallible version of <a href="struct.AhoCorasick.html#method.replace_all_with" title="AhoCorasick::replace_all_with"><code>AhoCorasick::replace_all_with</code></a>.</p> |
| <h5 id="errors-6"><a href="#errors-6">Errors</a></h5> |
| <p>This returns an error when this Aho-Corasick searcher does not support |
| the default <code>Input</code> configuration. More specifically, this occurs only |
| when the Aho-Corasick searcher does not support unanchored searches |
| since this replacement routine always does an unanchored search.</p> |
| <h5 id="examples-8"><a href="#examples-8">Examples</a></h5> |
| <p>Basic usage:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>result = String::new(); |
| ac.try_replace_all_with(haystack, <span class="kw-2">&mut </span>result, |mat, <span class="kw">_</span>, dst| { |
| dst.push_str(<span class="kw-2">&</span>mat.pattern().as_usize().to_string()); |
| <span class="bool-val">true |
| </span>})<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="string">"0 the 2 to the 0age"</span>, result); |
| </code></pre></div> |
| <p>Stopping the replacement by returning <code>false</code> (continued from the |
| example above):</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>result = String::new(); |
| ac.try_replace_all_with(haystack, <span class="kw-2">&mut </span>result, |mat, <span class="kw">_</span>, dst| { |
| dst.push_str(<span class="kw-2">&</span>mat.pattern().as_usize().to_string()); |
| mat.pattern() != PatternID::must(<span class="number">2</span>) |
| })<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="string">"0 the 2 to the appendage"</span>, result); |
| </code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_replace_all_with_bytes" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1600-1611">source</a><h4 class="code-header">pub fn <a href="#method.try_replace_all_with_bytes" class="fnname">try_replace_all_with_bytes</a><F>(<br> &self,<br> haystack: &[u8],<br> dst: &mut Vec<u8>,<br> replace_with: F<br>) -> Result<(), <a class="struct" href="struct.MatchError.html" title="struct aho_corasick::MatchError">MatchError</a>><span class="where fmt-newline">where<br> F: FnMut(&<a class="struct" href="struct.Match.html" title="struct aho_corasick::Match">Match</a>, &[u8], &mut Vec<u8>) -> bool,</span></h4></section></summary><div class="docblock"><p>Replace all matches using raw bytes with a closure called on each |
| match. Matches correspond to the same matches as reported by |
| <a href="struct.AhoCorasick.html#method.try_find_iter" title="AhoCorasick::try_find_iter"><code>AhoCorasick::try_find_iter</code></a>.</p> |
| <p>The closure accepts three parameters: the match found, the text of |
| the match and a byte buffer with which to write the replaced text |
| (if any). If the closure returns <code>true</code>, then it continues to the next |
| match. If the closure returns <code>false</code>, then searching is stopped.</p> |
| <p>This is the fallible version of |
| <a href="struct.AhoCorasick.html#method.replace_all_with_bytes" title="AhoCorasick::replace_all_with_bytes"><code>AhoCorasick::replace_all_with_bytes</code></a>.</p> |
| <h5 id="errors-7"><a href="#errors-7">Errors</a></h5> |
| <p>This returns an error when this Aho-Corasick searcher does not support |
| the default <code>Input</code> configuration. More specifically, this occurs only |
| when the Aho-Corasick searcher does not support unanchored searches |
| since this replacement routine always does an unanchored search.</p> |
| <h5 id="examples-9"><a href="#examples-9">Examples</a></h5> |
| <p>Basic usage:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">b"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .match_kind(MatchKind::LeftmostFirst) |
| .build(patterns) |
| .unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>result = <span class="macro">vec!</span>[]; |
| ac.try_replace_all_with_bytes(haystack, <span class="kw-2">&mut </span>result, |mat, <span class="kw">_</span>, dst| { |
| dst.extend(mat.pattern().as_usize().to_string().bytes()); |
| <span class="bool-val">true |
| </span>})<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="string">b"0 the 2 to the 0age"</span>.to_vec(), result); |
| </code></pre></div> |
| <p>Stopping the replacement by returning <code>false</code> (continued from the |
| example above):</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>result = <span class="macro">vec!</span>[]; |
| ac.try_replace_all_with_bytes(haystack, <span class="kw-2">&mut </span>result, |mat, <span class="kw">_</span>, dst| { |
| dst.extend(mat.pattern().as_usize().to_string().bytes()); |
| mat.pattern() != PatternID::must(<span class="number">2</span>) |
| })<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="string">b"0 the 2 to the appendage"</span>.to_vec(), result); |
| </code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_stream_find_iter" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1677-1683">source</a><h4 class="code-header">pub fn <a href="#method.try_stream_find_iter" class="fnname">try_stream_find_iter</a><'a, R: Read>(<br> &'a self,<br> rdr: R<br>) -> Result<<a class="struct" href="struct.StreamFindIter.html" title="struct aho_corasick::StreamFindIter">StreamFindIter</a><'a, R>, <a class="struct" href="struct.MatchError.html" title="struct aho_corasick::MatchError">MatchError</a>></h4></section></summary><div class="docblock"><p>Returns an iterator of non-overlapping matches in the given |
| stream. Matches correspond to the same matches as reported by |
| <a href="struct.AhoCorasick.html#method.try_find_iter" title="AhoCorasick::try_find_iter"><code>AhoCorasick::try_find_iter</code></a>.</p> |
| <p>The matches yielded by this iterator use absolute position offsets in |
| the stream given, where the first byte has index <code>0</code>. Matches are |
| yieled until the stream is exhausted.</p> |
| <p>Each item yielded by the iterator is an <code>Result<Match, std::io::Error></code>, where an error is yielded if there was a problem |
| reading from the reader given.</p> |
| <p>When searching a stream, an internal buffer is used. Therefore, callers |
| should avoiding providing a buffered reader, if possible.</p> |
| <p>This is the fallible version of <a href="struct.AhoCorasick.html#method.stream_find_iter" title="AhoCorasick::stream_find_iter"><code>AhoCorasick::stream_find_iter</code></a>. |
| Note that both methods return iterators that produce <code>Result</code> values. |
| The difference is that this routine returns an error if <em>construction</em> |
| of the iterator failed. The <code>Result</code> values yield by the iterator |
| come from whether the given reader returns an error or not during the |
| search.</p> |
| <h5 id="memory-usage-1"><a href="#memory-usage-1">Memory usage</a></h5> |
| <p>In general, searching streams will use a constant amount of memory for |
| its internal buffer. The one requirement is that the internal buffer |
| must be at least the size of the longest possible match. In most use |
| cases, the default buffer size will be much larger than any individual |
| match.</p> |
| <h5 id="errors-8"><a href="#errors-8">Errors</a></h5> |
| <p>This returns an error when this Aho-Corasick searcher does not support |
| the default <code>Input</code> configuration. More specifically, this occurs only |
| when the Aho-Corasick searcher does not support unanchored searches |
| since this stream searching routine always does an unanchored search.</p> |
| <p>This also returns an error if the searcher does not support stream |
| searches. Only searchers built with <a href="enum.MatchKind.html#variant.Standard" title="MatchKind::Standard"><code>MatchKind::Standard</code></a> semantics |
| support stream searches.</p> |
| <h5 id="example-basic-usage-8"><a href="#example-basic-usage-8">Example: basic usage</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, PatternID}; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"append"</span>, <span class="string">"appendage"</span>, <span class="string">"app"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"append the app to the appendage"</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(patterns).unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>matches = <span class="macro">vec!</span>[]; |
| <span class="kw">for </span>result <span class="kw">in </span>ac.try_stream_find_iter(haystack.as_bytes())<span class="question-mark">? </span>{ |
| <span class="kw">let </span>mat = result<span class="question-mark">?</span>; |
| matches.push(mat.pattern()); |
| } |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[ |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">2</span>), |
| PatternID::must(<span class="number">2</span>), |
| ], matches); |
| </code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_stream_replace_all" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1751-1765">source</a><h4 class="code-header">pub fn <a href="#method.try_stream_replace_all" class="fnname">try_stream_replace_all</a><R, W, B>(<br> &self,<br> rdr: R,<br> wtr: W,<br> replace_with: &[B]<br>) -> Result<(), Error><span class="where fmt-newline">where<br> R: Read,<br> W: Write,<br> B: AsRef<[u8]>,</span></h4></section></summary><div class="docblock"><p>Search for and replace all matches of this automaton in |
| the given reader, and write the replacements to the given |
| writer. Matches correspond to the same matches as reported by |
| <a href="struct.AhoCorasick.html#method.try_find_iter" title="AhoCorasick::try_find_iter"><code>AhoCorasick::try_find_iter</code></a>.</p> |
| <p>Replacements are determined by the index of the matching pattern. For |
| example, if the pattern with index <code>2</code> is found, then it is replaced by |
| <code>replace_with[2]</code>.</p> |
| <p>After all matches are replaced, the writer is <em>not</em> flushed.</p> |
| <p>If there was a problem reading from the given reader or writing to the |
| given writer, then the corresponding <code>io::Error</code> is returned and all |
| replacement is stopped.</p> |
| <p>When searching a stream, an internal buffer is used. Therefore, callers |
| should avoiding providing a buffered reader, if possible. However, |
| callers may want to provide a buffered writer.</p> |
| <p>Note that there is currently no infallible version of this routine.</p> |
| <h5 id="memory-usage-2"><a href="#memory-usage-2">Memory usage</a></h5> |
| <p>In general, searching streams will use a constant amount of memory for |
| its internal buffer. The one requirement is that the internal buffer |
| must be at least the size of the longest possible match. In most use |
| cases, the default buffer size will be much larger than any individual |
| match.</p> |
| <h5 id="panics-11"><a href="#panics-11">Panics</a></h5> |
| <p>This panics when <code>replace_with.len()</code> does not equal |
| <a href="struct.AhoCorasick.html#method.patterns_len" title="AhoCorasick::patterns_len"><code>AhoCorasick::patterns_len</code></a>.</p> |
| <h5 id="errors-9"><a href="#errors-9">Errors</a></h5> |
| <p>This returns an error when this Aho-Corasick searcher does not support |
| the default <code>Input</code> configuration. More specifically, this occurs only |
| when the Aho-Corasick searcher does not support unanchored searches |
| since this stream searching routine always does an unanchored search.</p> |
| <p>This also returns an error if the searcher does not support stream |
| searches. Only searchers built with <a href="enum.MatchKind.html#variant.Standard" title="MatchKind::Standard"><code>MatchKind::Standard</code></a> semantics |
| support stream searches.</p> |
| <h5 id="example-basic-usage-9"><a href="#example-basic-usage-9">Example: basic usage</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::AhoCorasick; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"fox"</span>, <span class="string">"brown"</span>, <span class="string">"quick"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"The quick brown fox."</span>; |
| <span class="kw">let </span>replace_with = <span class="kw-2">&</span>[<span class="string">"sloth"</span>, <span class="string">"grey"</span>, <span class="string">"slow"</span>]; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(patterns).unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>result = <span class="macro">vec!</span>[]; |
| ac.try_stream_replace_all( |
| haystack.as_bytes(), |
| <span class="kw-2">&mut </span>result, |
| replace_with, |
| )<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="string">b"The slow grey sloth."</span>.to_vec(), result); |
| </code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_stream_replace_all_with" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1829-1843">source</a><h4 class="code-header">pub fn <a href="#method.try_stream_replace_all_with" class="fnname">try_stream_replace_all_with</a><R, W, F>(<br> &self,<br> rdr: R,<br> wtr: W,<br> replace_with: F<br>) -> Result<(), Error><span class="where fmt-newline">where<br> R: Read,<br> W: Write,<br> F: FnMut(&<a class="struct" href="struct.Match.html" title="struct aho_corasick::Match">Match</a>, &[u8], &mut W) -> Result<(), Error>,</span></h4></section></summary><div class="docblock"><p>Search the given reader and replace all matches of this automaton |
| using the given closure. The result is written to the given |
| writer. Matches correspond to the same matches as reported by |
| <a href="struct.AhoCorasick.html#method.try_find_iter" title="AhoCorasick::try_find_iter"><code>AhoCorasick::try_find_iter</code></a>.</p> |
| <p>The closure accepts three parameters: the match found, the text of |
| the match and the writer with which to write the replaced text (if any).</p> |
| <p>After all matches are replaced, the writer is <em>not</em> flushed.</p> |
| <p>If there was a problem reading from the given reader or writing to the |
| given writer, then the corresponding <code>io::Error</code> is returned and all |
| replacement is stopped.</p> |
| <p>When searching a stream, an internal buffer is used. Therefore, callers |
| should avoiding providing a buffered reader, if possible. However, |
| callers may want to provide a buffered writer.</p> |
| <p>Note that there is currently no infallible version of this routine.</p> |
| <h5 id="memory-usage-3"><a href="#memory-usage-3">Memory usage</a></h5> |
| <p>In general, searching streams will use a constant amount of memory for |
| its internal buffer. The one requirement is that the internal buffer |
| must be at least the size of the longest possible match. In most use |
| cases, the default buffer size will be much larger than any individual |
| match.</p> |
| <h5 id="errors-10"><a href="#errors-10">Errors</a></h5> |
| <p>This returns an error when this Aho-Corasick searcher does not support |
| the default <code>Input</code> configuration. More specifically, this occurs only |
| when the Aho-Corasick searcher does not support unanchored searches |
| since this stream searching routine always does an unanchored search.</p> |
| <p>This also returns an error if the searcher does not support stream |
| searches. Only searchers built with <a href="enum.MatchKind.html#variant.Standard" title="MatchKind::Standard"><code>MatchKind::Standard</code></a> semantics |
| support stream searches.</p> |
| <h5 id="example-basic-usage-10"><a href="#example-basic-usage-10">Example: basic usage</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::io::Write; |
| <span class="kw">use </span>aho_corasick::AhoCorasick; |
| |
| <span class="kw">let </span>patterns = <span class="kw-2">&</span>[<span class="string">"fox"</span>, <span class="string">"brown"</span>, <span class="string">"quick"</span>]; |
| <span class="kw">let </span>haystack = <span class="string">"The quick brown fox."</span>; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(patterns).unwrap(); |
| <span class="kw">let </span><span class="kw-2">mut </span>result = <span class="macro">vec!</span>[]; |
| ac.try_stream_replace_all_with( |
| haystack.as_bytes(), |
| <span class="kw-2">&mut </span>result, |
| |mat, <span class="kw">_</span>, wtr| { |
| wtr.write_all(mat.pattern().as_usize().to_string().as_bytes()) |
| }, |
| )<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(<span class="string">b"The 2 1 0."</span>.to_vec(), result); |
| </code></pre></div> |
| </div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-AhoCorasick-3" class="impl has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1847-2024">source</a><a href="#impl-AhoCorasick-3" class="anchor"></a><h3 class="code-header">impl <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h3></section></summary><div class="docblock"><p>Routines for querying information about the Aho-Corasick automaton.</p> |
| </div><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.kind" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1867-1869">source</a><h4 class="code-header">pub fn <a href="#method.kind" class="fnname">kind</a>(&self) -> <a class="enum" href="enum.AhoCorasickKind.html" title="enum aho_corasick::AhoCorasickKind">AhoCorasickKind</a></h4></section></summary><div class="docblock"><p>Returns the kind of the Aho-Corasick automaton used by this searcher.</p> |
| <p>Knowing the Aho-Corasick kind is principally useful for diagnostic |
| purposes. In particular, if no specific kind was given to |
| <a href="struct.AhoCorasickBuilder.html#method.kind" title="AhoCorasickBuilder::kind"><code>AhoCorasickBuilder::kind</code></a>, then one is automatically chosen and |
| this routine will report which one.</p> |
| <p>Note that the heuristics used for choosing which <code>AhoCorasickKind</code> |
| may be changed in a semver compatible release.</p> |
| <h5 id="examples-10"><a href="#examples-10">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, AhoCorasickKind}; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(<span class="kw-2">&</span>[<span class="string">"foo"</span>, <span class="string">"bar"</span>, <span class="string">"quux"</span>, <span class="string">"baz"</span>]).unwrap(); |
| <span class="comment">// The specific Aho-Corasick kind chosen is not guaranteed! |
| </span><span class="macro">assert_eq!</span>(AhoCorasickKind::DFA, ac.kind());</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.start_kind" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1882-1884">source</a><h4 class="code-header">pub fn <a href="#method.start_kind" class="fnname">start_kind</a>(&self) -> <a class="enum" href="enum.StartKind.html" title="enum aho_corasick::StartKind">StartKind</a></h4></section></summary><div class="docblock"><p>Returns the type of starting search configuration supported by this |
| Aho-Corasick automaton.</p> |
| <h5 id="examples-11"><a href="#examples-11">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, StartKind}; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(<span class="kw-2">&</span>[<span class="string">"foo"</span>, <span class="string">"bar"</span>, <span class="string">"quux"</span>, <span class="string">"baz"</span>]).unwrap(); |
| <span class="macro">assert_eq!</span>(StartKind::Unanchored, ac.start_kind());</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.match_kind" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1901-1903">source</a><h4 class="code-header">pub fn <a href="#method.match_kind" class="fnname">match_kind</a>(&self) -> <a class="enum" href="enum.MatchKind.html" title="enum aho_corasick::MatchKind">MatchKind</a></h4></section></summary><div class="docblock"><p>Returns the match kind used by this automaton.</p> |
| <p>The match kind is important because it determines what kinds of |
| matches are returned. Also, some operations (such as overlapping |
| search and stream searching) are only supported when using the |
| <a href="enum.MatchKind.html#variant.Standard" title="MatchKind::Standard"><code>MatchKind::Standard</code></a> match kind.</p> |
| <h5 id="examples-12"><a href="#examples-12">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, MatchKind}; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(<span class="kw-2">&</span>[<span class="string">"foo"</span>, <span class="string">"bar"</span>, <span class="string">"quux"</span>, <span class="string">"baz"</span>]).unwrap(); |
| <span class="macro">assert_eq!</span>(MatchKind::Standard, ac.match_kind());</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.min_pattern_len" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1927-1929">source</a><h4 class="code-header">pub fn <a href="#method.min_pattern_len" class="fnname">min_pattern_len</a>(&self) -> usize</h4></section></summary><div class="docblock"><p>Returns the length of the shortest pattern matched by this automaton.</p> |
| <h5 id="examples-13"><a href="#examples-13">Examples</a></h5> |
| <p>Basic usage:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::AhoCorasick; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(<span class="kw-2">&</span>[<span class="string">"foo"</span>, <span class="string">"bar"</span>, <span class="string">"quux"</span>, <span class="string">"baz"</span>]).unwrap(); |
| <span class="macro">assert_eq!</span>(<span class="number">3</span>, ac.min_pattern_len());</code></pre></div> |
| <p>Note that an <code>AhoCorasick</code> automaton has a minimum length of <code>0</code> if |
| and only if it can match the empty string:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::AhoCorasick; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(<span class="kw-2">&</span>[<span class="string">"foo"</span>, <span class="string">""</span>, <span class="string">"quux"</span>, <span class="string">"baz"</span>]).unwrap(); |
| <span class="macro">assert_eq!</span>(<span class="number">0</span>, ac.min_pattern_len());</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.max_pattern_len" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1943-1945">source</a><h4 class="code-header">pub fn <a href="#method.max_pattern_len" class="fnname">max_pattern_len</a>(&self) -> usize</h4></section></summary><div class="docblock"><p>Returns the length of the longest pattern matched by this automaton.</p> |
| <h5 id="examples-14"><a href="#examples-14">Examples</a></h5> |
| <p>Basic usage:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::AhoCorasick; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(<span class="kw-2">&</span>[<span class="string">"foo"</span>, <span class="string">"bar"</span>, <span class="string">"quux"</span>, <span class="string">"baz"</span>]).unwrap(); |
| <span class="macro">assert_eq!</span>(<span class="number">4</span>, ac.max_pattern_len());</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.patterns_len" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#1965-1967">source</a><h4 class="code-header">pub fn <a href="#method.patterns_len" class="fnname">patterns_len</a>(&self) -> usize</h4></section></summary><div class="docblock"><p>Return the total number of patterns matched by this automaton.</p> |
| <p>This includes patterns that may never participate in a match. For |
| example, if <a href="enum.MatchKind.html#variant.LeftmostFirst" title="MatchKind::LeftmostFirst"><code>MatchKind::LeftmostFirst</code></a> match semantics are used, and |
| the patterns <code>Sam</code> and <code>Samwise</code> were used to build the automaton (in |
| that order), then <code>Samwise</code> can never participate in a match because |
| <code>Sam</code> will always take priority.</p> |
| <h5 id="examples-15"><a href="#examples-15">Examples</a></h5> |
| <p>Basic usage:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::AhoCorasick; |
| |
| <span class="kw">let </span>ac = AhoCorasick::new(<span class="kw-2">&</span>[<span class="string">"foo"</span>, <span class="string">"bar"</span>, <span class="string">"baz"</span>]).unwrap(); |
| <span class="macro">assert_eq!</span>(<span class="number">3</span>, ac.patterns_len());</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.memory_usage" class="method has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#2021-2023">source</a><h4 class="code-header">pub fn <a href="#method.memory_usage" class="fnname">memory_usage</a>(&self) -> usize</h4></section></summary><div class="docblock"><p>Returns the approximate total amount of heap used by this automaton, in |
| units of bytes.</p> |
| <h5 id="examples-16"><a href="#examples-16">Examples</a></h5> |
| <p>This example shows the difference in heap usage between a few |
| configurations:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>aho_corasick::{AhoCorasick, AhoCorasickKind, MatchKind}; |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .kind(<span class="prelude-val">None</span>) <span class="comment">// default |
| </span>.build(<span class="kw-2">&</span>[<span class="string">"foobar"</span>, <span class="string">"bruce"</span>, <span class="string">"triskaidekaphobia"</span>, <span class="string">"springsteen"</span>]) |
| .unwrap(); |
| <span class="macro">assert_eq!</span>(<span class="number">5_632</span>, ac.memory_usage()); |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .kind(<span class="prelude-val">None</span>) <span class="comment">// default |
| </span>.ascii_case_insensitive(<span class="bool-val">true</span>) |
| .build(<span class="kw-2">&</span>[<span class="string">"foobar"</span>, <span class="string">"bruce"</span>, <span class="string">"triskaidekaphobia"</span>, <span class="string">"springsteen"</span>]) |
| .unwrap(); |
| <span class="macro">assert_eq!</span>(<span class="number">11_136</span>, ac.memory_usage()); |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .kind(<span class="prelude-val">Some</span>(AhoCorasickKind::NoncontiguousNFA)) |
| .ascii_case_insensitive(<span class="bool-val">true</span>) |
| .build(<span class="kw-2">&</span>[<span class="string">"foobar"</span>, <span class="string">"bruce"</span>, <span class="string">"triskaidekaphobia"</span>, <span class="string">"springsteen"</span>]) |
| .unwrap(); |
| <span class="macro">assert_eq!</span>(<span class="number">9_128</span>, ac.memory_usage()); |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .kind(<span class="prelude-val">Some</span>(AhoCorasickKind::ContiguousNFA)) |
| .ascii_case_insensitive(<span class="bool-val">true</span>) |
| .build(<span class="kw-2">&</span>[<span class="string">"foobar"</span>, <span class="string">"bruce"</span>, <span class="string">"triskaidekaphobia"</span>, <span class="string">"springsteen"</span>]) |
| .unwrap(); |
| <span class="macro">assert_eq!</span>(<span class="number">2_584</span>, ac.memory_usage()); |
| |
| <span class="kw">let </span>ac = AhoCorasick::builder() |
| .kind(<span class="prelude-val">Some</span>(AhoCorasickKind::DFA)) |
| .ascii_case_insensitive(<span class="bool-val">true</span>) |
| .build(<span class="kw-2">&</span>[<span class="string">"foobar"</span>, <span class="string">"bruce"</span>, <span class="string">"triskaidekaphobia"</span>, <span class="string">"springsteen"</span>]) |
| .unwrap(); |
| <span class="comment">// While this shows the DFA being the biggest here by a small margin, |
| // don't let the difference fool you. With such a small number of |
| // patterns, the difference is small, but a bigger number of patterns |
| // will reveal that the rate of growth of the DFA is far bigger than |
| // the NFAs above. For a large number of patterns, it is easy for the |
| // DFA to take an order of magnitude more heap space (or more!). |
| </span><span class="macro">assert_eq!</span>(<span class="number">11_136</span>, ac.memory_usage());</code></pre></div> |
| </div></details></div></details></div><h2 id="trait-implementations" class="small-section-header">Trait Implementations<a href="#trait-implementations" class="anchor"></a></h2><div id="trait-implementations-list"><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-Clone-for-AhoCorasick" class="impl has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#176">source</a><a href="#impl-Clone-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl Clone for <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.clone" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#176">source</a><a href="#method.clone" class="anchor"></a><h4 class="code-header">fn <a class="fnname">clone</a>(&self) -> <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h4></section></summary><div class='docblock'>Returns a copy of the value. <a>Read more</a></div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.clone_from" class="method trait-impl has-srclink"><span class="since rightside" title="Stable since Rust version 1.0.0">1.0.0</span><a href="#method.clone_from" class="anchor"></a><h4 class="code-header">const fn <a class="fnname">clone_from</a>(&mut self, source: &Self)</h4></section></summary><div class='docblock'>Performs copy-assignment from <code>source</code>. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-Debug-for-AhoCorasick" class="impl has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#2029-2033">source</a><a href="#impl-Debug-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl Debug for <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.fmt" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/aho_corasick/ahocorasick.rs.html#2030-2032">source</a><a href="#method.fmt" class="anchor"></a><h4 class="code-header">fn <a class="fnname">fmt</a>(&self, f: &mut Formatter<'_>) -> Result</h4></section></summary><div class='docblock'>Formats the value using the given formatter. <a>Read more</a></div></details></div></details></div><h2 id="synthetic-implementations" class="small-section-header">Auto Trait Implementations<a href="#synthetic-implementations" class="anchor"></a></h2><div id="synthetic-implementations-list"><section id="impl-RefUnwindSafe-for-AhoCorasick" class="impl has-srclink"><a href="#impl-RefUnwindSafe-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl RefUnwindSafe for <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h3></section><section id="impl-Send-for-AhoCorasick" class="impl has-srclink"><a href="#impl-Send-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl Send for <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h3></section><section id="impl-Sync-for-AhoCorasick" class="impl has-srclink"><a href="#impl-Sync-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl Sync for <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h3></section><section id="impl-Unpin-for-AhoCorasick" class="impl has-srclink"><a href="#impl-Unpin-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl Unpin for <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h3></section><section id="impl-UnwindSafe-for-AhoCorasick" class="impl has-srclink"><a href="#impl-UnwindSafe-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl UnwindSafe for <a class="struct" href="struct.AhoCorasick.html" title="struct aho_corasick::AhoCorasick">AhoCorasick</a></h3></section></div><h2 id="blanket-implementations" class="small-section-header">Blanket Implementations<a href="#blanket-implementations" class="anchor"></a></h2><div id="blanket-implementations-list"><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Any-for-AhoCorasick" class="impl has-srclink"><a href="#impl-Any-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl<T> Any for T<span class="where fmt-newline">where<br> T: 'static + ?Sized,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl has-srclink"><a href="#method.type_id" class="anchor"></a><h4 class="code-header">fn <a class="fnname">type_id</a>(&self) -> TypeId</h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-AhoCorasick" class="impl has-srclink"><a href="#impl-Borrow%3CT%3E-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl<T> Borrow<T> for T<span class="where fmt-newline">where<br> T: ?Sized,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/91522" title="Tracking issue for const_borrow">unstable</a></span><a href="#method.borrow" class="anchor"></a><h4 class="code-header">fn <a class="fnname">borrow</a>(&self) -> &T</h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-AhoCorasick" class="impl has-srclink"><a href="#impl-BorrowMut%3CT%3E-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl<T> BorrowMut<T> for T<span class="where fmt-newline">where<br> T: ?Sized,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/91522" title="Tracking issue for const_borrow">unstable</a></span><a href="#method.borrow_mut" class="anchor"></a><h4 class="code-header">fn <a class="fnname">borrow_mut</a>(&mut self) -> &mut T</h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-AhoCorasick" class="impl has-srclink"><a href="#impl-From%3CT%3E-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl<T> From<T> for T</h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.from" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.from" class="anchor"></a><h4 class="code-header">fn <a class="fnname">from</a>(t: T) -> T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p> |
| </div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-AhoCorasick" class="impl has-srclink"><a href="#impl-Into%3CU%3E-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl<T, U> Into<U> for T<span class="where fmt-newline">where<br> U: From<T>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.into" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.into" class="anchor"></a><h4 class="code-header">fn <a class="fnname">into</a>(self) -> U</h4></section></summary><div class="docblock"><p>Calls <code>U::from(self)</code>.</p> |
| <p>That is, this conversion is whatever the implementation of |
| <code>[From]<T> for U</code> chooses to do.</p> |
| </div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-ToOwned-for-AhoCorasick" class="impl has-srclink"><a href="#impl-ToOwned-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl<T> ToOwned for T<span class="where fmt-newline">where<br> T: Clone,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle" open><summary><section id="associatedtype.Owned" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Owned" class="anchor"></a><h4 class="code-header">type <a class="associatedtype">Owned</a> = T</h4></section></summary><div class='docblock'>The resulting type after obtaining ownership.</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.to_owned" class="method trait-impl has-srclink"><a href="#method.to_owned" class="anchor"></a><h4 class="code-header">fn <a class="fnname">to_owned</a>(&self) -> T</h4></section></summary><div class='docblock'>Creates owned data from borrowed data, usually by cloning. <a>Read more</a></div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.clone_into" class="method trait-impl has-srclink"><a href="#method.clone_into" class="anchor"></a><h4 class="code-header">fn <a class="fnname">clone_into</a>(&self, target: &mut T)</h4></section></summary><div class='docblock'>Uses borrowed data to replace owned data, usually by cloning. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-AhoCorasick" class="impl has-srclink"><a href="#impl-TryFrom%3CU%3E-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl<T, U> TryFrom<U> for T<span class="where fmt-newline">where<br> U: Into<T>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Error-1" class="anchor"></a><h4 class="code-header">type <a class="associatedtype">Error</a> = Infallible</h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.try_from" class="anchor"></a><h4 class="code-header">fn <a class="fnname">try_from</a>(value: U) -> Result<T, <T as TryFrom<U>>::Error></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-AhoCorasick" class="impl has-srclink"><a href="#impl-TryInto%3CU%3E-for-AhoCorasick" class="anchor"></a><h3 class="code-header">impl<T, U> TryInto<U> for T<span class="where fmt-newline">where<br> U: TryFrom<T>,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Error" class="anchor"></a><h4 class="code-header">type <a class="associatedtype">Error</a> = <U as TryFrom<T>>::Error</h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.try_into" class="anchor"></a><h4 class="code-header">fn <a class="fnname">try_into</a>(self) -> Result<U, <U as TryFrom<T>>::Error></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="aho_corasick" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |