logo       
Google Custom Search
    AddThis Social Bookmark Button

Better "input_all": msg#00002

Subject: Better "input_all"
Hi,

function "input_all" in module Std is obviously not thread-safe due to
the use of a static buffer. Furthermore, it is not optimally efficient.
I'd propose the following patch:

Index: std.ml
===================================================================
RCS file: /cvsroot/ocaml-lib/extlib-dev/std.ml,v
retrieving revision 1.6
diff -r1.6 std.ml
43d42
< let static_buf = String.create buf_len
45,54c44,64
< let input_all ch =
<       let buf = Buffer.create 0 in
<       let rec loop() =
<               match input ch static_buf 0 buf_len with
<               | 0 -> Buffer.contents buf
<               | len ->
<                       Buffer.add_substring buf static_buf 0 len;
<                       loop()
<       in
<       loop()
---
> let input_all ic =
>   let rec loop acc total buf ofs =
>     let n = input ic buf ofs (buf_len - ofs) in
>     if n = 0 then
>       let res = String.create total in
>       let pos = total - ofs in
>       let _ = String.blit buf 0 res pos ofs in
>       let coll pos buf =
>         let new_pos = pos - buf_len in
>         String.blit buf 0 res new_pos buf_len;
>         new_pos in
>       let _ = List.fold_left coll pos acc in
>       res
>     else
>       let new_ofs = ofs + n in
>       let new_total = total + n in
>       if new_ofs = buf_len then
>         loop (buf :: acc) new_total (String.create buf_len) 0
>       else
>         loop acc new_total buf new_ofs in
>   loop [] 0 (String.create buf_len) 0

Regards,
Markus

-- 
Markus Mottl          http://www.oefai.at/~markus          markus@xxxxxxxx


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf



Try Searching:
servers, voip, java, networking, microsoft ...
<Prev in Thread] Current Thread [Next in Thread>