Skip to content

Commit f7185ca

Browse files
committed
Add convenience function to DFAState struct.
We're getting pretty fancy here what with the custom methods and everything.
1 parent 09079ea commit f7185ca

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ impl DFAState {
3030
let state_id = dfa.start_state(&config).unwrap();
3131
DFAState{dfa: dfa.into(), state_id}
3232
}
33+
34+
/// Convenience function to set the state how we want it.
35+
fn advance(&mut self, input: &str) {
36+
for &b in input.as_bytes().iter() {
37+
self.state_id = self.dfa.next_state(self.state_id, b);
38+
}
39+
}
3340
}
3441

3542

@@ -200,8 +207,7 @@ mod tests {
200207
fn test_dfa_mask_name() {
201208
// Illustrative example from page 13 of the paper.
202209
let mut dfa = DFAState::new(r"[a-zA-Z_]*");
203-
dfa.state_id = dfa.dfa.next_state(dfa.state_id, "i".as_bytes()[0]);
204-
dfa.state_id = dfa.dfa.next_state(dfa.state_id, "s".as_bytes()[0]);
210+
dfa.advance("is");
205211
let vocabulary = vec!["_prime():", ":#", "'''", " hi", "_indeed"];
206212
let terminal_sequence = vec![r"\(", r"\)"];
207213
assert_eq!(

0 commit comments

Comments
 (0)