Source file alcotest_stdlib_ext.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
let ( >> ) f g x = x |> f |> g
module Int = struct
module Set = Set.Make (Int)
end
module String = struct
include Astring.String
let fold_utf8 f acc s =
let rec go acc i =
if i >= String.length s then acc
else
let d = String.get_utf_8_uchar s i in
go (f acc i d) (i + Uchar.utf_decode_length d)
in
go acc 0
let length_utf8 = fold_utf8 (fun count _ _ -> count + 1) 0
let prefix_utf8 uchars string =
let exception Found_new_length of int in
try
let (_ : int) =
fold_utf8
(fun count start_pos _ ->
if count = uchars then raise (Found_new_length start_pos)
else count + 1)
0 string
in
string
with Found_new_length l -> String.sub string 0 l
end
module List = struct
include List
type 'a t = 'a list
let rev_head n l =
let rec aux acc n l =
match l with
| x :: xs ->
if n > 0 then (aux [@tailcall]) (x :: acc) (n - 1) xs else acc
| [] -> acc
in
aux [] n l
let lift_result l =
List.fold_right
(fun a b ->
match (a, b) with
| Ok o, Ok acc -> Ok (o :: acc)
| Ok _, Error e -> Error e
| Error e, Error acc -> Error (e :: acc)
| Error e, Ok _ -> Error [ e ])
l (Ok [])
end
module Option = struct
include Option
let ( || ) a b =
match (a, b) with
| None, None -> None
| (Some _ as x), _ | None, (Some _ as x) -> x
end
module Path = struct
let ancestors ~sep path =
let is_win_drive_letter x =
String.length x = 2 && x.[1] = ':' && Astring.Char.Ascii.is_letter x.[0]
in
let rec mk parent = function
| [] -> []
| name :: names ->
let path = parent ^ sep ^ name in
path :: mk path names
in
match String.cuts ~empty:true ~sep path with
| "" :: xs -> mk "" xs
| dl :: xs when is_win_drive_letter dl -> mk dl xs
| xs -> mk "." xs
end
module Cmdliner_syntax = struct
open Cmdliner
let ( let+ ) t f = Term.(const f $ t)
let ( and+ ) a b = Term.(const (fun x y -> (x, y)) $ a $ b)
let ( >>| ) x f = Term.(app (const f) x)
end