comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Substrings as argument to procedures/functions
Date: Tue, 12 Apr 2016 11:34:56 -0700
Date: 2016-04-12T11:34:56-07:00	[thread overview]
Message-ID: <nejeuc$57d$1@dont-email.me> (raw)
In-Reply-To: <2055a188-fb5f-496a-ab37-b25d81cebe1b@googlegroups.com>

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

  parent reply	other threads:[~2016-04-12 18:34 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-12  8:25 Substrings as argument to procedures/functions reinkor
2016-04-12  9:01 ` Dmitry A. Kazakov
2016-04-12 16:55   ` reinkor
2016-04-12 18:37     ` Dmitry A. Kazakov
2016-04-13  4:44       ` reinkor
2016-04-13  7:19         ` Dmitry A. Kazakov
2016-04-13 10:16         ` Brian Drummond
2016-04-13 11:43           ` reinkor
2016-04-13 11:54             ` Pascal Obry
2016-04-13 12:30               ` Mart van de Wege
2016-04-13 12:47                 ` Egil H H
2016-04-13 13:19                   ` Mart van de Wege
2016-04-13 16:57                 ` Jeffrey R. Carter
2016-04-12 18:17 ` Robert A Duff
2016-04-12 18:34 ` Jeffrey R. Carter [this message]
2016-04-13 21:29   ` Randy Brukardt
2016-04-25 15:33     ` rieachus
2016-04-25 22:07       ` Randy Brukardt
2016-04-26  6:12         ` Georg Bauhaus
2016-04-26 18:41           ` Randy Brukardt
2016-04-13 11:22 ` G.B.
replies disabled

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