Skip to content

Commit

Permalink
api: add Regex::captures_at
Browse files Browse the repository at this point in the history
This isn't *strictly* needed because of the existence of
Regex::captures_read_at, but it does fill out the singular missing
method. Namely, all other search routines have an *_at variant, so we
might as well add it for Regex::captures too.

Closes #547
  • Loading branch information
BurntSushi committed Apr 17, 2023
1 parent f3de42b commit cdf6325
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
26 changes: 20 additions & 6 deletions src/re_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,7 @@ impl Regex {
/// The `0`th capture group is always unnamed, so it must always be
/// accessed with `get(0)` or `[0]`.
pub fn captures<'t>(&self, text: &'t [u8]) -> Option<Captures<'t>> {
let mut locs = self.capture_locations();
self.captures_read_at(&mut locs, text, 0).map(move |_| Captures {
text,
locs: locs.0,
named_groups: self.0.capture_name_idx().clone(),
})
self.captures_at(text, 0)
}

/// Returns an iterator over all the non-overlapping capture groups matched
Expand Down Expand Up @@ -617,6 +612,25 @@ impl Regex {
.map(|(s, e)| Match::new(text, s, e))
}

/// Returns the same as [`Regex::captures`], but starts the search at the
/// given offset.
///
/// The significance of the starting point is that it takes the surrounding
/// context into consideration. For example, the `\A` anchor can only
/// match when `start == 0`.
pub fn captures_at<'t>(
&self,
text: &'t [u8],
start: usize,
) -> Option<Captures<'t>> {
let mut locs = self.capture_locations();
self.captures_read_at(&mut locs, text, start).map(move |_| Captures {
text,
locs: locs.0,
named_groups: self.0.capture_name_idx().clone(),
})
}

/// This is like `captures`, but uses
/// [`CaptureLocations`](struct.CaptureLocations.html)
/// instead of
Expand Down
26 changes: 20 additions & 6 deletions src/re_unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,7 @@ impl Regex {
/// The `0`th capture group is always unnamed, so it must always be
/// accessed with `get(0)` or `[0]`.
pub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>> {
let mut locs = self.capture_locations();
self.captures_read_at(&mut locs, text, 0).map(move |_| Captures {
text,
locs: locs.0,
named_groups: self.0.capture_name_idx().clone(),
})
self.captures_at(text, 0)
}

/// Returns an iterator over all the non-overlapping capture groups matched
Expand Down Expand Up @@ -675,6 +670,25 @@ impl Regex {
.map(|(s, e)| Match::new(text, s, e))
}

/// Returns the same as [`Regex::captures`], but starts the search at the
/// given offset.
///
/// The significance of the starting point is that it takes the surrounding
/// context into consideration. For example, the `\A` anchor can only
/// match when `start == 0`.
pub fn captures_at<'t>(
&self,
text: &'t str,
start: usize,
) -> Option<Captures<'t>> {
let mut locs = self.capture_locations();
self.captures_read_at(&mut locs, text, start).map(move |_| Captures {
text,
locs: locs.0,
named_groups: self.0.capture_name_idx().clone(),
})
}

/// This is like `captures`, but uses
/// [`CaptureLocations`](struct.CaptureLocations.html)
/// instead of
Expand Down

0 comments on commit cdf6325

Please sign in to comment.