国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

2個(gè)不錯(cuò)的通配符比較函數(shù)

2019-11-18 18:05:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

近日在和朋友討論 MaskMatch 時(shí)偶得2個(gè)不錯(cuò)的算法。
函數(shù)1 只支持'*','?'模糊匹配。速度比采用遞歸算法的快近2倍,比TMask方法快很多。
函數(shù)2 完全支持正規(guī)表達(dá)式。速度于之前的相同。(不會(huì)正規(guī)表達(dá)式的朋友慎用)



// ===========================
// 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;


上一篇:看一小會(huì)兒COM所做筆記

下一篇:如何從MySQL數(shù)據(jù)庫(kù)表中檢索數(shù)據(jù)

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
學(xué)習(xí)交流
熱門(mén)圖片

新聞熱點(diǎn)

疑難解答

圖片精選

網(wǎng)友關(guān)注

主站蜘蛛池模板: 海安县| 文水县| 瓦房店市| 南部县| 雷州市| 保山市| 项城市| 神农架林区| 睢宁县| 阿图什市| 会东县| 祁门县| 柞水县| 岳阳市| 襄垣县| 白朗县| 永安市| 林州市| 新乐市| 罗城| 布尔津县| 扎赉特旗| 牡丹江市| 鄂伦春自治旗| 北流市| 洛宁县| 阿拉善左旗| 容城县| 蕉岭县| 青浦区| 静宁县| 徐闻县| 吐鲁番市| 神农架林区| 昭苏县| 维西| 凤山县| 日照市| 丰县| 南木林县| 巢湖市|