comp.lang.ada
 help / color / mirror / Atom feed
From: Ted Dennison<dennison@telepath.com>
Subject: Re: String manupulation
Date: Wed, 22 Aug 2001 15:53:32 GMT
Date: 2001-08-22T15:53:32+00:00	[thread overview]
Message-ID: <0AQg7.10582$2u.75569@www.newsranger.com> (raw)
In-Reply-To: 9lvq0p$cpm$1@snipp.uninett.no

In article <9lvq0p$cpm$1@snipp.uninett.no>, Reinert Korsnes says...
>
>Thanks to all of you.  Is this the (dirty ?) way to do it (?) :

Some style comments:

o  Anything that never changes after the declaration should be declared
"constant".

o  There's already an instantiation of Integer_IO for Integer. Its
Ada.Integer_Text_IO.

o Unless it won't work for some reason, I generally prefer to use 'Image rather
than a numeric Text_IO instantiation.

o You don't need to "with" a package if you already "with" one of its children.

o Except in extreme quick-n-dirty code, I always specify the parameter names in
multi-parameter subprogram calls.

o (Contraversial) I prefer to aviod the "use" clause.

o There's no indication where the "1" you initialize those variables to comes
from or why its there. Instead, only initialize the one(s) you need to, and do
it from the array it is using to index through.

Also, while transforming that appropriately, I noticed that you have a bug where
it doesn't handle a final substring of length 1 properly. That's the kind of
thing you are more apt to notice the more closely you tie your indices to your
array. 

Also, Find_Token returns 0 for Last when no more spaces are found. If you supply
a string that doesn't end in a space, your code will loop endlessly. The
*proper* way to terminate your "Find_Token" based loop is to terminate when no
further token is found (Last = 0).

Given all that, I'd transform it to:
------------------------
with Ada.Text_IO;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;

procedure Rtest1 is
FS    : constant String := " This is a test to split a string ";
C1    : constant String := "123456789012345678901234";
Space : constant Ada.Strings.Maps.Character_Set := Ada.Strings.Maps.To_Set(" ");

First : Natural := Fs'First;
Last  : Natural;
begin
Ada.Text_IO.Put_Line (C1);
Ada.Text_IO.Put_Line (FS);

loop
Ada.Strings.Fixed.Find_Token
(Source => FS(First..FS'Length),
Set    => Space,
Test   => Ada.Strings.Outside,
First  => First,
Last   => Last
);

exit when Last = 0;

Ada.Text_IO.Put_Line
(Integer'Image(First) & Integer'Image(Last) & " " & Fs (First..Last));

First := Last + 1;
end loop;
end Rtest1;
-------------

To satify those who don't agree with the "no use" bit, I'll also include a
version with "use"s. Obviously *I* don't think it looks better, but some here
surely will:

-------------
with Ada.Text_IO;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;

use Ada.Text_IO;
use Ada.Strings.Fixed;
use Ada.Strings.Maps;

procedure Rtest1 is
FS    : constant String := " This is a test to split a string ";
C1    : constant String := "123456789012345678901234";
Space : constant Character_Set := To_Set(" ");

First : Natural := Fs'First;
Last  : Natural;
begin
Put_Line (C1);
Put_Line (FS);

loop
Find_Token
(Source => FS(First..FS'Length),
Set    => Space,
Test   => Ada.Strings.Outside,
First  => First,
Last   => Last
);

exit when Last = 0;

Put_Line
(Integer'Image(First) & Integer'Image(Last) & " " & Fs (First..Last));

First := Last + 1;
end loop;
end Rtest1;
-------------

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



  reply	other threads:[~2001-08-22 15:53 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-21  7:27 String manupulation Reinert Korsnes
2001-08-21 10:52 ` Georg Bauhaus
2001-08-21 12:52 ` David C. Hoos
2001-08-21 18:04 ` Randy Brukardt
2001-08-22  8:15   ` Reinert Korsnes
2001-08-22 15:53     ` Ted Dennison [this message]
2001-08-22 16:09       ` Ted Dennison
2001-08-22 16:23       ` David C. Hoos
2001-08-22 16:45         ` Ted Dennison
2001-08-23  8:21         ` Reinert Korsnes
2001-08-23 12:46           ` David C. Hoos
2001-08-22 16:39       ` Warren W. Gay VE3WWG
replies disabled

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