Convert Figma logo to code with AI

BurntSushi logoaho-corasick

A fast implementation of Aho-Corasick in Rust.

1,004
93
1,004
3

Quick Overview

The BurntSushi/aho-corasick repository is a Rust implementation of the Aho-Corasick algorithm, which is a string matching algorithm used to find all occurrences of a set of patterns in a given text. This library is designed to be efficient and easy to use, making it a valuable tool for tasks such as text processing, data mining, and network security.

Pros

  • Efficient Performance: The Aho-Corasick algorithm is known for its efficient performance, and the Rust implementation in this repository is designed to take advantage of Rust's performance characteristics.
  • Ease of Use: The library provides a simple and intuitive API, making it easy to integrate into a wide range of projects.
  • Flexibility: The library supports a variety of input and output formats, allowing it to be used in a wide range of applications.
  • Actively Maintained: The repository is actively maintained, with regular updates and bug fixes, ensuring that the library remains reliable and up-to-date.

Cons

  • Rust-specific: The library is written in Rust, which may not be the preferred language for all developers.
  • Limited Documentation: While the library is well-documented, the documentation could be more comprehensive, especially for more advanced use cases.
  • Limited Ecosystem: As a Rust-specific library, the ecosystem of related tools and libraries may be smaller than for more widely-used languages.
  • Learning Curve: Developers who are new to Rust may need to invest time in learning the language and its ecosystem before they can effectively use this library.

Code Examples

Here are a few examples of how to use the BurntSushi/aho-corasick library:

  1. Basic Usage:
use aho_corasick::AhoCorasick;

let patterns = &["foo", "bar", "baz"];
let text = "foobarbaz";

let ac = AhoCorasick::new(patterns);
let matches: Vec<_> = ac.find_iter(text).collect();

println!("{:?}", matches); // Prints [(0, 3), (3, 6), (6, 9)]
  1. Searching for Matches with Callbacks:
use aho_corasick::AhoCorasick;

let patterns = &["foo", "bar", "baz"];
let text = "foobarbaz";

let ac = AhoCorasick::new(patterns);
ac.find_iter(text).for_each(|(start, end)| {
    println!("Found match: {}", &text[start..end]);
});
  1. Searching for Matches with Overlapping Patterns:
use aho_corasick::AhoCorasick;

let patterns = &["foo", "bar", "baz", "foobar"];
let text = "foobarbaz";

let ac = AhoCorasick::new(patterns);
let matches: Vec<_> = ac.find_overlapping(text).collect();

println!("{:?}", matches); // Prints [(0, 3), (0, 6), (3, 6), (6, 9)]
  1. Searching for Matches with Replacements:
use aho_corasick::AhoCorasick;

let patterns = &["foo", "bar", "baz"];
let text = "foobarbaz";

let ac = AhoCorasick::new(patterns);
let replaced = ac.replace(text, "_");

println!("{}", replaced); // Prints "_____"

Getting Started

To get started with the BurntSushi/aho-corasick library, follow these steps:

  1. Add the library to your Cargo.toml file:
[dependencies]
aho-corasick = "0.7.18"
  1. Import the library in your Rust code:
use aho_corasick::AhoCorasick;
  1. Create a new AhoCorasick instance with your desired patterns:
