Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2003-11

Format() incompatibility with Delphi

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michalis Kamburelis

Hi

What should
Format('%d %d %0:d %d', [0, 1, 2, 3])
return ? Delphi help is clear about that : "Setting the index specifier affects all subsequent formatting", so it should return '0 1 0 1' (and it does when compiled with Delphi).

FPC help is not clear about that. And unfortunately it turns out that Format() implemented in FPC's SysUtils does not behave like in Delphi: Format() call mentioned above returns '0 1 0 2' when compiled with FPC (so it is like "Setting the index specifier _does not_ affect subsequent formatting").

To fix this (i.e., make it Delphi-compatible) you have to change in file
sysstr.inc in function Checkarg at line 862 (as of latest FPC version
from CVS) from
   if Index=-1 then
     begin
     DoArg:=Argpos;
     inc(ArgPos);
     end
   else
     DoArg:=Index;
to
   if Index=-1 then
     DoArg:=Argpos else
     DoArg:=Index;
   ArgPos:=DoArg+1;

This fix may broke existing code (that depends on the way how Format() in FPC works) but I guess that such code may be considered buggy anyway since FPC documentation was not clear about whether setting index specifier affects subsequent formatting or not. Besides, if you want to make Format() fully Delphi-compatible, there is no other way to fix this, you have to change it's present behaviour.

BTW, I would also advice adding some notes about that to the FPC SysUtils.Format documentation.

Regards,
Michalis Kamburelis

Reply by email

Michael Van Canneyt
to Michalis Kamburelis
Michalis Kamburelis wrote 7 lines
Hi What should Format('%d %d %0:d %d', [0, 1, 2, 3]) return ? Delphi help is clear about that : "Setting the index specifier affects all subsequent formatting", so it should return '0 1 0 1' (and it does when compiled with Delphi).

Let me elaborate a bit on this:

The text you refer to is a 'Note' that appeared only in Delphi 6. In Delphi 5 it is NOT present, it was therefore not obvious what should happen, also not from the example given in delphi 5:

Quote from D5 help:

For example "Format('%d %d %0:d %1:d', [10, 20])" produces the string '10 20 10 20'.

End of quote. (and end of D5 help on the topic !)

There is NO way to know from this example what is supposed to be done in the case of for example:
Format('%d %d %0:d %d', [10, 20])

So the behaviour was definitely not well documented in D5. The FPC implementation dates from D5 or even D4...

But OK, in D6 (and presumably D7) the behaviour is properly documented, and I'll change the FPC implementation _and_ documentation accordingly. Thanks for pointing this out (and for the fix).

Michael.

Show quoted text 41 lines
FPC help is not clear about that. And unfortunately it turns out that Format() implemented in FPC's SysUtils does not behave like in Delphi: Format() call mentioned above returns '0 1 0 2' when compiled with FPC (so it is like "Setting the index specifier _does not_ affect subsequent formatting"). To fix this (i.e., make it Delphi-compatible) you have to change in file sysstr.inc in function Checkarg at line 862 (as of latest FPC version from CVS) from if Index=-1 then begin DoArg:=Argpos; inc(ArgPos); end else DoArg:=Index; to if Index=-1 then DoArg:=Argpos else DoArg:=Index; ArgPos:=DoArg+1; This fix may broke existing code (that depends on the way how Format() in FPC works) but I guess that such code may be considered buggy anyway since FPC documentation was not clear about whether setting index specifier affects subsequent formatting or not. Besides, if you want to make Format() fully Delphi-compatible, there is no other way to fix this, you have to change it's present behaviour. BTW, I would also advice adding some notes about that to the FPC SysUtils.Format documentation. Regards, Michalis Kamburelis _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email