#1 2015-06-15 22:32:37

alexsv
Member
Registered: 2014-12-07
Posts: 3

Probably bug in RawUnicodeToUtf8

Is this code correct? (SynCommons unit)

function RawUnicodeToUtf8(Dest: PUTF8Char; DestLen: PtrInt; Source: PWideChar; SourceLen: PtrInt): PtrInt; overload;
var c: Cardinal;
    Tail: PWideChar;
    i,j: integer;
begin
  result := PtrInt(Dest);
  if (Source<>nil) and (Dest<>nil) then begin
    // first handle 7 bit ASCII WideChars, by pairs (Sha optimization)
    SourceLen := SourceLen*2+PtrInt(Source);
    Tail := PWideChar(SourceLen)-2;
    if Source<=Tail then
    repeat
      c := PCardinal(Source)^;
      if c and $ff80ff80<>0 then
        break; // break on first non ASCII pair
      inc(Source,2);
      c := c shr 8 or c;
      pWord(Dest)^ := c;
      inc(Dest,2);
    until Source>Tail;
    // generic loop, handling one UCS4 char per iteration
    Inc(DestLen,PtrInt(Dest));

Shouldn't
    Inc(DestLen,PtrInt(Dest));

be placed before the cycle? Now DestLen do not point to real Dest end

Offline

#2 2015-06-16 06:44:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,662
Website

Re: Probably bug in RawUnicodeToUtf8

You are right.

Should be fixed by http://synopse.info/fossil/info/fab73a9a7c

Offline

Board footer

Powered by FluxBB