Skip to content

Commit a82a96b

Browse files
committed
Remove the submatch iterators.
All use cases can be replaced with Regex::capture_names.
1 parent 3e36cff commit a82a96b

File tree

5 files changed

+11
-219
lines changed

5 files changed

+11
-219
lines changed

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,9 @@ extern crate utf8_ranges;
460460
pub use error::Error;
461461
pub use re_builder::unicode::*;
462462
pub use re_set::unicode::*;
463-
pub use re_trait::{Locations, SubCapturesPosIter};
463+
pub use re_trait::Locations;
464464
pub use re_unicode::{
465-
Regex, Captures, SubCapturesIter, SubCapturesNamedIter,
465+
Regex, Captures,
466466
CaptureNamesIter, CapturesIter, FindIter,
467467
Replacer, NoExpand, SplitsIter, SplitsNIter,
468468
quote,
@@ -558,7 +558,7 @@ pub mod bytes {
558558
pub use re_builder::bytes::*;
559559
pub use re_bytes::*;
560560
pub use re_set::bytes::*;
561-
pub use re_trait::{Locations, SubCapturesPosIter};
561+
pub use re_trait::Locations;
562562
}
563563

564564
mod backtrack;

src/re_bytes.rs

+2-69
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use std::borrow::Cow;
1212
use std::collections::HashMap;
13-
use std::collections::hash_map;
1413
use std::fmt;
1514
use std::ops::Index;
1615
use std::str::FromStr;
@@ -22,7 +21,7 @@ use exec::{Exec, ExecNoSync};
2221
use expand::expand_bytes;
2322
use error::Error;
2423
use re_builder::bytes::RegexBuilder;
25-
use re_trait::{self, RegularExpression, Locations, SubCapturesPosIter};
24+
use re_trait::{self, RegularExpression, Locations};
2625

2726
/// Match represents a single match of a regex in a haystack.
2827
///
@@ -790,29 +789,6 @@ impl<'t> Captures<'t> {
790789
self.named_groups.get(name).and_then(|&i| self.get(i))
791790
}
792791

793-
/// Creates an iterator of all the capture groups in order of appearance
794-
/// in the regular expression.
795-
pub fn iter<'c>(&'c self) -> SubCapturesIter<'c, 't> {
796-
SubCapturesIter { idx: 0, caps: self }
797-
}
798-
799-
/// Creates an iterator of all the capture group positions in order of
800-
/// appearance in the regular expression. Positions are byte indices
801-
/// in terms of the original string matched.
802-
pub fn iter_pos(&self) -> SubCapturesPosIter {
803-
self.locs.iter()
804-
}
805-
806-
/// Creates an iterator of all named groups as an tuple with the group
807-
/// name and the value. The iterator returns these values in arbitrary
808-
/// order.
809-
pub fn iter_named<'c>(&'c self) -> SubCapturesNamedIter<'c, 't> {
810-
SubCapturesNamedIter {
811-
caps: self,
812-
names: self.named_groups.iter()
813-
}
814-
}
815-
816792
/// Expands all instances of `$name` in `text` to the corresponding capture
817793
/// group `name`, and writes them to the `dst` buffer given.
818794
///
@@ -873,7 +849,7 @@ impl<'c, 't> fmt::Debug for CapturesDebug<'c, 't> {
873849
let slot_to_name: HashMap<&usize, &String> =
874850
self.0.named_groups.iter().map(|(a, b)| (b, a)).collect();
875851
let mut map = f.debug_map();
876-
for (slot, m) in self.0.iter_pos().enumerate() {
852+
for (slot, m) in self.0.locs.iter().enumerate() {
877853
let m = m.map(|(s, e)| escape_bytes(&self.0.text[s..e]));
878854
if let Some(ref name) = slot_to_name.get(&slot) {
879855
map.entry(&name, &m);
@@ -926,49 +902,6 @@ impl<'t, 'i> Index<&'i str> for Captures<'t> {
926902
}
927903
}
928904

929-
/// An iterator over capture groups for a particular match of a regular
930-
/// expression.
931-
///
932-
/// `'c` is the lifetime of the captures and `'t` is the lifetime of the
933-
/// matched text.
934-
pub struct SubCapturesIter<'c, 't: 'c> {
935-
idx: usize,
936-
caps: &'c Captures<'t>,
937-
}
938-
939-
impl<'c, 't> Iterator for SubCapturesIter<'c, 't> {
940-
type Item = Option<&'t [u8]>;
941-
942-
fn next(&mut self) -> Option<Option<&'t [u8]>> {
943-
if self.idx < self.caps.len() {
944-
self.idx += 1;
945-
Some(self.caps.get(self.idx - 1).map(|m| m.as_bytes()))
946-
} else {
947-
None
948-
}
949-
}
950-
}
951-
952-
/// An Iterator over named capture groups as a tuple with the group name and
953-
/// the value.
954-
///
955-
/// `'c` is the lifetime of the captures and `'t` is the lifetime of the
956-
/// matched text.
957-
pub struct SubCapturesNamedIter<'c, 't: 'c> {
958-
caps: &'c Captures<'t>,
959-
names: hash_map::Iter<'c, String, usize>,
960-
}
961-
962-
impl<'c, 't> Iterator for SubCapturesNamedIter<'c, 't> {
963-
type Item = (&'c str, Option<&'t [u8]>);
964-
965-
fn next(&mut self) -> Option<(&'c str, Option<&'t [u8]>)> {
966-
self.names.next().map(|(name, &pos)| {
967-
(&**name, self.caps.get(pos).map(|m| m.as_bytes()))
968-
})
969-
}
970-
}
971-
972905
/// Replacer describes types that can be used to replace matches in a byte
973906
/// string.
974907
///

