- let descr t = t.node
-
- let first_child n =
- let node' =
- match n.node with
- | Nil | String _ -> failwith "first_child"
- | Node (`Node t) ->
- let fs = first_child n.doc t in
- let txt = prev_text n.doc t in
- if Text.is_empty (text_collection n.doc) txt
- then norm fs
- else Node (`String (txt, fs))
-
- | Node(`String (i,_)) -> String i
- in
- { n with node = node'}
-
- let next_sibling n =
- let node' =
- match n.node with
- | Nil | String _ -> failwith "next_sibling"
- | Node (`String (_,ns)) -> norm ns
- | Node(`Node t) ->
- let ns = next_sibling n.doc t in
- let txt = next_text n.doc t in
- if Text.is_empty (text_collection n.doc) txt
- then norm ns
- else Node (`String (txt, ns))
- in
- { n with node = node'}
-
-
- let left = first_child
- let right = next_sibling
- let id =
- function { doc=d; node=Node(`Node n)} -> text_xml_id d n
- | { doc=d; node=Node(`String (i,_) )} -> node_xml_id d i
- | _ -> failwith "id"
-
- let tag =
- function { node=Node(`String _) } -> Tag.pcdata
- | { doc=d; node=Node(`Node n)} -> tag d n
- | _ -> failwith "Tag"
-
-
-
- let print_xml_fast outc t =
- let rec loop ?(print_right=true) t = match t.node with
- | Nil -> ()
- | String (s) -> output_string outc (string t)
- | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
-
- | Node (_) ->
- let tg = Tag.to_string (tag t) in
- let l = left t
- and r = right t
+let tag t n = if n == nil then Tag.nullt else tree_tag_id t.doc n
+
+let tagged_desc t tag = (); fun n -> tree_tagged_desc t.doc n tag
+
+let select_desc t = fun ts ->
+ let v = (ptset_to_vector ts) in ();
+ fun n -> tree_select_desc t.doc n v
+
+let tagged_foll_ctx t tag = (); fun n ctx -> tree_tagged_foll_below t.doc n tag ctx
+
+let select_foll_ctx t = fun ts ->
+ let v = (ptset_to_vector ts) in ();
+ fun n ctx -> tree_select_foll_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_cached_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_id 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_id tree t)
+ and loop_attr t n =
+ if tree_is_open tree t then
+ let attname = att_str (tree_tag_id tree t) in
+ output_char outc ' ';
+ output_string outc attname;
+ output_string outc "=\"";
+ let t = next t in (* open $@ *)
+ output_string outc (text_get_cached_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_id 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_cached_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