block: minimize work when determining block

This commit is contained in:
Noah Hellman 2023-06-15 23:06:20 +02:00
parent e55e7a129f
commit 3e4bb127c7

View file

@ -1079,21 +1079,24 @@ impl<'s> IdentifiedBlock<'s> {
impl<'s> Kind<'s> { impl<'s> Kind<'s> {
/// Determine if a line continues the block. /// Determine if a line continues the block.
fn continues(&mut self, line: &'s str) -> bool { fn continues(&mut self, line: &'s str) -> bool {
let IdentifiedBlock { kind: next, .. } = IdentifiedBlock::new(line);
match self { match self {
Self::Atom(..) Self::Atom(..)
| Self::Fenced { | Self::Fenced {
has_closing_fence: true, has_closing_fence: true,
.. ..
} => false, } => false,
Self::Blockquote => matches!(next, Self::Blockquote | Self::Paragraph), Self::Blockquote => matches!(
IdentifiedBlock::new(line).kind,
Self::Blockquote | Self::Paragraph
),
Self::Heading { level } => { Self::Heading { level } => {
let next = IdentifiedBlock::new(line).kind;
matches!(next, Self::Paragraph) matches!(next, Self::Paragraph)
|| matches!(next, Self::Heading { level: l } if l == *level ) || matches!(next, Self::Heading { level: l } if l == *level )
} }
Self::Paragraph | Self::Table { caption: true } => { Self::Paragraph | Self::Table { caption: true } => !line
!matches!(next, Self::Atom(Blankline)) .trim_matches(|c: char| c.is_ascii_whitespace())
} .is_empty(),
Self::ListItem { Self::ListItem {
indent, indent,
last_blankline, last_blankline,
@ -1101,25 +1104,29 @@ impl<'s> Kind<'s> {
} => { } => {
let line_t = line.trim_start_matches(|c: char| c.is_ascii_whitespace()); let line_t = line.trim_start_matches(|c: char| c.is_ascii_whitespace());
let whitespace = line.len() - line_t.len(); let whitespace = line.len() - line_t.len();
let next = IdentifiedBlock::new(line).kind;
let para = !*last_blankline && matches!(next, Self::Paragraph); let para = !*last_blankline && matches!(next, Self::Paragraph);
*last_blankline = matches!(next, Self::Atom(Blankline)); *last_blankline = matches!(next, Self::Atom(Blankline));
*last_blankline || whitespace > *indent || para *last_blankline || whitespace > *indent || para
} }
Self::Definition { Self::Definition {
indent, indent,
footnote, footnote: true,
last_blankline, last_blankline,
.. ..
} => { } => {
if *footnote { let next = IdentifiedBlock::new(line).kind;
let line_t = line.trim_start_matches(|c: char| c.is_ascii_whitespace()); let line_t = line.trim_start_matches(|c: char| c.is_ascii_whitespace());
let whitespace = line.len() - line_t.len(); let whitespace = line.len() - line_t.len();
let cont_para = !*last_blankline && matches!(next, Self::Paragraph); let cont_para = !*last_blankline && matches!(next, Self::Paragraph);
*last_blankline = matches!(next, Self::Atom(Blankline)); *last_blankline = matches!(next, Self::Atom(Blankline));
whitespace > *indent || *last_blankline || cont_para whitespace > *indent || *last_blankline || cont_para
} else {
line.starts_with(' ') && !matches!(next, Self::Atom(Blankline))
} }
Self::Definition { .. } => {
let blankline = line
.trim_matches(|c: char| c.is_ascii_whitespace())
.is_empty();
line.starts_with(' ') && !blankline
} }
Self::Fenced { Self::Fenced {
fence_length, fence_length,
@ -1132,7 +1139,7 @@ impl<'s> Kind<'s> {
fence_length: l, fence_length: l,
spec, spec,
.. ..
} = next } = IdentifiedBlock::new(line).kind
{ {
if spec.is_empty() { if spec.is_empty() {
*has_closing_fence = k == *kind *has_closing_fence = k == *kind
@ -1143,11 +1150,13 @@ impl<'s> Kind<'s> {
true true
} }
Self::Table { caption } => { Self::Table { caption } => {
matches!(next, Self::Table { .. } | Self::Atom(Blankline)) || { let line_t = line.trim_matches(|c: char| c.is_ascii_whitespace());
if line line_t.is_empty()
.trim_matches(|c: char| c.is_ascii_whitespace()) || (line_t.starts_with('|')
.starts_with("^ ") && line_t.ends_with('|')
{ && !line_t.ends_with("\\|"))
|| {
if line_t.starts_with("^ ") {
*caption = true; *caption = true;
true true
} else { } else {