diff options
author | alainfrisch <alain@frisch.fr> | 2017-10-04 16:49:42 +0200 |
---|---|---|
committer | alainfrisch <alain@frisch.fr> | 2017-10-04 16:49:42 +0200 |
commit | 35e15f60294ea2120cb8938aee65aff0ea386165 (patch) | |
tree | 395d24cc475e82bb6e96c8fd15001d75e1618f4a | |
parent | 0e19f896ab6c074865d87ffd5464c81b6216eff1 (diff) | |
download | ocaml-35e15f60294ea2120cb8938aee65aff0ea386165.tar.gz |
Avoid non-tail recursion when loading files in Arg.read_arg.improve_arg_read_arg
-rw-r--r-- | stdlib/arg.ml | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/stdlib/arg.ml b/stdlib/arg.ml index e315e51121..eb06f795cf 100644 --- a/stdlib/arg.ml +++ b/stdlib/arg.ml @@ -369,23 +369,19 @@ let read_aux trim sep file = let buf = Buffer.create 200 in let words = ref [] in let stash () = - let word = (Buffer.contents buf) in + let word = Buffer.contents buf in let word = if trim then trim_cr word else word in words := word :: !words; Buffer.clear buf in - let rec read () = - try - let c = input_char ic in - if c = sep then begin - stash (); read () - end else begin - Buffer.add_char buf c; read () - end - with End_of_file -> - if Buffer.length buf > 0 then - stash () in - read (); + begin + try while true do + let c = input_char ic in + if c = sep then stash () else Buffer.add_char buf c + done + with End_of_file -> () + end; + if Buffer.length buf > 0 then stash (); close_in ic; Array.of_list (List.rev !words) |