src/re_unicode.rs

+2-66
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use exec::{Exec, ExecNoSyncStr};
2323
use expand::expand_str;
2424
use re_builder::unicode::RegexBuilder;
2525
use re_plugin::Plugin;
26-
use re_trait::{self, RegularExpression, Locations, SubCapturesPosIter};
26+
use re_trait::{self, RegularExpression, Locations};
2727

2828
/// Escapes all regular expression meta characters in `text`.
2929
///
@@ -927,29 +927,6 @@ impl<'t> Captures<'t> {
927927
self.named_groups.pos(name).and_then(|i| self.get(i))
928928
}
929929

930-
/// Creates an iterator of all the capture groups in order of appearance
931-
/// in the regular expression.
932-
pub fn iter<'c>(&'c self) -> SubCapturesIter<'c, 't> {
933-
SubCapturesIter { idx: 0, caps: self, }
934-
}
935-
936-
/// Creates an iterator of all the capture group positions in order of
937-
/// appearance in the regular expression. Positions are byte indices
938-
/// in terms of the original string matched.
939-
pub fn iter_pos(&self) -> SubCapturesPosIter {
940-
self.locs.iter()
941-
}
942-
943-
/// Creates an iterator of all named groups as an tuple with the group
944-
/// name and the value. The iterator returns these values in arbitrary
945-
/// order.
946-
pub fn iter_named<'c>(&'c self) -> SubCapturesNamedIter<'c, 't> {
947-
SubCapturesNamedIter {
948-
caps: self,
949-
names: self.named_groups.iter()
950-
}
951-
}
952-
953930
/// Expands all instances of `$name` in `text` to the corresponding capture
954931
/// group `name`, and writes them to the `dst` buffer given.
955932
///
@@ -995,7 +972,7 @@ impl<'c, 't> fmt::Debug for CapturesDebug<'c, 't> {
995972
let slot_to_name: HashMap<usize, &str> =
996973
self.0.named_groups.iter().map(|(a, b)| (b, a)).collect();
997974
let mut map = f.debug_map();
998-
for (slot, m) in self.0.iter_pos().enumerate() {
975+
for (slot, m) in self.0.locs.iter().enumerate() {
999976
let m = m.map(|(s, e)| &self.0.text[s..e]);
1000977
if let Some(ref name) = slot_to_name.get(&slot) {
1001978
map.entry(&name, &m);
@@ -1048,47 +1025,6 @@ impl<'t, 'i> Index<&'i str> for Captures<'t> {
10481025
}
10491026
}
10501027

1051-
/// An iterator over capture groups for a particular match of a regular
1052-
/// expression.
1053-
///
1054-
/// `'c` is the lifetime of the captures.
1055-
pub struct SubCapturesIter<'c, 't: 'c> {
1056-
idx: usize,
1057-
caps: &'c Captures<'t>,
1058-
}
1059-
1060-
impl<'c, 't> Iterator for SubCapturesIter<'c, 't> {
1061-
type Item = Option<&'t str>;
1062-
1063-
fn next(&mut self) -> Option<Option<&'t str>> {
1064-
if self.idx < self.caps.len() {
1065-
self.idx += 1;
1066-
Some(self.caps.get(self.idx - 1).map(|m| m.as_str()))
1067-
} else {
1068-
None
1069-
}
1070-
}
1071-
}
1072-
1073-
/// An Iterator over named capture groups as a tuple with the group
1074-
/// name and the value.
1075-
///
1076-
/// `'c` is the lifetime of the captures.
1077-
pub struct SubCapturesNamedIter<'c, 't: 'c> {
1078-
caps: &'c Captures<'t>,
1079-
names: NamedGroupsIter<'c>,
1080-
}
1081-
1082-
impl<'c, 't> Iterator for SubCapturesNamedIter<'c, 't> {
1083-
type Item = (&'c str, Option<&'t str>);
1084-
1085-
fn next(&mut self) -> Option<(&'c str, Option<&'t str>)> {
1086-
self.names.next().map(|(name, pos)| {
1087-
(name, self.caps.get(pos).map(|m| m.as_str()))
1088-
})
1089-
}
1090-
}
1091-
10921028
/// An iterator that yields all non-overlapping capture groups matching a
10931029
/// particular regular expression.
10941030
///

