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=-2.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, MAILING_LIST_MULTI autolearn=unavailable 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!usenet-fr.net!enst.fr!melchior!cuivre.fr.eu.org!melchior.frmug.org!not-for-mail From: Marius Amado Alves Newsgroups: comp.lang.ada Subject: Re: Ada bench : count words Date: Tue, 22 Mar 2005 17:39:42 +0000 Organization: Cuivre, Argent, Or Message-ID: References: NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 (Apple Message framework v619.2) Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-Trace: melchior.cuivre.fr.eu.org 1111513203 5916 212.85.156.195 (22 Mar 2005 17:40:03 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Tue, 22 Mar 2005 17:40:03 +0000 (UTC) To: comp.lang.ada@ada-france.org Return-Path: In-Reply-To: X-Mailer: Apple Mail (2.619.2) X-OriginalArrivalTime: 22 Mar 2005 17:39:46.0514 (UTC) FILETIME=[23B28B20:01C52F06] X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Gateway to the comp.lang.ada Usenet newsgroup" List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: g2news1.google.com comp.lang.ada:9743 Date: 2005-03-22T17:39:42+00:00 > 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, 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;