Skip to content

Commit 5d3a1df

Browse files
committed
Cleaned up discover code
1 parent 9975b2b commit 5d3a1df

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/config/discover.ml

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
let exn_protect ~finally x ~f =
2-
try
3-
let y = f x in
4-
finally x;
5-
y
6-
with e ->
7-
finally x;
8-
raise e
1+
exception Finally_raised of exn
2+
3+
let protect ~(finally : unit -> unit) work =
4+
let finally_no_exn () =
5+
try finally () with e ->
6+
let bt = Printexc.get_raw_backtrace () in
7+
Printexc.raise_with_backtrace (Finally_raised e) bt
8+
in
9+
match work () with
10+
| result -> finally_no_exn () ; result
11+
| exception work_exn ->
12+
let work_bt = Printexc.get_raw_backtrace () in
13+
finally_no_exn () ;
14+
Printexc.raise_with_backtrace work_exn work_bt
915

1016
let read_lines_from_cmd ~max_lines cmd =
1117
let ic =
@@ -14,29 +20,29 @@ let read_lines_from_cmd ~max_lines cmd =
1420
Printf.eprintf "read_lines_from_cmd: could not open cmd: '%s'" cmd;
1521
raise exc
1622
in
17-
exn_protect ic ~finally:close_in_noerr ~f:(fun ic ->
23+
protect ~finally:(fun () -> close_in_noerr ic) (fun () ->
1824
let rec loop n lines =
1925
if n <= 0 then List.rev lines
2026
else
2127
match input_line ic with
2228
| line -> loop (n - 1) (line :: lines)
2329
| exception _ ->
24-
Printf.eprintf "read_lines_from_cmd: failed reading line %d, cmd: '%s'"
30+
Printf.eprintf
31+
"read_lines_from_cmd: failed reading line %d, cmd: '%s'"
2532
(max_lines - n + 1) cmd;
2633
raise End_of_file
2734
in
2835
loop max_lines [])
2936

30-
let opt_map ~default ~f x = match x with
37+
let opt_map ~default ~f = function
3138
| Some y -> f y
3239
| None -> default
40+
3341
let opt_is_some = function Some _ -> true | _ -> false
3442
let getenv_opt s = try Some (Sys.getenv s) with _ -> None
3543

3644
let pkg_export =
37-
let has_brewcheck =
38-
opt_is_some (getenv_opt "SQLITE3_OCAML_BREWCHECK")
39-
in
45+
let has_brewcheck = opt_is_some (getenv_opt "SQLITE3_OCAML_BREWCHECK") in
4046
if not has_brewcheck then ""
4147
else
4248
let cmd = "brew ls sqlite | grep pkgconfig" in
@@ -47,17 +53,18 @@ let pkg_export =
4753
| _ -> ""
4854

4955
let split_ws str =
50-
let l = ref [] in
56+
let lst = ref [] in
5157
let i = ref 0 in
52-
while !i < String.length str do
53-
let j = String.index_from str !i ' ' in
54-
if !i=j then incr i
55-
else (
56-
l := String.sub str !i (j- !i) :: !l;
57-
i := j+1;
58-
)
58+
let len = String.length str in
59+
while !i < len do
60+
let j = try String.index_from str !i ' ' with Not_found -> len in
61+
if !i = j then incr i
62+
else begin
63+
lst := String.sub str !i (j - !i) :: !lst;
64+
i := j + 1;
65+
end
5966
done;
60-
List.rev !l
67+
List.rev !lst
6168

6269
let () =
6370
let module C = Configurator.V1 in

0 commit comments

Comments
 (0)