tests/api.rs

-80
Original file line numberDiff line numberDiff line change
@@ -140,86 +140,6 @@ fn capture_misc() {
140140
assert_eq!(t!("c"), match_text!(cap.name("b").unwrap()));
141141
}
142142

143-
#[test]
144-
fn capture_iter() {
145-
let re = regex!(r"(.)(?P<a>.)(.)(?P<b>.)");
146-
let cap = re.captures(t!("abcd")).unwrap();
147-
assert_eq!(5, cap.len());
148-
149-
let expected = vec![
150-
t!("abcd"), t!("a"), t!("b"), t!("c"), t!("d"),
151-
].into_iter().map(Some).collect::<Vec<_>>();
152-
let got = cap.iter().collect::<Vec<_>>();
153-
assert_eq!(expected, got);
154-
}
155-
156-
#[test]
157-
fn capture_iter_missing() {
158-
let re = regex!(r"(.)(?P<a>a)?(.)(?P<b>.)");
159-
let cap = re.captures(t!("abc")).unwrap();
160-
assert_eq!(5, cap.len());
161-
162-
let expected = vec![
163-
Some(t!("abc")), Some(t!("a")), None, Some(t!("b")), Some(t!("c")),
164-
];
165-
let got = cap.iter().collect::<Vec<_>>();
166-
assert_eq!(expected, got);
167-
}
168-
169-
#[test]
170-
fn capture_iter_pos() {
171-
let re = regex!(r"(.)(?P<a>.)(.)(?P<b>.)");
172-
let cap = re.captures(t!("abcd")).unwrap();
173-
174-
let expected = vec![
175-
(0, 4), (0, 1), (1, 2), (2, 3), (3, 4),
176-
].into_iter().map(Some).collect::<Vec<_>>();
177-
let got = cap.iter_pos().collect::<Vec<_>>();
178-
assert_eq!(expected, got);
179-
}
180-
181-
#[test]
182-
fn capture_iter_pos_missing() {
183-
let re = regex!(r"(.)(?P<a>a)?(.)(?P<b>.)");
184-
let cap = re.captures(t!("abc")).unwrap();
185-
186-
let expected = vec![
187-
Some((0, 3)), Some((0, 1)), None, Some((1, 2)), Some((2, 3)),
188-
];
189-
let got = cap.iter_pos().collect::<Vec<_>>();
190-
assert_eq!(expected, got);
191-
}
192-
193-
#[test]
194-
fn capture_iter_named() {
195-
let re = regex!(r"(.)(?P<a>.)(.)(?P<b>.)");
196-
let cap = re.captures(t!("abcd")).unwrap();
197-
198-
let expected1 = vec![
199-
("a", Some(t!("b"))), ("b", Some(t!("d"))),
200-
];
201-
let expected2 = vec![
202-
("b", Some(t!("d"))), ("a", Some(t!("b"))),
203-
];
204-
let got = cap.iter_named().collect::<Vec<_>>();
205-
assert!(got == expected1 || got == expected2);
206-
}
207-
208-
#[test]
209-
fn capture_iter_named_missing() {
210-
let re = regex!(r"(.)(?P<a>.)?(.)(?P<b>.)");
211-
let cap = re.captures(t!("abc")).unwrap();
212-
213-
let expected1 = vec![
214-
("a", None), ("b", Some(t!("c"))),
215-
];
216-
let expected2 = vec![
217-
("b", Some(t!("c"))), ("a", None),
218-
];
219-
let got = cap.iter_named().collect::<Vec<_>>();
220-
assert!(got == expected1 || got == expected2);
221-
}
222-
223143
expand!(expand1, r"(?P<foo>\w+)", "abc", "$foo", "abc");
224144
expand!(expand2, r"(?P<foo>\w+)", "abc", "$0", "abc");
225145
expand!(expand3, r"(?P<foo>\w+)", "abc", "$1", "abc");

tests/macros.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ macro_rules! mat(
2020
Some(c) => {
2121
assert!(r.is_match(text));
2222
assert!(r.shortest_match(text).is_some());
23-
c.iter_pos().collect()
23+
r.capture_names()
24+
.enumerate()
25+
.map(|(i, _)| c.get(i).map(|m| (m.start(), m.end())))
26+
.collect()
2427
}
2528
None => vec![None],
2629
};

0 commit comments

Comments
 (0)