Add EOF check method

master
trivernis 4 years ago
parent 9c0edf1494
commit 9711e9e9e0

@ -1,6 +1,6 @@
[package]
name = "charred"
version = "0.1.2"
version = "0.1.3"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
license-file = "LICENSE"

@ -68,4 +68,13 @@ mod tests {
Ok(())
}
}
#[test]
fn it_checks_eof() -> TapeResult<()> {
let mut ctm = CharTapeMachine::new(TEST_STRING.chars().collect());
let _ = ctm.get_string_until_any(&['n'], &[]);
assert!(ctm.check_eof());
Ok(())
}
}

@ -108,22 +108,27 @@ impl CharTapeMachine {
}
}
/// Checks if the machine has reached the eof
pub fn check_eof(&self) -> bool {
self.index >= self.text.len()
}
/// checks if the current char is escaped
#[inline]
pub fn check_escaped(&mut self) -> bool {
pub fn check_escaped(&self) -> bool {
self.previous_char == ESCAPE
}
/// Returns true if the given character is equal to the current one
/// and the current character is not escaped
#[inline]
pub fn check_char(&mut self, value: &char) -> bool {
pub fn check_char(&self, value: &char) -> bool {
self.current_char == *value && !self.check_escaped()
}
/// Checks if one of the given chars matches the current one
#[inline]
pub fn check_any(&mut self, chars: &[char]) -> bool {
pub fn check_any(&self, chars: &[char]) -> bool {
!self.check_escaped() && chars.contains(&self.current_char)
}

Loading…
Cancel
Save