Skip to content

Commit

Permalink
regex: General style tweaks.
Browse files Browse the repository at this point in the history
For loops are nicer than manual whiles, etc.
  • Loading branch information
huonw committed Apr 30, 2014
1 parent de14a73 commit 33f98ad
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
9 changes: 4 additions & 5 deletions src/libregex/re.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down
24 changes: 6 additions & 18 deletions src/libregex/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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
}
6 changes: 2 additions & 4 deletions src/libregex_macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

9 comments on commit 33f98ad

@bors
Copy link
Contributor

@bors bors commented on 33f98ad Apr 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at huonw@33f98ad

@bors
Copy link
Contributor

@bors bors commented on 33f98ad Apr 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging huonw/rust/re-tweaks = 33f98ad into auto

@bors
Copy link
Contributor

@bors bors commented on 33f98ad Apr 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huonw/rust/re-tweaks = 33f98ad merged ok, testing candidate = 551df2dd

@bors
Copy link
Contributor

@bors bors commented on 33f98ad Apr 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at huonw@33f98ad

@bors
Copy link
Contributor

@bors bors commented on 33f98ad Apr 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging huonw/rust/re-tweaks = 33f98ad into auto

@bors
Copy link
Contributor

@bors bors commented on 33f98ad Apr 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huonw/rust/re-tweaks = 33f98ad merged ok, testing candidate = 7e9f3ea

@bors
Copy link
Contributor

@bors bors commented on 33f98ad Apr 30, 2014

@bors
Copy link
Contributor

@bors bors commented on 33f98ad Apr 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 7e9f3ea

Please sign in to comment.