Debian Codegraph @

clib/cString.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 module type S = module type of String
12
13 include String
14
15 let rec hash len s i accu =
16 if i = len then accu
17 else
18 let c = Char.code (String.unsafe_get s i) in
19 hash len s (succ i) (accu * 19 + c)
20
21 let hash s =
22 let len = String.length s in
23 hash len s 0 0
24
25 let explode s =
26 let rec explode_rec n =
27 if n >= String.length s then
28 []
29 else
30 String.make 1 (String.get s n) :: explode_rec (succ n)
31 in
32 explode_rec 0
33
34 let implode sl = String.concat "" sl
35
36 let is_empty s = String.length s = 0
37
38 let drop_simple_quotes s =
39 let n = String.length s in
40 if n > 2 && s.[0] = '\'' && s.[n-1] = '\'' then String.sub s 1 (n-2) else s
41
42 let quote_coq_string s =
43 let b = Buffer.create (String.length s + 2) in
44 Buffer.add_char b '"';
45 for i = 0 to String.length s - 1 do
46 Buffer.add_char b s.[i];
47 if s.[i] = '"' then Buffer.add_char b s.[i];
48 done;
49 Buffer.add_char b '"';
50 Buffer.contents b
51
52 let unquote_coq_string s =
53 let b = Buffer.create (String.length s) in
54 let n = String.length s in
55 if n < 2 || s.[0] <> '"' || s.[n-1] <> '"' then None else
56 let i = ref 1 in
57 try
58 while !i < n - 1 do
59 Buffer.add_char b s.[!i];
60 if s.[!i] = '"' then
61 if !i < n - 2 && s.[!i+1] = '"' then incr i
62 else raise Exit;
63 incr i
64 done;
65 Some (Buffer.contents b)
66 with Exit -> None
67
68 let html_escape msg =
69 let buf = Buffer.create (String.length msg) in
70 String.iter (fun c ->
71 if String.contains "\"&'<>" c then
72 Buffer.add_string buf (Printf.sprintf "&#%d;" (Char.code c))
73 else
74 Buffer.add_char buf c)
75 msg;
76 Buffer.contents buf
77
78 (* substring searching... *)
79
80 (* gdzie = where, co = what *)
81 (* gdzie=gdzie(string) gl=gdzie(length) gi=gdzie(index) *)
82 let rec raw_is_sub gdzie gl gi co cl ci =
83 (ci>=cl) ||
84 ((String.unsafe_get gdzie gi = String.unsafe_get co ci) &&
85 (raw_is_sub gdzie gl (gi+1) co cl (ci+1)))
86
87 let rec raw_str_index i gdzie l c co cl =
88 (* First adapt to ocaml 3.11 new semantics of index_from *)
89 if (i+cl > l) then raise Not_found;
90 (* Then proceed as in ocaml < 3.11 *)
91 let i' = String.index_from gdzie i c in
92 if (i'+cl <= l) && (raw_is_sub gdzie l i' co cl 0) then i' else
93 raw_str_index (i'+1) gdzie l c co cl
94
95 let string_index_from gdzie i co =
96 if co="" then i else
97 raw_str_index i gdzie (String.length gdzie)
98 (String.unsafe_get co 0) co (String.length co)
99
100 let string_contains ~where ~what =
101 try
102 let _ = string_index_from where 0 what in true
103 with
104 Not_found -> false
105
106 let is_sub p s off =
107 let lp = String.length p in
108 let ls = String.length s in
109 if ls < off + lp then false
110 else
111 let rec aux i =
112 if lp <= i then true
113 else
114 let cp = String.unsafe_get p i in
115 let cs = String.unsafe_get s (off + i) in
116 if cp = cs then aux (succ i) else false
117 in
118 aux 0
119
120 let is_prefix p s =
121 is_sub p s 0
122
123 let is_suffix p s =
124 is_sub p s (String.length s - String.length p)
125
126 let plural n s = if n<>1 then s^"s" else s
127
128 let lplural l s =
129 match l with
130 | [_] -> s
131 | _ -> s^"s"
132
133 let conjugate_verb_to_be n = if n<>1 then "are" else "is"
134
135 let ordinal n =
136 let s =
137 if (n / 10) mod 10 = 1 then "th"
138 else match n mod 10 with
139 | 1 -> "st"
140 | 2 -> "nd"
141 | 3 -> "rd"
142 | _ -> "th"
143 in
144 string_of_int n ^ s
145
146 let uchar_array_of_utf_8_string s =
147 let slen = length s in (* is an upper bound on Uchar.t count *)
148 let uchars = Array.make slen Uchar.max in
149 let k = ref 0 and i = ref 0 in
150 while (!i < slen) do
151 let dec = get_utf_8_uchar s !i in
152 i := !i + Uchar.utf_decode_length dec;
153 uchars.(!k) <- Uchar.utf_decode_uchar dec;
154 incr k;
155 done;
156 uchars, !k
157
158 let edit_distance ?(limit = Stdlib.Int.max_int) s0 s1 =
159 if limit <= 1 then (if equal s0 s1 then 0 else limit) else
160 let[@inline] minimum a b c = Stdlib.Int.min a (Stdlib.Int.min b c) in
161 let s0, len0 = uchar_array_of_utf_8_string s0 in
162 let s1, len1 = uchar_array_of_utf_8_string s1 in
163 let limit = Stdlib.Int.min (Stdlib.Int.max len0 len1) limit in
164 if Stdlib.Int.abs (len1 - len0) >= limit then limit else
165 let s0, s1 = if len0 > len1 then s0, s1 else s1, s0 in
166 let len0, len1 = if len0 > len1 then len0, len1 else len1, len0 in
167 let rec loop row_minus2 row_minus1 row i len0 limit s0 s1 =
168 if i > len0 then row_minus1.(Array.length row_minus1 - 1) else
169 let len1 = Array.length row - 1 in
170 let row_min = ref Stdlib.Int.max_int in
171 row.(0) <- i;
172 let jmax =
173 let jmax = Stdlib.Int.min len1 (i + limit - 1) in
174 if jmax < 0 then (* overflow *) len1 else jmax
175 in
176 for j = Stdlib.Int.max 1 (i - limit) to jmax do
177 let cost = if Uchar.equal s0.(i-1) s1.(j-1) then 0 else 1 in
178 let min = minimum
179 (row_minus1.(j-1) + cost) (* substitute *)
180 (row_minus1.(j) + 1) (* delete *)
181 (row.(j-1) + 1) (* insert *)
182 (* Note when j = i - limit, the latter [row] read makes a bogus read
183 on the value that was in the matrix at d.(i-2).(i - limit - 1).
184 Since by induction for all i,j, d.(i).(j) >= abs (i - j),
185 (row.(j-1) + 1) is greater or equal to [limit] and thus does
186 not affect adversely the minimum computation. *)
187 in
188 let min =
189 if (i > 1 && j > 1 &&
190 Uchar.equal s0.(i-1) s1.(j-2) &&
191 Uchar.equal s0.(i-2) s1.(j-1))
192 then Stdlib.Int.min min (row_minus2.(j-2) + cost) (* transpose *)
193 else min
194 in
195 row.(j) <- min;
196 row_min := Stdlib.Int.min !row_min min;
197 done;
198 if !row_min >= limit then (* can no longer decrease *) limit else
199 loop row_minus1 row row_minus2 (i + 1) len0 limit s0 s1
200 in
201 let ignore =
202 (* Value used to make the values around the diagonal stripe ignored
203 by the min computations when we have a limit. *)
204 limit + 1
205 in
206 let row_minus2 = Array.make (len1 + 1) ignore in
207 let row_minus1 = Array.init (len1 + 1) (fun x -> x) in
208 let row = Array.make (len1 + 1) ignore in
209 let d = loop row_minus2 row_minus1 row 1 len0 limit s0 s1 in
210 if d > limit then limit else d
211
212 (* string parsing *)
213
214 module Self =
215 struct
216 type t = string
217 let compare = compare
218 end
219
220 module Set = CSet.Make(Self)
221 module Map = CMap.Make(Self)
222 module Pred = Predicate.Make(Self)
223
224 module List = struct
225 type elt = string
226 let mem id l = List.exists (fun s -> equal id s) l
227 let assoc id l = CList.assoc_f equal id l
228 let remove_assoc id l = CList.remove_assoc_f equal id l
229 let mem_assoc id l = List.exists (fun (a,_) -> equal id a) l
230 let mem_assoc_sym id l = List.exists (fun (_,b) -> equal id b) l
231 let equal l l' = CList.equal equal l l'
232 end
233
234 module Hstring = Hashcons.Make(struct
235 type t = string
236 let hashcons s = hash s, s
237 let eq = String.equal
238 end)
239
240 let hcons = Hashcons.simple_hcons Hstring.generate Hstring.hcons ()