Hi GPC crew,
I encounter a problem with SetLength: I have a string of 40 characters long coming from a file (no control on it). On these 40 characters the first 13 are "meaningfull" and the others are Chr(0). The String as I can see it through gdb indicates a capacity of 40, a length of 40 and the contents as explained. I want to get rid of the Chr(0) by setting the actual string length to the meaningful part (13). So the following code should do this in a clean way (I think):
Procedure CleanZeroesAndDisplay(Const S: String); Var LocalS : String(40) = S; P : Integer; Begin P := Pos(Chr(0), LocalS); If (P > 0 And P < Length(LocalS)) Then SetLength(LocalS, P); ...
End;
When executing this code with the previous example, P returns 13, Length(S) returns 40 so I enter the "If" and SetLength is called. The problem is that after the call I see that the actual length of LocalS still 40. The debugging is somewhat approximate since I can enter the "Pos" function with gdb but not "SetLength". Even better, if I replace the SetLength call by LocalS := Copy(LocalS, 1, P); it does not work either (the length stays 40).
I don't know if the presence of Chr(0) makes the functions behave erratically but the following code solves the problem in a somewhat dirty and strange way:
Procedure CleanZeroes(Const S: String); Var LocalS : String(40) = S; P : Integer; Begin P := Pos(Chr(0), LocalS); If (P > 0 And P < Length(LocalS)) Then FillChar(LocalS[P], (Length(LocalS) -P) + 1, ' '); ...
End;
The "strange" part (for me :-) is that the call to the "Fillchar monster", fills (replace) the string's contents as expected AND sets the actual length of the string to 13, as if I called Trim! I am somewhat confused on what is going on there. May this be a bug?
I run gpc 20050331 on gcc 3.4.3 under Sun Solaris 2.6
Thanks
Pascal Viandier pascal@accovia.com