From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 45B7A7EE99 for ; Mon, 6 Jan 2014 11:32:27 +0100 (CET) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of john@coherentgraphics.co.uk) identity=pra; client-ip=188.64.184.40; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="john@coherentgraphics.co.uk"; x-sender="john@coherentgraphics.co.uk"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of john@coherentgraphics.co.uk) identity=mailfrom; client-ip=188.64.184.40; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="john@coherentgraphics.co.uk"; x-sender="john@coherentgraphics.co.uk"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@bluechip4.ukhost4u.com) identity=helo; client-ip=188.64.184.40; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="john@coherentgraphics.co.uk"; x-sender="postmaster@bluechip4.ukhost4u.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvACADiFylK8QLgoemdsb2JhbABYg0O6HIEQFg4BAQkJDQk8giUBAQEEJxFAARALBxEJFg8JAwIBAgFFBg6IDQEIw2EXjlwzB4Q3AQOeXIQGinc X-IPAS-Result: AvACADiFylK8QLgoemdsb2JhbABYg0O6HIEQFg4BAQkJDQk8giUBAQEEJxFAARALBxEJFg8JAwIBAgFFBg6IDQEIw2EXjlwzB4Q3AQOeXIQGinc X-IronPort-AV: E=Sophos;i="4.95,612,1384297200"; d="scan'208";a="51882148" Received: from bluechip4.ukhost4u.com ([188.64.184.40]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 06 Jan 2014 11:32:26 +0100 Received: from [46.64.58.255] (port=60305 helo=[192.168.1.66]) by bluechip4.ukhost4u.com with esmtpsa (TLSv1:DHE-RSA-CAMELLIA256-SHA:256) (Exim 4.82) (envelope-from ) id 1W07Tt-003jEp-9M; Mon, 06 Jan 2014 10:32:25 +0000 Message-ID: <52CA8637.7010905@coherentgraphics.co.uk> Date: Mon, 06 Jan 2014 10:32:23 +0000 From: John Whitington User-Agent: Postbox 3.0.8 (Macintosh/20130427) MIME-Version: 1.0 To: Martin DeMello CC: OCaml List References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bluechip4.ukhost4u.com X-AntiAbuse: Original Domain - yquem.inria.fr X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - coherentgraphics.co.uk X-Get-Message-Sender-Via: bluechip4.ukhost4u.com: authenticated_id: john@coherentgraphics.co.uk Subject: Re: [Caml-list] wrapping a string in an in_channel Martin DeMello wrote: > Does OCaml have any equivalent to ruby's StringIO, to wrap an > in_channel around a string and then treat it as if it were a file? Here's another example of wrapping IO around strings, bigarrays, etc as a record of functions: https://github.com/johnwhitington/camlpdf/blob/master/pdfio.ml Here's a type: type input = {pos_in : unit -> pos; seek_in : pos -> unit; input_char : unit -> char option; input_byte : unit -> int; in_channel_length : pos; set_offset : pos -> unit; caml_channel : in_channel option; source : string} It's good to add the 'source' field - which is an arbitrary string for debugging purposes. Of course, if the input came from a file, we would use the file name. The caml_channel field optionally stores a channel this input was derived from. So, we can still drill down and use fast functions from the standard library such as Pervasives.really_input. (The set_offset field is a nasty PDF-related hack: a PDF file can have garbage bytes preceding the header, and PDF absolute byte offsets are offsets from the header, so every read and write might have to be offset) Functions in records as an abstraction don't seem to be a speed problem. The use of option-returning functions like input_char above rather than exception-raising ones or minus-one-as-exception functions like input_byte above is big, though, if the data processed is large enough, due to garbage collection. -- John Whitington Director, Coherent Graphics Ltd http://www.coherentpdf.com/