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