comp.lang.ada
 help / color / mirror / Atom feed
* Re: Some problems with string type
  2001-06-07  2:18 Some problems with string type Valdes
@ 2001-06-06 16:34 ` Philip Anderson
  2001-06-06 20:29   ` Some problems with string type(Exact String?) McDoobie
  2001-06-07  4:06   ` Some problems with string type Valdes
  2001-06-06 17:49 ` tmoran
  1 sibling, 2 replies; 11+ messages in thread
From: Philip Anderson @ 2001-06-06 16:34 UTC (permalink / raw)


Valdes wrote:
> 
> I have my program writen in Ada that raise some exceptions:
> 
> raised CONSTRAINT_ERROR : gestindex.adb:5
> 
> ... And at line 5...
> 
>  function createindex (S:string) return index is
>       I:Index;
>    begin
>       i.nameindex:=s; -- Ligne cinq

Note that Constraint Error will be raised if s is not _exactly_ 20
characters long.

>       i.firstletter:=null;
>       return i;
>    end createindex;
> 
> ... The index record is defined as follow...
> 
>   type index is record
>       nameindex:String(1..20);
>       firstletter:ptrletter;
>    end record;
> 
> ... The mistake seems to be trivial but I don't see it.
> 
> I have seen the same for this also:
> 
>     Ptrtemp.Letter:=Charat(M,Ni);
> 
> there is the same error but Charat return a character and letter is also
> one. (ptrtemp is also type ptrlettre)

Is M a String and Ni an index into it?  If so Ni must be within the
range M'first .. M'last, and M'first may be greater than 1 (and won't be
0).


> All my greetings if you have some ideas. And forgive me for my bad
> english.

Pas de probleme.

> --
> Le Vif Z�phyr Jubile Sur Les Kumquats Du Clown Gracieux.
The quick brown fox jumps over the lazy dog.


-- 
hwyl/cheers,
Philip Anderson
Alenia Marconi Systems
Cwmbr�n, Cymru/Wales



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

* Re: Some problems with string type
  2001-06-07  2:18 Some problems with string type Valdes
  2001-06-06 16:34 ` Philip Anderson
@ 2001-06-06 17:49 ` tmoran
  2001-06-07  4:07   ` Valdes
  1 sibling, 1 reply; 11+ messages in thread
From: tmoran @ 2001-06-06 17:49 UTC (permalink / raw)


>     i.nameindex:=s; -- Ligne cinq
i.nameindex is a 20 character String.  Does S have 20 characters?



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

* Re: Some problems with string type(Exact String?)
  2001-06-06 16:34 ` Philip Anderson
@ 2001-06-06 20:29   ` McDoobie
  2001-06-06 21:30     ` tmoran
                       ` (2 more replies)
  2001-06-07  4:06   ` Some problems with string type Valdes
  1 sibling, 3 replies; 11+ messages in thread
From: McDoobie @ 2001-06-06 20:29 UTC (permalink / raw)


In article <3B1E5BAA.49F1EF9B@amsjv.com>, Philip Anderson
<phil.anderson@amsjv.com> wrote:

> Valdes wrote:
>> 
>> I have my program writen in Ada that raise some exceptions:
>> 
>> raised CONSTRAINT_ERROR : gestindex.adb:5
>> 
>> ... And at line 5...
>> 
>>  function createindex (S:string) return index is
>>       I:Index;
>>    begin
>>       i.nameindex:=s; -- Ligne cinq
> 
> Note that Constraint Error will be raised if s is not _exactly_ 20
> characters long.
> 
>>       i.firstletter:=null; return i;
>>    end createindex;
>> 
>> ... The index record is defined as follow...
>> 
>>   type index is record
>>       nameindex:String(1..20); firstletter:ptrletter;
>>    end record;
>> 
>> ... The mistake seems to be trivial but I don't see it.
>> 
>> I have seen the same for this also:
>> 
>>     Ptrtemp.Letter:=Charat(M,Ni);
>> 
>> there is the same error but Charat return a character and letter is
>> also one. (ptrtemp is also type ptrlettre)
> 
> Is M a String and Ni an index into it?  If so Ni must be within the
> range M'first .. M'last, and M'first may be greater than 1 (and won't be
> 0).
> 
> 
>> All my greetings if you have some ideas. And forgive me for my bad
>> english.
> 
> Pas de probleme.

Alright, so in this example, the string has to be EXACTLY 20 characters.

Now, in a couple other languages I've used, the string can be String<20
characters but  not 20<String, rather than having to be String=20. So if I
understand correctly, a string (or  more appropriately the elements of an
ARRAY) have to precisely match the declared  bounds of the array. Yes, no?

