Couperin wrote:
After spending a long time to find why my program didn't work, I discovered something which seems to be a bug.
It's about the "Copy" function.
Here is a test program :
Program TestCopy;
Var Chaine : String(1296); {That's the value I need in my real program} SousChaine : String(9); LongueurChaine : MedWord; IndexPosition : MedWord;
Begin Chaine := '1000000000'; LongueurChaine := Length(Chaine);
SousChaine := Copy(Chaine,LongueurChaine - 8,9); {Here is the problem} Writeln('SousChaine : ',SousChaine); {Writes nothing !} Writeln('Length(SousChaine) : ',Length(SousChaine)); {Writes 0}
IndexPosition := LongueurChaine - 8; SousChaine := Copy(Chaine,IndexPosition,9); {Now it works} Writeln('SousChaine : ',SousChaine); {Writes 000000000} Writeln('Length(SousChaine) : ',Length(SousChaine)); {Writes 9}
{There is no problem with the "Delete" procedure :} Delete(Chaine,LongueurChaine - 8,9); Writeln('Chaine : ',Chaine) {Writes 1} End.
I can't understand why "Copy" works with "IndexPosition" as parameter, but not with "LongueurChaine - 8". Is there something wrong in : Copy(Chaine,LongueurChaine - 8,9) ? Or is this really a bug ?
I must confess I use GPC 2.8.1 (I need GRX and BGI2GRX that I failed to install with the newest version). If it's a bug, maybe it's solved now. In this case, excuse me for this somewhat useless message.
It is a GPC bug. In fact, it's a special case of the following bug listed on the To-Do list (most other cases have been fixed by now, but this one with `Copy' still exists in the latest GPC version):
* integer parameters to built-in routines are not always converted to the right type (fjf349.pas)
The difference between the two cases is that `IndexPosition' is of type MedWord which is probably the same size as Integer on your machine and therefore happens to work, while `LongueurChaine - 8' is internally of type LongInt (the smallest type that can hold a MedInt and negative values because the difference might be negative), and that triggers the bug.
So, a work-around is to type cast the difference, i.e. `Integer (LongueurChaine - 8)'.
Frank