From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by sympa.inria.fr (Postfix) with ESMTPS id A84577ED26 for ; Thu, 7 Jun 2012 21:54:56 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhwDAOcF0U/U4xEIk2dsb2JhbABFo1KQXiIBAQEBCQkLCRQDJIIYAQEEAScTNAsFO10JEgYTCQmHawMGCQevPgOJWBSLCYYAA40miQmIEYk5 X-IronPort-AV: E=Sophos;i="4.75,732,1330902000"; d="scan'208";a="161825896" Received: from moutng.kundenserver.de ([212.227.17.8]) by mail1-smtp-roc.national.inria.fr with ESMTP; 07 Jun 2012 21:54:56 +0200 Received: from office1.lan.sumadev.de (dslb-094-219-220-051.pools.arcor-ip.net [94.219.220.51]) by mrelayeu.kundenserver.de (node=mreu3) with ESMTP (Nemesis) id 0MeYyR-1SKNvd431r-00QMxC; Thu, 07 Jun 2012 21:54:55 +0200 Received: from samsung (dslb-178-004-232-062.pools.arcor-ip.net [178.4.232.62]) by office1.lan.sumadev.de (Postfix) with ESMTPSA id D25D1C00D1; Thu, 7 Jun 2012 21:54:53 +0200 (CEST) Date: Thu, 07 Jun 2012 21:54:52 +0200 From: Gerd Stolpmann To: Joel Reymont Cc: caml-list In-Reply-To: (from joelr1@gmail.com on Thu Jun 7 19:37:08 2012) X-Mailer: Balsa 2.4.11 Message-Id: <1339098892.4950.7@samsung> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; DelSp=Yes; Format=Flowed Content-Disposition: inline Content-Transfer-Encoding: quoted-printable X-Provags-ID: V02:K0:djRSZ++2Hqa4yxFC7XVl+AgzhR+bGHJaoRlOMji7JYg ibr1MrtMsFE4TXP7VKRr89PXoDQO1lGeJ2X+wMNkFvilhWgXu9 FlJby5zlc/yr8dUlrgyO7BpfD68XRlENfL/Vpa/2f9tmlUxGSb Y6kZnaCNU2x+mcJQEz8LgC98TVHfc1RNpX+L3KIVfWD8S94d3+ IFkh5hewRYQRR2YiRKvU42ogktm2ra6UI2Ieb9PfG9bksPK+uv Fhfoq94SqL2hcbp6tICrPI7IhiUKIvncFM/GplErgGNHt/sHvw s/ag55SPLkjy7GDERfYyxVGl9tCxVEt5uEE05VMnhj5A4DLy6J gHkYHAUt8bIQKRns794U= Subject: AW: [Caml-list] input_line for zero-terminated strings Am 07.06.2012 19:37:08 schrieb(en) Joel Reymont: > I'm trying to read C-style strings terminated with \0. >=20 > Does Pervasives.input_line provide the optimal implementation for me=20= =20 > to clone? Algorithmically, yes. I'd replace the manual buffer management (for=20=20 buf) with a Buffer.t, though, because this is exactly what is done=20=20 inside Buffer, and with inlining, the performance should be the same.=20=20 (I guess Buffer can simply not be called from Pervasives, because it is=20= =20 defined later.) The input_char is probably non-optimal here, because it is a C call,=20=20 and cannot be inlined. If you afford to have your own input buffering=20=20 this will even be better (I say "afford" because you will finally read=20= =20 beyond the last 0-terminated line, so do this only when you read until=20= =20 EOF). Gerd > Thanks, Joel >=20 > --- > let input_line ic =3D > let buf =3D ref (string_create 128) in > let pos =3D ref 0 in > begin try > while true do > if !pos =3D string_length !buf then begin > let newbuf =3D string_create (2 * !pos) in > string_blit !buf 0 newbuf 0 !pos; > buf :=3D newbuf > end; > let c =3D input_char ic in > if c =3D '\n' then raise Exit; > !buf.[!pos] <- c; > incr pos > done > with Exit -> () > | End_of_file -> if !pos =3D 0 then raise End_of_file > end; > let res =3D string_create !pos in > string_blit !buf 0 res 0 !pos; > res >=20 > -------------------------------------------------------------------------- > AlgoKit: EasyLanguage trading strategies, on the server, w/ Rithmic=20=20 > R|API > ---------------------+------------+--------------------------------------- > http://wagerlabs.com | @wagerlabs |=20=20 > http://www.linkedin.com/in/joelreymont > ---------------------+------------+--------------------------------------- >=20 > -- > 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 >=20 >=20 >=20 --=20 ------------------------------------------------------------ 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 ------------------------------------------------------------=