Skip to content
This repository has been archived by the owner on Apr 24, 2021. It is now read-only.

Commit

Permalink
Faster readfile
Browse files Browse the repository at this point in the history
Probably good if we're reading a lot
  • Loading branch information
chenglou committed Apr 13, 2021
1 parent 7ff2eb3 commit 528cc07
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/Files.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,13 @@ let maybeStat path =
let getMtime path =
match maybeStat path with Some {Unix.st_mtime} -> Some st_mtime | _ -> None

let readFile path =
match maybeStat path with
| Some {Unix.st_kind = Unix.S_REG} ->
let ic = open_in path in
let try_read () =
match input_line ic with exception End_of_file -> None | x -> Some x
in
let rec loop acc =
match try_read () with
| Some s -> loop (s :: acc)
| None ->
close_in ic;
List.rev acc
in
let text = loop [] |> String.concat (String.make 1 '\n') in
Some text
let readFile ~filename =
try
let chan = open_in filename in
let content = really_input_string chan (in_channel_length chan) in
close_in_noerr chan;
Some content
with
| _ -> None

let exists path = match maybeStat path with None -> false | Some _ -> true
Expand Down

0 comments on commit 528cc07

Please sign in to comment.