[fpc-devel] lazarus bug report + fix: Utf8ToUnicode doesn't workcorrectly

Dr. Diettrich drdiettrich at compuserve.de
Fri May 6 08:46:47 CEST 2005

Jonas Maebe wrote:

> Anyway, here's the plain ascii version of the "-fixed" file.
> Jonas

I've several questions and comments:

> function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: SizeUInt; Source:
> PChar; SourceBytes: SizeUInt): SizeUInt;

How can -1 be a return value of this function?
I understand that this value can be created as SizeUInt(-1), and that it
is a special error code, but it's everything but strict typing as I
expect in Pascal code.

Using such conventions every SizeUInt (above: MaxDestChars...) has to
checked for -1 before further use, in order to catch error codes from
some function that provided the actual value.

With such conventions and functions, with PChar parameters, I suspect
that "FPC" really stands for "Fits Pascal or C" :-(

About the source code, I'd appreciate to have an unoptimized version of
the code as well, that can be used to validate the manual optimization.
Then an Optimization switch could be used to select the optimized or
safe version.

>      if assigned(Dest) then
>        begin
>          while (j<MaxDestChars) and (i<SourceBytes) do
>        end
>      else
>        begin
>          while i<SourceBytes do
>        end;

The ... sections should contain equivalent code, but this is a fragile
assumption, impossible to verify in a reliable way.

In a further step of optimization the function should be split into two
distinct functions, running even faster due to eliminated checks like
assigned(Dest), and without superfluous parameters.


More information about the fpc-devel mailing list