If something goes wrong and I run the create function with "" then the strings length is zero. Why is a string type Positive and not Natural, because empty strings are in fact useless?
I suspect that I am doing something outside of Ada norms.
Are empty strings avoided in Ada?
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.
If something goes wrong and I run the create function with "" then
the strings length is zero.
Why is a string type Positive and not Natural, because empty strings
are in fact useless?
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?
I suspect that I am doing something outside of Ada norms.From your description, I suspect that you are using different
Are empty strings avoided in Ada?
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.
If something goes wrong and I run the create function with "" then the strings length is zero. Why is a string type Positive and not Natural, because empty strings are in fact useless?
I guess I shall consider re-engineering my functions, rather than add checks, but all thoughts are welcome.
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?
Hopefully I will have a doh moment when I get into the office in the
morning and post code. I am getting a range check exception when I
assign "" to a standard.String
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?
Hopefully I will have a doh moment when I get into the office in the
morning and post code. I am getting a range check exception when I
assign "" to a standard.String in a record by returning the record
from a function. Surely, I must have caused it somehow.
Empty_String : Unbounded_String := To_Unbounded_String("");
Hopefully I will have a doh moment when I get into the office in the morning and post code. I am getting a range check exception when I assign "" to a standard.String in a record by returning the record from a function. Surely, I must have caused itsomehow.
ldries46 <bertus.dries@planet.nl> writes:You are right. it should be
Empty_String : Unbounded_String := To_Unbounded_String("");* Given the name, you should probably declare it constant.
* You don't need to initialize it, ARM 4.5(73) [1] says "If an object of
type Unbounded_String is not otherwise initialized, it will be
initialized to the same value as Null_Unbounded_String".
* Ada.Strings.Unbounded.Null_Unbounded_String is a constant empty
string.
[1] http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-4-5.html#p73
Since String is indexed by Positive (not Natural), the lower bound
cannot be 0. OTOH, there is no check for bounds of empty arrays,
therefore the following would work:
type RunOutput(LogLen, OutputLen : Natural) is record
LogStr : String(1..LogLen);
Output : String(1..OutputLen);
and you can have empty strings if Loglen or Outputlen is 0, without Constraint_Error. (It's precisely for that reason that there is no check
of bounds of empty arrays).
I have a create function that returns a type RunOutput
I always get range check failed even with a string of multiple characters.
type RunOutput(LogLen, OutputLen : Natural) is record
LogStr : String(0..LogLen);
Output : String(0..OutputLen);
...
end record;
I did have the following which works, so long as the string is not empty
type RunOutput(LogLen, OutputLen : Positive) is record
LogStr : String(1..LogLen);
Output : String(1..OutputLen);
...
end record;
P.s. I am not using exceptions with messages for log strings because I would rather use features that work with the zero footprint runtime as much as possible, where it makes sense.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 399 |
Nodes: | 16 (3 / 13) |
Uptime: | 64:54:29 |
Calls: | 8,355 |
Calls today: | 15 |
Files: | 13,159 |
Messages: | 5,893,946 |
Posted today: | 1 |