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.3 required=5.0 tests=BAYES_00,INVALID_MSGID, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d93b7c6dd17cbc81 X-Google-Attributes: gid103376,public From: jrcarter001@my-dejanews.com Subject: Re: Strings and reading from a file Date: 1999/05/15 Message-ID: <7hl1ph$p3g$1@nnrp1.deja.com>#1/1 X-Deja-AN: 478305873 References: <7han2q$jkp$1@news.iinet.net.au> <37399913.BD928DD1@pwfl.com> <7hevh1$g08$1@nnrp1.deja.com> <7hf2bc$imm$1@nnrp1.deja.com> <7hf611$16i$1@cnn.Princeton.EDU> X-Http-Proxy: 1.0 x21.deja.com:80 (Squid/1.1.22) for client 206.239.237.114 Organization: Deja.com - Share what you know. Learn what you don't. X-Article-Creation-Date: Sat May 15 23:59:46 1999 GMT Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.51 [en] (Win95; U) Date: 1999-05-15T00:00:00+00:00 List-Id: In article <7hf611$16i$1@cnn.Princeton.EDU>, mcc@entropy.cs.princeton.edu (Martin C. Carlisle) wrote: > I can't imagine! Probably something more like: > > function Next_Line(File : in Ada.Text_IO.File_Type := > Ada.Text_Io.Standard_Input) return String is > Answer : String(1..256); > Last : Natural; > begin > Ada.Text_IO.Get_Line(File => File, > Item => Answer, > Last => Last); > if Last = Answer'Last then > return Answer & Next_Line; > else > return Answer(1..Last); > end if; > end Next_Line; > > --Martin > > In article <7hf2bc$imm$1@nnrp1.deja.com>, wrote: > >In article <7hevh1$g08$1@nnrp1.deja.com>, > >> Make the function recursive, and it can return a string of any length, > >> eliminating that nagging worry about strings that are longer than > >> whatever magic number you used in the function. > > > >Surely you don't mean (forgive me if Deja decides to throw away my > >formatting): > > > >function Next_Line return String is > > Char : Character; > > Next_Char : Character; > > EOL : Boolean; > >begin > > Get (Char); > > Look_Ahead (Next_Char, EOL); > > if EOL then > > return (1 => Char); > > else > > return Char & Next_Line; > > end if; > >end Next_Line; > > > >I think we may have just smoked out a Lisp programmer... I didn't expect to generate such a response... The original inspiration for this is Carter, J., "Variable-Length String Input in Ada", _Ada Letters_, 1989 May/Jun which presents the following (Ada-83) algorithm: with Text_Io; function Get_Line (File : Text_Io.File_Type := Text_Io.Current_Input) return String is Char : Character; begin -- Get_Line if Text_Io.End_Of_Line (File) then Text_Io.Skip_Line (File); return ""; else Text_Io.Get (File, Char); return Char & Get_Line (File); end if; end Get_Line; This is the output of a straightforward algorithm creation process, which goes something like: In Ada, it has to be a function so the result can be any length. Ok, assume I have a function that will return the rest of the line, provided I get the first character myself. Use that function to implement itself: begin Text_Io.Get (Char); return Char & Get_Line; end; How do I terminate the recursion? When I'm at the end of the line. What do I return then? A null string. End of exercise, and the function is finished, and works correctly. Pretty it up by adding a File parameter with appropriate default, and you're done. However, most lines are short, and recursion for every character seems wasteful, even though it ran as fast as I could want. My current implementation has improved the algorithm: with Ada.Text_Io; use Ada; function Get_Line (File : Text_Io.File_Type := Text_Io.Current_Input) return String is Line : String (1 .. 100); Last : Natural; begin -- Get_Line Text_IO.Get_Line (File => File, Item => Line, Last => Last); if Last < Line'Last then return Line (Line'First .. Last); else return Line & Get_Line (File); end if; end Get_Line; This is effectively the same as Carlisle's version. This does no recursion for most lines I've encountered, and gracefully handles the "pathological" cases. It still has trouble with those lines that are Positive'Last characters long, especially on 32-bit machines. --== Sent via Deja.com http://www.deja.com/ ==-- ---Share what you know. Learn what you don't.---