comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Empty String confusion; Positive and Natural
Date: Mon, 29 Nov 2021 21:19:16 +0200	[thread overview]
Message-ID: <j0kndlFnvleU1@mid.individual.net> (raw)
In-Reply-To: <053c8a45-2829-4f2e-925c-b72308c1fe61n@googlegroups.com>

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).

  parent reply	other threads:[~2021-11-29 19:19 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-29 17:34 Empty String confusion; Positive and Natural Kevin Chadwick
2021-11-29 18:05 ` Jeffrey R.Carter
2021-11-29 19:19 ` Niklas Holsti [this message]
2021-11-29 22:50   ` Kevin Chadwick
2021-11-30  8:29     ` Simon Wright
2021-11-30  8:34     ` Niklas Holsti
2021-11-30 10:00     ` Jeffrey R.Carter
2021-11-30 12:17       ` Kevin Chadwick
2021-11-30 12:54         ` J-P. Rosen
2021-11-30 13:12           ` Kevin Chadwick
2021-11-30  7:40 ` ldries46
2021-11-30  8:21   ` Simon Wright
2021-11-30 12:39     ` ldries46
replies disabled

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