2 nice wildcard comparison functions

xiaoxiao2021-03-06  49

I have two good algorithms when I discuss Maskmatch with my friends. Function 1 support '*', '?' Blur match. The speed is nearly 2 times faster than the recursive algorithm, which is much faster than the TMASK method. Function 2 fully supports regular expressions. The speed is the same as before.

(Will not use a regular expression to use carefully) / / =========================== // 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-24function 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 vorancegangenes "*" Case Patternptr ^ OF # 0: Begin Result: = StringPtr ^ = # 0; If Result or (stringRES = nil) instrument; stringptr: = stringRES; Patternptr: = patternres; Break; end; '*': begin inc (Patternptr); patternres: = patternptr; Break; end; '?': begin if stringptr ^ = # 0 THEN EXIT; INC; INC (Patternptr); End; Else Begin if stringptr ^ = # 0 THEN EXIT; if StringPtr ^ <> patternptr ^ dam = = nil) or (patternres = nil) Then exit; StringPtr: = Patternptr: = Patternres; Break;

ELSE BEGIN INC; INC (PATTERNPTR); End; End; end; Until false; repeat // mit vorancegangeneM "*" Case Patternptr ^ OF # 0: Begin Result: = true; exit; end; '*' : Begin INC; 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; unsil 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 asseource. Exit; end; '*': begin // match zero or more obscurances of any char. If (apattern [1] =

# 0) THEN Begin // Match Any Number of Trailing Chars. Result: = TRUE; EXIT; 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; //undinue testing next char ... incod; inckey; 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 ]) The begin // Given Char failed set exclusion range. Result: = false; Break

END ELSE INC (Apattern, 3); Else Begin // Match Individual Char Exclusion. IF (Asource [0] = apattern [0]) THEN BEGIN / / GIVEN CHAR FAILED SET ELIN / / GIVEN CHAR FAILED SET ELIN / / BREAK; 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 becom // given char Matched Set Range Inclusion. // Continue Testing ... Result: = true; Break; Else Inc (Apattern, 3); Else Begin // Match Individual Char Inclusion. IF (Asource [0] = apattern [0]) THEN BEGIN / / GIVEN CHAR MATCHED SET ELIN // Given Char Matched Set Element Inclusion. / / Continue Testing ... result: = true; break; end else inc (apattern); end; end;

转载请注明原文地址:https://www.9cbs.com/read-117506.html

New Post(0)