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 } 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); WriteLn('After Trim:'); FOR I := 1 TO 20 DO WriteLn('St[', i:2,']=', Ord(St[i])); { ***This shows the string exactly AS BEFORE! } END.
Am I right when I think the function TRIM should replace the blanks with some CHR(0)?
Thanks
Pascal Viandier
Pascal Viandier wrote:
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 } 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); WriteLn('After Trim:'); FOR I := 1 TO 20 DO WriteLn('St[', i:2,']=', Ord(St[i])); { ***This shows the string exactly AS BEFORE! } END.
Am I right when I think the function TRIM should replace the blanks with some CHR(0)?
Nope. Fixed strings (i.e., packed arrays of char) are blank-padded on assignments (EP, 6.4.6 last paragraph). This implicit operation effectively undoes the effect of `Trim'.
Bottom line: There's no direct way to store strings with and without trailing spaces in fixed-strings (as strings with trailing spaces as some kind of "un-strings" in standard Pascal, one thing I dislike most about it). The only way is to store the length in a separate variable (which means reimplementing many built-in string operations), or use EP strings (`String (20)') which store their length and are not automatically blank padded.
BTW, Chr (0) has no significance as a string terminator in Pascal. (GPC adds it when "converting" to a CString, but that's about it, and this won't help you here.)
Frank
On Mon, 30 May 2005, Pascal Viandier wrote:
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.
[..]
trim works fine with strings:
program trim; var s: string(20); begin s:='x '; writeln(s,s, length(s)); s:= trim(s); writeln(s,s, length(s)); end.
russ
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.