From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-2.9 required=3.0 tests=BAYES_00,NICE_REPLY_A autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader02.eternal-september.org!news.szaf.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Empty String confusion; Positive and Natural Date: Mon, 29 Nov 2021 21:19:16 +0200 Organization: Tidorum Ltd Message-ID: References: <053c8a45-2829-4f2e-925c-b72308c1fe61n@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net qKzs4ahAdx5Fs7XUnaIYow7o+/eteU0wkUrfC3I5oFa3zLEDUq Cancel-Lock: sha1:gzT1oXWavUtYAQH3vfnuvTxl4bc= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 In-Reply-To: <053c8a45-2829-4f2e-925c-b72308c1fe61n@googlegroups.com> Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:63163 List-Id: On 2021-11-29 19:34, Kevin Chadwick wrote: > I suspect that I am doing something outside of Ada norms. > > Are empty strings avoided in Ada? No, empty strings (and empty arrays in general) are quite usable. > I have a function to create a record containing strings amongst other > things like a subtyped integer, simply to group the outputs neatly of > another function. It would clarify your question if you would show the declaration of that record type. Does it use the String type, or the Unbounded_String type? > If something goes wrong and I run the create function with "" then > the strings length is zero. Is there something wrong with that? An empty string should have length zero, it seems to me. > Why is a string type Positive and not Natural, because empty strings > are in fact useless? An object S : String is an empty string if and only if S'Length = 0. (And, as Jeff said, S'Length is not a Positive, but a universal integer, so it can be zero even if Strings are indexed with Positive numbers.) Also, an object S : String is an empty string if and only if S'Last < S'First. Note that S'Last can be much less than S'First, so in this case you cannot compute the length of the string from S'Last - S'First + 1. Or if you do, you can get a negative result, and understand that as zero length. The case where S'Last is much less than S'First usually comes about when one takes an empty slice of an existing string, for example X(5 .. 2), where X is for example String (1 .. 10).