summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Scherer <gabriel.scherer@gmail.com>2017-10-04 23:21:49 +0200
committerGitHub <noreply@github.com>2017-10-04 23:21:49 +0200
commit302738f5e6d2933f39e5d871e1ac43b0ac5a1728 (patch)
tree0ab9161a20faf302bba1d8f5be1aea41baeb2407
parent74ccacd80e05d737455e1f5e3222ea5ca0a0ccc3 (diff)
parent35e15f60294ea2120cb8938aee65aff0ea386165 (diff)
downloadocaml-302738f5e6d2933f39e5d871e1ac43b0ac5a1728.tar.gz
Merge pull request #1396 from ocaml/improve_arg_read_arg
Avoid non-tail recursion in Arg.read_arg
-rw-r--r--stdlib/arg.ml22
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)