cmark

My personal build of CMark ✏️

Commit
8a023286198a7e408398e282f293e3b0baebb644
Parent
ade396db95e2fcfef4d1769850f4a322e175981b
Author
Nick Wellnhofer <wellnhofer@aevum.de>
Date

Speed up hierarchy check in tree manipulation API

Skip hierarchy check in the common case that the inserted child has no children.

Diffstat

1 file changed, 11 insertions, 10 deletions

Status File Name N° Changes Insertions Deletions
Modified src/node.c 21 11 10
diff --git a/src/node.c b/src/node.c
@@ -23,20 +23,21 @@ static CMARK_INLINE bool S_is_inline(cmark_node *node) {
 }
 
 static bool S_can_contain(cmark_node *node, cmark_node *child) {
-  cmark_node *cur;
-
-  if (node == NULL || child == NULL) {
+  if (node == NULL || child == NULL || node == child) {
     return false;
   }
 
-  // Verify that child is not an ancestor of node or equal to node.
-  cur = node;
-  do {
-    if (cur == child) {
-      return false;
+  // Verify that child is not an ancestor of node.
+  if (child->first_child != NULL) {
+    cmark_node *cur = node->parent;
+
+    while (cur != NULL) {
+      if (cur == child) {
+        return false;
+      }
+      cur = cur->parent;
     }
-    cur = cur->parent;
-  } while (cur != NULL);
+  }
 
   if (child->type == CMARK_NODE_DOCUMENT) {
     return false;