[fpc-devel]Format() incompatibility with Delphi

Michael Van Canneyt michael.vancanneyt at wisa.be
Fri Nov 7 23:41:03 CET 2003


On Fri, 7 Nov 2003, Michalis Kamburelis wrote:

> 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.

>
> 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 <michalis at camelot.homedns.org>
>
>
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel
>




More information about the fpc-devel mailing list