I'm currently reading through the "Arrays and Records" chapter of
"Programming in Ada95" by Barnes. It's throwing alot of information at me,
and I'm hitting a couple kinks in digesting  it all. But from what I can
glean, in Ada95 arrays are fixed. Unlike C where I could go less  than but
not greater than the bounds of the array.  My first hunch on the way to
work with this strictness(or so it seems) would be keep count of the
number of elements (characters of a string, numbers, whatever) going into
the array, and then pad the rest of the array with with NULLs or something
similar when there is no more  information to add to the array.

Am I thinking along the right track here? Or am I missing something
important?

McDoobie chris@dont.spam.me



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

* Re: Some problems with string type(Exact String?)
  2001-06-06 20:29   ` Some problems with string type(Exact String?) McDoobie
@ 2001-06-06 21:30     ` tmoran
  2001-06-07  8:07       ` Jean-Pierre Rosen
  2001-06-06 22:08     ` Marin David Condic
  2001-06-07  6:48     ` Ada.Strings.Fixed procedure Move Petter Fryklund
  2 siblings, 1 reply; 11+ messages in thread
From: tmoran @ 2001-06-06 21:30 UTC (permalink / raw)


>not greater than the bounds of the array.  My first hunch on the way to
>work with this strictness(or so it seems) would be keep count of the
>number of elements (characters of a string, numbers, whatever) going into
>the array, and then pad the rest of the array with with NULLs or something
  In Barnes, look at the chapter "Predefined Library", section "Characters
and strings" and you'll see Ada.Strings.Bounded (and Ada.Strings.Unbounded).
Bounded strings essentially keep a "current length", along with the fixed
length array.  There's no need then to pad with nulls or anything else.

  Alternatively, you could change
>       i.nameindex:=s; -- Ligne cinq
to
  if s'length > i.nameindex'lenth then    -- just take the first part
    i.nameindex := s(s'first .. s'first-1+i.nameindex'length)
  else  -- pad with blanks
    i.nameindex := s & (s'length+1 .. i.nameindex'length => ' ');
  end if;

or
  i.nameindex := s(s'first .. integer'min(s'length, i.nameindex'length)
                              + s'first -1)
                 & (1 .. i.namelength'length - s'length => ' ');
which would truncate if s is too long, and pad with ' ' if s is too short.

or, more simply,
  i.nameindex := Ada.Strings.Fixed.Head(s, i.namelength'length, pad => ' ');



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

* Re: Some problems with string type(Exact String?)
  2001-06-06 20:29   ` Some problems with string type(Exact String?) McDoobie
  2001-06-06 21:30     ` tmoran
@ 2001-06-06 22:08     ` Marin David Condic
  2001-06-07  6:48     ` Ada.Strings.Fixed procedure Move Petter Fryklund
  2 siblings, 0 replies; 11+ messages in thread
From: Marin David Condic @ 2001-06-06 22:08 UTC (permalink / raw)


In Ada, a string is just an array of characters - the array bounds have to
be fixed for an object of type String. The string you declare is thus always
N elements big. This is true of C as well. Any arrays of characters you
allocate have some fixed length for their lifetime. Referencing something
outside of their length is not checked by C - hence instead of an exception,
you get an error that may or may not kill the program on any given day of
the week and when it dies mysteriously, you get to haul out your debugger.

By *convention* (and ONLY by convention) when you have an array of char in
C, you stick null characters into the unused portions and all string
handling routines look for this as an indicator of the end of the string.
You could just as easily write a bunch of string routines in Ada that
depended on null termination. Its just that there are better ways.

When using fixed length strings, remember that the attributes 'First, 'Last,
'Range, etc. apply to the *array* and not to the *content* of that array. If
you want strings with more C-ish behavior look at the package
Ada.Strings.Bounded. This is essentially a record with a string element of
some maximum length and a current "length" element telling you where you
stopped storing characters in it. Hence they have a maximum size, but will
give you subprograms to work with the content. If you want to avoid the
whole mess - go to Ada.Strings.Unbounded and use that. This will have some
version of dynamic memory allocation to satisfy the current size of whatever
you store there. Its more expensive time-wise, but the size is always
whatever the contents is.

You *could* do what you suggest as far as padding - but in my experience,
its just a lot easier to convert anything of type String into something of
type Unbounded_String and use all the stuff you have in
Ada.Strings.Unbounded to manipulate the contents.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/

"McDoobie" <someone@nospam.net> wrote in message
news:lowT6.40598$DG1.6446489@news1.rdc1.mi.home.com...
>
> Alright, so in this example, the string has to be EXACTLY 20 characters.
>
> Now, in a couple other languages I've used, the string can be String<20
> characters but  not 20<String, rather than having to be String=20. So if I
> understand correctly, a string (or  more appropriately the elements of an
> ARRAY) have to precisely match the declared  bounds of the array. Yes, no?
>
> I'm currently reading through the "Arrays and Records" chapter of
> "Programming in Ada95" by Barnes. It's throwing alot of information at me,
> and I'm hitting a couple kinks in digesting  it all. But from what I can
> glean, in Ada95 arrays are fixed. Unlike C where I could go less  than but
> not greater than the bounds of the array.  My first hunch on the way to
> work with this strictness(or so it seems) would be keep count of the
> number of elements (characters of a string, numbers, whatever) going into
> the array, and then pad the rest of the array with with NULLs or something
> similar when there is no more  information to add to the array.
>
> Am I thinking along the right track here? Or am I missing something
> important?
>
> McDoobie chris@dont.spam.me





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

* Some problems with string type
@ 2001-06-07  2:18 Valdes
  2001-06-06 16:34 ` Philip Anderson
  2001-06-06 17:49 ` tmoran
  0 siblings, 2 replies; 11+ messages in thread
From: Valdes @ 2001-06-07  2:18 UTC (permalink / raw)


I have my program writen in Ada that raise some exceptions:

raised CONSTRAINT_ERROR : gestindex.adb:5

... And at line 5...

 function createindex (S:string) return index is
      I:Index;
   begin
      i.nameindex:=s; -- Ligne cinq
      i.firstletter:=null;
      return i;
   end createindex;

... The index record is defined as follow...

  type index is record
      nameindex:String(1..20);
      firstletter:ptrletter;
   end record;

... The mistake seems to be trivial but I don't see it.

I have seen the same for this also:

    Ptrtemp.Letter:=Charat(M,Ni);

there is the same error but Charat return a character and letter is also
one. (ptrtemp is also type ptrlettre)

All my greetings if you have some ideas. And forgive me for my bad
english.

--
Le Vif Z�phyr Jubile Sur Les Kumquats Du Clown Gracieux.






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

* Re: Some problems with string type
  2001-06-06 16:34 ` Philip Anderson
  2001-06-06 20:29   ` Some problems with string type(Exact String?) McDoobie
@ 2001-06-07  4:06   ` Valdes
  2001-06-07 13:20     ` Valdes Laurent
  1 sibling, 1 reply; 11+ messages in thread
From: Valdes @ 2001-06-07  4:06 UTC (permalink / raw)




Philip Anderson a �crit :

> Valdes wrote:
> >
> > I have my program writen in Ada that raise some exceptions:
> >
> > raised CONSTRAINT_ERROR : gestindex.adb:5
> >
> > ... And at line 5...
> >
> >  function createindex (S:string) return index is
> >       I:Index;
> >    begin
> >       i.nameindex:=s; -- Ligne cinq
>
> Note that Constraint Error will be raised if s is not _exactly_ 20
> characters long.

I have undertood this one. It works fine now.

>
> >       i.firstletter:=null;
> >       return i;
> >    end createindex;
> >
> > ... The index record is defined as follow...
> >
> >   type index is record
> >       nameindex:String(1..20);
> >       firstletter:ptrletter;
> >    end record;
> >
> > ... The mistake seems to be trivial but I don't see it.
> >
> > I have seen the same for this also:
> >
> >     Ptrtemp.Letter:=Charat(M,Ni);
> >
> > there is the same error but Charat return a character and letter is also
> > one. (ptrtemp is also type ptrlettre)
>
> Is M a String and Ni an index into it?

M is not exactly a string. It is a pointer to a string. I had to make this one
for the first error.

>  If so Ni must be within the
> range M'first .. M'last, and M'first may be greater than 1 (and won't be
> 0).

Perhaps I have better to show the function:


--------------------------------------
  function addwordtoindex(I :Index;M:word) return Index is
      ptrtemp:ptrword:=I.firstletter;
      aux:firstletter;
      J:Index;
      long,Ni:Natural;
      B:Boolean;
   begin
      long:=Lengthword(M);Ni:=1;

      if (ptrtemp /= Null) then
         while(ptrtemp /=null) loop
            Aux:=ptrtemp;
            if (ptrtemp.letter /=Charat(M,ni+1)) then
               ptrtemp:=ptrtemp.right;
               B:=false;
            else
               ptrtemp:=ptrtemp.bottom;
               ni:=ni+1;
               B:=True;
            end if;
         end loop;
         ptrtemp:=Aux;
         if (B) then
            ptrtemp.bottom:=new Cellword;
            ptrtemp:=ptrtemp.bottom;
            Ptrtemp.letter:=Charat(M,Ni+1); --possible exception
         else
            ptrtemp.Droite:=new Celword;
            ptrtemp:=ptrtemp.right;
            Ptrtemp.Letter:=Charat(M,Ni+1);  --possible exception

         end if;
      else
          Ptrtemp:=new Cellword;
          Ptrtemp:=I.firstletter;
          Ptrtemp.Letter:=Charat(M,Ni);  --possible exception


      end if;
      Ni:=ni+1;

      while(ni<Long)loop
         ptrtemp.bottom:=new Cellword;
         ptrtemp:=ptrtemp.bottom;
         ni:=Ni+1;
      end loop;
      ptrtemp.end:=True;
      J.Nomindex:=I.nameindex;
      J.Premlettre:=I.firstletter;
      return J;
   end addwordtoindex;
------------------------
I use this function to put some words whose letters will be put into an index
- a tree -
They are an other function whose functionnality is to create a new index.
This one (that I show there) works bad.

The tree(with the words phone and phoque) :

p
|
h
|
o
|
n-q
| |
e u
  |
  e



          |
one node: r- right
          |
          bottom

And the signature:

Package Gestindex is

   type index is private;
   type cellword is private;
   type ptrword is access cellword;
   function addwordtoindex (I:Index;M:word) return index;

private
   type cellword is record
      word: character;
      bottom: ptrlettre;
      right: ptrlettre;
      end: Boolean;
   end record;

   type index is record
      nameindex:Mot;
      firstletter:ptrword;
   end record;
end Gestindex;

>
> > All my greetings if you have some ideas. And forgive me for my bad
> > english.
>
> Pas de probleme.

Thank you. All my greetings.


> > --
> > Le Vif Z�phyr Jubile Sur Les Kumquats Du Clown Gracieux.
> The quick brown fox jumps over the lazy dog.

Where did you read this sentence ? On some Macintosh ? ;-)





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

* Re: Some problems with string type
  2001-06-06 17:49 ` tmoran
