diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-07-18 17:24:32 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-07-18 17:24:32 +0200 |
commit | d2a7c1fb0344971ac537e15d1a4b00fe18ff8c43 (patch) | |
tree | 1a9e8f889a3a0a63d0fde7fc1baf8eb3a8e6ba61 /src/content/string.rs | |
parent | 7186ce91784a647b3015e04b55a95b109deceeb3 (diff) | |
download | markdown-rs-d2a7c1fb0344971ac537e15d1a4b00fe18ff8c43.tar.gz markdown-rs-d2a7c1fb0344971ac537e15d1a4b00fe18ff8c43.tar.bz2 markdown-rs-d2a7c1fb0344971ac537e15d1a4b00fe18ff8c43.zip |
Refactor to improve performance when constructs are turned off
Diffstat (limited to '')
-rw-r--r-- | src/content/string.rs | 47 |
1 files changed, 25 insertions, 22 deletions
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<Code>) -> 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<Code>) -> StateFnResult { + let copy = markers.clone(); + tokenizer.go(|t, c| data(t, c, copy), |t, c| before_marker(t, c, markers))(tokenizer, code) } |