comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Help
Date: 1996/11/19
Date: 1996-11-19T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023280001911960936000001@news.ni.net> (raw)
In-Reply-To: 328C2BF5.1348@informatik.tu-muenchen.de


In article <328C2BF5.1348@informatik.tu-muenchen.de>, "Alexander B.
Schmidt" <schmiale@informatik.tu-muenchen.de> wrote:

>Hi,
>
>does anybody know how to declare a string of variable length in Ada 
>83? Declaring a variable of type String like
>str: STRING(1..20);
>and assigning a string like
>str := "a String" 
>results in a constraint error if the length of the string is not equal 
>to 20.
>Is it possible to declare strings with a maximum length which accept 
>the assignment of shorter strings too?

The technique I often use in Ada 83 is to use a discriminated record, as
follows:

subtype String_Buffer_Length_Range is Natural range 0 .. 20;

type String_Buffer (Length : String_Buffer_Length_Range := 0) is
   record
      Text : String (1 .. Length);
   end record;

function "+" (Text : String) return String_Buffer is
   subtype Slided is String (1 .. Text'Length);
begin
   return (Text'Length, Slided (Text));
end;

Now, given those definitions, I can do this:

declare
   The_String : String_Buffer;
begin
   The_String := +"Robert Dewar";
   The_String := +"Norm";
   The_String := +"Ada";
  
When you refer to the string, you don't need to apply an index constraint, ie

   Put_Line (The_String.Text); 
   -- The_String.Length worth of characters, not 20

This is also how to make a ragged array in Ada 83; just declare an array of
String_Buffer.

M.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




  parent reply	other threads:[~1996-11-19  0:00 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-15  0:00 Help Alexander B. Schmidt
1996-11-15  0:00 ` Help Michael Paus
1996-11-18  0:00 ` Help Norman H. Cohen
1996-11-19  0:00 ` Matthew Heaney [this message]
  -- strict thread matches above, loose matches on Subject: below --
2001-09-19 19:51 help mop
2001-09-20  8:41 ` help John McCabe
1998-12-07  0:00 help Salvador
1998-12-07  0:00 ` help dennison
1997-02-27  0:00 Help Desperate
1997-03-04  0:00 ` Help Jim Dorman
1994-10-19 16:26 HELP CONDIC
1994-10-14  3:33 HELP Chiu Bo (Hung Chiu Hung)
1994-10-14 13:21 ` HELP Robert Dewar
1994-10-15 14:56   ` HELP Bob Duff
1994-10-18 16:31     ` HELP Robert Dewar
1994-10-16  5:11   ` HELP Chiu Bo (Hung Chiu Hung)
1994-10-14 19:36 ` HELP riehler
1994-10-14 19:36 ` HELP Richard Riehle
1994-10-19  3:10 ` HELP Michael M. Bishop
1993-03-12 22:28 Help Basavaraj B Patil
1990-08-01 18:19 Help Kenneth Anderson
replies disabled

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