Struct grep_matcher::Match
source · pub struct Match { /* private fields */ }
Expand description
The type of a match.
The type of a match is a possibly empty range pointing to a contiguous block of addressable memory.
Every Match
is guaranteed to satisfy the invariant that start <= end
.
Indexing
This type is structurally identical to std::ops::Range<usize>
, but
is a bit more ergonomic for dealing with match indices. In particular,
this type implements Copy
and provides methods for building new Match
values based on old Match
values. Finally, the invariant that start
is always less than or equal to end
is enforced.
A Match
can be used to slice a &[u8]
, &mut [u8]
or &str
using
range notation. e.g.,
use grep_matcher::Match;
let m = Match::new(2, 5);
let bytes = b"abcdefghi";
assert_eq!(b"cde", &bytes[m]);
Implementations§
source§impl Match
impl Match
sourcepub fn with_start(&self, start: usize) -> Match
pub fn with_start(&self, start: usize) -> Match
Return a new match with the start offset replaced with the given value.
Panics
This method panics if start > self.end
.
sourcepub fn with_end(&self, end: usize) -> Match
pub fn with_end(&self, end: usize) -> Match
Return a new match with the end offset replaced with the given value.
Panics
This method panics if self.start > end
.
sourcepub fn offset(&self, amount: usize) -> Match
pub fn offset(&self, amount: usize) -> Match
Offset this match by the given amount and return a new match.
This adds the given offset to the start and end of this match, and returns the resulting match.
Panics
This panics if adding the given amount to either the start or end offset would result in an overflow.