Skip to content

Commit b0b3ac2

Browse files
cakebakersylvestre
authored andcommitted
cut: use "?" instead of "match"
1 parent c6a57bb commit b0b3ac2

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

src/uu/cut/src/matcher.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,14 @@ impl Matcher for ExactMatcher<'_> {
2727
fn next_match(&self, haystack: &[u8]) -> Option<(usize, usize)> {
2828
let mut pos = 0usize;
2929
loop {
30-
match memchr(self.needle[0], &haystack[pos..]) {
31-
Some(match_idx) => {
32-
let match_idx = match_idx + pos; // account for starting from pos
33-
if self.needle.len() == 1
34-
|| haystack[match_idx + 1..].starts_with(&self.needle[1..])
35-
{
36-
return Some((match_idx, match_idx + self.needle.len()));
37-
}
38-
pos = match_idx + 1;
39-
}
40-
None => {
41-
return None;
42-
}
30+
let match_idx = memchr(self.needle[0], &haystack[pos..])?;
31+
let match_idx = match_idx + pos; // account for starting from pos
32+
33+
if self.needle.len() == 1 || haystack[match_idx + 1..].starts_with(&self.needle[1..]) {
34+
return Some((match_idx, match_idx + self.needle.len()));
4335
}
36+
37+
pos = match_idx + 1;
4438
}
4539
}
4640
}
@@ -50,19 +44,17 @@ pub struct WhitespaceMatcher {}
5044

5145
impl Matcher for WhitespaceMatcher {
5246
fn next_match(&self, haystack: &[u8]) -> Option<(usize, usize)> {
53-
match memchr2(b' ', b'\t', haystack) {
54-
Some(match_idx) => {
55-
let mut skip = match_idx + 1;
56-
while skip < haystack.len() {
57-
match haystack[skip] {
58-
b' ' | b'\t' => skip += 1,
59-
_ => break,
60-
}
61-
}
62-
Some((match_idx, skip))
47+
let match_idx = memchr2(b' ', b'\t', haystack)?;
48+
let mut skip = match_idx + 1;
49+
50+
while skip < haystack.len() {
51+
match haystack[skip] {
52+
b' ' | b'\t' => skip += 1,
53+
_ => break,
6354
}
64-
None => None,
6555
}
56+
57+
Some((match_idx, skip))
6658
}
6759
}
6860

src/uu/cut/src/searcher.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@ impl<M: Matcher> Iterator for Searcher<'_, '_, M> {
3131
type Item = (usize, usize);
3232

3333
fn next(&mut self) -> Option<Self::Item> {
34-
match self.matcher.next_match(&self.haystack[self.position..]) {
35-
Some((first, last)) => {
36-
let result = (first + self.position, last + self.position);
37-
self.position += last;
38-
Some(result)
39-
}
40-
None => None,
41-
}
34+
let (first, last) = self.matcher.next_match(&self.haystack[self.position..])?;
35+
let result = (first + self.position, last + self.position);
36+
self.position += last;
37+
38+
Some(result)
4239
}
4340
}
4441

0 commit comments

Comments
 (0)