+let last_idx = ref 0
+let array_find a i j =
+ let l = Array.length a in
+ let rec loop idx x y =
+ if x > y || idx >= l then nil
+ else
+ if a.(idx) >= x then if a.(idx) > y then nil else (last_idx := idx;a.(idx))
+ else loop (idx+1) x y
+ in
+ if a.(0) > j || a.(l-1) < i then nil
+ else loop !last_idx i j
+
+
+
+ let count t s = text_count t.doc s
+
+ let print_xml_fast outc tree t =
+ let rec loop ?(print_right=true) t =
+ if t != nil
+ then
+ let tagid = tree_tag_id tree.doc t in
+ if tagid==Tag.pcdata
+ then
+ begin
+ let tid = tree_my_text tree.doc t in
+ let _ = Printf.eprintf "my_text %i returned %i\n%!" (inode t) (inode tid)
+ in
+ output_string outc (text_get_cached_text tree.doc tid);
+ if print_right
+ then loop (next_sibling tree t);
+ end
+ else
+ let tagstr = Tag.to_string tagid in
+ let l = first_child tree t
+ and r = next_sibling tree t
+ in
+ output_char outc '<';
+ output_string outc tagstr;
+ if l == nil then output_string outc "/>"
+ else
+ if (tag tree l) == Tag.attribute then
+ begin
+ loop_attributes (first_child tree l);
+ if (next_sibling tree l) == nil then output_string outc "/>"
+ else
+ begin
+ output_char outc '>';
+ loop (next_sibling tree l);
+ output_string outc "</";
+ output_string outc tagstr;
+ output_char outc '>';
+ end;
+ end
+ else
+ begin
+ output_char outc '>';
+ loop l;
+ output_string outc "</";
+ output_string outc tagstr;
+ output_char outc '>';
+ end;
+ if print_right then loop r
+ and loop_attributes a =
+ if a != nil
+ then
+ let s = (Tag.to_string (tag tree a)) in
+ let attname = String.sub s 3 ((String.length s) -3) in
+ let fsa = first_child tree a in
+ let tid = tree_my_text tree.doc fsa in
+ let _ = Printf.eprintf "my_text %i returned %i\n%!" (inode fsa) (inode tid)
+ in
+ output_char outc ' ';
+ output_string outc attname;
+ output_string outc "=\"";
+ output_string outc (text_get_cached_text tree.doc tid);
+ output_char outc '"';
+ loop_attributes (next_sibling tree a)
+ in
+ loop ~print_right:false t
+
+
+ let print_xml_fast outc tree t =
+ if (tag tree t) = Tag.document_node then
+ print_xml_fast outc tree (first_child tree t)
+ else print_xml_fast outc tree t
+
+let tags_children t tag =
+ let a,_,_,_ = Hashtbl.find t.ttable tag in a
+let tags_below t tag =
+ let _,a,_,_ = Hashtbl.find t.ttable tag in a
+let tags_siblings t tag =
+ let _,_,a,_ = Hashtbl.find t.ttable tag in a
+let tags_after t tag =
+ let _,_,_,a = Hashtbl.find t.ttable tag in a
+
+
+let tags t tag = Hashtbl.find t.ttable tag
+
+
+let rec binary_parent t n =
+ let r =
+ if tree_is_first_child t.doc n
+ then tree_parent t.doc n
+ else tree_prev_sibling t.doc n
+ in if tree_tag_id t.doc r = Tag.pcdata then
+ binary_parent t r
+ else r
+
+let doc_ids t n = tree_doc_ids t.doc n
+
+let subtree_tags t tag = ();
+ fun n -> if n == nil then 0 else
+ tree_subtree_tags t.doc n tag
+
+let get_text t n =
+ let tid = tree_my_text t.doc n in
+ if tid == nulldoc then "" else
+ text_get_cached_text t.doc tid
+
+
+let dump_tree fmt tree =
+ let rec loop t n =
+ if t != nil then
+ let tag = (tree_tag_id tree.doc t ) in
+ let tagstr = Tag.to_string tag in
+ let tab = String.make n ' ' in
+
+ if tag == Tag.pcdata || tag == Tag.attribute_data
+ then
+ Format.fprintf fmt "%s<%s>%s</%s>\n"
+ tab tagstr (text_get_cached_text tree.doc (tree_my_text tree.doc t)) tagstr
+ else begin
+ Format.fprintf fmt "%s<%s>\n" tab tagstr;
+ loop (tree_first_child tree.doc t) (n+2);
+ Format.fprintf fmt "%s</%s>\n%!" tab tagstr;
+ end;
+ loop (tree_next_sibling tree.doc t) n
+ in
+ loop root 0
+;;