gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Access of elements of a string constant


From: Gaius Mulley
Subject: Re: Access of elements of a string constant
Date: Mon, 06 Jan 2020 12:23:52 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Michael Riedl <address@hidden> writes:

> Hallo John,
>
> had a look in the standard (only have a late draft at hand).
>
> Would not have interpreted that in the sense you indicated, but it
> seems you are right !
>
> So I recoded to ...
>
> MODULE TstStrConst;
>
> IMPORT STextIO,SWholeIO;
>
> CONST len     = 16;
>
> TYPE  TNumStr = ARRAY [0..len-1] OF CHAR;
>
> CONST NumStr  = TNumStr{ (* "0123456789ABCDEF" *)
>                  "0","1","2","3","4","5","6","7","8","9",
>                  "A","B","C","D","E","F"
>                };
>
> VAR   i : CARDINAL;
>       c : CHAR;
> BEGIN
>       STextIO.WriteLn;
>       FOR i:=0 TO len-1 DO
>         SWholeIO.WriteCard(i,2);
>         STextIO.WriteString(" '");
>         c := NumStr[i];
>         STextIO.WriteChar(c);
>         STextIO.WriteString("' ");
>         SWholeIO.WriteCard(ORD(c),1);
>         STextIO.WriteLn;
>       END;
>       STextIO.WriteLn;
> END TstStrConst.
>
> This is compiled but does not give the expected result on my machine ...
>
> Should be
>
>  0 '0' 48
>  1 '1' 49
>  2 '2' 50
>  3 '3' 51
>  4 '4' 52
>  5 '5' 53
>  6 '6' 54
>  7 '7' 55
>  8 '8' 56
>  9 '9' 57
> 10 'A' 65
> 11 'B' 66
> 12 'C' 67
> 13 'D' 68
> 14 'E' 69
> 15 'F' 70
>
> but ends up with
>
>  0 '0' 48
>  1 '' 0
>  2 '' 0
>  3 '' 0
>  4 '' 0
>  5 '' 0
>  6 '' 0
>  7 '' 0
>  8 '' 0
>  9 '' 0
> 10 '' 0
> 11 '' 0
> 12 '' 0
> 13 '' 0
> 14 '' 0
> 15 '' 0
>
> interrestingly the fist line is OK ...
>
> Any idea ?
>
> If I use a construct like
>
> ...
>
> CONST digits = "0123456789ABCDEF"
>
> VAR   Digits = ARRAY [0..15] OF CHAR; (* do not need the terminating "0C" *)
>
> BEGIN
>
>       Digits := digits;
>
> ...
>
> all seems fine ..
>
> Michael
>
>
> PS: By the way, does anybody in the audience know if my original code
> would be OK in PIM ? Just for interest.

Hi Michael and John,

the Pim4 book opens with:

"""
And finally, the language rule about assigning a string s to an array a of 
characters
is made more restrictive, The assignment a := s is acceptable, if the length of 
s is
strictly less (rather than less or equal) to the number of elements of a. This 
ensures
that a string is always terminated by a OC character, simplifying tests for the 
end of a
string.
"""

which doesn't resolve the question asked.  However I think it unlikely
as the direction of travel appears to be making the language tighter.  


>
>
> Am 05.01.20 um 03:00 schrieb john o goyo:
>> Greetings, Michael.
>>
>> On 01/04/20 12:05, Michael Riedl wrote (in part):
>>> [...]
>>>
>>> MODULE TstStrConst;
>>>
>>> IMPORT STextIO,SWholeIO;
>>>
>>> CONST NumStr = "0123456789ABCDF";
>>>
>>> VAR   i : CARDINAL;
>>>
>>> BEGIN
>>>       STextIO.WriteLn;
>>>       FOR i:=0 TO LENGTH(NumStr)-1 DO
>>>         SWholeIO.WriteCard(i,2);
>>>         STextIO.WriteString(" '");
>>>         STextIO.WriteChar(NumStr[i]);
>>>         STextIO.WriteString("' ");
>>>         SWholeIO.WriteCard(ORD(NumStr[i]),1);
>>>         STextIO.WriteLn;
>>>       END;
>>>       STextIO.WriteLn;
>>> END TstStrConst.
>>>
>>> It can be fixed by a VAR string initialized in the module body -
>>> but that's less elegant.
>> I know not about PIM but that construct seems to be disallowed by ISO:
>>
>>     NOTE 1 - An indexed value cannot be formed by indexing a string
>> constant.
>>
>>     (ISO/IEC 10514-1, Sect. 6.8.4.2)
>>
>> john
>>

I'll add regression test code to stress the CONST NumStr concept and
fix,




reply via email to

[Prev in Thread] Current Thread [Next in Thread]