From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Lower bounds of Strings Date: Fri, 15 Jan 2021 12:48:25 +0100 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <1cc09f04-98f2-4ef3-ac84-9a9ca5aa3fd5n@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 15 Jan 2021 11:48:25 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="0a4722599d5fc32be9a107ac3d80a907"; logging-data="8010"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8DIeE4G2iyB3H9Fv8K3+fLnauPnHkwwc=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 Cancel-Lock: sha1:8Acjnzhgnd7Ki+eLXCbR61HYK5g= In-Reply-To: Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:61139 List-Id: 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