Various fixes, mainly tagged_sibling/select_sibling.
[SXSI/XMLTree.git] / xml-tree-inc.hpp
index 6ce1724..cd8db71 100644 (file)
@@ -5,6 +5,19 @@
 #ifndef XML_TREE_INC_HPP_
 #define XML_TREE_INC_HPP_
 
+#include <cstdio>
+
+#if 0
+#define ASSERT_NODE(orig, res) do {                                     \
+    if (res < -1 || res >= par->n|| (res != -1 && res < orig))          \
+    fprintf(stderr,                                                     \
+      "Assertion failure: original node %i, result %i, line %i\n",      \
+            orig, res, __LINE__);                                       \
+  } while (0)
+#else
+#define ASSERT_NODE(orig, res)
+#endif
+
 inline uint32_t xml_tree::size() const
 {
   return tag_seq_len / 2;
@@ -86,7 +99,9 @@ inline xml_tree::node_t xml_tree::parent(xml_tree::node_t x) const
 
 inline xml_tree::node_t xml_tree::first_child(node_t x) const
 {
-  return bp_first_child(this->par, x);
+  xml_tree::node_t result = bp_first_child(this->par, x);
+  ASSERT_NODE(x, result);
+  return result;
 }
 
 inline xml_tree::node_t xml_tree::last_child(xml_tree::node_t x) const
@@ -99,7 +114,9 @@ inline xml_tree::node_t xml_tree::last_child(xml_tree::node_t x) const
 
 inline xml_tree::node_t xml_tree::next_sibling(xml_tree::node_t x) const
 {
-  return bp_next_sibling(this->par, x);
+  xml_tree::node_t result = bp_next_sibling(this->par, x);
+  ASSERT_NODE(x, result);
+  return result;
 }
 
 inline xml_tree::node_t xml_tree::prev_sibling(xml_tree::node_t x) const
@@ -119,6 +136,8 @@ inline xml_tree::node_t xml_tree::first_element(xml_tree::node_t x) const
   case PCDATA_OPEN_TAG_ID:
     n = n + 2;
     return bp_inspect(this->par, n) ? n : xml_tree::NIL;
+  default:
+    return n;
   };
 }
 
@@ -161,17 +180,29 @@ inline xml_tree::node_t xml_tree::tagged_child(xml_tree::node_t x,
                                                xml_tree::tag_t t) const
 {
   xml_tree::node_t c = first_child(x);
+  xml_tree::node_t result;
   if (is_nil(c) || tag(c) == t)
     return c;
   else
-    tagged_sibling(c, t);
+    return tagged_sibling(c, t);
+  /*  ASSERT_NODE(x, result);
+      return result;*/
 }
 
 inline xml_tree::node_t xml_tree::tagged_sibling(xml_tree::node_t x,
                                                  xml_tree::tag_t t) const
 {
   xml_tree::node_t sibling = next_sibling(x);
-  while(!is_nil(sibling) && tag(sibling) != t) sibling = next_sibling(sibling);
+  xml_tree::tag_t stag;
+  while (sibling  != xml_tree::NIL) {
+    stag = tag(sibling);
+    if (stag == t) {
+      ASSERT_NODE(x, sibling);
+      return  sibling;
+    }
+    sibling = next_sibling(sibling);
+  };
+  ASSERT_NODE(x, sibling);
   return sibling;
 }