diff --git a/components/lazutils/lazutf8.pas b/components/lazutils/lazutf8.pas index 864570f472..44820b857c 100644 --- a/components/lazutils/lazutf8.pas +++ b/components/lazutils/lazutf8.pas @@ -84,8 +84,8 @@ function UTF8Length(p: PChar; ByteCount: PtrInt): PtrInt; function UTF8LengthFast(const s: string): PtrInt; inline; function UTF8LengthFast(p: PChar; ByteCount: PtrInt): PtrInt; -function UTF8CodepointCount(const s: string): PtrInt; inline; -function UTF8CodepointCount(p: PChar; ByteCount: PtrInt): PtrInt; +function UTF8CodepointCount(const s: string): SizeInt; inline; +function UTF8CodepointCount(p: PChar; ByteCount: SizeInt): SizeInt; // Functions dealing with unicode number U+xxx. function UTF8CodepointToUnicode(p: PChar; out CodepointLen: integer): Cardinal; @@ -575,16 +575,18 @@ begin Result:=UTF8CodepointCount(PChar(s),length(s)); end; -function UTF8CodepointCount(p: PChar; ByteCount: PtrInt): PtrInt; +function UTF8CodepointCount(p: PChar; ByteCount: SizeInt): SizeInt; var - codepointLen: LongInt; + codepointLen: SizeInt; begin Result:=0; while (ByteCount>0) do begin - inc(Result); - codepointLen:=UTF8CodepointLen(p,ByteCount,false); - if codepointLen=0 then + codepointLen:=UTF8CodepointLen(p,ByteCount,False); + if codepointLen >= 0 then + inc(Result); + if codepointLen = 0 then Exit; + codepointLen:= abs( codepointLen); inc(p,codepointLen); dec(ByteCount,codepointLen); end;