Skip to content

Commit 2d71cc7

Browse files
committed
api: add Regex::captures_at
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
1 parent f17e500 commit 2d71cc7

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/re_bytes.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,7 @@ impl Regex {
265265
/// The `0`th capture group is always unnamed, so it must always be
266266
/// accessed with `get(0)` or `[0]`.
267267
pub fn captures<'t>(&self, text: &'t [u8]) -> Option<Captures<'t>> {
268-
let mut locs = self.capture_locations();
269-
self.captures_read_at(&mut locs, text, 0).map(move |_| Captures {
270-
text,
271-
locs: locs.0,
272-
named_groups: self.0.capture_name_idx().clone(),
273-
})
268+
self.captures_at(text, 0)
274269
}
275270

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

615+
/// Returns the same as [`Regex::captures`], but starts the search at the
616+
/// given offset.
617+
///
618+
/// The significance of the starting point is that it takes the surrounding
619+
/// context into consideration. For example, the `\A` anchor can only
620+
/// match when `start == 0`.
621+
pub fn captures_at<'t>(
622+
&self,
623+
text: &'t [u8],
624+
start: usize,
625+
) -> Option<Captures<'t>> {
626+
let mut locs = self.capture_locations();
627+
self.captures_read_at(&mut locs, text, start).map(move |_| Captures {
628+
text,
629+
locs: locs.0,
630+
named_groups: self.0.capture_name_idx().clone(),
631+
})
632+
}
633+
620634
/// This is like `captures`, but uses
621635
/// [`CaptureLocations`](struct.CaptureLocations.html)
622636
/// instead of

src/re_unicode.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,7 @@ impl Regex {
321321
/// The `0`th capture group is always unnamed, so it must always be
322322
/// accessed with `get(0)` or `[0]`.
323323
pub fn captures<'t>(&self, text: &'t str) -> Option<Captures<'t>> {
324-
let mut locs = self.capture_locations();
325-
self.captures_read_at(&mut locs, text, 0).map(move |_| Captures {
326-
text,
327-
locs: locs.0,
328-
named_groups: self.0.capture_name_idx().clone(),
329-
})
324+
self.captures_at(text, 0)
330325
}
331326

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

673+
/// Returns the same as [`Regex::captures`], but starts the search at the
674+
/// given offset.
675+
///
676+
/// The significance of the starting point is that it takes the surrounding
677+
/// context into consideration. For example, the `\A` anchor can only
678+
/// match when `start == 0`.
679+
pub fn captures_at<'t>(
680+
&self,
681+
text: &'t str,
682+
start: usize,
683+
) -> Option<Captures<'t>> {
684+
let mut locs = self.capture_locations();
685+
self.captures_read_at(&mut locs, text, start).map(move |_| Captures {
686+
text,
687+
locs: locs.0,
688+
named_groups: self.0.capture_name_idx().clone(),
689+
})
690+
}
691+
678692
/// This is like `captures`, but uses
679693
/// [`CaptureLocations`](struct.CaptureLocations.html)
680694
/// instead of

0 commit comments

Comments
 (0)