-
Notifications
You must be signed in to change notification settings - Fork 27
/
utils.ml
173 lines (147 loc) · 4.6 KB
/
utils.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
(****************************************************************
* Generic utility functions
*
* Copyright Arm Limited (c) 2017-2019
* SPDX-Licence-Identifier: BSD-3-Clause
****************************************************************)
(** Generic utility functions *)
(****************************************************************
* Pretty-printer related
****************************************************************)
let to_string (d: PPrintEngine.document): string =
let buf = Buffer.create 100 in
PPrintEngine.ToBuffer.compact buf d;
Buffer.contents buf
(****************************************************************
* List related
****************************************************************)
let nub (xs: 'a list): 'a list =
let rec nub_aux seen xs = (match xs with
| [] -> seen
| (y::ys) -> if List.mem y seen then nub_aux seen ys else nub_aux (y::seen) ys
) in
nub_aux [] xs
let zip_list (xs: 'a list) (ys: 'b list): ('a * 'b) list =
List.map2 (fun x y -> (x, y)) xs ys
let zipWithIndex (f: 'a -> int -> 'b) (xs: 'a list): 'b list =
let rec aux i xs = (match xs with
| [] -> []
| (y::ys) -> f y i :: aux (i+1) ys
) in
aux 0 xs
(****************************************************************
* Option related
****************************************************************)
let isNone (ox : 'a option): bool =
(match ox with
| None -> true
| Some _ -> false
)
let map_option (f: 'a -> 'b) (ox: 'a option): 'b option =
(match ox with
| None -> None
| Some x -> Some (f x)
)
let get_option (ox: 'a option): 'a =
(match ox with
| None -> raise Not_found
| Some x -> x
)
let from_option (ox: 'a option) (d: unit -> 'a): 'a =
(match ox with
| None -> d()
| Some x -> x
)
let bind_option (ox: 'a option) (f: 'a -> 'b option): 'b option =
(match ox with
| None -> None
| Some x -> f x
)
let orelse_option (ox: 'a option) (f: unit -> 'a option): 'a option =
(match ox with
| None -> f()
| Some x -> ox
)
let rec concat_option (oss: (('a list) option) list): ('a list) option =
(match oss with
| [] -> Some []
| None::_ -> None
| (Some xs)::xss -> map_option (List.append xs) (concat_option xss)
)
(* extract all non-None elements from a list *)
let flatten_option (os: ('a option) list): 'a list =
let rec aux r os = (match os with
| [] -> List.rev r
| Some o :: os' -> aux (o::r) os'
| None :: os' -> aux r os'
)
in
aux [] os
(* extract all non-None elements from a list *)
let flatmap_option (f: 'a -> 'b option) (xs: 'a list): 'b list =
let rec aux r xs = (match xs with
| [] -> List.rev r
| x :: xs' ->
(match f x with
| Some b -> aux (b::r) xs'
| None -> aux r xs'
)
)
in
aux [] xs
(* todo: give this a better name *)
let flatten_map_option (f: 'a -> 'b option) (xs: 'a list): 'b list option =
let rec aux r xs = (match xs with
| [] -> Some (List.rev r)
| x :: xs' ->
(match f x with
| Some b -> aux (b::r) xs'
| None -> None
)
)
in
aux [] xs
(* find first non-None result from function 'f' on list 'xs' *)
let rec first_option (f: 'a -> 'b option) (xs: 'a list): 'b option =
(match xs with
| [] -> None
| x :: xs' ->
(match f x with
| Some b -> Some b
| None -> first_option f xs'
)
)
(****************************************************************
* String related
****************************************************************)
(** Test whether 'x' starts with (is prefixed by) 'y' *)
let startswith (x: string) (y: string): bool =
let lx = String.length x in
let ly = String.length y in
if lx < ly then begin
false
end else begin
let head = String.sub x 0 ly in
String.equal head y
end
(** Test whether 'x' ends with (is suffixed by) 'y' *)
let endswith (x: string) (y: string): bool =
let lx = String.length x in
let ly = String.length y in
if lx < ly then begin
false
end else begin
let tail = String.sub x (lx - ly) ly in
String.equal tail y
end
(** Drop first n characters from string *)
let stringDrop (n: int) (s: string): string =
let l = String.length s in
if n > l then begin
""
end else begin
String.sub s n (l-n)
end
(****************************************************************
* End
****************************************************************)