Cocoa: improve in UTF8CodepointCount()

This commit is contained in:
rich2014 2024-08-30 21:32:05 +08:00
parent a851bf46fa
commit 27063a7dca

View File

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