Skip to content

Commit

Permalink
Add CMARK_NODE__LAST_LINE_CHECKED flag.
Browse files Browse the repository at this point in the history
Use this to avoid unnecessary recursion in ends_with_blank_line.

Closes #284.
  • Loading branch information
jgm committed Mar 17, 2019
1 parent 441ac86 commit 854010a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ static bool S_last_line_blank(const cmark_node *node) {
return (node->flags & CMARK_NODE__LAST_LINE_BLANK) != 0;
}

static bool S_last_line_checked(const cmark_node *node) {
return (node->flags & CMARK_NODE__LAST_LINE_CHECKED) != 0;
}

static CMARK_INLINE cmark_node_type S_type(const cmark_node *node) {
return (cmark_node_type)node->type;
}
Expand All @@ -45,6 +49,10 @@ static void S_set_last_line_blank(cmark_node *node, bool is_blank) {
node->flags &= ~CMARK_NODE__LAST_LINE_BLANK;
}

static void S_set_last_line_checked(cmark_node *node) {
node->flags |= CMARK_NODE__LAST_LINE_CHECKED;
}

static CMARK_INLINE bool S_is_line_end_char(char c) {
return (c == '\n' || c == '\r');
}
Expand Down Expand Up @@ -208,20 +216,16 @@ static void remove_trailing_blank_lines(cmark_strbuf *ln) {
// Check to see if a node ends with a blank line, descending
// if needed into lists and sublists.
static bool ends_with_blank_line(cmark_node *node) {
cmark_node *cur = node;
while (cur != NULL) {
if (S_last_line_blank(cur)) {
S_set_last_line_blank(node, true);
return true;
}
if (S_type(cur) == CMARK_NODE_LIST || S_type(cur) == CMARK_NODE_ITEM) {
cur = cur->last_child;
} else {
cur = NULL;
}
if (S_last_line_checked(node)) {
return(S_last_line_blank(node));
} else if ((S_type(node) == CMARK_NODE_LIST ||
S_type(node) == CMARK_NODE_ITEM) && node->last_child) {
S_set_last_line_checked(node);
return(ends_with_blank_line(node->last_child));
} else {
S_set_last_line_checked(node);
return (S_last_line_blank(node));
}
S_set_last_line_blank(node, false);
return false;
}

static cmark_node *finalize(cmark_parser *parser, cmark_node *b) {
Expand Down
1 change: 1 addition & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ typedef struct {
enum cmark_node__internal_flags {
CMARK_NODE__OPEN = (1 << 0),
CMARK_NODE__LAST_LINE_BLANK = (1 << 1),
CMARK_NODE__LAST_LINE_CHECKED = (1 << 2),
};

struct cmark_node {
Expand Down

0 comments on commit 854010a

Please sign in to comment.