// TCodePageDetector //
type
TKeyMap = (kmNone, kmAscii, kmAnsi, kmKoi8r);
TCodePageDetector = class
public
Koi, KoiA, Win, WinA, Win2: LongInt;
constructor Create;
destructor Destroy;
procedure CheckString(const S: string);
function DetectedCodePage: TKeyMap;
end;
constructor TCodePageDetector.Create;
begin
Koi := 0;
KoiA := 0;
Win := 0;
WinA := 0;
Win2 := 0;
end;
destructor TCodePageDetector.Destroy;
begin
//
end;
const
WinSetA = ['а', 'е', 'и', 'о', 'у']; { [' ','_','Ё','R','г'] }
KoiSetA = ['Б', 'Е', 'Й', 'П', 'Х'];
procedure TCodePageDetector.CheckString(const S: string);
var
J, I: Integer;
C: Char;
begin
J := Length(S);
I := 1;
while I < J do begin
C := S[I];
Inc(I);
if C >= #$C0 then
if C <= #$DF then begin
Inc(Koi);
if C in KoiSetA then Inc(KoiA);
end
else begin
Inc(Win);
if C in WinSetA then Inc(WinA);
if C >= #$F0 then Inc(Win2);
end;
end;
end;
function TCodePageDetector.DetectedCodePage: TKeyMap;
begin
DetectedCodePage := kmAscii;
if (Koi <> 0) and (KoiA <> 0) and (Win <> 0) and
(Win >= Koi / 500) and (Win <= Koi / 5) and
(KoiA >= Koi / 5) then
DetectedCodePage := kmKoi8r
else if (Win <> 0) and (WinA <> 0) and (Koi <> 0) and
(Koi >= Win / 500) and (Koi <= Win / 5) and
(WinA >= Win / 5) and (Win2 >= Win / 5) then
DetectedCodePage := kmAnsi;
end;
// TCodePageDetector //
|