comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: An example was Re: Tokens
Date: Sun, 19 May 2002 22:44:04 -0500
Date: 2002-05-19T22:44:04-05:00	[thread overview]
Message-ID: <mailman.1021866303.25974.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: rAWF8.34892$Iv.4013048@news6-win.server.ntlworld.com


----- Original Message -----
From: "chris.danx" <spamoff.danx@ntlworld.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: May 19, 2002 6:30 PM
Subject: Re: An example was Re: Tokens


>
> "chris.danx" <spamoff.danx@ntlworld.com> wrote in message
> news:dkWF8.34770$Iv.4001930@news6-win.server.ntlworld.com...
>
> Hi,
>
> Is there a cheap way to prevent the last "&" from being printed in the code
> below?  I can think of several options but all of them require an additional
> call to find_token or switching to a loop.  Is there another way that
> doesn't have a repeated call to find token?

Here is a more elegant example that I assert is more generally useful, as it
simply returns an array containing the first and last characters of each
token meeting the criterion.  The result can then be used to access the
tokens individually.

with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Text_IO;
procedure Test_Token_Limits is

   type Limits is record
      First : Positive;
      Last  : Natural;
   end record;

   type Limits_Array is array (Positive range <>) of Limits;

   function Token_Limits
     (Source     : String;
      Delimiters : Ada.Strings.Maps.Character_Set)
     return Limits_Array is
      The_Limits : Limits;
   begin
      Ada.Strings.Fixed.Find_Token
        (Source,
         Delimiters,
         Ada.Strings.Outside,
         The_Limits.First,
         The_Limits.Last);
      if The_Limits.Last /= 0 then
         return The_Limits & Token_Limits
           (Source (The_Limits.Last + 1 .. Source'Last), Delimiters);
      else
         return Limits_Array'(1 .. 0 => The_Limits);
      end if;
   end Token_Limits;

   Delimiters : constant Ada.Strings.Maps.Character_Set :=
     Ada.Strings.Maps.To_Set (" !?,:;.");

   Source : constant String := "Now is the time for all good men to " &
     "come to the aid of their country.";

   Word_Limits : constant Limits_Array := Token_Limits
     (Source     => Source,
      Delimiters => Delimiters);
begin
   for W in Word_Limits'Range loop
      Ada.Text_IO.Put_Line
        ("Word no." & W'Img & " is """ &
         Source (Word_Limits (W).First .. Word_Limits (W).Last) & """."
        );
   end loop;
end Test_Token_Limits;







  reply	other threads:[~2002-05-20  3:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-19 15:15 Tokens ProLogic
2002-05-19 16:32 ` Tokens Pascal Obry
2002-05-19 21:23   ` Tokens ProLogic
2002-05-19 21:48   ` Tokens ProLogic
2002-05-19 23:13     ` An example was Tokens chris.danx
2002-05-19 23:30       ` chris.danx
2002-05-20  3:44         ` David C. Hoos, Sr. [this message]
2002-05-20  1:36       ` ProLogic
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox