comp.lang.ada
 help / color / mirror / Atom feed
* variable lenght strings
@ 2004-10-21 17:52 fabio de francesco
  2004-10-21 21:22 ` Martin Dowie
                   ` (5 more replies)
  0 siblings, 6 replies; 70+ messages in thread
From: fabio de francesco @ 2004-10-21 17:52 UTC (permalink / raw)


Hi,

while studying from "Ada as a 2nd Language" I am having some problem
to cope with Strings (Fixed, Unbounded and Bounded) manipulation and
especially with assigning between different types of them and with
overall with input/output.

1) Is it possible to use Get_Line with Unbounded and/or Bounded
Strings?

2) If not, how should user input be managed when lines length isn't
known a priori?

3) I have to write a program that parses a file in which there are
lines that have to be "tokenized". I didn't find any Find_Token()
usage example on the Book so I wrote a little piece of code in order
to understand the procedure workings on a fixed string (Line A) that
simulates file input.

Think that I am used with C function strtok() and I must say that Ada
Find_Token is much more elegant and not destructive as strtok() is.

Ok, please let me know if the following is the correct usage:


with Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed, 
    Ada.Strings.Maps, Ada.Strings.Maps.Constants;
use Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed,
    Ada.Strings.Maps, Ada.Strings.Maps.Constants;

procedure Tokenize is
        
    S : constant String := ".symbol HP = High_Pressure    "; -- Line A
    F : Positive := S'First;
    L : Natural := S'Last;
    W : String( 1..30 );
        
begin
  loop
    Find_Token(S(F..L), Alphanumeric_Set or To_Set("_."), Inside, F,
L);
    if L /= 0 then
       Put_Line( Positive'Image( F ) & ( L - F ) * ' ' &
Natural'Image( L ) );
         Move( S( F..L ), W );
         Put_Line( ' ' & W );
         F := L + 1;
         L := S'Last;
    else 
       exit;
    end if;
  end loop;      
end Tokenize;


Is it correct to check the "L" value as a condition to exit the loop?


Thank you in advance for any help,

Fabio De Francesco



^ permalink raw reply	[flat|nested] 70+ messages in thread
* Variable Length strings...
@ 1989-06-04  7:03 "Jonathan B. Owen"
  0 siblings, 0 replies; 70+ messages in thread
From: "Jonathan B. Owen" @ 1989-06-04  7:03 UTC (permalink / raw)


Thomas Hoyt writes that having the following:

   generic
         Max_len : Integer;
   package VSTR is

         subtype Len_range is integer range 0..Max_len;

   etc...

DOES imply a maximum length for a discriminant of type Len_range, be it
integer'LAST.   Also, he suggests using a fixed upper limit to Max_len,
say 20000.

   First, as far as I observed, on Vax Ada, the actuall value for Max_len
for a particular instantiation is taken as the upper limit for a disc.
of type Len_range.   I see no reason why Verdix Ada should not do it.

   Secondly, I do believe defining the range of Max_len to be limited to
20000.   Still, I think it would be unreasonable to allocate 20K for each
V_string defined, when typicalalues for Max_len would be much smaller
(say 15, 80 and such).

   It seems to me Verdix Ada handles Dynamically defined types/Subtypes
differently than the Vax Ada.

   Come to think of it, I ran into a related problem as the above, in
a non-generic package.

   Consider the following:

       type Record_header is
           record
               key  : Key_type;
               time : Time_type;
           end record;

       Max_data_len : constant integer := 2048-Record_header'SIZE/8;

       subtype Data_len_range is integer range 0..Max_data_len;

       type Record_type( data_len : Data_len_range := 0 ) is
           record
               header : Record_header;
               data   : String(1..data_len);
           end record;

Say the Record_header occupies 48 bytes.  The purpose of the above is
to define a record which would occupy 2K at the most, including the
header.   The above works find on Vax-Ada but fails on Verdix with
the same problem of not recognizing the upper limit of Data_len_range.

                      I hope the shortcoming of Verix is now obvious...

                                                   Jonathan

______________________________________________________________________________
  (--)    /--)     /-(\                 Email: gdau100@bguvm (bitnet)
  \ /    /--K      | \|/\   /\/) /|-\   Snail: 55 Hovevei Zion
  _/_/o /L__)_/o \/\__/  \X/  \_/ | |_/        Tel-Aviv, 63346  ISRAEL
 (/        Jonathan B. Owen             Voice: (03) 281-422

 Point of view:  A chicken is the means by which an egg reproduces an egg.
______________________________________________________________________________

^ permalink raw reply	[flat|nested] 70+ messages in thread
* Variable Length Strings...
@ 1989-06-02 16:19 Thomas Hoyt
  0 siblings, 0 replies; 70+ messages in thread
From: Thomas Hoyt @ 1989-06-02 16:19 UTC (permalink / raw)


In Issue #143, Jonathon B. Owen writes...
>...
>Baring the above in mind, I don't understand why the generic parameter
>Max_len should not be considered constant for the issue of determining
>the max. size.  After all, it's value is avaliable during elaboration
>time, and of course cannot change, there after.

From what I understand of your problem, it *is* a constant, just a constant
that is too big for the compiler to handle.  Max_Len is declared:

   generic
      Max_Len : Integer;
   Package (etc)...

The string range statement thus defines the variable length string to be
anywhere from 0 to Integer'Last.  This raises the possiblity that Integer'Last
length strings could be created on the fly.  This frightening implication
causes the anxiety from the compiler.

Try this in your original generic.  Put some constaints on the generic
parameter:

   generic
      Max_Len : Integer range 0..20000 := 20000;
   Package (etc)...

This does two things.  It allows for a default max variable string length
if you choose to pass in nothing for the parameter.  It also tells the
compiler that at most there will be 20000 char length strings created
dynamically.  This is a whole lot less than Integer'Last( normally around
2^32 or there abouts ) and will calm Verdix's nerves.  Of course, you can
adjust the constants accordingly.  Also there may be implimentation limits
on how big you can define dynamic objects at one time.  As always, check
your manuals.

This ought to permit you to keep the generic solution, i.e. without hard
coding string range limits into separate packages. (*Disclaimer* I do
not *sigh* have access to a Verdix compiler...as always, if this patch should 
fail, the secretary will disavow all knowledge...etc...)


******
thoyt@ddn-wms.arpa      |  "Oh no...it's written in COBOL..."
Thomas Hoyt             |  "Government Computers for Government business..."
CRC Systems, Inc., Fairfax, VA -- 703-359-9400       |  "NO FUN ALLOWED..."
******

^ permalink raw reply	[flat|nested] 70+ messages in thread
* re: Variable Length Strings
@ 1989-06-02 13:26 "Jonathan B. Owen"
  1989-06-06 13:19 ` Paul Warren
  0 siblings, 1 reply; 70+ messages in thread
From: "Jonathan B. Owen" @ 1989-06-02 13:26 UTC (permalink / raw)


In issue #143, Thomas Hoyt replied to my question about variable length
strings.  He is right by commenting that if the type V_string is used
without the default of zero, your string is not of a variable length...

Cliff baker replied to my question by saying he thinks the Verdix Ada
is not smart enough to recognize the Max. size of the type by taking
into consideration the upper limit of Len_range (and that the problem
has nothing to do with the fact that the package is generic).  Well,
I disagree with this, as the Verdix Ada DOES recognize the max. size,
IF THE UPPER LIMIT OF THE SUBTYPE IS CONSTANT, as in:

   subtype Len_range is integer range 0..132;

I did not bother to mention my work-around to this problem.  I defined
a few non-generic packages to handle V_strings of length 15,80,132,512,
1024,2048.  I had to replicate manually the body of these packages.  Of
course working this way is a compromise...  The point is that the Verdix
Ada works fine with these non-generic packages, in respect to the
issue at hand.

Baring the above in mind, I don't understand why the generic parameter
Max_len should not be considered constant for the issue of determining
the max. size.  After all, it's value is avaliable during elaboration
time, and of course cannot change, there after.

                                   Any thoughts?

                                                Jonathan

______________________________________________________________________________
  (--)    /--)     /-(\                 Email: gdau100@bguvm (bitnet)
  \ /    /--K      | \|/\   /\/) /|-\   Snail: 55 Hovevei Zion
  _/_/o /L__)_/o \/\__/  \X/  \_/ | |_/        Tel-Aviv, 63346  ISRAEL
 (/        Jonathan B. Owen             Voice: (03) 281-422

 Point of view:  A chicken is the means by which an egg reproduces an egg.
______________________________________________________________________________

^ permalink raw reply	[flat|nested] 70+ messages in thread
* Variable length strings...
@ 1989-05-30 15:39 Thomas Hoyt
  0 siblings, 0 replies; 70+ messages in thread
From: Thomas Hoyt @ 1989-05-30 15:39 UTC (permalink / raw)


In issue #142, D. Papay is right on the $$$ with his comments on variable
length strings.  However...

>...I suggest you remove the default expression " := 0" from your
> record type ... and always declare ... with explicit discriminant
> contraints...

If you do this, you lose all the advantages of a parameterized record,
i.e. variable length strings.  Why not constrain the subtype of the
range of the record parameter:
^^^^^
   subtype Len_range is integer range 0..256; -- or some other max
                                              -- string length

This way the compiler would know exactly how big the strings could ever
get and plan accordingly...i.e. stop complaining.

Of course, you could still have pretty huge strings without coming close to
whatever Integer'Last is.  Another advantage is that the writer of the
generic could explicitly control how big the variable length strings
could ever get, without having to rely upon implimentation details. 

You also eliminate a generic parameter, relieving the user of an unnecessary
detail.  Most text editing functions wouldn't ever need more than about
1024 chars at a time (need...hope?:-))
 

******
Thomas Hoyt             |  "Government Computers for Government business..."
CRC Systems, Inc.       |  "NO FUN ALLOWED..."
thoyt@ddn-wms.arpa		|	"Oh no...it's written in COBOL..."
******

^ permalink raw reply	[flat|nested] 70+ messages in thread

end of thread, other threads:[~2004-11-01 19:59 UTC | newest]

Thread overview: 70+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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
  -- strict thread matches above, loose matches on Subject: below --
1989-06-04  7:03 Variable Length strings "Jonathan B. Owen"
1989-06-02 16:19 Variable Length Strings Thomas Hoyt
1989-06-02 13:26 "Jonathan B. Owen"
1989-06-06 13:19 ` Paul Warren
1989-05-30 15:39 Variable length strings Thomas Hoyt

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