| 1 | //! ANSI escape code parsing state machine |
| 2 | |
| 3 | #[cfg (test)] |
| 4 | mod codegen; |
| 5 | mod definitions; |
| 6 | mod table; |
| 7 | |
| 8 | #[cfg (test)] |
| 9 | pub(crate) use definitions::pack; |
| 10 | pub(crate) use definitions::unpack; |
| 11 | pub use definitions::Action; |
| 12 | pub use definitions::State; |
| 13 | |
| 14 | /// Transition to next [`State`] |
| 15 | /// |
| 16 | /// Note: This does not directly support UTF-8. |
| 17 | /// - If the data is validated as UTF-8 (e.g. `str`) or single-byte C1 control codes are |
| 18 | /// unsupported, then treat [`Action::BeginUtf8`] and [`Action::Execute`] for UTF-8 continuations |
| 19 | /// as [`Action::Print`]. |
| 20 | /// - If the data is not validated, then a UTF-8 state machine will need to be implemented on top, |
| 21 | /// starting with [`Action::BeginUtf8`]. |
| 22 | /// |
| 23 | /// Note: When [`State::Anywhere`] is returned, revert back to the prior state. |
| 24 | #[inline ] |
| 25 | pub const fn state_change(state: State, byte: u8) -> (State, Action) { |
| 26 | // Handle state changes in the anywhere state before evaluating changes |
| 27 | // for current state. |
| 28 | let mut change: u8 = state_change_(State::Anywhere, byte); |
| 29 | if change == 0 { |
| 30 | change = state_change_(state, byte); |
| 31 | } |
| 32 | |
| 33 | // Unpack into a state and action |
| 34 | unpack(delta:change) |
| 35 | } |
| 36 | |
| 37 | #[inline ] |
| 38 | const fn state_change_(state: State, byte: u8) -> u8 { |
| 39 | let state_idx: usize = state as usize; |
| 40 | let byte_idx: usize = byte as usize; |
| 41 | |
| 42 | table::STATE_CHANGES[state_idx][byte_idx] |
| 43 | } |
| 44 | |