+let tag t n = if n == nil then Tag.nullt else tree_tag t.doc n
+
+let tagged_descendant t tag =
+ let doc = t.doc in (); fun n -> tree_tagged_descendant doc n tag
+
+let select_descendant t = fun ts ->
+ let v = (ptset_to_vector ts) in ();
+ fun n -> tree_select_descendant t.doc n v
+
+let tagged_following_below t tag =
+ let doc = t.doc in
+ (); fun n ctx -> tree_tagged_following_below doc n tag ctx
+
+let select_following_below t = fun ts ->
+ let v = (ptset_to_vector ts) in ();
+ fun n ctx -> tree_select_following_below t.doc n v ctx
+
+let closing t n = tree_closing t.doc n
+let is_open t n = tree_is_open t.doc n
+let get_text_id t n = tree_my_text t.doc n
+
+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 stack = ref []
+ let init_stack () = stack := []
+ let push x = stack:= x::!stack
+ let peek () = match !stack with
+ p::_ -> p
+ | _ -> failwith "peek"
+ let pop () = match !stack with
+ p::r -> stack:=r; p
+ | _ -> failwith "pop"
+
+ let next t = nodei ( (inode t) + 1 )
+ let next2 t = nodei ( (inode t) + 2 )
+ let next3 t = nodei ( (inode t) + 3 )
+
+ let print_xml_fast2 =
+ let _ = init_stack () in
+ let h = Hashtbl.create MED_H_SIZE in
+ let tag_str t = try Hashtbl.find h t with
+ Not_found -> let s = Tag.to_string t in
+ Hashtbl.add h t s;s
+ in
+ let h_att = Hashtbl.create MED_H_SIZE in
+ let att_str t = try Hashtbl.find h_att t with
+ Not_found -> let s = Tag.to_string t in
+ let attname = String.sub s 3 ((String.length s) -3) in
+ Hashtbl.add h_att t attname;attname
+ in fun outc tree t ->
+ let tree = tree.doc in
+ let fin = tree_closing tree t in
+ let rec loop_tag t tag =
+ if t <= fin then
+ if tree_is_open tree t then
+ (* opening tag *)
+ if tag == Tag.pcdata then
+ begin
+ output_string outc (text_get_text tree (tree_my_text_unsafe tree t));
+ loop (next2 t) (* skip closing $ *)
+ end
+ else
+ let tagstr = tag_str tag in
+ let _ = output_char outc '<';
+ output_string outc tagstr in
+ let t' = next t in
+ if tree_is_open tree t' then
+ let _ = push tagstr in
+ let tag' = tree_tag tree t' in
+ if tag' == Tag.attribute then let t'' = loop_attr (next t') 0 in
+ output_string outc ">"; loop t'' else (output_string outc ">";loop_tag t' tag')
+ else (* closing with no content *)
+ let _ = output_string outc "/>" in
+ loop (next t')
+ else
+ begin
+ (* closing tag *)
+ output_string outc "</";
+ output_string outc (pop());
+ output_char outc '>';
+ loop (next t);
+ end
+ and loop t = loop_tag t (tree_tag tree t)
+ and loop_attr t n =
+ if tree_is_open tree t then
+ let attname = att_str (tree_tag tree t) in
+ output_char outc ' ';
+ output_string outc attname;
+ output_string outc "=\"";
+ let t = next t in (* open $@ *)
+ output_string outc (text_get_text tree (tree_my_text_unsafe tree t));
+ output_char outc '"';
+ loop_attr (next3 t) (n+1)
+ else
+ next t (* close @ *)
+ in loop t
+
+ let print_xml_fast =
+ let h = Hashtbl.create MED_H_SIZE in
+ let tag_str t = try Hashtbl.find h t with
+ Not_found -> let s = Tag.to_string t in
+ Hashtbl.add h t s;s
+ in
+ let h_att = Hashtbl.create MED_H_SIZE in
+ let att_str t = try Hashtbl.find h_att t with
+ Not_found -> let s = Tag.to_string t in
+ let attname = String.sub s 3 ((String.length s) -3) in
+ Hashtbl.add h_att t attname;attname
+ in fun outc tree t ->
+ let rec loop ?(print_right=true) t =
+ if t != nil
+ then
+ let tagid = tree_tag tree.doc t in
+ if tagid==Tag.pcdata
+ then
+ begin
+ let tid = tree_my_text_unsafe tree.doc t in
+ output_string outc (text_get_text tree.doc tid);
+ if print_right
+ then loop (next_sibling tree t);
+ end
+ else
+ let tagstr = tag_str 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 attname = att_str (tag tree a) in
+ let fsa = first_child tree a in
+ let tid = tree_my_text_unsafe tree.doc fsa in
+ output_char outc ' ';
+ output_string outc attname;
+ output_string outc "=\"";
+ output_string outc (text_get_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