From 8ffed1822bcbc1b6ce6647b840fb03996b0635ea Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 2 Aug 2022 14:27:31 +0200 Subject: Refactor to remove most closures --- src/construct/partial_bom.rs | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) (limited to 'src/construct/partial_bom.rs') 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 } } -- cgit