Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] (Sorry for my last post) lazyness, exceptions?, ocaml syntax rule-of-thumbs
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: malc <malc@p...>
Subject: Re: [Caml-list] (Sorry for my last post) lazyness, exceptions?, ocaml syntax rule-of-thumbs
On Fri, 27 Sep 2002, Maxence Guesdon wrote:

> If you want to read a whole file as a string, you can use the following function:
> 
> let input_file_as_string file =
>   let chanin = open_in_bin file in
>   let len = 1024 in
>   let s = String.create len in
>   let buf = Buffer.create len in
>   let rec iter () =
>     try
>       let n = input chanin s 0 len in
>       if n = 0 then
>         ()
>       else
>         (
>          Buffer.add_substring buf s 0 n;
>          iter ()
>         )
>     with
>       End_of_file -> ()
>   in
>   iter ();
>   close_in chanin;
>   Buffer.contents buf
> 

I might be way off here, but shouldnt it be:

let input_file_as_string file =
  let chanin = open_in_bin file in
  let len = in_channel_length chanin in
  let s = String.create len in
  really_input chanin s 0 (pred len);
  close_in chanin;
  s

Even if your variant ran faster(and it doesnt), it suffers from
Buffers resizing policy, meaning it will bail out sooner than
String's maximal capacity is reached.

-- 
mailto:malc@pulsesoft.com

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners