Hi,
Yeah. I was forgot about that LastPos function
Here is a minimal LastPos function that support widestrings. It use of anothor function named InternalPosW grabbed carefully
from DIUtils unit of DIHtmlParser
http://www.zeitungsjunge.de/delphi/htmlparser/
Code: Select all
function InternalPosW(Search: PWideChar; lSearch: Cardinal; const Source: PWideChar; LSource: Cardinal; const StartPos: Cardinal): Cardinal;
label
Zero, One, Two, Three, Match, Fail, Success;
var
PSource, pSearchTemp, PSourceTemp: PWideChar;
lSearchTemp: Cardinal;
c: WideChar;
begin
if lSearch > LSource then goto Fail;
Dec(lSearch);
Dec(LSource, lSearch);
if LSource <= StartPos then goto Fail;
Dec(LSource, StartPos);
PSource := Source;
Inc(PSource, StartPos);
c := Search^;
Inc(Search);
while LSource > 0 do
begin
while LSource >= 4 do
begin
if PSource^ = c then goto Zero;
if PSource[1] = c then goto One;
if PSource[2] = c then goto Two;
if PSource[3] = c then goto Three;
Inc(PSource, 4);
Dec(LSource, 4);
end;
if LSource = 0 then Break;
if PSource^ = c then goto Zero;
if LSource = 1 then Break;
if PSource[1] = c then goto One;
if LSource = 2 then Break;
if PSource[2] = c then goto Two;
Break;
Three:
Inc(PSource, 4);
Dec(LSource, 3);
goto Match;
Two:
Inc(PSource, 3);
Dec(LSource, 2);
goto Match;
One:
Inc(PSource, 2);
Dec(LSource, 1);
goto Match;
Zero:
Inc(PSource);
Match:
PSourceTemp := PSource;
pSearchTemp := Search;
lSearchTemp := lSearch;
while (lSearchTemp >= 4) and
(PCardinal(PSourceTemp)^ = PCardinal(pSearchTemp)^) and
(PCardinal(@PSourceTemp[2])^ = PCardinal(@pSearchTemp[2])^) do
begin
Inc(PSourceTemp, 4);
Inc(pSearchTemp, 4);
Dec(lSearchTemp, 4);
end;
if lSearchTemp = 0 then goto Success;
if PSourceTemp^ = pSearchTemp^ then
begin
if lSearchTemp = 1 then goto Success;
if PSourceTemp[1] = pSearchTemp[1] then
begin
if lSearchTemp = 2 then goto Success;
if PSourceTemp[2] = pSearchTemp[2] then
begin
if lSearchTemp = 3 then goto Success;
end;
end;
end;
Dec(LSource);
end;
Fail:
Result := 0;
Exit;
Success:
Result := (Cardinal(PSource) - Cardinal(Source)) shr 1;
end;
function LastPosW(const Substr, Str: PWideChar): Integer;
var
P: Integer;
begin
Result := 0;
P := 1;
while P > 0 do begin
P := InternalPosW(SubStr, Length(SubStr), Str, Length(Str), P);
if P > 0 then
Result := P;
end;
end;