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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!2.eu.feeder.erje.net!feeder.erje.net!2.us.feeder.erje.net!bloom-beacon.mit.edu!bloom-beacon.mit.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!.POSTED!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Introductory Presentations, especially aimed at C++ programmers! Date: Tue, 13 Dec 2016 16:15:54 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <1905815374.502825168.454102.laguest-archeia.com@nntp.aioe.org> <877f7b5llo.fsf@nightsong.com> NNTP-Posting-Host: shell02.theworld.com Mime-Version: 1.0 Content-Type: text/plain X-Trace: pcls7.std.com 1481663754 22074 192.74.137.72 (13 Dec 2016 21:15:54 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Tue, 13 Dec 2016 21:15:54 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:rmVe6EE03bp67XhiN/9y84WZmSU= Xref: news.eternal-september.org comp.lang.ada:32788 Date: 2016-12-13T16:15:54-05:00 List-Id: "Jeffrey R. Carter" writes: > On 12/12/2016 05:53 PM, Randy Brukardt wrote: >> >> A Get_Line like the above >> generally requires some sort of incremental allocation (a buffer is >> allocated, if its not big enough a bigger one is allocated, the first one is >> copied into it, and then we repeat until a appears), > > As the person who created the Get_Line function, I can assure that's not > how most versions are implemented. That's how it's done in GNAT. How is it done in "most versions"? >From a-textio.adb: function Get_Line (File : File_Type) return String is function Get_Rest (S : String) return String; -- This is a recursive function that reads the rest of the line and -- returns it. S is the part read so far. -------------- -- Get_Rest -- -------------- function Get_Rest (S : String) return String is -- The first time we allocate a buffer of size 500. Each following -- time we allocate a buffer the same size as what we have read so -- far. This limits us to a logarithmic number of calls to Get_Rest -- and also ensures only a linear use of stack space. Buffer : String (1 .. Integer'Max (500, S'Length)); Last : Natural; begin Get_Line (File, Buffer, Last); declare R : constant String := S & Buffer (1 .. Last); begin if Last < Buffer'Last then return R; else pragma Assert (Last = Buffer'Last); -- If the String has the same length as the buffer, and there -- is no end of line, check whether we are at the end of file, -- in which case we have the full String in the buffer. if End_Of_File (File) then return R; else return Get_Rest (R); end if; end if; end; end Get_Rest; -- Start of processing for Get_Line begin return Get_Rest (""); end Get_Line; - Bob