comp.lang.ada
 help / color / mirror / Atom feed
From: "Warren W. Gay VE3WWG" <ve3wwg@home.com>
Subject: Re: Slicing ( Ada Idioms Progress Preview )
Date: Tue, 14 Aug 2001 01:39:24 GMT
Date: 2001-08-14T01:39:24+00:00	[thread overview]
Message-ID: <3B78814B.70B65E93@home.com> (raw)
In-Reply-To: E8Rd7.282$D4.307@www.newsranger.com

Ted Dennison wrote:
...
> >I'm interested in how to "think in Ada"...
...
> Based on my experience with native C speakers, here are some basic things to get
> you started:
> 
> o  Strings in Ada are *very* different from Strings in C. Ada strings end at the
> end of the array (or slice of it you specify), not at some arbitrarily-chosen
> "terminator" character. Look into the "&" operator for arrays, and the array
> attributes 'length, 'first, and 'last. 'image is damn nice too. Don't be
> discouraged if you have trouble with this seemingly simple thing. Conceptually,
> this is probably the biggest difference the languages have.
> 
> o  The techniques you learn for dealing with strings can be used for *all*
> arrays.
...
> o  You almost never need pointers.
> 
> o  If you think you need a pointer, see the previous point. :-)

I think the other very important aspect of string handling is something Ted
pointed out in another thread: You work with fixed length strings a little
differently than you're used to (ie. Unbounded_Strings are not always necessary).
 
  o  sometimes keeping track of it's true length (when necessary)

     My_String        : String(1..64);  -- of max size
     My_String_Length : Integer;        -- My_String's logical length

  o  declaring the fixed length string with the required size, upon demand:

  procedure whatever is
     Computed_Length : Integer;
  begin
     ...
     Computed_Length := ...;

     declare
       Buffer : String(1..Computed_Length);
     begin
       -- code that uses Buffer...

In the same vein :

  o  Fixed length strings of an exact size can be passed to functions/procedures :

     Procedure_Call(Buffer(1..Computed_Length),...);

THe called procedure gets a string of the exact size :

     procedure Procedure_Call(Buffer : String) is
     begin
        -- Use string of Buffer'Length bytes..

While it was obvious that declare blocks allow you to introduce new variables
mid-way through a procedure, it was not obvious to me that you might want to do
that for the simple reason of getting a precisely sized buffer or string. Once
I caught onto that, I've found that I've rarely wanted to use Bounded or Unbounded
strings.

One last warning important thing about Ada strings, concerns slicing!
I got burnt on this recently (even though I know that I should always
use 'First and 'Last etc... I learned my lesson).

When using a string passed to you, be careful, as in this fragment :

     procedure My_Procedure(Buffer : String) is
       Copied_Str : String(1..Buffer'Length);
     begin
       if Buffer'Length >= 2 then
          Copied_Str(1..2) := Buffer(1..2); -- This is bad!

You look at Buffer, and you think, "but I know String() starts at 1", and so
this slice assignment looks OK. But if the caller does this:

     My_Procedure(Supplied_String(16..20));

then My_Procedure fails, because Buffer'First is 16, and Buffer'Last is 20!

So always be diligent about using 'First and 'Last, and be very careful
about assumptions concerning the array bounds in general.
-- 
Warren W. Gay VE3WWG
http://members.home.net/ve3wwg



      parent reply	other threads:[~2001-08-14  1:39 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-03  4:16 Ada Idioms Progress Preview James Rogers
2001-08-03 19:45 ` Robert Dewar
2001-08-03 22:02   ` James Rogers
2001-08-06 22:33   ` Stanley R. Allen
2001-08-07  2:45     ` tmoran
2001-08-07 12:15       ` Larry Kilgallen
2001-08-07 13:26         ` Philip Anderson
2001-08-08  2:23         ` Robert Dewar
2001-08-08  5:58           ` Ehud Lamm
2001-08-08  2:19       ` Robert Dewar
2001-08-08 15:13         ` Ted Dennison
2001-08-08 18:03           ` tmoran
2001-08-09 20:36           ` Florian Weimer
2001-08-10 21:02         ` Jay Nabonne
2001-08-10 21:51           ` Larry Kilgallen
2001-08-13 14:19             ` Ted Dennison
2001-08-13 14:05           ` Ted Dennison
2001-08-13 14:19             ` Marin David Condic
2001-08-13 15:47             ` Ole-Hjalmar Kristensen
2001-08-13 16:22               ` Marin David Condic
2001-08-13 18:48               ` Larry Kilgallen
2001-08-14  7:05                 ` Ole-Hjalmar Kristensen
2001-08-13 20:20               ` James Rogers
2001-08-14  1:09                 ` Warren W. Gay VE3WWG
2001-08-14  6:15                   ` James Rogers
2001-08-14 14:03                     ` Warren W. Gay VE3WWG
2001-08-21  5:54                   ` C strings, was " David Thompson
2001-08-16 18:42                 ` Jay Nabonne
2001-08-17  1:25                   ` Robert Dewar
2001-08-13 21:47               ` Ted Dennison
2001-08-14  7:37                 ` Ole-Hjalmar Kristensen
2001-08-14 14:59                   ` Ted Dennison
2001-08-14 13:22                 ` Marin David Condic
2001-08-14 15:12                   ` Ted Dennison
2001-08-14 15:33                     ` Marin David Condic
2001-08-14  8:49               ` Lutz Donnerhacke
2001-08-14  9:38                 ` Ole-Hjalmar Kristensen
2001-08-14  9:54                   ` Lutz Donnerhacke
2001-08-14 14:51                     ` James Rogers
2001-08-14 16:44                   ` Darren New
2001-08-14  1:39             ` Warren W. Gay VE3WWG [this message]
replies disabled

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