Parsing UTF8 strings

Windows API functions like CharNextExA do not support UTF8 encoding; you can test CharNextExA with UTF8 codepage (65001) and UTF8 strings and see it does not work. If you need to calculate for example the number of unicode codepoints in UTF8 string you should parse UTF8 string manually. Fortunately the task is very simple and boils down to the following function:

function UTF8IsLeadChar(Ch: AnsiChar): Boolean;
begin
  Result:= Ord(Ch) and $C0 <> $80;
end;

the function that returns number of unicode codepoints in UTF8 string looks as follows:

function UTF8CharCount(const S: UTF8String): Integer;
var
  P: PAnsiChar;
  I: Integer;

begin
  I:= 0;
  P:= PAnsiChar(S);
  Result:= 0;
  while I < Length(S) do begin
    if UTF8IsLeadChar(P^) then Inc(Result);
    Inc(P);
    Inc(I);
  end;
end;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s