Fixed bug in NextElement, improved caching
[SXSI/xpathcomp.git] / tag.ml
1 (******************************************************************************)
2 (*  SXSI : XPath evaluator                                                    *)
3 (*  Kim Nguyen (Kim.Nguyen@nicta.com.au)                                      *)
4 (*  Copyright NICTA 2008                                                      *)
5 (*  Distributed under the terms of the LGPL (see LICENCE)                     *)
6 (******************************************************************************)
7 (*  maybe utf8 string... *)
8
9
10 type t = int
11 type pool
12
13 external null_pool : unit -> pool = "caml_xml_tree_nullt"
14 external null_tag : unit -> t = "caml_xml_tree_nullt"
15 external register_tag : pool -> string -> t = "caml_xml_tree_register_tag"
16 external tag_name : pool -> t -> string = "caml_xml_tree_tag_name"
17
18 let nullt = null_tag ()   
19 (* Defined in XMLTree.cpp *)
20 let document_node = 0
21 let attribute = 1
22 let pcdata = 2
23 let attribute_data= 3
24
25
26 let pool = Weak.create 1
27
28 let init p = Weak.set pool 0 (Some p)
29
30 let get_pool () =  match Weak.get pool 0 with
31   | Some x -> x
32   | None -> failwith "Tag.ml: Uninitialized Document"
33
34 let tag s = match s with
35   | "<$>" -> pcdata
36   | "<@>" -> attribute
37   | "" -> document_node
38   | "<@$>" -> attribute_data
39   | _ -> register_tag (get_pool()) s
40
41 let compare = (-)
42 let equal = (==)
43
44 let hash x = x
45
46
47 let to_string t = 
48   if t == pcdata then "<$>"
49   else if t == attribute_data then "<@$>"
50   else if t == attribute then "<@>"
51   else if t == nullt then "<!NIL!>"
52   else tag_name (get_pool()) t
53
54
55 let print ppf t = Format.fprintf ppf "%s" (to_string t)
56 (* Check internal invariants *)
57 let check t = 
58   if (t != tag (to_string t))
59   then failwith "module Tag: internal check failed"
60
61 let dump = print
62