手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>delphi>列表

2个不错的通配符比较函数

来源:互联网 作者:西部数码 时间:2008-04-09
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

近日在和朋友讨论 MaskMatch 时偶得2个不错的算法。
函数1 只支持''''*'''',''''?''''模糊匹配。速度比采用递归算法的快近2倍,比TMask方法快很多。
函数2 完全支持正规表达式。速度于之前的相同。(不会正规表达式的朋友慎用)



// ===========================
// Funtion 1
// ===========================

// Check if the string can match the wildcard. It can be used for unicode strings as well!
// C: 2004-07-24 | M: 2004-07-24
function MaskMatch(const aPattern, aSource: string): Boolean;
var
StringPtr, PatternPtr: PChar;
StringRes, PatternRes: PChar;
begin
Result := False;
StringPtr := PChar(UpperCase(aSource));
PatternPtr := PChar(UpperCase(aPattern));
StringRes := nil;
PatternRes := nil;
repeat
repeat // ohne vorangegangenes "*"
case PatternPtr^ of
#0 : begin
Result := StringPtr^ = #0;
if Result or (StringRes = nil) or (PatternRes = nil) then Exit;
StringPtr := StringRes;
PatternPtr := PatternRes;
Break;
end;
''''*'''': begin
Inc(PatternPtr);
PatternRes := PatternPtr;
Break;
end;
''''?'''': begin
if StringPtr^ = #0 then Exit;
Inc(StringPtr);
Inc(PatternPtr);
end;
else begin
if StringPtr^ = #0 then Exit;
if StringPtr^ <> PatternPtr^ then
begin
if (StringRes = nil) or (PatternRes = nil) then Exit;
StringPtr := StringRes;
PatternPtr := PatternRes;
Break;
end else
begin
Inc(StringPtr);
Inc(PatternPtr);
end;
end;
end;
until False;

repeat // mit vorangegangenem "*"
case PatternPtr^ of
#0 : begin
Result := True;
Exit;
end;
''''*'''': begin
Inc(PatternPtr);
PatternRes := PatternPtr;
end;
''''?'''': begin
if StringPtr^ = #0 then Exit;
Inc(StringPtr);
Inc(PatternPtr);
end;
else begin
repeat
if StringPtr^ = #0 then Exit;
if StringPtr^ = PatternPtr^ then Break;
Inc(StringPtr);
until False;
Inc(StringPtr);
StringRes := StringPtr;
Inc(PatternPtr);
Break;
end;
end;
until False;
until False;
end;


// ===========================
// Funtion 2
// ===========================

function _MatchPattern(aPattern, aSource: PChar): Boolean;
begin
Result := True;
while (True) do
begin
case aPattern[0] of
#0 : begin
//End of pattern reached.
Result := (aSource[0] = #0); //TRUE if end of aSource.
Exit;
end;

''''*'''': begin //Match zero or more occurances of any char.
if (aPattern[1] = #0) then
begin
//Match any number of trailing chars.
Result := True;
Exit;
end else
Inc(aPattern);

while (aSource[0] <> #0) do
begin
//Try to match any substring of aSource.
if (_MatchPattern(aSource, aPattern)) then
begin
Result := True;
Exit;
end;

//Continue testing next char...
Inc(aSource);
end;
end;

''''?'''': begin //Match any one char.
if (aSource[0] = #0) then
begin
Result := False;
Exit;
end;

//Continue testing next char...
Inc(aSource);
Inc(aPattern);
end;

''''['''': begin //Match given set of chars.
if (aPattern[1] in [#0,''''['''','''']'''']) then
begin
//Invalid Set - So no match.
Result := False;
Exit;
end;

if (aPattern[1] = ''''^'''') then
begin
//Match for exclusion of given set...
Inc(aPattern, 2);
Result := True;
while (aPattern[0] <> '''']'''') do

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!