From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,45b47ecb995e7a3 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-08-13 18:39:24 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!headwall.stanford.edu!feeder.via.net!newshub2.rdc1.sfba.home.com!news.home.com!news1.rdc2.on.home.com.POSTED!not-for-mail Message-ID: <3B78814B.70B65E93@home.com> From: "Warren W. Gay VE3WWG" X-Mailer: Mozilla 4.75 [en] (Windows NT 5.0; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Slicing ( Ada Idioms Progress Preview ) References: <3B6F1B2F.4FC3C833@gsde.hou.us.ray.com> <5ee5b646.0108071819.6e84e33d@posting.google.com> <3_Xc7.45$NM5.84779@news.pacbell.net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Tue, 14 Aug 2001 01:39:24 GMT NNTP-Posting-Host: 24.141.193.224 X-Complaints-To: abuse@home.net X-Trace: news1.rdc2.on.home.com 997753164 24.141.193.224 (Mon, 13 Aug 2001 18:39:24 PDT) NNTP-Posting-Date: Mon, 13 Aug 2001 18:39:24 PDT Organization: Excite@Home - The Leader in Broadband http://home.com/faster Xref: archiver1.google.com comp.lang.ada:11882 Date: 2001-08-14T01:39:24+00:00 List-Id: 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