diff options
Diffstat (limited to 'src/construct/partial_bom.rs')
-rw-r--r-- | src/construct/partial_bom.rs | 41 |
1 files changed, 17 insertions, 24 deletions
diff --git a/src/construct/partial_bom.rs b/src/construct/partial_bom.rs index d92c9c1..2257bfd 100644 --- a/src/construct/partial_bom.rs +++ b/src/construct/partial_bom.rs @@ -13,6 +13,8 @@ use crate::token::Token; use crate::tokenizer::{State, Tokenizer}; +const BOM: [u8; 3] = [0xEF, 0xBB, 0xBF]; + /// Before a BOM. /// /// ```text @@ -20,42 +22,33 @@ use crate::tokenizer::{State, Tokenizer}; /// ^^^^ /// ``` pub fn start(tokenizer: &mut Tokenizer) -> State { - if tokenizer.current == Some(0xEF) { + if tokenizer.current == Some(BOM[0]) { tokenizer.enter(Token::ByteOrderMark); - tokenizer.consume(); - State::Fn(Box::new(cont)) - } else { - State::Nok - } -} - -/// Second byte in BOM. -/// -/// ```text -/// > | 0xEF 0xBB 0xBF -/// ^^^^ -/// ``` -fn cont(tokenizer: &mut Tokenizer) -> State { - if tokenizer.current == Some(0xBB) { - tokenizer.consume(); - State::Fn(Box::new(end)) + inside(tokenizer) } else { State::Nok } } -/// Last byte in BOM. +/// Inside the BOM. /// /// ```text /// > | 0xEF 0xBB 0xBF -/// ^^^^ +/// ^^^^ ^^^^ ^^^^ /// ``` -fn end(tokenizer: &mut Tokenizer) -> State { - if tokenizer.current == Some(0xBF) { +fn inside(tokenizer: &mut Tokenizer) -> State { + if tokenizer.current == Some(BOM[tokenizer.tokenize_state.size]) { + tokenizer.tokenize_state.size += 1; tokenizer.consume(); - tokenizer.exit(Token::ByteOrderMark); - State::Ok + if tokenizer.tokenize_state.size == BOM.len() { + tokenizer.exit(Token::ByteOrderMark); + tokenizer.tokenize_state.size = 0; + State::Ok + } else { + State::Fn(Box::new(inside)) + } } else { + tokenizer.tokenize_state.size = 0; State::Nok } } |