Skip to content

Commit 73872b6

Browse files
committed
Matching char classes should only match the next char and
not search ahead for any match anywhere. Rename matches so it's more appropriate.
1 parent 56f5bb9 commit 73872b6

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

src/parse.rs

+24-23
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,26 @@ pub enum Faction {
220220
NonCapture,
221221
}
222222

223-
trait FindSet {
223+
trait SetMatch {
224224
fn find_set(&self, &Set, &Membership) -> Option<usize>;
225+
fn starts_with_set(&self, &Set, &Membership) -> bool;
225226
}
226227

227-
impl FindSet for str {
228+
impl SetMatch for str {
228229
fn find_set(&self, set: &Set, membership: &Membership) -> Option<usize> {
229-
for (index, c) in self.chars().enumerate() {
230-
match *membership {
231-
Inclusive => if set.contains(c) { return Some(index) },
232-
Exclusive => if !set.contains(c) { return Some(index) },
233-
}
234-
}
235-
236-
None
230+
self.chars()
231+
.position(|c| match *membership {
232+
Inclusive => set.contains(c),
233+
Exclusive => !set.contains(c),
234+
})
235+
}
236+
fn starts_with_set(&self, set: &Set, membership: &Membership) -> bool {
237+
self.chars()
238+
.take(1)
239+
.any(|c| match *membership {
240+
Inclusive => set.contains(c),
241+
Exclusive => !set.contains(c),
242+
})
237243
}
238244
}
239245

@@ -267,20 +273,15 @@ impl Ast {
267273
}
268274
// If the str matches, return the remainer of the str after the 1st match
269275
// has been trimmed off.
270-
pub fn matches<'a>(&self, txt: &'a str) -> Option<&'a str> {
271-
let vec: Vec<&str> = match self {
272-
&Ast::Char(c) => {
273-
if txt.starts_with(c) { txt.splitn(2, c).collect() }
274-
else { return None }
275-
},
276-
&Ast::Literal(ref s) => {
277-
if txt.starts_with(s) { txt.splitn(2, s).collect() }
278-
else { return None }
276+
pub fn trim_left_match<'a>(&self, txt: &'a str) -> Option<&'a str> {
277+
match self {
278+
&Ast::Char(c) => if txt.starts_with(c) { Some(&txt[1..]) } else { None },
279+
&Ast::Literal(ref s) => if txt.starts_with(s) { Some(&txt[s.len()..]) } else { None },
280+
&Ast::Set(ref set, ref membership) => {
281+
if txt.starts_with_set(set, membership) { Some(&txt[1..]) } else { None }
279282
},
280-
ast => return if let Some(index) = ast.find(txt) { Some(&txt[index + 1..]) } else { None },
281-
};
282-
283-
Some(vec[1])
283+
_ => unimplemented!(),
284+
}
284285
}
285286
fn negate(self) -> Self {
286287
match self {

src/re.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Regex {
4444
// is the workaround.
4545
matches = true;
4646

47-
if let Some(trimmed) = ast.matches(substr) {
47+
if let Some(trimmed) = ast.trim_left_match(substr) {
4848
substr = trimmed;
4949
} else {
5050
matches = false;

0 commit comments

Comments
 (0)