快速查询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 rest客户端程序

Delphi textfile utf8编码读写

Delphi datetimepicker控件日期格式

Delphi 为数字补充前缀0

Delphi html转义

Delphi 如何从html格式的字符串中提取纯文本?

Delphi 提高unigui开发效率的两个方法

Delphi 如何将颜色值转换为html格式?

orangeui作者开源作品

Delphi 如何将颜色值转换为灰度颜色值?

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



打赏

取消

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

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

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

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

评论

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