1 (* Copyright (C) 2000, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
27 * "cic:/a/b/c.con" => ("cic:/a/b/c.con", id )
28 * "cic:/a/b/c.ind#xpointer(1/1)" => ("cic:/a/b/c.con#xpointer(1/1)", id)
29 * "cic:/a/b/c.ind#xpointer(1/1/1)" => ("cic:/a/b/c.con#xpointer(1/1/1)", id)
38 (* (uriwithxpointer, uniqueid)
39 * where uniqueid is used to build a set of uri *)
40 type uri = string * int;;
46 let string_of_uri (uri,_) =
49 let name_of_uri (uri, _) =
51 try String.rindex uri '#' with Not_found -> String.length uri - 1
53 let index1 = String.rindex_from uri xpointer_offset '/' + 1 in
54 let index2 = String.rindex uri '.' in
55 String.sub uri index1 (index2 - index1)
57 let buri_of_uri (uri,_) =
58 let index = String.rindex uri '/' in
59 String.sub uri 0 index
61 module OrderedStrings =
64 let compare (s1 : t) (s2 : t) = compare s1 s2
68 module MapStringsToUri = Map.Make(OrderedStrings);;
70 (* Invariant: the map is the identity function,
72 * let str' = (MapStringsToUri.find str !set_of_uri) in
73 * str' == (MapStringsToUri.find str' !set_of_uri)
75 let set_of_uri = ref MapStringsToUri.empty;;
77 (* hash conses an uri *)
78 let uri_of_string suri =
80 MapStringsToUri.find suri !set_of_uri
82 let new_uri = suri, fresh_id () in
83 set_of_uri := MapStringsToUri.add suri new_uri !set_of_uri;
86 exception IllFormedUri of string;;
88 let strip_xpointer ((uri,_) as olduri) =
90 let index = String.rindex uri '#' in
91 let no_xpointer = String.sub uri 0 index in
92 uri_of_string no_xpointer
96 let clear_suffix uri ?(pat2="",0) pat1 =
98 let index = String.rindex uri '.' in
99 let index' = index + 1 in
100 let suffix = String.sub uri index' (String.length uri - index') in
101 if fst pat1 = suffix || fst pat2 = suffix then
102 String.sub uri 0 index
106 Not_found -> assert false
108 let has_suffix uri (pat,n) =
110 let suffix = String.sub uri (String.length uri - n) n in
113 Not_found -> assert false
115 let _dottypes = ".types"
116 let _types = "types",5
120 let _dotbody = ".body"
122 let _xpointer = "#xpointer(1/"
124 let cicuri_of_uri (uri, _) = uri_of_string (clear_suffix uri ~pat2:_types _ann)
126 let annuri_of_uri (uri , _) = uri_of_string ((clear_suffix uri _ann) ^ _dotann)
128 let uri_is_annuri (uri, _) = has_suffix uri _ann
130 let uri_is_var (uri, _) = has_suffix uri _var
132 let uri_is_con (uri, _) = has_suffix uri _con
134 let bodyuri_of_uri (uri, _) =
135 if has_suffix uri _con then
136 Some (uri_of_string (uri ^ _dotbody))
141 let innertypesuri_of_uri (uri, _) =
142 uri_of_string ((clear_suffix uri _types) ^ _dottypes)
145 let uri_of_uriref (uri, _) typeno consno =
146 let typeno = typeno + 1 in
149 | None -> Printf.sprintf "%s%s%d)" uri _xpointer typeno
150 | Some n -> Printf.sprintf "%s%s%d/%d)" uri _xpointer typeno n
154 let compare (_,id1) (_,id2) = id1 - id2
159 let compare = compare (* the one above, not Pervasives.compare *)
162 module UriSet = Set.Make (OrderedUri)