Skip to content

Commit

Permalink
fix fd leak and optimize Eol_detect.get_eol_for_file
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Feb 9, 2025
1 parent 90e866a commit 9ea2468
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/refmt/eol_detect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ let default_eol = match Sys.win32 with true -> CRLF | _ -> LF

let get_eol_for_file filename =
let ic = open_in_bin filename in
let line = ref "" in
let c = ref ' ' in
let prev = ref None in
try
while !c <> '\n' do
prev := Some !c;
c := input_char ic;
line := !line ^ String.make 1 !c
done;
match !prev with None -> default_eol | Some '\r' -> CRLF | Some _ -> LF
with
| End_of_file ->
close_in ic;
default_eol
let rec loop prev =
match input_char ic with
| '\n' -> if prev = Some '\r' then CRLF else LF
| c -> loop (Some c)
in
let eol = try loop None with End_of_file -> default_eol in
close_in ic;
eol

0 comments on commit 9ea2468

Please # to comment.