快速查询PosEx与PosRightEx


本文整理自网络,侵删。

 1.PosEx

功能说明:实现正向增强查询,跟FastCodePosEx功能相同,不依赖fastCode;

OffSet为偏移位置,通过测试,速度要比FastCodePosEx稍慢。

function PosEx(const SubStr,S:AnsiString;const Offset:Cardinal=1):Integer;
var
iPos: Integer;
i, j,LenS,LenSub: Integer;
PCharS, PCharSub: PChar;
begin
Result := 0;
LenS:=Length(S);
lenSub := length(Substr);
if (LenS=0) Or (lenSub=0) then Exit;
if (LenS-lenSub)<Offset then Exit;

PCharS := PChar(s);
PCharSub := PChar(Substr);

for I := Offset-1 to LenS-1 do
begin
if lens-I<LenSub then Exit;
for j := 0 to lenSub - 1 do
if PCharS[i + j] <> PCharSub[j] then
break
else if J=LenSub-1 then
begin
Result:=I+1;
Exit;
end;
end;
end;

2.PosRightEx

功能说明:实现反向增强查询;

OffSet为偏移位置,通过指定Offset,只查询Offset以前位置(包括Offset所在位置字符)。查询方法为从右向左最比。与PosEx效率相同。

function PosRightEx(const SubStr,S:AnsiString;const Offset:Cardinal=MaxInt):Integer;
var
iPos: Integer;
i, j,Len,LenS,LenSub: Integer;
PCharS, PCharSub: PChar;
begin
Result := 0;
LenS:=Length(S);
lenSub := length(Substr);
if (LenS=0) Or (lenSub=0) then Exit;
if Offset<LenSub then Exit;

PCharS := PChar(s);
PCharSub := PChar(Substr);

Len:=Offset;
if Len>LenS then
Len:=LenS;

for I := Len-1 downto 0 do
begin
if I<LenSub-1 then Exit;
for j := lenSub - 1 downto 0 do
if PCharS[i -lenSub+j+1] <> PCharSub[j] then
break
else if J=0 then
begin
Result:=I-lenSub+2;
Exit;
end;
end;
end;

以上两个函数都是大小写敏感的,可以通过自己修改对比方法。

相关阅读 >>

Delphi 检查声卡是否安装

Delphi unigui程序部署到服务器

Delphi 之 对话框组件

Delphi 让嵌入窗体的 webbrowser 控件无边框

Delphi版本qq木马

Delphi 比较俩组ip地址是否一样

Delphi 如何取得键盘每个键

Delphi申请和释放内存

Delphi 字符串保存utf-8过程

Delphi decodedate、decodetime ... decodedatetime ... 分解时间

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...