Skip to content

Commit

Permalink
Improve autocomplete performance in try_parse from O(n!) to O(n^2) (#…
Browse files Browse the repository at this point in the history
…16)

* fix(try_parse): improve performance from O(n!) to O(n^2)

* chore(test): add a test case for try_parse

* chore(fmt): cargo fmt
  • Loading branch information
lomirus committed Oct 22, 2023
1 parent df84f27 commit 86cb8c5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,16 @@ fn try_stack_to_dom(token_stack: Vec<Token>) -> Vec<Node> {
}
}

while let Some(token) = start_tags_stack.pop() {
let node = match token {
Token::Start(name, attrs) => Element {
if !start_tags_stack.is_empty() {
if let Token::Start(name, attrs) = start_tags_stack[0].clone() {
nodes.push(Node::Element(Element {
name,
attrs,
children: try_stack_to_dom(token_stack[start_tag_index + 1..].to_vec()),
}
.into_node(),
_ => unreachable!(),
};
nodes = vec![node];
}));
} else {
unreachable!()
}
}
nodes
}
Expand Down
8 changes: 8 additions & 0 deletions tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ fn fault_tolerance() {
);
}

#[test]
fn autocomplete_multiple_unclosed_tags() {
assert_eq!(
try_parse(r#"<img><h1><h2><h3><img><h4><h5><h6><h7><h8><h9><img>"#).html(),
"<img><h1><h2><h3><img><h4><h5><h6><h7><h8><h9><img></h9></h8></h7></h6></h5></h4></h3></h2></h1>"
);
}

#[test]
fn xml() {
let mut html = parse(
Expand Down

0 comments on commit 86cb8c5

Please sign in to comment.