@ 2001-06-07  4:07   ` Valdes
  0 siblings, 0 replies; 11+ messages in thread
From: Valdes @ 2001-06-07  4:07 UTC (permalink / raw)




tmoran@acm.org a �crit :

> >     i.nameindex:=s; -- Ligne cinq
> i.nameindex is a 20 character String.  Does S have 20 characters?

The hour before it was no. It is yes now. And it works. Thank you. You
should see my other message.





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

* Ada.Strings.Fixed procedure Move.
  2001-06-06 20:29   ` Some problems with string type(Exact String?) McDoobie
  2001-06-06 21:30     ` tmoran
  2001-06-06 22:08     ` Marin David Condic
@ 2001-06-07  6:48     ` Petter Fryklund
  2 siblings, 0 replies; 11+ messages in thread
From: Petter Fryklund @ 2001-06-07  6:48 UTC (permalink / raw)


can simplify assignments with truncation and/or padding.





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

* Re: Some problems with string type(Exact String?)
  2001-06-06 21:30     ` tmoran
@ 2001-06-07  8:07       ` Jean-Pierre Rosen
  0 siblings, 0 replies; 11+ messages in thread
From: Jean-Pierre Rosen @ 2001-06-07  8:07 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1115 bytes --]


<tmoran@acm.org> a �crit dans le message news: whxT6.68591$%i7.51923767@news1.rdc1.sfba.home.com...
> >not greater than the bounds of the array.  My first hunch on the way to
> >work with this strictness(or so it seems) would be keep count of the
> >number of elements (characters of a string, numbers, whatever) going into
> >the array, and then pad the rest of the array with with NULLs or something
>   In Barnes, look at the chapter "Predefined Library", section "Characters
> and strings" and you'll see Ada.Strings.Bounded (and Ada.Strings.Unbounded).
> Bounded strings essentially keep a "current length", along with the fixed
> length array.  There's no need then to pad with nulls or anything else.
>
Alternatively, you can use the Variable_String package that is available on Adalog's components page:
http://www.adalog.fr/compo2.htm

Note that this package is pure, which (unlike the other packages) makes it usable for distributed applications.
--
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr







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

* Re: Some problems with string type
  2001-06-07  4:06   ` Some problems with string type Valdes
@ 2001-06-07 13:20     ` Valdes Laurent
  0 siblings, 0 replies; 11+ messages in thread
From: Valdes Laurent @ 2001-06-07 13:20 UTC (permalink / raw)


Valdes wrote:
[...]

You would be kind to help me for the second error:

The function has been posted.

-- 
Le Vif Zephyr Jubile Sur les Kumquats du Clown Gracieux.



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

end of thread, other threads:[~2001-06-07 13:20 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-07  2:18 Some problems with string type Valdes
2001-06-06 16:34 ` Philip Anderson
2001-06-06 20:29   ` Some problems with string type(Exact String?) McDoobie
2001-06-06 21:30     ` tmoran
2001-06-07  8:07       ` Jean-Pierre Rosen
2001-06-06 22:08     ` Marin David Condic
2001-06-07  6:48     ` Ada.Strings.Fixed procedure Move Petter Fryklund
2001-06-07  4:06   ` Some problems with string type Valdes
2001-06-07 13:20     ` Valdes Laurent
2001-06-06 17:49 ` tmoran
2001-06-07  4:07   ` Valdes

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