|
| 1 | +// src/data/utf8/types.rs |
| 2 | +// |
| 3 | +// UTF-8 types. |
| 4 | +// Thank you to Alacritty for their crate "utf8parse"! |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +// What to do when byte is received |
| 9 | +#[derive(Debug, Copy, Clone)] |
| 10 | +pub enum Action |
| 11 | +{ |
| 12 | + InvalidSeq = 0, |
| 13 | + EmitByte = 1, |
| 14 | + SetByte1 = 2, |
| 15 | + SetByte2 = 3, |
| 16 | + SetByte2Top = 4, |
| 17 | + SetByte3 = 5, |
| 18 | + SetByte3Top = 6, |
| 19 | + SetByte4 = 7, |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +// These are the states that the parser can be in |
| 24 | +#[allow(non_camel_case_types)] |
| 25 | +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)] |
| 26 | +pub enum State |
| 27 | +{ |
| 28 | + #[default] |
| 29 | + Ground = 0, |
| 30 | + Tail3 = 1, |
| 31 | + Tail2 = 2, |
| 32 | + Tail1 = 3, |
| 33 | + U3_2_e0 = 4, |
| 34 | + U3_2_ed = 5, |
| 35 | + UTF8_4_3_f0 = 6, |
| 36 | + UTF8_4_3_f4 = 7, |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +impl State |
| 41 | +{ |
| 42 | + #[inline] |
| 43 | + pub fn adv(self, byte: u8) -> (State, Action) |
| 44 | + { |
| 45 | + match self |
| 46 | + { |
| 47 | + State::Ground => match byte |
| 48 | + { |
| 49 | + 0x00..=0x7f => (State::Ground, Action::EmitByte), |
| 50 | + 0xc2..=0xdf => (State::Tail1, Action::SetByte2Top), |
| 51 | + 0xe0 => (State::U3_2_e0, Action::SetByte2Top), |
| 52 | + 0xe1..=0xec => (State::Tail2, Action::SetByte3Top), |
| 53 | + 0xed => (State::U3_2_ed, Action::SetByte3Top), |
| 54 | + 0xee..=0xef => (State::Tail2, Action::SetByte3Top), |
| 55 | + 0xf0 => (State::UTF8_4_3_f0, Action::SetByte4), |
| 56 | + 0xf1..=0xf3 => (State::Tail3, Action::SetByte4), |
| 57 | + 0xf4 => (State::UTF8_4_3_f4, Action::SetByte4), |
| 58 | + _ => (State::Ground, Action::InvalidSeq), |
| 59 | + }, |
| 60 | + |
| 61 | + State::U3_2_e0 => match byte |
| 62 | + { |
| 63 | + 0xa0..=0xbf => (State::Tail1, Action::SetByte2), |
| 64 | + _ => (State::Ground, Action::InvalidSeq), |
| 65 | + }, |
| 66 | + |
| 67 | + State::U3_2_ed => match byte |
| 68 | + { |
| 69 | + 0x80..=0x9f => (State::Tail1, Action::SetByte2), |
| 70 | + _ => (State::Ground, Action::InvalidSeq), |
| 71 | + }, |
| 72 | + |
| 73 | + State::UTF8_4_3_f0 => match byte |
| 74 | + { |
| 75 | + 0x90..=0xbf => (State::Tail2, Action::SetByte3), |
| 76 | + _ => (State::Ground, Action::InvalidSeq), |
| 77 | + }, |
| 78 | + |
| 79 | + State::UTF8_4_3_f4 => match byte |
| 80 | + { |
| 81 | + 0x80..=0x8f => (State::Tail2, Action::SetByte3), |
| 82 | + _ => (State::Ground, Action::InvalidSeq), |
| 83 | + }, |
| 84 | + |
| 85 | + State::Tail3 => match byte |
| 86 | + { |
| 87 | + 0x80..=0xbf => (State::Tail2, Action::SetByte3), |
| 88 | + _ => (State::Ground, Action::InvalidSeq), |
| 89 | + }, |
| 90 | + |
| 91 | + |
| 92 | + State::Tail2 => match byte |
| 93 | + { |
| 94 | + 0x80..=0xbf => (State::Tail1, Action::SetByte2), |
| 95 | + _ => (State::Ground, Action::InvalidSeq), |
| 96 | + }, |
| 97 | + |
| 98 | + State::Tail1 => match byte |
| 99 | + { |
| 100 | + 0x80..=0xbf => (State::Ground, Action::SetByte1), |
| 101 | + _ => (State::Ground, Action::InvalidSeq), |
| 102 | + }, |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments