Skip to content

Commit 33f98ad

Browse files
committed
regex: General style tweaks.
For loops are nicer than manual whiles, etc.
1 parent de14a73 commit 33f98ad

File tree

3 files changed

+12
-27
lines changed

3 files changed

+12
-27
lines changed

src/libregex/re.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,13 @@ impl Regex {
477477
(&self, text: &str, limit: uint, mut rep: R) -> StrBuf {
478478
let mut new = StrBuf::with_capacity(text.len());
479479
let mut last_match = 0u;
480-
let mut i = 0;
481-
for cap in self.captures_iter(text) {
480+
481+
for (i, cap) in self.captures_iter(text).enumerate() {
482482
// It'd be nicer to use the 'take' iterator instead, but it seemed
483483
// awkward given that '0' => no limit.
484484
if limit > 0 && i >= limit {
485485
break
486486
}
487-
i += 1;
488487

489488
let (s, e) = cap.pos(0).unwrap(); // captures only reports matches
490489
new.push_str(text.slice(last_match, s));
@@ -800,7 +799,7 @@ impl<'r, 't> Iterator<Captures<'t>> for FindCaptures<'r, 't> {
800799

801800
// Don't accept empty matches immediately following a match.
802801
// i.e., no infinite loops please.
803-
if e - s == 0 && Some(self.last_end) == self.last_match {
802+
if e == s && Some(self.last_end) == self.last_match {
804803
self.last_end += 1;
805804
return self.next()
806805
}
@@ -842,7 +841,7 @@ impl<'r, 't> Iterator<(uint, uint)> for FindMatches<'r, 't> {
842841

843842
// Don't accept empty matches immediately following a match.
844843
// i.e., no infinite loops please.
845-
if e - s == 0 && Some(self.last_end) == self.last_match {
844+
if e == s && Some(self.last_end) == self.last_match {
846845
self.last_end += 1;
847846
return self.next()
848847
}

src/libregex/vm.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,15 @@ impl<'r, 't> Nfa<'r, 't> {
169169
self.ic = next_ic;
170170
next_ic = self.chars.advance();
171171

172-
let mut i = 0;
173-
while i < clist.size {
172+
for i in range(0, clist.size) {
174173
let pc = clist.pc(i);
175174
let step_state = self.step(groups.as_mut_slice(), nlist,
176175
clist.groups(i), pc);
177176
match step_state {
178177
StepMatchEarlyReturn => return vec![Some(0), Some(0)],
179-
StepMatch => { matched = true; clist.empty() },
178+
StepMatch => { matched = true; break },
180179
StepContinue => {},
181180
}
182-
i += 1;
183181
}
184182
mem::swap(&mut clist, &mut nlist);
185183
nlist.empty();
@@ -226,7 +224,7 @@ impl<'r, 't> Nfa<'r, 't> {
226224
let found = ranges.as_slice();
227225
let found = found.bsearch(|&rc| class_cmp(casei, c, rc));
228226
let found = found.is_some();
229-
if (found && !negate) || (!found && negate) {
227+
if found ^ negate {
230228
self.add(nlist, pc+1, caps);
231229
}
232230
}
@@ -568,20 +566,10 @@ pub fn find_prefix(needle: &[u8], haystack: &[u8]) -> Option<uint> {
568566
if nlen > hlen || nlen == 0 {
569567
return None
570568
}
571-
let mut hayi = 0u;
572-
'HAYSTACK: loop {
573-
if hayi > hlen - nlen {
574-
break
569+
for (offset, window) in haystack.windows(nlen).enumerate() {
570+
if window == needle {
571+
return Some(offset)
575572
}
576-
let mut nedi = 0;
577-
while nedi < nlen {
578-
if haystack[hayi+nedi] != needle[nedi] {
579-
hayi += 1;
580-
continue 'HAYSTACK
581-
}
582-
nedi += 1;
583-
}
584-
return Some(hayi)
585573
}
586574
None
587575
}

src/libregex_macros/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,16 @@ fn exec<'t>(which: ::regex::native::MatchKind, input: &'t str,
187187
self.ic = next_ic;
188188
next_ic = self.chars.advance();
189189

190-
let mut i = 0;
191-
while i < clist.size {
190+
for i in range(0, clist.size) {
192191
let pc = clist.pc(i);
193192
let step_state = self.step(&mut groups, nlist,
194193
clist.groups(i), pc);
195194
match step_state {
196195
StepMatchEarlyReturn =>
197196
return vec![Some(0u), Some(0u)],
198-
StepMatch => { matched = true; clist.empty() },
197+
StepMatch => { matched = true; break },
199198
StepContinue => {},
200199
}
201-
i += 1;
202200
}
203201
::std::mem::swap(&mut clist, &mut nlist);
204202
nlist.empty();

0 commit comments

Comments
 (0)