From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,b5cd7bf26d091c6f X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!x6g2000yqj.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Reading the while standard input into a String Date: Mon, 6 Jun 2011 03:27:50 -0700 (PDT) Organization: http://groups.google.com Message-ID: <608759e5-ffb5-4223-aba4-b180f2cd83a3@x6g2000yqj.googlegroups.com> References: NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1307356070 25375 127.0.0.1 (6 Jun 2011 10:27:50 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 6 Jun 2011 10:27:50 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: x6g2000yqj.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HUALESRCNK X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.6) Gecko/2009012111 Red Hat/3.0.6-1.el5 Firefox/3.0.6,gzip(gfe) Xref: g2news2.google.com comp.lang.ada:20602 Date: 2011-06-06T03:27:50-07:00 List-Id: Natasha Kerensikova wrote on comp.lang.ada: > On 2011-06-06, Ludovic Brenta wrote: >> I'd rather process the input one fixed-length chunk at a time and >> discard each chunk after processing (possibly by recycling the in- >> memory buffer for the next chunk). > > I would do so if online processing was an option. However in the program > containing code similar to what I posted, I cannot do any processing > before reaching the end of input, because the processed format allows > forward references, so references have to be gathered in a first pass on > the whole input, before doing a second pass where dereference can take > place. OK, fair enough. >> Note that reading one character at a time from a stream is quite fast >> since streams normally use the operating system's buffered IO. Adding >> a second layer of buffering inside your program is probably counter- >> productive. > > That second layer of buffer was actually meant to reduce the number of > Ada.Strings.Unbounded.Append calls (and the expensive reallocations that > go with them). Actually, Ada.Strings.Unbounded.Append itself has mechanisms to reduce memory reallocations. But I see your point. > However I thought that something similar to C's fread(), which can use > efficient bulk memory copy, would be better than a loop of single > character read (even when buffered in memory). How about: procedure Read_In_Chunks (Stream : in out Ada.Streams.Root_Stream_Type'Class; Result : out Ada.Strings.Unbounded.Unbounded_String) is Chunk : Ada.Streams.Stream_Element_Array (1 .. 1024); Chunk_As_String : String (1 .. 1024); for Chunk_As_String'Address use Chunk'Address; Last : Natural; begin loop Ada.Streams.Read (Stream => Standard_Input, Item => Chunk, Last => Last); exit when Last = Chunk'First - 1; -- end of stream reached Ada.Strings.Unbounded.Append (Result, Chunk_As_String (1 .. Last)); exit when Last < Chunk'Last; -- end of stream reached end loop; end Read_In_Chunks; -- Ludovic Brenta.