* [Caml-list] input_line for zero-terminated strings
@ 2012-06-07 17:37 Joel Reymont
2012-06-07 19:54 ` AW: " Gerd Stolpmann
0 siblings, 1 reply; 2+ messages in thread
From: Joel Reymont @ 2012-06-07 17:37 UTC (permalink / raw)
To: caml-list
I'm trying to read C-style strings terminated with \0.
Does Pervasives.input_line provide the optimal implementation for me to clone?
Thanks, Joel
---
let input_line ic =
let buf = ref (string_create 128) in
let pos = ref 0 in
begin try
while true do
if !pos = string_length !buf then begin
let newbuf = string_create (2 * !pos) in
string_blit !buf 0 newbuf 0 !pos;
buf := newbuf
end;
let c = input_char ic in
if c = '\n' then raise Exit;
!buf.[!pos] <- c;
incr pos
done
with Exit -> ()
| End_of_file -> if !pos = 0 then raise End_of_file
end;
let res = string_create !pos in
string_blit !buf 0 res 0 !pos;
res
--------------------------------------------------------------------------
AlgoKit: EasyLanguage trading strategies, on the server, w/ Rithmic R|API
---------------------+------------+---------------------------------------
http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
---------------------+------------+---------------------------------------
^ permalink raw reply [flat|nested] 2+ messages in thread
* AW: [Caml-list] input_line for zero-terminated strings
2012-06-07 17:37 [Caml-list] input_line for zero-terminated strings Joel Reymont
@ 2012-06-07 19:54 ` Gerd Stolpmann
0 siblings, 0 replies; 2+ messages in thread
From: Gerd Stolpmann @ 2012-06-07 19:54 UTC (permalink / raw)
To: Joel Reymont; +Cc: caml-list
Am 07.06.2012 19:37:08 schrieb(en) Joel Reymont:
> I'm trying to read C-style strings terminated with \0.
>
> Does Pervasives.input_line provide the optimal implementation for me
> to clone?
Algorithmically, yes. I'd replace the manual buffer management (for
buf) with a Buffer.t, though, because this is exactly what is done
inside Buffer, and with inlining, the performance should be the same.
(I guess Buffer can simply not be called from Pervasives, because it is
defined later.)
The input_char is probably non-optimal here, because it is a C call,
and cannot be inlined. If you afford to have your own input buffering
this will even be better (I say "afford" because you will finally read
beyond the last 0-terminated line, so do this only when you read until
EOF).
Gerd
> Thanks, Joel
>
> ---
> let input_line ic =
> let buf = ref (string_create 128) in
> let pos = ref 0 in
> begin try
> while true do
> if !pos = string_length !buf then begin
> let newbuf = string_create (2 * !pos) in
> string_blit !buf 0 newbuf 0 !pos;
> buf := newbuf
> end;
> let c = input_char ic in
> if c = '\n' then raise Exit;
> !buf.[!pos] <- c;
> incr pos
> done
> with Exit -> ()
> | End_of_file -> if !pos = 0 then raise End_of_file
> end;
> let res = string_create !pos in
> string_blit !buf 0 res 0 !pos;
> res
>
> --------------------------------------------------------------------------
> AlgoKit: EasyLanguage trading strategies, on the server, w/ Rithmic
> R|API
> ---------------------+------------+---------------------------------------
> http://wagerlabs.com | @wagerlabs |
> http://www.linkedin.com/in/joelreymont
> ---------------------+------------+---------------------------------------
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
>
--
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de
Creator of GODI and camlcity.org.
Contact details: http://www.camlcity.org/contact.html
Company homepage: http://www.gerd-stolpmann.de
------------------------------------------------------------
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2012-06-07 19:54 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-07 17:37 [Caml-list] input_line for zero-terminated strings Joel Reymont
2012-06-07 19:54 ` AW: " Gerd Stolpmann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox