Skip to content
Myr

lib.rs

This file

Path

crates/searcher/src/lib.rs

106 lines · 3.6 KiB · Rust

Last changed by

doc: fix typoSaitockbefore adoption3a57099

History of this file

Take it

The walk stopped after 200 commits: lines older than 5f5da48 point at it.

globset: support nested alternatesLuke Sandbergbefore adoption5f5da48

lines 1–24why, one step back
1/*!
2This crate provides an implementation of line oriented search, with optional3support for multi-line search.45# Brief overview67The principle type in this crate is a [`Searcher`], which can be configured8and built by a [`SearcherBuilder`]. A `Searcher` is responsible for reading9bytes from a source (e.g., a file), executing a search of those bytes using10a `Matcher` (e.g., a regex) and then reporting the results of that search to11a [`Sink`] (e.g., stdout). The `Searcher` itself is principally responsible12for managing the consumption of bytes from a source and applying a `Matcher`13over those bytes in an efficient way. The `Searcher` is also responsible for14inverting a search, counting lines, reporting contextual lines, detecting15binary data and even deciding whether or not to use memory maps.1617A `Matcher` (which is defined in the18[`grep-matcher`](https://crates.io/crates/grep-matcher) crate) is a trait19for describing the lowest levels of pattern search in a generic way. The20interface itself is very similar to the interface of a regular expression.21For example, the [`grep-regex`](https://crates.io/crates/grep-regex)22crate provides an implementation of the `Matcher` trait using Rust's23[`regex`](https://crates.io/crates/regex) crate.24

doc: fix typoSaitockbefore adoption3a57099

line 25why, one step back
25Finally, a `Sink` describes how callers receive search results produced by a

globset: support nested alternatesLuke Sandbergbefore adoption5f5da48

lines 26–92why, one step back
26`Searcher`. This includes routines that are called at the beginning and end of
27a search, in addition to routines that are called when matching or contextual28lines are found by the `Searcher`. Implementations of `Sink` can be trivially29simple, or extraordinarily complex, such as the `Standard` printer found in30the [`grep-printer`](https://crates.io/crates/grep-printer) crate, which31effectively implements grep-like output. This crate also provides convenience32`Sink` implementations in the [`sinks`] sub-module for easy searching with33closures.3435# Example3637This example shows how to execute the searcher and read the search results38using the [`UTF8`](sinks::UTF8) implementation of `Sink`.3940```41use {42 grep_matcher::Matcher,43 grep_regex::RegexMatcher,44 grep_searcher::Searcher,45 grep_searcher::sinks::UTF8,46};4748const SHERLOCK: &'static [u8] = b"\49For the Doctor Watsons of this world, as opposed to the Sherlock50Holmeses, success in the province of detective work must always51be, to a very large extent, the result of luck. Sherlock Holmes52can extract a clew from a wisp of straw or a flake of cigar ash;53but Doctor Watson has to have it taken out for him and dusted,54and exhibited clearly, with a label attached.55";5657let matcher = RegexMatcher::new(r"Doctor \w+")?;58let mut matches: Vec<(u64, String)> = vec![];59Searcher::new().search_slice(&matcher, SHERLOCK, UTF8(|lnum, line| {60 // We are guaranteed to find a match, so the unwrap is OK.61 let mymatch = matcher.find(line.as_bytes())?.unwrap();62 matches.push((lnum, line[mymatch].to_string()));63 Ok(true)64}))?;6566assert_eq!(matches.len(), 2);67assert_eq!(68 matches[0],69 (1, "Doctor Watsons".to_string())70);71assert_eq!(72 matches[1],73 (5, "Doctor Watson".to_string())74);7576# Ok::<(), Box<dyn std::error::Error>>(())77```7879See also `examples/search-stdin.rs` from the root of this crate's directory80to see a similar example that accepts a pattern on the command line and81searches stdin.82*/8384#![deny(missing_docs)]8586pub use crate::{87 lines::{LineIter, LineStep},88 searcher::{89 BinaryDetection, ConfigError, Encoding, MmapChoice, Searcher,90 SearcherBuilder,91 },92 sink::{

style: apply rustfmtAndrew Gallantbefore adoptionbb8172f

lines 93–94why, one step back
93 Sink, SinkContext, SinkContextKind, SinkError, SinkFinish, SinkMatch,
94 sinks,

globset: support nested alternatesLuke Sandbergbefore adoption5f5da48

lines 95–106why, one step back
95 },
96};9798#[macro_use]99mod macros;100101mod line_buffer;102mod lines;103mod searcher;104mod sink;105#[cfg(test)]106mod testutil;