1Misc(3) OCaml library Misc(3)
2
3
4
6 Misc - Miscellaneous useful types and functions
7
9 Module Misc
10
12 Module Misc
13 : sig end
14
15
16 Miscellaneous useful types and functions
17
18 Warning: this module is unstable and part of Compiler_libs .
19
20
21
22
23
24
25 val fatal_error : string -> 'a
26
27
28
29
30 val fatal_errorf : ('a, Format.formatter, unit, 'b) format4 -> 'a
31
32
33
34
35 exception Fatal_error
36
37
38
39
40
41 val try_finally : ?always:(unit -> unit) -> ?exceptionally:(unit ->
42 unit) -> (unit -> 'a) -> 'a
43
44
45 try_finally work ~always ~exceptionally is designed to run code in work
46 that may fail with an exception, and has two kind of cleanup routines:
47 always , that must be run after any execution of the function (typi‐
48 cally, freeing system resources), and exceptionally , that should be
49 run only if work or always failed with an exception (typically, undoing
50 user-visible state changes that would only make sense if the function
51 completes correctly). For example:
52
53
54 let objfile = outputprefix ^ ".cmo" in
55 let oc = open_out_bin objfile in
56 Misc.try_finally
57 (fun () ->
58 bytecode
59 ++ Timings.(accumulate_time (Generate sourcefile))
60 (Emitcode.to_file oc modulename objfile);
61 Warnings.check_fatal ())
62 ~always:(fun () -> close_out oc)
63 ~exceptionally:(fun _exn -> remove_file objfile);
64
65
66 If exceptionally fail with an exception, it is propagated as usual.
67
68 If always or exceptionally use exceptions internally for control-flow
69 but do not raise, then try_finally is careful to preserve any exception
70 backtrace coming from work or always for easier debugging.
71
72
73
74 val map_end : ('a -> 'b) -> 'a list -> 'b list -> 'b list
75
76
77
78
79 val map_left_right : ('a -> 'b) -> 'a list -> 'b list
80
81
82
83
84 val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
85
86
87
88
89 val replicate_list : 'a -> int -> 'a list
90
91
92
93
94 val list_remove : 'a -> 'a list -> 'a list
95
96
97
98
99 val split_last : 'a list -> 'a list * 'a
100
101
102
103 type ref_and_value =
104 | R : 'a ref * 'a -> ref_and_value
105
106
107
108
109
110 val protect_refs : ref_and_value list -> (unit -> 'a) -> 'a
111
112
113 protect_refs l f temporarily sets r to v for each R (r, v) in l while
114 executing f . The previous contents of the references is restored even
115 if f raises an exception.
116
117
118 module Stdlib : sig end
119
120
121
122
123
124 val find_in_path : string list -> string -> string
125
126
127
128
129 val find_in_path_rel : string list -> string -> string
130
131
132
133
134 val find_in_path_uncap : string list -> string -> string
135
136
137
138
139 val remove_file : string -> unit
140
141
142
143
144 val expand_directory : string -> string -> string
145
146
147
148
149 val split_path_contents : ?sep:char -> string -> string list
150
151
152
153
154 val create_hashtable : int -> ('a * 'b) list -> ('a, 'b) Hashtbl.t
155
156
157
158
159 val copy_file : in_channel -> out_channel -> unit
160
161
162
163
164 val copy_file_chunk : in_channel -> out_channel -> int -> unit
165
166
167
168
169 val string_of_file : in_channel -> string
170
171
172
173
174 val output_to_file_via_temporary : ?mode:open_flag list -> string ->
175 (string -> out_channel -> 'a) -> 'a
176
177
178
179
180 val protect_writing_to_file : filename:string -> f:(out_channel -> 'a)
181 -> 'a
182
183 Open the given filename for writing (in binary mode), pass the
184 out_channel to the given function, then close the channel. If the func‐
185 tion raises an exception then filename will be removed.
186
187
188
189 val log2 : int -> int
190
191
192
193
194 val align : int -> int -> int
195
196
197
198
199 val no_overflow_add : int -> int -> bool
200
201
202
203
204 val no_overflow_sub : int -> int -> bool
205
206
207
208
209 val no_overflow_mul : int -> int -> bool
210
211
212
213
214 val no_overflow_lsl : int -> int -> bool
215
216
217
218 module Int_literal_converter : sig end
219
220
221
222
223
224 val chop_extensions : string -> string
225
226
227
228
229 val search_substring : string -> string -> int -> int
230
231
232
233
234 val replace_substring : before:string -> after:string -> string ->
235 string
236
237
238
239
240 val rev_split_words : string -> string list
241
242
243
244
245 val get_ref : 'a list ref -> 'a list
246
247
248
249
250 val set_or_ignore : ('a -> 'b option) -> 'b option ref -> 'a -> unit
251
252
253
254
255 val fst3 : 'a * 'b * 'c -> 'a
256
257
258
259
260 val snd3 : 'a * 'b * 'c -> 'b
261
262
263
264
265 val thd3 : 'a * 'b * 'c -> 'c
266
267
268
269
270 val fst4 : 'a * 'b * 'c * 'd -> 'a
271
272
273
274
275 val snd4 : 'a * 'b * 'c * 'd -> 'b
276
277
278
279
280 val thd4 : 'a * 'b * 'c * 'd -> 'c
281
282
283
284
285 val for4 : 'a * 'b * 'c * 'd -> 'd
286
287
288
289 module LongString : sig end
290
291
292
293
294
295 val edit_distance : string -> string -> int -> int option
296
297
298 edit_distance a b cutoff computes the edit distance between strings a
299 and b . To help efficiency, it uses a cutoff: if the distance d is
300 smaller than cutoff , it returns Some d , else None .
301
302 The distance algorithm currently used is Damerau-Levenshtein: it com‐
303 putes the number of insertion, deletion, substitution of letters, or
304 swapping of adjacent letters to go from one word to the other. The par‐
305 ticular algorithm may change in the future.
306
307
308
309 val spellcheck : string list -> string -> string list
310
311
312 spellcheck env name takes a list of names env that exist in the current
313 environment and an erroneous name , and returns a list of suggestions
314 taken from env , that are close enough to name that it may be a typo
315 for one of them.
316
317
318
319 val did_you_mean : Format.formatter -> (unit -> string list) -> unit
320
321
322 did_you_mean ppf get_choices hints that the user may have meant one of
323 the option returned by calling get_choices . It does nothing if the
324 returned list is empty.
325
326 The unit -> ... thunking is meant to delay any potentially-slow compu‐
327 tation (typically computing edit-distance with many things from the
328 current environment) to when the hint message is to be printed. You
329 should print an understandable error message before calling
330 did_you_mean , so that users get a clear notification of the failure
331 even if producing the hint is slow.
332
333
334
335 val cut_at : string -> char -> string * string
336
337
338 String.cut_at s c returns a pair containing the sub-string before the
339 first occurrence of c in s , and the sub-string after the first occur‐
340 rence of c in s . let (before, after) = String.cut_at s c in
341 before ^ String.make 1 c ^ after is the identity if s contains c .
342
343 Raise Not_found if the character does not appear in the string
344
345
346 Since 4.01
347
348
349 module Color : sig end
350
351
352
353
354 module Error_style : sig end
355
356
357
358
359
360 val normalise_eol : string -> string
361
362
363 normalise_eol s returns a fresh copy of s with any '\r' characters
364 removed. Intended for pre-processing text which will subsequently be
365 printed on a channel which performs EOL transformations (i.e. Windows)
366
367
368
369 val delete_eol_spaces : string -> string
370
371
372 delete_eol_spaces s returns a fresh copy of s with any end of line spa‐
373 ces removed. Intended to normalize the output of the toplevel for
374 tests.
375
376
377
378 val pp_two_columns : ?sep:string -> ?max_lines:int -> Format.formatter
379 -> (string * string) list -> unit
380
381
382 pp_two_columns ?sep ?max_lines ppf l prints the lines in l as two col‐
383 umns separated by sep ("|" by default). max_lines can be used to indi‐
384 cate a maximum number of lines to print -- an ellipsis gets inserted at
385 the middle if the input has too many lines.
386
387 Example:
388
389 pp_two_columns ~max_lines:3 Format.std_formatter [ "abc", "hello";
390 "def", "zzz"; "a" , "bllbl"; "bb" , "dddddd"; ]
391
392 prints
393
394 abc | hello ... bb | dddddd
395
396
397
398
399 val show_config_and_exit : unit -> unit
400
401 configuration variables
402
403
404
405 val show_config_variable_and_exit : string -> unit
406
407
408
409
410 val get_build_path_prefix_map : unit -> Build_path_prefix_map.map
411 option
412
413 Returns the map encoded in the BUILD_PATH_PREFIX_MAP environment vari‐
414 able.
415
416
417
418 val debug_prefix_map_flags : unit -> string list
419
420 Returns the list of --debug-prefix-map flags to be passed to the assem‐
421 bler, built from the BUILD_PATH_PREFIX_MAP environment variable.
422
423
424
425 val print_if : Format.formatter -> bool ref -> (Format.formatter -> 'a
426 -> unit) -> 'a -> 'a
427
428
429 print_if ppf flag fmt x prints x with fmt on ppf if b is true.
430
431
432 type filepath = string
433
434
435
436
437 type modname = string
438
439
440
441
442 type crcs = (modname * Digest.t option) list
443
444
445
446
447 type alerts = string Stdlib.String.Map.t
448
449
450
451
452 module EnvLazy : sig end
453
454
455
456
457
458
459
460OCamldoc 2020-02-27 Misc(3)