17-10-2011, 08:04 PM
هناك غنائم في المكتبات كنا في امس الحاجة لها فيما سبق
كود :
function PosStr(const subStr : AnsiString;
const str : AnsiString;
fromPos : cardinal = 1;
toPos : cardinal = maxInt) : integer;
begin
if (fromPos > 0) and (toPos > 0) then
result := PosPChar(PAnsiChar(subStr), PAnsiChar(str), Length(subStr), Length(str), false, fromPos - 1, toPos - 1) + 1
else result := 0;
end;كود :
function PosPChar(subStr : PAnsiChar;
str : PAnsiChar;
subStrLen : cardinal = 0; // 0 -> StrLen is called internally
strLen : cardinal = 0;
ignoreCase : boolean = false;
fromPos : cardinal = 0;
toPos : cardinal = maxInt) : integer;
function GetPCharLen(const pc: PAnsiChar) : cardinal;
asm
MOV EDX,EDI
MOV EDI,EAX
MOV ECX,0FFFFFFFFH
XOR AL,AL
REPNE SCASB
MOV EAX,0FFFFFFFEH
SUB EAX,ECX
MOV EDI,EDX
end;
var pc1, pc2, pc3, pc4, pc5, pc6 : PAnsiChar;
c1 : cardinal;
ch1 : AnsiChar;
begin
result := -1;
if (subStr <> nil) and ((subStrLen <> 0) or (subStr^ <> #0)) and
( str <> nil) and (( strLen <> 0) or ( str^ <> #0)) then begin
if subStrLen = 0 then subStrLen := GetPCharLen(subStr);
if strLen = 0 then strLen := GetPCharLen( str);
if strLen >= subStrLen then begin
c1 := strLen - subStrLen;
if ignoreCase then
ch1 := lowCharTable[subStr^]
else
ch1 := subStr^;
if fromPos > toPos then begin
if toPos <= c1 then begin
if fromPos > c1 then
fromPos := c1;
pc1 := str + fromPos;
pc2 := str + toPos;
pc3 := subStr + 1;
pc4 := subStr + subStrLen - 1;
pc6 := pc3;
if ignoreCase then begin
while pc1 >= pc2 do
if lowCharTable[pc1^] = ch1 then begin
inc(pc1);
pc5 := pc1;
while (pc3 <= pc4) and (lowCharTable[pc1^] = lowCharTable[pc3^]) do begin
inc(pc1);
inc(pc3);
end;
if pc3 > pc4 then begin
result := pc5 - PAnsiChar(str) - 1;
break;
end;
pc3 := pc6;
pc1 := pc5 - 2;
end else
dec(pc1);
end else
while pc1 >= pc2 do
if pc1^ = ch1 then begin
inc(pc1);
pc5 := pc1;
while (pc3 <= pc4) and (pc1^ = pc3^) do begin
inc(pc1); inc(pc3);
end;
if pc3 > pc4 then begin
result := pc5 - PAnsiChar(str) - 1;
break;
end;
pc3 := pc6;
pc1 := pc5 - 2;
end else
dec(pc1);
end;
end else
if fromPos <= c1 then begin
if toPos > c1 then
toPos := c1;
pc1 := str + fromPos;
pc2 := str + toPos;
pc3 := subStr + 1;
pc4 := subStr + subStrLen - 1;
pc6 := pc3;
if ignoreCase then begin
while pc1 <= pc2 do
if lowCharTable[pc1^] = ch1 then begin
inc(pc1);
pc5 := pc1;
while (pc3 <= pc4) and (lowCharTable[pc1^] = lowCharTable[pc3^]) do begin
inc(pc1);
inc(pc3);
end;
if pc3 > pc4 then begin
result := pc5 - PAnsiChar(str) - 1;
break;
end;
pc3 := pc6;
pc1 := pc5;
end else
inc(pc1);
end else
while pc1 <= pc2 do
if pc1^ = ch1 then begin
inc(pc1);
pc5 := pc1;
while (pc3 <= pc4) and (pc1^ = pc3^) do begin
inc(pc1); inc(pc3);
end;
if pc3 > pc4 then begin
result := pc5 - PAnsiChar(str) - 1;
break;
end;
pc3 := pc6;
pc1 := pc5;
end else
inc(pc1);
end;
end;
end;
end;
