comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Lower bounds of Strings
Date: Fri, 15 Jan 2021 12:48:25 +0100	[thread overview]
Message-ID: <rtrve9$7qa$1@dont-email.me> (raw)
In-Reply-To: <fbe75221-b91c-4d98-b537-61f6b33acae6n@googlegroups.com>

On 1/15/21 11:24 AM, Stephen Davies wrote:
> 
> I think the root of the problem is that Ada Strings almost always
> start at 1 (note that the functions in Ada.Strings.Fixed all
> return Strings that start at 1), so the cases when they don't
> are at best annoying, and potentially erroneous.

There are many cases where having String values with a lower bound other than 1 
is more convenient, clearer, and less error prone than if all String values have 
a lower bound of 1. For example

loop
    exit when End_Of_File;

    declare
       Line : constant String := Get_Line;
    begin
       Idx := 0;

       loop
          Idx := Index (Line (Idx + 1 .. Line'Last), Pattern);

          exit when Idx = 0;

          Put_Line (Item => Idx'Image);
       end loop;
    end;
end loop;

where Index is Ada.Strings.Fixed.Index. Even without comments and descriptive 
loop and block names, this is reasonably clear.

Compare that to a language where the slice slides to have a lower bound of 1 
(because Index takes a String, which always has a lower bound of 1), and you'll 
see that it is more complex, less clear, and has more opportunities for error 
than current Ada.

A string, being a sequence, should usually have a lower bound of 1, but a decent 
language needs to also allow string values with other lower bounds. Maybe 
something like

type String_Base is array (Positive range <>) of Character;
subtype String is String_Base (Positive range 1 .. <>);

Slices would be String_Base, not String, and Index would take String_Base.

-- 
Jeff Carter
"[I]t is more important to make the purpose
of the code unmistakable than to display
virtuosity. Even storage requirements and
execution time are unimportant by
comparison ..."
Elements of Programming Style
184

  parent reply	other threads:[~2021-01-15 11:48 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
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 [this message]
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