Skip to main content

memmem/
lib.rs

1// Copyright 2015 Joe Neeman
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9/*!
10A crate for substring searching, with functionality similar to the `memmem`
11function in C.
12
13The main trait is [`Searcher`], which finds fixed byte-strings in longer
14byte-strings. The only implementer is [`TwoWaySearcher`], a copy of the two-way
15search implementation from Rust's standard library adapted to search `&[u8]`.
16
17# Example
18
19```rust
20use memmem::{Searcher, TwoWaySearcher};
21let search = TwoWaySearcher::new("dog".as_bytes());
22assert_eq!(search.search_in("The quick brown fox jumped over the lazy dog.".as_bytes()), Some(41));
23```
24*/
25
26mod two_way;
27
28pub use two_way::TwoWaySearcher;
29
30/// A trait that searches for patterns in byte-strings.
31pub trait Searcher {
32    /// Search for something in a byte-string. Returns the starting index of the match, if found.
33    fn search_in(&self, haystack: &[u8]) -> Option<usize>;
34}