Skip to content

Commit 3ad7018

Browse files
committed
loader: Refactoring compare_entry()
Signed-off-by: Akira Moroo <[email protected]>
1 parent 56ccf0c commit 3ad7018

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/loader.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub enum Error {
3333
File(fat::Error),
3434
BzImage(bzimage::Error),
3535
UnterminatedString,
36+
InvalidPattern,
3637
}
3738

3839
impl From<fat::Error> for Error {
@@ -110,7 +111,7 @@ fn find_entry(fs: &fat::Filesystem, pattern: &[u8]) -> Result<[u8; 255], Error>
110111
fn compare_entry(file_name: &[u8], pattern: &[u8]) -> Result<bool, Error> {
111112
fn compare_entry_inner<I>(
112113
mut name_iter: core::iter::Peekable<I>,
113-
mut pattern: &[u8],
114+
pattern: &[u8],
114115
max_depth: usize,
115116
) -> Result<bool, Error>
116117
where
@@ -119,21 +120,24 @@ fn compare_entry(file_name: &[u8], pattern: &[u8]) -> Result<bool, Error> {
119120
if max_depth == 0 {
120121
return Ok(false);
121122
}
122-
while let Some(p) = pattern.take_first() {
123+
let mut idx = 0;
124+
while let Some(p) = pattern.get(idx) {
125+
idx += 1;
123126
let f = name_iter.peek().ok_or(Error::UnterminatedString)?;
124127
#[cfg(test)]
125128
println!("{} ~ {}", *p as char, *f as char);
126129
match p {
127130
b'\0' => return Ok(*f == b'\0'),
128131
b'\\' => {
129-
match pattern.take_first() {
132+
match pattern.get(idx) {
130133
// trailing escape
131134
Some(b'\0') | None => return Ok(false),
132135
// no match
133136
Some(p) if p != f => return Ok(false),
134137
// continue
135138
_ => (),
136139
}
140+
idx += 1;
137141
}
138142
b'?' => {
139143
if *f == b'\0' {
@@ -142,12 +146,16 @@ fn compare_entry(file_name: &[u8], pattern: &[u8]) -> Result<bool, Error> {
142146
}
143147
b'*' => {
144148
while name_iter.peek().is_some() {
145-
if compare_entry_inner(name_iter.clone(), pattern, max_depth - 1)? {
149+
if compare_entry_inner(
150+
name_iter.clone(),
151+
pattern.get(idx..).ok_or(Error::InvalidPattern)?,
152+
max_depth - 1,
153+
)? {
146154
return Ok(true);
147155
}
148156
name_iter.next().ok_or(Error::UnterminatedString)?;
149157
}
150-
return Ok(*pattern.first().ok_or(Error::UnterminatedString)? == b'\0');
158+
return Ok(*pattern.get(idx).ok_or(Error::UnterminatedString)? == b'\0');
151159
}
152160
// TODO
153161
b'[' => todo!("patterns containing `[...]` sets are not supported"),

0 commit comments

Comments
 (0)