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

2016-02-19 18:25 1 1 收藏

在这个颜值当道,屌丝闪边的时代,拼不过颜值拼内涵,只有知识丰富才能提升一个人的内在气质和修养,所谓人丑就要多学习,今天图老师给大家分享2个不错的通配符比较函数,希望可以对大家能有小小的帮助。

【 tulaoshi.com - 编程语言 】

近日在和朋友讨论 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
                 begin
                   if (aPattern[1] = '-') then
                   begin
                     //Match char exclusion range.
                     if (aSource[0] = aPattern[0]) and (aSource[0] = aPattern[2]) then
                     begin
                       //Given char failed set exclusion range.
                       Result := False;
                       Break;
                     end else
                       Inc(aPattern, 3);
                   end else
                   begin
                     //Match individual char exclusion.
                     if (aSource[0] = aPattern[0]) then
                     begin
                       //Given char failed set element exclusion.
                       Result := False;
                       Break;
                     end else
                      Inc(aPattern);
                   end;
                 end;
               end else
               begin
                 //Match for inclusion of given set...
                 Inc(aPattern);
                 Result := False;
                 while (aPattern[0] ']') do
                 begin
                   if (aPattern[1] = '-') then
                   begin
                     //Match char inclusion range.
                     if (aSource[0] = aPattern[0]) and (aSource[0] = aPattern[2]) then
                     begin
                       //Given char matched set range inclusion.
                       // Continue testing...
                       Result := True;
                       Break;
                     end else
                      Inc(aPattern, 3);
                   end else
                   begin
                     //Match individual char inclusion.
                     if (aSource[0] = aPattern[0]) then
                     begin
                       //Given char matched set element inclusion.
                       // Continue testing...
                       Result := True;
                       Break;
                     end else
                       Inc(aPattern);
                   end;
                 end;
               end;
  
               if (Result) then
               begin
                 //Match was found. Continue further.
                 Inc(aSource);
                 //Position Pattern to char after "]"
                 while (aPattern[0] ']') and (aPattern[0] #0) do Inc(aPattern);
                 if (aPattern[0] = #0) then
                 begin
                   //Invalid Pattern - missing "]"
                   Result := False;
                   Exit;
                 end else
                   Inc(aPattern);
               end else
                 Exit;
             end;
  
        else begin //Match given single char.
               if (aSource[0] aPattern[0]) then
               begin
                 Result := False;
                 Break;
               end;
  
               //Continue testing next char...
               Inc(aSource);
               Inc(aPattern);
             end;
      end;
    end;
  end;
  
  function MatchPattern(const aPattern, aSource: string): Boolean;
  begin
    Result := _MatchPattern(PChar(aPattern), PChar(aSource));
  end;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

来源:https://www.tulaoshi.com/n/20160219/1617594.html

延伸阅读
标签: Web开发
这两天M$出了个IE8beta1版~害得我的Google Reader里全是IE8的信息,可惜有用的信息太少了,在翻M$的网站时,倒是发现了一个比较帅的功能:WebSlices。 简单的来说,它的功能就是把网页的某一块剪出来放收藏夹里。这个功能与Mac OS上的Web Clip Widget性质不大一样,Web Clip是由用户自由剪网页,而WebSlices是要网页输出特定的格式可以剪的...
标签: Web开发
SCA Ad Image Process [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
标签: Web开发
适合阅读范围:对JavaScript一无所知~离精通只差一步之遥的人 基础知识:HTML JavaScript就这么回事1:基础知识  1 创建脚本块 1: script language=”JavaScript” 2: JavaScript code goes here 3: /script  2 隐藏脚本代码 1: script language=”JavaScri...
标签: Web开发
拖动效果函数演示 by Longbill.cn body { font-size:12px; color:#333333; border : 0px solid blue; } div { position : absolute; background-color : #c3d9ff; margin : 0px; padding : 5px; border : 0px; width : 100px; height:100px; } div1:我可以被拖动 div2:来拖我呀 div3:我随便你拖 div4:我...
标签: Web开发
//-------- // 检查当前浏览器是否为Netscape //-------- function isNetscape(){     app=navigator.appName.substring(0,1);     if (app=='N') return true;     else {return false;} } //-------- // 保存当前Form表单(...

经验教程

207

收藏

56
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部