From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Joel Reymont <joelr1@gmail.com>
Cc: caml-list <caml-list@inria.fr>
Subject: AW: [Caml-list] input_line for zero-terminated strings
Date: Thu, 07 Jun 2012 21:54:52 +0200 [thread overview]
Message-ID: <1339098892.4950.7@samsung> (raw)
In-Reply-To: <CAOQwdX1HvL8E_G7R05UkkHUro78jdtWirg074Z_zaSbO0rCkJg@mail.gmail.com> (from joelr1@gmail.com on Thu Jun 7 19:37:08 2012)
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
------------------------------------------------------------
prev parent reply other threads:[~2012-06-07 19:54 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-07 17:37 Joel Reymont
2012-06-07 19:54 ` Gerd Stolpmann [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1339098892.4950.7@samsung \
--to=info@gerd-stolpmann.de \
--cc=caml-list@inria.fr \
--cc=joelr1@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox