summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2019-03-17 12:48:11 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2019-03-17 12:48:11 -0700
commite5a65e02bef6856e4ee0c2f928e3a41d4d2e18de (patch)
tree3edac3e487743f151f33a91d7d14d5eb7f3c7360
parentb4138c5b9b284547f56d5bc1a3a77f1ab18acda0 (diff)
Add CMARK_NODE__LAST_LINE_CHECKED flag.
Use this to avoid unnecessary recursion in ends_with_blank_line. Closes #284.
-rw-r--r--src/blocks.c30
-rw-r--r--src/node.h1
2 files changed, 18 insertions, 13 deletions
diff --git a/src/blocks.c b/src/blocks.c
index fb2d54a..e17cc15 100644
--- a/src/blocks.c
+++ b/src/blocks.c
@@ -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;
}
@@ -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');
}
@@ -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) {
diff --git a/src/node.h b/src/node.h
index 13901ba..2163230 100644
--- a/src/node.h
+++ b/src/node.h
@@ -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 {