function TUnicodeEncoding.GetPreamble: TBytes; begin SetLength(Result, 2); Result[0] := $FF; Result[1] := $FE; end;class function TEncoding.GetBufferEncoding(const Buffer: TBytes; var AEncoding: TEncoding): Integer; function ContainsPreamble(const Buffer, Signature: TBytes): Boolean; var I: Integer; begin Result := True; if Length(Buffer) >= Length(Signature) then begin //完全匹配上就是这个 Signature 的Encoding for I := 1 to Length(Signature) do if Buffer[I - 1] <> Signature [I - 1] then begin Result := False; Break; end; end else Result := False; end;var Preamble: TBytes; begin Result := 0; if AEncoding = nil then begin // Find the appropraite encoding if ContainsPreamble(Buffer, TEncoding.Unicode.GetPreamble) then AEncoding := TEncoding.Unicode else if ContainsPreamble(Buffer, TEncoding.BigEndianUnicode.GetPreamble) then AEncoding := TEncoding.BigEndianUnicode else if ContainsPreamble(Buffer, TEncoding.UTF8.GetPreamble) then AEncoding := TEncoding.UTF8 else AEncoding := TEncoding.Default; Result := Length(AEncoding.GetPreamble); end else begin Preamble := AEncoding.GetPreamble; if ContainsPreamble(Buffer, Preamble) then Result := Length(Preamble); end; end;
它是把字节数组和对应编码一些貌似特征数组进行比较.
function TUnicodeEncoding.GetPreamble: TBytes;
begin
SetLength(Result, 2);
Result[0] := $FF;
Result[1] := $FE;
end;class function TEncoding.GetBufferEncoding(const Buffer: TBytes; var AEncoding: TEncoding): Integer; function ContainsPreamble(const Buffer, Signature: TBytes): Boolean;
var
I: Integer;
begin
Result := True;
if Length(Buffer) >= Length(Signature) then
begin
//完全匹配上就是这个 Signature 的Encoding
for I := 1 to Length(Signature) do
if Buffer[I - 1] <> Signature [I - 1] then
begin
Result := False;
Break;
end;
end
else
Result := False;
end;var
Preamble: TBytes;
begin
Result := 0;
if AEncoding = nil then
begin
// Find the appropraite encoding
if ContainsPreamble(Buffer, TEncoding.Unicode.GetPreamble) then
AEncoding := TEncoding.Unicode
else
if ContainsPreamble(Buffer, TEncoding.BigEndianUnicode.GetPreamble) then
AEncoding := TEncoding.BigEndianUnicode
else
if ContainsPreamble(Buffer, TEncoding.UTF8.GetPreamble) then
AEncoding := TEncoding.UTF8
else
AEncoding := TEncoding.Default;
Result := Length(AEncoding.GetPreamble);
end
else
begin
Preamble := AEncoding.GetPreamble;
if ContainsPreamble(Buffer, Preamble) then
Result := Length(Preamble);
end;
end;