From d2a7c1fb0344971ac537e15d1a4b00fe18ff8c43 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 18 Jul 2022 17:24:32 +0200 Subject: Refactor to improve performance when constructs are turned off --- src/content/string.rs | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) (limited to 'src/content/string.rs') diff --git a/src/content/string.rs b/src/content/string.rs index f2650df..0a3f5eb 100644 --- a/src/content/string.rs +++ b/src/content/string.rs @@ -18,23 +18,26 @@ use crate::construct::{ }; use crate::tokenizer::{Code, State, StateFnResult, Tokenizer}; -const MARKERS: [Code; 5] = [ - // To do: build this vec based on whether they are enabled? - Code::VirtualSpace, // `whitespace` - Code::Char('\t'), // `whitespace` - Code::Char(' '), // `whitespace` - Code::Char('&'), // `character_reference` - Code::Char('\\'), // `character_escape` -]; - /// Before string. -/// -/// ```markdown -/// |& -/// |\& -/// |qwe -/// ``` pub fn start(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult { + let mut markers = vec![ + Code::VirtualSpace, // `whitespace` + Code::Char('\t'), // `whitespace` + Code::Char(' '), // `hard_break_trailing`, `whitespace` + ]; + + if tokenizer.parse_state.constructs.character_reference { + markers.push(Code::Char('&')); + } + if tokenizer.parse_state.constructs.character_escape { + markers.push(Code::Char('\\')); + } + + before_marker(tokenizer, code, markers) +} + +/// Before string. +fn before_marker(tokenizer: &mut Tokenizer, code: Code, markers: Vec) -> StateFnResult { match code { Code::None => (State::Ok, None), _ => tokenizer.attempt_n( @@ -43,16 +46,16 @@ pub fn start(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult { Box::new(character_escape), Box::new(whitespace), ], - |ok| Box::new(if ok { start } else { before_data }), + |ok| { + let func = if ok { before_marker } else { before_data }; + Box::new(move |t, c| func(t, c, markers)) + }, )(tokenizer, code), } } /// At data. -/// -/// ```markdown -/// |qwe -/// ``` -fn before_data(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult { - tokenizer.go(|t, c| data(t, c, MARKERS.to_vec()), start)(tokenizer, code) +fn before_data(tokenizer: &mut Tokenizer, code: Code, markers: Vec) -> StateFnResult { + let copy = markers.clone(); + tokenizer.go(|t, c| data(t, c, copy), |t, c| before_marker(t, c, markers))(tokenizer, code) } -- cgit