Loopback
|
Posted: Tue Aug 17, 2010 09:45 Post subject: |
|
|
Насколько я понимаю, нужно определить UTF-8 или нет, т.к. BOM к другого рода кодировкам (типа 1251, KOI-8 ) не имеет отношения. Я пользуюсь такой функцией (где-то когда-то нашел):
Code: |
function IsUTF8Memory(AMem: PBYTE; ASize: Int64): boolean;
var
i: Int64;
c: Integer;
function UTF8CharLength(const c: BYTE): Integer;
begin
// First Byte: 0xxxxxxx
if ((c and $80) = $00) then
Result:=1
// First Byte: 110yyyyy
else if ((c and $E0) = $C0) then
Result:=2
// First Byte: 1110zzzz
else if ((c and $F0) = $E0) then
Result:=3
// First Byte: 11110uuu
else if ((c and $F8) = $F0) then
Result:=4
// not valid, return the error value
else
Result:=-1;
end;
//After than you check all the trail bytes for that characters (if any)
//for conformity with this:
function UTF8IsTrailChar(const c: BYTE): BOOLEAN;
begin
// trail bytes have this form: 10xxxxxx
Result:=((c and $C0) = $80);
end;
begin
Result := True;
i := 0;
while (i < ASize) do
begin
// get the length if the current UTF-8 character
c:=UTF8CharLength(AMem^);
// check if it is valid and fits into ASize
if ((c>= 1) and (c <= 4) and ((i+c-1) < ASize)) then
begin
inc(i, c);
inc(AMem);
// if it is a multi-byte character, check the trail bytes
while (c>1) do
begin
if (not UTF8IsTrailChar(AMem^)) then
begin
Result := False;
break;
end
else
begin
dec(c);
inc(AMem);
end;
end;
end
else
begin
Result:=False;
end;
if (not Result) then break;
end;
end;
|
|
|