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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Substrings as argument to procedures/functions Date: Tue, 12 Apr 2016 11:34:56 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <2055a188-fb5f-496a-ab37-b25d81cebe1b@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 12 Apr 2016 18:31:42 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="48b46be33beed75863f69afa437f956b"; logging-data="5357"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19aifPGhJG8SEUZt0FG0Lcp0kZwPSPiypE=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: <2055a188-fb5f-496a-ab37-b25d81cebe1b@googlegroups.com> Cancel-Lock: sha1:A5VBIJdoVc0f2+JIGcwWlc/WBdM= Xref: news.eternal-september.org comp.lang.ada:30085 Date: 2016-04-12T11:34:56-07:00 List-Id: On 04/12/2016 01:25 AM, reinkor wrote: > > I believed that if a substring is an argument to a subroutine, > then its "range" would start at 1 (and not inherited from the calling program). > But this seems not to be the case. What would you expect for a slice of a 1D array other than String? What would you expect for a slice of a 1D array with a non-numeric index type? Remember that the declaration of type String is type String is array (Positive range <>) of Character with Pack; (ARM A.1, http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-1.html); it's no different than any other 1D array type. And it's possible to declare your own string types (ARM 3.6.3), so you could declare type Str is array (Positive range <>) of Character; and do anything with it that you can do with String. This isn't very useful, but a possibly useful example would be subtype Hex_Range is Natural range 0 .. 15; type Hex_Image_List is array (Hex_Range) of Character; Hex_Image : constant Hex_Image_List := "0123456789ABCDEF"; which allows you to map a hex-digit value onto its Character representation. Note also that when you call a subprogram with a parameter of type String, the actual parameter is always a subtype of String, with the subtype determined by its bounds. During the call, the formal parameter takes on the subtype of the actual parameter. While one normally declares a subtype of String of length 3 as S : String (1 .. 3); there's no reason one can't declare S : String (3 ..5); and sometimes there are good reasons for such a declaration. Should the subtype and the good reasons for its declaration disappear if S is passed to a subprogram? A slice has a different subtype than the array being sliced, and sometimes there are good reasons for it to have a different subtype. > Is this correct? And in case, is it "good" ? :-) Yes, it's good. For example, parsing a simple CSV file (no quoted fields) using an Index function from Ada.Strings.Fixed looks like Source : constant String := Get_Line (CSV_File); Start : Positive := Source'First; Stop : Natural; Parse : loop Stop := Index (Source (Start .. Source'Last), ","); if Stop = 0 then Stop := Source'Last; else Stop := Stop - 1; end if; -- Do something with Source (Start .. Stop) exit Parse when Stop = Source'Last; Start := Stop + 2; end loop Parse; This works because the 1st formal parameter of Index retains the subtype of its actual parameter, which is the slice Source (Start .. Source'Last). If it didn't, the values it returned would always be relative to the start of the slice passed to it, and the example would be much more complicated. Finally, in Ada a string type with the lower bound always 1 looks like type String_1 (Length : Natural) is record Value : String (1 .. Length); end record; and you can define slicing for it: function Slice (Source : String_1; Low : Positive; High : Natural) return String_1; -- Jeff Carter "Apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, the fresh water system, and public health, what have the Romans ever done for us?" Monty Python's Life of Brian 80