Hi,
During some tests I ran with gpc 2050331 to evaluate the gap between SUN and GNU Pascal, I discovered that the function TRIM() is broken when it is passed a packed array of char.
The small program bellow shows incorrect result:
PROGRAM Trim; VAR St : PACKED ARRAY [1..20] OF CHAR; I : INTEGER; BEGIN WriteLn('Before assignment:'); FOR I := 1 TO 20 DO WriteLn('St[', i:2,']=', Ord(St[i])); { This shows a series of 0 }
This writes any garbage contained in St (since St is uninitialized)
WriteLn('After assignment:'); St := 'The String'; FOR I := 1 TO 20 DO WriteLn('St[', i:2,']=', Ord(St[i])); { This shows
the string padded with blanks (CHR(32)} St := Trim(St);
^^^^^^^^^^^^^^^ This is a no-op
WriteLn('After Trim:'); FOR I := 1 TO 20 DO WriteLn('St[', i:2,']=', Ord(St[i])); { ***This
shows the string exactly AS BEFORE! }
As expected.
END.
Am I right when I think the function TRIM should replace the blanks with some CHR(0)?
No, TRIM makes _shorter_ string remowing blanks at the end. Assignment pads the value with blanks to fill whole St. You can see the difference if you have:
program tstrim(Output); Var Sv : String(20); St : packed array [1..20] of char; begin St := 'The String'; writeln('->', St, '<-'); Sv := St; writeln('->', Sv, '<-'); Sv := TRIM(St); writeln('->', Sv, '<-'); St := Sv; writeln('->', St, '<-'); end .
Blank padding on assignment is required by ISO standards.