let patterns = &["foo", "bar", "baz"];
let ac = AhoCorasick::new(

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

aho-corasick

A library for finding occurrences of many patterns at once with SIMD acceleration in some cases. This library provides multiple pattern search principally through an implementation of the Aho-Corasick algorithm, which builds a finite state machine for executing searches in linear time. Features include case insensitive matching, overlapping matches, fast searching via SIMD and optional full DFA construction and search & replace in streams.

Build status crates.io

Dual-licensed under MIT or the UNLICENSE.

Documentation

https://docs.rs/aho-corasick

Usage

Run cargo add aho-corasick to automatically add this crate as a dependency in your Cargo.toml file.

Example: basic searching

This example shows how to search for occurrences of multiple patterns simultaneously. Each match includes the pattern that matched along with the byte offsets of the match.

use aho_corasick::{AhoCorasick, PatternID};

let patterns = &["apple", "maple", "Snapple"];
let haystack = "Nobody likes maple in their apple flavored Snapple.";

let ac = AhoCorasick::new(patterns).unwrap();
let mut matches = vec![];
for mat in ac.find_iter(haystack) {
    matches.push((mat.pattern(), mat.start(), mat.end()));
}
assert_eq!(matches, vec![
    (PatternID::must(1), 13, 18),
    (PatternID::must(0), 28, 33),
    (PatternID::must(2), 43, 50),
]);

Example: ASCII case insensitivity

This is like the previous example, but matches Snapple case insensitively using AhoCorasickBuilder:

use aho_corasick::{AhoCorasick, PatternID};

let patterns = &["apple", "maple", "snapple"];
let haystack = "Nobody likes maple in their apple flavored Snapple.";

let ac = AhoCorasick::builder()
    .ascii_case_insensitive(true)
    .build(patterns)
    .unwrap();
let mut matches = vec![];
for mat in ac.find_iter(haystack) {
    matches.push((mat.pattern(), mat.start(), mat.end()));
}
assert_eq!(matches, vec![
    (PatternID::must(1), 13, 18),
    (PatternID::must(0), 28, 33),
    (PatternID::must(2), 43, 50),
]);

Example: replacing matches in a stream

This example shows how to execute a search and replace on a stream without loading the entire stream into memory first.

use aho_corasick::AhoCorasick;

let patterns = &["fox", "brown", "quick"];
let replace_with = &["sloth", "grey", "slow"];

// In a real example, these might be `std::fs::File`s instead. All you need to
// do is supply a pair of `std::io::Read` and `std::io::Write` implementations.
let rdr = "The quick brown fox.";
let mut wtr = vec![];

let ac = AhoCorasick::new(patterns).unwrap();
ac.stream_replace_all(rdr.as_bytes(), &mut wtr, replace_with)
    .expect("stream_replace_all failed");
assert_eq!(b"The slow grey sloth.".to_vec(), wtr);

Example: finding the leftmost first match

In the textbook description of Aho-Corasick, its formulation is typically structured such that it reports all possible matches, even when they overlap with another. In many cases, overlapping matches may not be desired, such as the case of finding all successive non-overlapping matches like you might with a standard regular expression.

Unfortunately the "obvious" way to modify the Aho-Corasick algorithm to do this doesn't always work in the expected way, since it will report matches as soon as they are seen. For example, consider matching the regex Samwise|Sam against the text Samwise. Most regex engines (that are Perl-like, or non-POSIX) will report Samwise as a match, but the standard Aho-Corasick algorithm modified for reporting non-overlapping matches will report Sam.

A novel contribution of this library is the ability to change the match semantics of Aho-Corasick (without additional search time overhead) such that Samwise is reported instead. For example, here's the standard approach:

use aho_corasick::AhoCorasick;

let patterns = &["Samwise", "Sam"];
let haystack = "Samwise";

let ac = AhoCorasick::new(patterns).unwrap();
let mat = ac.find(haystack).expect("should have a match");
assert_eq!("Sam", &haystack[mat.start()..mat.end()]);

And now here's the leftmost-first version, which matches how a Perl-like regex will work:

use aho_corasick::{AhoCorasick, MatchKind};

let patterns = &["Samwise", "Sam"];
let haystack = "Samwise";

let ac = AhoCorasick::builder()
    .match_kind(MatchKind::LeftmostFirst)
    .build(patterns)
    .unwrap();
let mat = ac.find(haystack).expect("should have a match");
assert_eq!("Samwise", &haystack[mat.start()..mat.end()]);

In addition to leftmost-first semantics, this library also supports leftmost-longest semantics, which match the POSIX behavior of a regular expression alternation. See MatchKind in the docs for more details.

Minimum Rust version policy

This crate's minimum supported rustc version is 1.60.0.

The current policy is that the minimum Rust version required to use this crate can be increased in minor version updates. For example, if crate 1.0 requires Rust 1.20.0, then crate 1.0.z for all values of z will also require Rust 1.20.0 or newer. However, crate 1.y for y > 0 may require a newer minimum version of Rust.

In general, this crate will be conservative with respect to the minimum supported version of Rust.

FFI bindings

NPM DownloadsLast 30 Days