comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Lower bounds of Strings
Date: Tue, 5 Jan 2021 12:57:16 +0100	[thread overview]
Message-ID: <rt1k6r$20c$1@gioia.aioe.org> (raw)
In-Reply-To: 1cc09f04-98f2-4ef3-ac84-9a9ca5aa3fd5n@googlegroups.com

On 2021-01-05 12:04, Stephen Davies wrote:
> 
> I'm sure this must have been discussed before, but the issue doesn't
> seem to have been resolved and I think it makes Ada code look ugly and
> frankly reflects poorly on the language.

There is no issue, it must be this way.

> I'm referring to the fact that any subprogram with a String
> parameter, e.g. Expiration_Date, has to use something like
> Expiration_Date (Expiration_Date'First .. Expiration_Date'First + 1)
> to refer to the first two characters rather than simply saying
> Expiration_Date (1..2).

This is a different operation. See function Head in Ada.Strings.Fixed. I 
does what you want.

> Not only is it ugly, but it's potentially dangerous if code uses the
> latter and works for ages until one day somebody passes a slice instead
> of a string starting at 1 (yes, compilers might generate warnings,
> but that doesn't negate the issue, imho).

Yes, slicing using constant values is dangerous. Slicing using indices 
is safe and consitent.

> There must be many possible solutions, without breaking compatibility
> for those very rare occasions where code actually makes use of the
> lower bound of a string.
> 
> e.g. Perhaps the following could be made legal and added to Standard:
> 
> subtype Mono_String is String (1 .. <>);

This is a constraint, a meaningless and dangerous one:

    procedure Foo (X : Mono_String);
    S : String := "abcdefgh";
begin
    Foo (S (2..S'Last)); -- Boom! Constraint_Error

> One question with this would be whether or not to allow procedure bodies
> to specify parameters as Mono_String when the corresponding procedure
> declaration uses String.

There are two separate issues.

1. Explicit forced index sliding. There is no operation for that, though 
it could be easily implemented, e.g.

    function "abs" (S : String) return String is
       Result : constant String (1..S'Length) := S;
    begin
       return Result;
    end "abs";

2. Position-based indices an slices. One could add [] brackets for that 
stuff to make C crowd happy.

    S [1] - First array element
    S (1) - Array element at index 1

Same with slices. Differently to #1, this is would not require excessive 
string copying.

------------------
The real problem is though, that traditionally integer types were used 
for array indices since very beginning of computing.

They never should be because indices are not additive:

    A'First + A'First

is garbage. It is same as with Time and Duration. Time is index (a fixed 
point on the time axis). Duration is position, an offset to some 
unspecified epoch. You can do Time + Duration and Duration + Duration, 
but not Time + Time. So addition must apply to index and position or two 
positions only.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  reply	other threads:[~2021-01-05 11:57 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-05 11:04 Lower bounds of Strings Stephen Davies
2021-01-05 11:57 ` Dmitry A. Kazakov [this message]
2021-01-05 12:32   ` Jeffrey R. Carter
2021-01-05 13:40     ` Dmitry A. Kazakov
2021-01-05 14:31       ` Stephen Davies
2021-01-05 17:24         ` Stephen Davies
2021-01-05 18:28           ` Jeffrey R. Carter
2021-01-05 21:02             ` Stephen Davies
2021-01-07 10:38               ` Stephen Davies
2021-01-07 21:39                 ` Randy Brukardt
2021-01-07 22:38                   ` Stephen Davies
2021-01-05 12:24 ` Luke A. Guest
2021-01-05 12:49 ` Simon Wright
2021-01-05 12:51 ` Jeffrey R. Carter
2021-01-06  3:08 ` Randy Brukardt
2021-01-06  9:13   ` Dmitry A. Kazakov
2021-01-07  0:17     ` Randy Brukardt
2021-01-07  9:57       ` Dmitry A. Kazakov
2021-01-07 22:03         ` Randy Brukardt
2021-01-08  9:04           ` Dmitry A. Kazakov
2021-01-08 17:23           ` Shark8
2021-01-08 20:19             ` Dmitry A. Kazakov
2021-01-09  2:18               ` Randy Brukardt
2021-01-09 10:53                 ` Dmitry A. Kazakov
2021-01-12  8:19                   ` Randy Brukardt
2021-01-12  9:37                     ` Dmitry A. Kazakov
2021-01-09  2:31             ` Randy Brukardt
2021-01-09 14:52               ` Why UTF-8 (was Re: Lower bounds of Strings) Jeffrey R. Carter
2021-01-09 18:08                 ` Dmitry A. Kazakov
2021-01-12  7:58                   ` Randy Brukardt
2021-01-11 21:35               ` Lower bounds of Strings Shark8
2021-01-12  8:12                 ` Randy Brukardt
2021-01-12 20:51                   ` Shark8
2021-01-12 22:56                     ` Randy Brukardt
2021-01-13 12:00                       ` Dmitry A. Kazakov
2021-01-13 13:27                         ` AdaMagica
2021-01-13 13:53                           ` Dmitry A. Kazakov
2021-01-13 14:08                   ` Jeffrey R. Carter
2021-01-14 11:38 ` AdaMagica
2021-01-14 12:27   ` Dmitry A. Kazakov
2021-01-14 13:31   ` AdaMagica
2021-01-14 14:02   ` Jeffrey R. Carter
2021-01-14 14:34     ` Dmitry A. Kazakov
2021-01-14 15:28       ` Shark8
2021-01-14 15:41         ` Dmitry A. Kazakov
2021-01-19 21:02           ` G.B.
2021-01-19 22:27             ` Dmitry A. Kazakov
2021-01-20 20:10               ` G.B.
2021-01-20 20:25                 ` Dmitry A. Kazakov
2021-01-15 10:24   ` Stephen Davies
2021-01-15 11:41     ` J-P. Rosen
2021-01-15 17:35       ` Stephen Davies
2021-01-15 19:36         ` Egil H H
2021-01-16 12:57           ` Stephen Davies
2021-01-17 14:10         ` Stephen Davies
2021-01-19  5:48           ` Randy Brukardt
2021-01-19  6:13         ` Gautier write-only address
2021-01-15 11:48     ` Jeffrey R. Carter
2021-01-15 13:34       ` Dmitry A. Kazakov
2021-01-15 13:56       ` Stephen Davies
2021-01-15 15:12         ` Jeffrey R. Carter
2021-01-15 17:22           ` Stephen Davies
2021-01-15 21:10             ` Jeffrey R. Carter
2021-01-15 14:00       ` Stephen Davies
2021-01-16  9:30     ` G.B.
2021-01-16 13:13       ` Stephen Davies
replies disabled

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