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,8de7eedad50552f1 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!uio.no!newsfeed1.funet.fi!newsfeed3.funet.fi!newsfeeds.funet.fi!feeder2.news.jippii.net!reader1.news.jippii.net!53ab2750!not-for-mail From: Tapio Kelloniemi Subject: Re: Ada bench : count words References: Newsgroups: comp.lang.ada Message-ID: Date: Tue, 22 Mar 2005 19:08:56 GMT NNTP-Posting-Host: 217.30.176.187 X-Complaints-To: newsmaster@saunalahti.com X-Trace: reader1.news.jippii.net 1111518536 217.30.176.187 (Tue, 22 Mar 2005 21:08:56 EET) NNTP-Posting-Date: Tue, 22 Mar 2005 21:08:56 EET Organization: Saunalahti Customer Xref: g2news1.google.com comp.lang.ada:9746 Date: 2005-03-22T19:08:56+00:00 List-Id: Marius Amado Alves wrote: >> Why not use GNAT.OS_Lib. > >I'm trying, but the program does not work properly. It seems to >terminate too early, and the results oscillate between 20 and 49 lines. >I'll be damned if I understand what's happening. [--] > procedure End_Line is > begin > Lines := Lines + 1; Remove the following line: > Total := Total + 1; > End_Word; > end; > > procedure Count_Words (S : in String) is > begin > Total := Total + S'Length; Remove the following line (it checks every character of the buffer once more): > Lines := Lines + Ada.Strings.Fixed.Count (S, EOL); > for I in S'Range loop Add: if S (I) = LF then End_Line; elsif Is_Separator (S (I)) then End_Word; Note that the In-Word condition was checked in the loop and in In_Word. elsif not In_Word then Begin_Word; > end if; > end loop; > end; > > pragma Inline (Begin_Word, End_Word, End_Line, Count_Words); > >begin > loop > N := GNAT.OS_Lib.Read > (GNAT.OS_Lib.Standin, > Buffer'Address, Replace the above line with Buffer (Buffer'First)'Address, And Read is given the address of first byte to fill, not some internal pointers which are used for bounds checking. Notice that when you leave that safe Text_IO, it is very easy to make the same mistakes as in C (we are programming in C style). -- Tapio