From 363c25c0a5584c0f936aaa9481b2130e29afa291 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 8 Jan 2015 10:29:09 -0800 Subject: Added `cmark_iter_reset` and a note about handling destructive updates. --- src/cmark.h | 14 ++++++++++++++ src/iterator.c | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/src/cmark.h b/src/cmark.h index 45bc338..84a15c8 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -163,6 +163,11 @@ cmark_node_last_child(cmark_node *node); * * cmark_iter_free(iter); * } + * + * Note that if you delete the current node, its first child, or its + * next sibling, the iterator may point to a nonexistent note. + * Use 'cmark_iter_reset' to set its pointer to the next node that + * should be traversed. */ /** Creates a new iterator starting at 'root'. @@ -177,6 +182,15 @@ CMARK_EXPORT void cmark_iter_free(cmark_iter *iter); +/** Resets the iterator so that the current node is 'current' and + the event type is 'event_type'. Use this to resume after destructively + modifying the tree structure. + */ +CMARK_EXPORT +void +cmark_iter_reset(cmark_iter *iter, cmark_node *current, + cmark_event_type event_type); + /** Returns the event type (`CMARK_EVENT_ENTER`, `CMARK_EVENT_EXIT`, * or `CMARK_EVENT_DONE`) for the next node. */ diff --git a/src/iterator.c b/src/iterator.c index b0ac9d2..2c15246 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -48,6 +48,14 @@ int S_is_leaf(cmark_node *node) } } +void +cmark_iter_reset(cmark_iter *iter, cmark_node *current, + cmark_event_type event_type) +{ + iter->event_type = event_type; + iter->current = current; +} + cmark_node* cmark_iter_get_node(cmark_iter *iter) { -- cgit v1.2.3