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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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!news1.google.com!proxad.net!proxad.net!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Ada bench : count words Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.14.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Tue, 22 Mar 2005 19:59:55 +0100 Message-ID: <16bih8s63gcv6$.1rb1l58qi0cv3.dlg@40tude.net> NNTP-Posting-Date: 22 Mar 2005 19:59:52 MET NNTP-Posting-Host: ab727b04.newsread2.arcor-online.net X-Trace: DXC=SK>U@c56Yb:_78OXD477\?Q5U85hF6f;4jW\KbG]kaM8]kI_X=5Kea6o1kN=nnQN]3[6LHn;2LCV>VVa[ZlQni_1:lL] On Tue, 22 Mar 2005 17:39:42 +0000, 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. > > -- Count words in Ada for the language shootout > -- by Marius Amado Alves > > with Ada.Characters.Handling; > with Ada.Characters.Latin_1; > with Ada.Strings.Fixed; > with Ada.Text_IO; > with GNAT.OS_Lib; > > procedure Count_Words_OS_Lib is > > use Ada.Characters.Handling; > use Ada.Characters.Latin_1; > use Ada.Text_IO; > > Buffer : String (1 .. 4096); > EOL : String := (1 => LF); > Lines : Natural := 0; > Words : Natural := 0; > Total : Natural := 0; > In_Word : Boolean := False; > N : Natural; > > function Is_Separator (C : Character) return Boolean is > begin > return Is_Control (C) or C = ' '; > end; > > procedure Begin_Word is > begin > In_Word := True; > end; > > procedure End_Word is > begin > if In_Word then > Words := Words + 1; > In_Word := False; > end if; > end; > > procedure End_Line is > begin > Lines := Lines + 1; > Total := Total + 1; > End_Word; > end; > > procedure Count_Words (S : in String) is > begin > Total := Total + S'Length; > Lines := Lines + Ada.Strings.Fixed.Count (S, EOL); > for I in S'Range loop > if Is_Separator (S (I)) then > if In_Word then End_Word; end if; > else > if not In_Word then Begin_Word; end if; > 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, Hmm, why not Buffer (Buffer'First)'Address? > Buffer'Length); > Count_Words (String (Buffer (1 .. N))); > exit when N < Buffer'Length; > end loop; > > Ada.Text_IO.Put_Line > (Natural'Image (Lines) & > Natural'Image (Words) & > Natural'Image (Total)); > end; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de