Created
April 20, 2017 22:23
-
-
Save mickvangelderen/e334ae9dff6affa1a127638b56f8b925 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Clone,Copy,Debug)] | |
enum State { | |
A, | |
B, | |
Trap, | |
} | |
impl State { | |
fn is_accepting(self) -> bool { | |
use State::*; | |
match self { | |
A | B => true, | |
Trap => false, | |
} | |
} | |
fn transition(self, symbol: char) -> State { | |
use State::*; | |
match self { | |
A => match symbol { | |
'_' | 'a' ... 'z' => B, | |
_ => Trap, | |
}, | |
B => match symbol { | |
'_' | 'a' ... 'z' | '0' ... '9' => B, | |
_ => Trap, | |
}, | |
Trap => Trap | |
} | |
} | |
} | |
fn main() { | |
let input = std::env::args().nth(1).unwrap_or_default(); | |
std::process::exit( | |
if input.chars().fold( | |
State::A, | |
State::transition | |
).is_accepting() { | |
0 | |
} else { | |
1 | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment