Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 14 additions & 28 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,22 @@ impl Scanner {

/// Returns the next character (if available) and advances the cursor.
pub fn pop(&mut self) -> Option<&char> {
match self.characters.get(self.cursor) {
Some(character) => {
self.cursor += 1;

Some(character)
}
None => None,
}
self.characters.get(self.cursor).map(|c| {
self.cursor += 1;
c
})
}

/// Returns true if the `target` is found at the current cursor position,
/// and advances the cursor.
/// Otherwise, returns false leaving the cursor unchanged.
pub fn take(&mut self, target: &char) -> bool {
match self.characters.get(self.cursor) {
Some(character) => {
if target == character {
self.cursor += 1;

true
} else {
false
}
Some(character) if character == target => {
self.cursor += 1;
true
}
None => false,
_ => false,
}
}

Expand Down Expand Up @@ -118,17 +109,12 @@ impl Scanner {
&mut self,
cb: impl FnOnce(&char) -> Option<T>,
) -> Option<T> {
match self.characters.get(self.cursor) {
Some(input) => match cb(input) {
Some(output) => {
self.cursor += 1;

Some(output)
}
None => None,
},
None => None,
}
self.characters.get(self.cursor).and_then(|c| {
cb(c).map(|c| {
self.cursor += 1;
c
})
})
}
}

Expand Down