regex: General style tweaks.

For loops are nicer than manual whiles, etc.
This commit is contained in:
Huon Wilson 2014-04-30 00:55:28 +10:00
parent de14a739ae
commit 33f98ada02
3 changed files with 12 additions and 27 deletions

View File

@ -477,14 +477,13 @@ impl Regex {
(&self, text: &str, limit: uint, mut rep: R) -> StrBuf {
let mut new = StrBuf::with_capacity(text.len());
let mut last_match = 0u;
let mut i = 0;
for cap in self.captures_iter(text) {
for (i, cap) in self.captures_iter(text).enumerate() {
// It'd be nicer to use the 'take' iterator instead, but it seemed
// awkward given that '0' => no limit.
if limit > 0 && i >= limit {
break
}
i += 1;
let (s, e) = cap.pos(0).unwrap(); // captures only reports matches
new.push_str(text.slice(last_match, s));
@ -800,7 +799,7 @@ impl<'r, 't> Iterator<Captures<'t>> for FindCaptures<'r, 't> {
// Don't accept empty matches immediately following a match.
// i.e., no infinite loops please.
if e - s == 0 && Some(self.last_end) == self.last_match {
if e == s && Some(self.last_end) == self.last_match {
self.last_end += 1;
return self.next()
}
@ -842,7 +841,7 @@ impl<'r, 't> Iterator<(uint, uint)> for FindMatches<'r, 't> {
// Don't accept empty matches immediately following a match.
// i.e., no infinite loops please.
if e - s == 0 && Some(self.last_end) == self.last_match {
if e == s && Some(self.last_end) == self.last_match {
self.last_end += 1;
return self.next()
}

View File

@ -169,17 +169,15 @@ impl<'r, 't> Nfa<'r, 't> {
self.ic = next_ic;
next_ic = self.chars.advance();
let mut i = 0;
while i < clist.size {
for i in range(0, clist.size) {
let pc = clist.pc(i);
let step_state = self.step(groups.as_mut_slice(), nlist,
clist.groups(i), pc);
match step_state {
StepMatchEarlyReturn => return vec![Some(0), Some(0)],
StepMatch => { matched = true; clist.empty() },
StepMatch => { matched = true; break },
StepContinue => {},
}
i += 1;
}
mem::swap(&mut clist, &mut nlist);
nlist.empty();
@ -226,7 +224,7 @@ impl<'r, 't> Nfa<'r, 't> {
let found = ranges.as_slice();
let found = found.bsearch(|&rc| class_cmp(casei, c, rc));
let found = found.is_some();
if (found && !negate) || (!found && negate) {
if found ^ negate {
self.add(nlist, pc+1, caps);
}
}
@ -568,20 +566,10 @@ pub fn find_prefix(needle: &[u8], haystack: &[u8]) -> Option<uint> {
if nlen > hlen || nlen == 0 {
return None
}
let mut hayi = 0u;
'HAYSTACK: loop {
if hayi > hlen - nlen {
break
for (offset, window) in haystack.windows(nlen).enumerate() {
if window == needle {
return Some(offset)
}
let mut nedi = 0;
while nedi < nlen {
if haystack[hayi+nedi] != needle[nedi] {
hayi += 1;
continue 'HAYSTACK
}
nedi += 1;
}
return Some(hayi)
}
None
}

View File

@ -187,18 +187,16 @@ fn exec<'t>(which: ::regex::native::MatchKind, input: &'t str,
self.ic = next_ic;
next_ic = self.chars.advance();
let mut i = 0;
while i < clist.size {
for i in range(0, clist.size) {
let pc = clist.pc(i);
let step_state = self.step(&mut groups, nlist,
clist.groups(i), pc);
match step_state {
StepMatchEarlyReturn =>
return vec![Some(0u), Some(0u)],
StepMatch => { matched = true; clist.empty() },
StepMatch => { matched = true; break },
StepContinue => {},
}
i += 1;
}
::std::mem::swap(&mut clist, &mut nlist);
nlist.empty();