comp.lang.ada
 help / color / mirror / Atom feed
* Bounds of Slice's return value
@ 2002-01-18 23:16 Matthew Woodcraft
  2002-01-19  1:24 ` Jeffrey Carter
  2002-01-21 20:19 ` Matthew Heaney
  0 siblings, 2 replies; 6+ messages in thread
From: Matthew Woodcraft @ 2002-01-18 23:16 UTC (permalink / raw)



Ada.Strings.Unbounded.Slice returns a Standard.String.

Am I guaranteed anything about the bounds of this string?

In particular, if I run the following program, should I always get '1',
or is an implementation allowed to choose a different value?


with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;

procedure Slice_Return_Value is
   U : Unbounded_String := To_Unbounded_String ("hello");
   S : String := Slice (U, 2, 3);
begin
   Put_Line (Integer'Image (S'first));
end Slice_Return_Value;

-M-



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Bounds of Slice's return value
  2002-01-18 23:16 Bounds of Slice's return value Matthew Woodcraft
@ 2002-01-19  1:24 ` Jeffrey Carter
  2002-01-19 10:45   ` Matthew Woodcraft
  2002-01-21 20:19 ` Matthew Heaney
  1 sibling, 1 reply; 6+ messages in thread
From: Jeffrey Carter @ 2002-01-19  1:24 UTC (permalink / raw)


Matthew Woodcraft wrote:
> 
> Ada.Strings.Unbounded.Slice returns a Standard.String.
> 
> Am I guaranteed anything about the bounds of this string?

As you noticed, the ARM is silent about this. The ARG is working towards
(if they haven't already completed) an AI stating that the bounds should
be the same as the indices passed to Slice, to correspond with the
bounds of a slice of a String. GNAT noticeably does not do this in the
most recent public version (3.13p), though I believe this has been
changed in more recent (as yet non-public) versions.

See program Strm_Sub, distributed as part of the PragmAda Reusable
Components, for an example of using Slice portably even when the lower
bound returned is one.

-- 
Jeff Carter
"I blow my nose on you."
Monty Python & the Holy Grail



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Bounds of Slice's return value
  2002-01-19  1:24 ` Jeffrey Carter
@ 2002-01-19 10:45   ` Matthew Woodcraft
  2002-01-19 14:32     ` Robert A Duff
  0 siblings, 1 reply; 6+ messages in thread
From: Matthew Woodcraft @ 2002-01-19 10:45 UTC (permalink / raw)


Jeffrey Carter <jrcarter@acm.org> writes:

> Matthew Woodcraft wrote:
> > 
> > Ada.Strings.Unbounded.Slice returns a Standard.String.
> > 
> > Am I guaranteed anything about the bounds of this string?
> 
> As you noticed, the ARM is silent about this. The ARG is working towards
> (if they haven't already completed) an AI stating that the bounds should
> be the same as the indices passed to Slice, to correspond with the
> bounds of a slice of a String.

Thanks. I'm glad they're nailing it down; it seems to be a needless
source of hard-to-spot errors, otherwise.


> GNAT noticeably does not do this in the most recent public version
> (3.13p), though I believe this has been changed in more recent (as yet
> non-public) versions.

Indeed, I came across this because one of my unit tests failed when I
tried compiling with cvs gcc.

-M-



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Bounds of Slice's return value
  2002-01-19 10:45   ` Matthew Woodcraft
@ 2002-01-19 14:32     ` Robert A Duff
  2002-01-19 17:19       ` Matthew Woodcraft
  0 siblings, 1 reply; 6+ messages in thread
From: Robert A Duff @ 2002-01-19 14:32 UTC (permalink / raw)


Matthew Woodcraft <mattheww@chiark.greenend.org.uk> writes:

> Thanks. I'm glad they're nailing it down; it seems to be a needless
> source of hard-to-spot errors, otherwise.

The fact that Ada allows strings to start at other than 1, depending on
where they came from, is a "needless source of hard-to-spot errors" even
in cases where it *is* nailed down.  Sigh.

- Bob



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Bounds of Slice's return value
  2002-01-19 14:32     ` Robert A Duff
@ 2002-01-19 17:19       ` Matthew Woodcraft
  0 siblings, 0 replies; 6+ messages in thread
From: Matthew Woodcraft @ 2002-01-19 17:19 UTC (permalink / raw)


Robert A Duff <bobduff@shell01.TheWorld.com> writes:

> Matthew Woodcraft <mattheww@chiark.greenend.org.uk> writes:
> > Thanks. I'm glad they're nailing it down; it seems to be a needless
> > source of hard-to-spot errors, otherwise.

> The fact that Ada allows strings to start at other than 1, depending
> on where they came from, is a "needless source of hard-to-spot errors"
> even in cases where it *is* nailed down. Sigh.

I agree it's an unpleasant source of errors. But it's only needless if
there's a fix available.

Perhaps saying that strings are just arrays of characters isn't such a
good idea? Or do you think arrays with differing lower bounds are the
trouble?

-M-



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Bounds of Slice's return value
  2002-01-18 23:16 Bounds of Slice's return value Matthew Woodcraft
  2002-01-19  1:24 ` Jeffrey Carter
@ 2002-01-21 20:19 ` Matthew Heaney
  1 sibling, 0 replies; 6+ messages in thread
From: Matthew Heaney @ 2002-01-21 20:19 UTC (permalink / raw)



"Matthew Woodcraft" <mattheww@chiark.greenend.org.uk> wrote in message
news:87bsfrcksf.fsf@chiark.greenend.org.uk...
> In particular, if I run the following program, should I always get '1',
> or is an implementation allowed to choose a different value?
>
> procedure Slice_Return_Value is
>    U : Unbounded_String := To_Unbounded_String ("hello");
>    S : String := Slice (U, 2, 3);
> begin
>    Put_Line (Integer'Image (S'first));
> end Slice_Return_Value;

You can fix this to specify the index constraint you desire, using a subtype
constraint:

declare
   F : constant Positive := 2;
   L : constant Positive := 3;
   S : constant String (1 .. L - F + 1) := Slice (U, F, L);
begin

or

declare
   F : constant Positive := 2;
   L : constant Positive := 3;
   S : constant String (F .. L) := Slice (U, F, L);
begin

Of course, this declares a constrained string object (no dope vector is
generated), so this is not strictly comparable to your original example.
You could fix that by using an explicit subtype:

declare
   F : constant Positive := 2;
   L : constant Positive := 3;
   subtype T is String (1 .. L - F + 1);
   S : constant String := T(Slice (U, F, L));
begin

or

declare
   F : constant Positive := 2;
   L : constant Positive := 3;
   subtype T is String (F .. L);
   S : constant String := T(Slice (U, F, L));
begin






^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2002-01-21 20:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-18 23:16 Bounds of Slice's return value Matthew Woodcraft
2002-01-19  1:24 ` Jeffrey Carter
2002-01-19 10:45   ` Matthew Woodcraft
2002-01-19 14:32     ` Robert A Duff
2002-01-19 17:19       ` Matthew Woodcraft
2002-01-21 20:19 ` Matthew Heaney

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