Source file opamCompat.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
module Gc = struct
[@@@warning "-32"]
let ramp_up f = (f (), ())
include Gc
let ramp_up f = fst (ramp_up f)
end
module String = struct
[@@@warning "-32"]
(** NOTE: OCaml >= 4.13 *)
let exists p s =
let n = String.length s in
let rec loop i =
if i = n then false
else if p (String.unsafe_get s i) then true
else loop (succ i) in
loop 0
(** NOTE: OCaml >= 4.13 *)
let starts_with ~prefix s =
let x = String.length prefix in
let n = String.length s in
n >= x &&
let rec chk i = i >= x || prefix.[i] = s.[i] && chk (i+1) in
chk 0
(** NOTE: OCaml >= 4.13 *)
let ends_with ~suffix s =
let x = String.length suffix in
let n = String.length s in
n >= x &&
let rec chk i = i >= x || suffix.[i] = s.[i+n-x] && chk (i+1) in
chk 0
(** NOTE: OCaml >= 4.13 *)
let for_all f s =
let len = String.length s in
let rec aux i = i >= len || f s.[i] && aux (i+1) in
aux 0
(** NOTE: OCaml >= 4.13 *)
let fold_left f acc s =
let acc = ref acc in
for i = 0 to String.length s - 1 do acc := f !acc s.[i] done;
!acc
include Stdlib.String
end
module Seq = struct
[@@@warning "-32"]
(** NOTE: OCaml >= 4.14 *)
let rec find_map f xs =
match xs() with
| Seq.Nil ->
None
| Seq.Cons (x, xs) ->
match f x with
| None ->
find_map f xs
| Some _ as result ->
result
(** NOTE: OCaml >= 4.14 *)
let to_dispenser seq =
let r = ref seq in
fun () ->
match !r () with
| Seq.Nil -> None
| Seq.Cons (x, xs) -> r := xs; Some x
include Seq
end
module Either = struct
(** NOTE: OCaml >= 4.12 *)
type ('a, 'b) t =
| Left of 'a
| Right of 'b
end
module Unix = struct
[@@@warning "-32"]
(** NOTE: OCaml >= 4.13 *)
let realpath s =
let orig_cwd = Sys.getcwd () in
Unix.chdir s;
let r = Unix.getcwd () in
Sys.chdir orig_cwd;
r
include Unix
end
module Lazy = struct
[@@@warning "-32"]
(** NOTE: OCaml >= 4.13 *)
let map f x =
lazy (f (Lazy.force x))
(** NOTE: OCaml >= 4.13 *)
let map_val f x =
if Lazy.is_val x
then Lazy.from_val (f (Lazy.force x))
else lazy (f (Lazy.force x))
include Stdlib.Lazy
end
module List = struct
[@@@warning "-32"]
let rec equal eq x y = match x, y with
| [], [] -> true
| [], _::_ | _::_, [] -> false
| x::_, y::_ when eq x y -> true
| _::xs, _::ys -> equal eq xs ys
include Stdlib.List
end
module type MAP = sig
include Stdlib.Map.S
(** NOTE: OCaml >= 5.1 *)
val add_to_list: key -> 'a -> 'a list t -> 'a list t
end
module Map(Ord : Stdlib.Map.OrderedType) = struct
[@@@warning "-32"]
module M = Stdlib.Map.Make(Ord)
(** NOTE: OCaml >= 5.1 *)
let add_to_list x data m =
let add = function None -> Some [data] | Some l -> Some (data :: l) in
M.update x add m
include M
end
module Pair = struct
(** NOTE: OCaml >= 5.4 *)
let equal eq1 eq2 (x1, y1) (x2, y2) =
eq1 x1 x2 && eq2 y1 y2
end
module Int = struct
[@@@warning "-32"]
let min : int -> int -> int = Stdlib.min
include Stdlib.Int
end
module Sys = struct
[@@@warning "-32"]
let sigwinch = 28
include Stdlib.Sys
end