diff options
author | John MacFarlane <jgm@berkeley.edu> | 2014-12-14 14:24:14 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2014-12-14 14:24:14 -0800 |
commit | 1e00e9b9f94b01744b1880f341162b05429f9c8f (patch) | |
tree | e01005768b14df55f117cf1a66ca6e4f73025ac6 | |
parent | 9dd7a6510aa63506b4ea13b40f44b3094d8f637a (diff) |
Use cmark_iter to avoid stack allocation in process_inlines.
-rw-r--r-- | src/blocks.c | 53 |
1 files changed, 13 insertions, 40 deletions
diff --git a/src/blocks.c b/src/blocks.c index d770c3d..9dece20 100644 --- a/src/blocks.c +++ b/src/blocks.c @@ -301,52 +301,25 @@ static cmark_node* add_child(cmark_parser *parser, cmark_node* parent, } -typedef struct BlockStack { - struct BlockStack *previous; - cmark_node *next_sibling; -} block_stack; - // Walk through cmark_node and all children, recursively, parsing // string content into inline content where appropriate. -static void process_inlines(cmark_node* cur, cmark_reference_map *refmap) +static void process_inlines(cmark_node* root, cmark_reference_map *refmap) { - block_stack* stack = NULL; - block_stack* newstack = NULL; - - while (cur != NULL) { - switch (cur->type) { - case NODE_PARAGRAPH: - case NODE_HEADER: + cmark_iter *iter = cmark_iter_new(root); + cmark_node *cur; + cmark_event_type ev_type; + + while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) { + cur = cmark_iter_get_node(iter); + if (ev_type == CMARK_EVENT_ENTER) { + if (cur->type == NODE_PARAGRAPH || + cur->type == NODE_HEADER) { cmark_parse_inlines(cur, refmap); - break; - - default: - break; - } - - if (cur->first_child) { - newstack = (block_stack*)malloc(sizeof(block_stack)); - if (newstack == NULL) break; - newstack->previous = stack; - stack = newstack; - stack->next_sibling = cur->next; - cur = cur->first_child; - } else { - cur = cur->next; - } - - while (cur == NULL && stack != NULL) { - cur = stack->next_sibling; - newstack = stack->previous; - free(stack); - stack = newstack; + } } } - while (stack != NULL) { - newstack = stack->previous; - free(stack); - stack = newstack; - } + + cmark_iter_free(iter); } // Attempts to parse a list item marker (bullet or enumerated). |