From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: variable lenght strings
Date: Thu, 21 Oct 2004 19:14:19 -0400
Date: 2004-10-21T23:14:21+00:00 [thread overview]
Message-ID: <417842cd$0$74191$39cecf19@news.twtelecom.net> (raw)
In-Reply-To: mailman.46.1098398641.10401.comp.lang.ada@ada-france.org
"Marius Amado Alves" <amado.alves@netcabo.pt> wrote in message
news:mailman.46.1098398641.10401.comp.lang.ada@ada-france.org...
>> 1) Is it possible to use Get_Line with Unbounded and/or Bounded
>> Strings?
>
> Not in the standard, but subprograms like those are usually around, e.g.
> in the GNAT Library, or end up being written in house.
>
>> 2) If not, how should usei input be managed when lines length isn't
>> known a priori?
>
> There's a way using the standard Get_Line, explained in AdaPower.
Mario is probably referring to an article I posted to CLA a few years' ago,
and which is now archived at the adapower website.
The basic idea is this: algorithms that consume input from a stream need a
way a identify when all of the input has been consumed. Typically this is
done using a special value that you know is outside the range of normal
values, e.g.
declare
I : Natural;
begin
loop
Get (Stream, I);
exit when I = 0; --the special value
... -- do something with I
end loop;
end;
Text_IO.Get_Line works just like this, except that it's not obvious what the
termination condition is:
declare
Line : String (1 .. 81);
Last : Natural;
begin
loop
Get_Line (Line, Last);
.. -- do something with Line (1 .. Last);
exit when ???
end loop;
end;
The termination condition is as follows: you know that you've consumed the
entire line when Last < Line'Last. We can now write:
declare
Line : String (1 .. 81);
Last : Natural;
begin
loop
Get_Line (Line, Last);
.. -- do something with Line (1 .. Last);
exit when Last < Line'Last;
end loop;
end;
Typically you know what a normal line length is, so you declare Line with a
length of Max + 1. (That's why Line'Last is 81 in the examples above.)
Now that you know the termination condition, the next problem is deciding
how to handle a line of input that is longer than your buffer. You can
either handle it as an error, or just copy the buffer into an expandable
string buffer such as Unbounded_String.
If you want, you can use recursion to read the line, and return the string
as a function result:
declare
function Get_Line (N : Positive := 80) return String is
Line : String (1 .. N + 1);
Last : Natural;
begin
Get_Line (Line, Last);
if Last < Line'Last then
return Line (1 .. Last);
else
return Line & Get_Line (2 * N);
end if;
end Get_Line;
Line : constant String := Get_Line;
begin
This avoids the loop, and having to store intermediate results in a buffer.
next prev parent reply other threads:[~2004-10-21 23:14 UTC|newest]
Thread overview: 65+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-10-21 17:52 variable lenght strings fabio de francesco
2004-10-21 21:22 ` Martin Dowie
2004-10-21 22:42 ` Marius Amado Alves
2004-10-21 23:14 ` Matthew Heaney [this message]
2004-10-22 7:38 ` Martin Krischik
2004-10-22 12:30 ` fabio de francesco
2004-10-22 7:29 ` Martin Krischik
2004-10-22 13:01 ` Matthew Heaney
2004-10-24 15:46 ` Martin Krischik
2004-10-24 19:54 ` Jeffrey Carter
2004-10-24 21:30 ` Larry Kilgallen
2004-10-25 4:02 ` Jeffrey Carter
2004-10-21 23:01 ` Marius Amado Alves
2004-10-21 23:05 ` Stephen Leake
2004-10-22 7:25 ` Martin Krischik
2004-10-22 11:11 ` Martin Dowie
2004-10-24 15:43 ` Martin Krischik
2004-10-24 17:39 ` Martin Dowie
2004-10-24 18:37 ` Björn Persson
2004-10-25 7:30 ` Martin Krischik
2004-10-26 0:06 ` Randy Brukardt
2004-10-26 1:53 ` Larry Kilgallen
2004-10-26 8:49 ` Martin Krischik
2004-10-26 11:18 ` Marius Amado Alves
2004-10-26 12:48 ` variable length strings Larry Kilgallen
2004-10-26 16:11 ` Warren W. Gay VE3WWG
2004-10-26 18:50 ` Björn Persson
2004-10-26 19:46 ` Larry Kilgallen
2004-10-26 8:43 ` variable lenght strings Jean-Pierre Rosen
2004-10-26 13:15 ` Martin Krischik
2004-10-26 17:37 ` Pascal Obry
2004-10-26 18:07 ` Hyman Rosen
2004-10-26 20:10 ` Pascal Obry
2004-10-27 10:26 ` variable length strings Jacob Sparre Andersen
2004-10-27 10:39 ` Pascal Obry
2004-10-27 11:47 ` Larry Kilgallen
2004-10-28 7:18 ` Jacob Sparre Andersen
2004-10-27 11:50 ` Larry Kilgallen
[not found] ` <uwtxcla1n.fsf@obry.Organization: LJK Software <PTzuwe3GsIg6@eisner.encompasserve.org>
2004-10-27 12:12 ` Samuel Tardieu
2004-10-27 12:58 ` Pascal Obry
2004-10-27 13:04 ` Pascal Obry
2004-10-27 14:54 ` Dmitry A. Kazakov
2004-10-27 16:38 ` Pascal Obry
2004-10-28 8:14 ` Dmitry A. Kazakov
2004-10-28 8:49 ` Pascal Obry
2004-10-28 9:06 ` Dmitry A. Kazakov
2004-10-28 16:07 ` Pascal Obry
2004-10-28 22:05 ` Jeffrey Carter
2004-10-28 22:41 ` Randy Brukardt
2004-10-29 1:11 ` Jeffrey Carter
2004-10-29 7:42 ` Dmitry A. Kazakov
2004-10-28 10:31 ` Larry Kilgallen
2004-10-26 18:13 ` variable lenght strings Martin Krischik
2004-10-27 12:01 ` Jean-Pierre Rosen
2004-10-26 17:46 ` Jeffrey Carter
2004-10-28 22:50 ` Randy Brukardt
2004-10-28 23:01 ` Larry Kilgallen
2004-10-29 21:52 ` Randy Brukardt
2004-10-29 8:48 ` Dale Stanbrough
2004-10-29 9:11 ` Larry Kilgallen
2004-10-24 18:38 ` Why these "Drop" parameters? (was: variable lenght strings) Björn Persson
2004-10-26 0:13 ` Randy Brukardt
2004-11-01 1:02 ` Why these "Drop" parameters? Björn Persson
2004-11-01 19:59 ` Randy Brukardt
2004-10-24 18:57 ` variable lenght strings Jeffrey Carter
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox