Struct grep_searcher::SearcherBuilder[][src]

pub struct SearcherBuilder { /* fields omitted */ }

A builder for configuring a searcher.

A search builder permits specifying the configuration of a searcher, including options like whether to invert the search or to enable multi line search.

Once a searcher has been built, it is beneficial to reuse that searcher for multiple searches, if possible.

Methods

impl SearcherBuilder
[src]

Create a new searcher builder with a default configuration.

Build a searcher with the given matcher.

Set the line terminator that is used by the searcher.

When using a searcher, if the matcher provided has a line terminator set, then it must be the same as this one. If they aren't, building a searcher will return an error.

By default, this is set to b'\n'.

Whether to invert matching, whereby lines that don't match are reported instead of reporting lines that do match.

By default, this is disabled.

Whether to count and include line numbers with matching lines.

This is enabled by default. There is a small performance penalty associated with computing line numbers, so this can be disabled when this isn't desirable.

Whether to enable multi line search or not.

When multi line search is enabled, matches may match across multiple lines. Conversely, when multi line search is disabled, it is impossible for any match to span more than one line.

Warning: multi line search requires having the entire contents to search mapped in memory at once. When searching files, memory maps will be used if possible and if they are enabled, which avoids using your program's heap. However, if memory maps cannot be used (e.g., for searching streams like stdin or if transcoding is necessary), then the entire contents of the stream are read on to the heap before starting the search.

This is disabled by default.

Whether to include a fixed number of lines after every match.

When this is set to a non-zero number, then the searcher will report line_count contextual lines after every match.

This is set to 0 by default.

Whether to include a fixed number of lines before every match.

When this is set to a non-zero number, then the searcher will report line_count contextual lines before every match.

This is set to 0 by default.

Whether to enable the "passthru" feature or not.

When passthru is enabled, it effectively treats all non-matching lines as contextual lines. In other words, enabling this is akin to requesting an unbounded number of before and after contextual lines.

When passthru mode is enabled, any before_context or after_context settings are ignored by setting them to 0.

This is disabled by default.

Set an approximate limit on the amount of heap space used by a searcher.

The heap limit is enforced in two scenarios:

  • When searching using a fixed size buffer, the heap limit controls how big this buffer is allowed to be. Assuming contexts are disabled, the minimum size of this buffer is the length (in bytes) of the largest single line in the contents being searched. If any line exceeds the heap limit, then an error will be returned.
  • When performing a multi line search, a fixed size buffer cannot be used. Thus, the only choices are to read the entire contents on to the heap, or use memory maps. In the former case, the heap limit set here is enforced.

If a heap limit is set to 0, then no heap space is used. If there are no alternative strategies available for searching without heap space (e.g., memory maps are disabled), then the searcher wil return an error immediately.

By default, no limit is set.

Set the strategy to employ use of memory maps.

Currently, there are only two strategies that can be employed:

  • Automatic - A searcher will use heuristics, including but not limited to file size and platform, to determine whether to use memory maps or not.
  • Never - Memory maps will never be used. If multi line search is enabled, then the entire contents will be read on to the heap before searching begins.

The default behavior is never. Generally speaking, and perhaps against conventional wisdom, memory maps don't necessarily enable faster searching. For example, depending on the platform, using memory maps while searching a large directory can actually be quite a bit slower than using normal read calls because of the overhead of managing the memory maps.

Memory maps can be faster in some cases however. On some platforms, when searching a very large file that is already in memory, it can be slightly faster to search it as a memory map instead of using normal read calls.

Finally, memory maps have a somewhat complicated safety story in Rust. If you aren't sure whether enabling memory maps is worth it, then just don't bother with it.

WARNING: If your process is searching a file backed memory map at the same time that file is truncated, then it's possible for the process to terminate with a bus error.

Set the binary detection strategy.

The binary detection strategy determines not only how the searcher detects binary data, but how it responds to the presence of binary data. See the BinaryDetection type for more information.

By default, binary detection is disabled.

Set the encoding used to read the source data before searching.

When an encoding is provided, then the source data is unconditionally transcoded using the encoding, unless a BOM is present. If a BOM is present, then the encoding indicated by the BOM is used instead. If the transcoding process encounters an error, then bytes are replaced with the Unicode replacement codepoint.

When no encoding is specified (the default), then BOM sniffing is used to determine whether the source data is UTF-8 or UTF-16, and transcoding will be performed automatically. If no BOM could be found, then the source data is searched as if it were UTF-8. However, so long as the source data is at least ASCII compatible, then it is possible for a search to produce useful results.

Trait Implementations

impl Clone for SearcherBuilder
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for SearcherBuilder
[src]

Formats the value using the given formatter. Read more

impl Default for SearcherBuilder
[src]

Returns the "default value" for a type. Read more

Auto Trait Implementations