aboutsummaryrefslogtreecommitdiffstats
path: root/src/construct/partial_bom.rs
diff options
context:
space:
mode:
authorLibravatar Titus Wormer <tituswormer@gmail.com>2022-08-02 14:27:31 +0200
committerLibravatar Titus Wormer <tituswormer@gmail.com>2022-08-02 14:27:31 +0200
commit8ffed1822bcbc1b6ce6647b840fb03996b0635ea (patch)
treedc01319d360b6e5ec08c893413ff056d3abd43bc /src/construct/partial_bom.rs
parent13cf914b5162fb12c842e983538b42d4b60cdcb8 (diff)
downloadmarkdown-rs-8ffed1822bcbc1b6ce6647b840fb03996b0635ea.tar.gz
markdown-rs-8ffed1822bcbc1b6ce6647b840fb03996b0635ea.tar.bz2
markdown-rs-8ffed1822bcbc1b6ce6647b840fb03996b0635ea.zip
Refactor to remove most closures
Diffstat (limited to 'src/construct/partial_bom.rs')
-rw-r--r--src/construct/partial_bom.rs41
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
}
}