Delphi's wildcard comparison (fifth edition)

zhaozj2021-02-16  50

DELPHI wildcard comparison (Fifth Edition) Author: Lee Kwan Yu email: e271828@163.net 2003.1.5 I used to think there is no ready-made DELPHI wildcard function, and later found MatchesMask (). I used to implement this function when I was in a free state. The algorithm of the program is more complicated, first adds on the end of the substrings? * ', Reciprocate strings, find characters between wildcards in substrings, ie sub-strings in the subtrial strings, and then look for a substring in the substring in the source string, but implement it still Many twists and turns. This function implements the following features: 1. Most cases are higher than that of comparative algorithm and matchesmask () speeds; 2. Realize the correct comparison of all situations of the asterisk and question mark; // This may still take time to verify 3. Support Chinese; // The asterisk and question mark are valid in English. Support case sensitive choices.

Pay attention to the beginning and end of the substrings and adding an asterisk. This algorithm is likely to be similar to the rendering algorithm, but in fact, there are some different modifications, and it is possible to make some improvements in most cases, how many difficulties are faster than recursive process. set. At least in this estimate: When the wildcard is used as a search substring, if the source string is "11111111" substrings as "* 1111112 *", the time complexity using the recursive algorithm is O (N * m), but I This function will be simplified to simplify the time complexity of the POS () function, maybe you can imagine the POS () in Delphi into "Kuts - Morris - Prat (KMP) O (n m) under the algorithm. A small amount is not obvious with the speed of the recursive algorithm. When the source string is 100 1, the substrings are 99 consecutive 1, and the character 2 is added, and it is more than a few seconds in a 1000 cycle, which is faster than the matchesmask () function. Twenty A few seconds. I actually test the test show that the three are sometimes the fastest, but matchesmask () seems to be more slow, and the recursive changes are bigger, and the function I write may be average at speed. However, the function I wrote is for reference only, I have a problem, I don't have any responsibility.

function isABClikeAX (const abc, ax: widestring): boolean; file: // abc is the source string, ax is a substring varabcstart, axstart, abclength, axlength: integer; endpartabc, endpartax, subax: widestring; temp, abcwww, axwww: INTEGER; begin file: // aaatemp: = 0; abcstart: = 1; axstart: = 1; AXWW: = 1; abcww: = 1; Abclightth: = Length (ABC); Axlength: = Length (AX); IsabClikeax: = True; while axstart <= axlength do // source string length greater than or equal to substrings Begin // BBB if (AX [Axlength] = '*') and (axlength = axstart) Then IsabClikeax: = True else; // substrs long excess s struts Break; end; if ax [axstart] = '?' Then Begin incstart; inc (AbcStart); Continue; End; if AX [AxStart ] = '*' THEN BEGIN INC; TEMP: = 1; AXWW: = AXStart; ABCww: = Abstart; Continue; End; if NOT ((AX [AxStart] = '?') Or (Ax [AxStart] = '*')) Then Begin // CCC Endpartax: = COPY (AX, AXSTART, AXLENGTH-AXSTART 1) '? *'; Subax: = Copy (Endpartax, 1, Min (POS ('?', Endpartax ), POS ('*', endpartax) - 1); AxStart: = AxStart min (POS ('?', endpartax), POS ('*', endpartax) - 1; endpartabc: = copy (abc, abcstart, abcLength-abcstart 1); if ((Subax, EndpartAbc) <> 0) and (Temp = 1)) OR ((Subax, EndpartAbc) = 1) And (Temp = 0)) Then Begin // DDD if Temp = 1 Temp: = 0; Abcstart: = Abcstart (POS (Subax (Subax, EndpartAbc) ) length (subax) -1); end // DDD ELSE // DDD begin // DDD IF (Temp = 0) And (AXWWW> 1) Then Begin AxStart: = AXwww; abcww: = ABCWW 1; Abcstart: = Abcwww; temp: = 1;

Continue; end; isabclikeax: = false; break; end; // ddd end; // ccc end; // bbb IF (result) AND (abcstart <= Abclength) and (AX [Axlength] <> * ') THEN ISABCLIKEAX: = false; // Source Strings End; // Aaafunction Islike (ABC, AAAFunction islike): Boolean; File: // Sensitive Sensitive Sensitive Function Begin Islike: = IsabClikeax (ABC, AX); End; Function WideCard: Boolean; File: // Unsmooth Insensitive Function Begin ABC: = Uppercase (AX: = Uppercase (AX); widecard: = isabclikeax (ABC, AX) ; END; Note Uses Math, because of the use of min (), can also use the IF statement to replace min (), but not enough.

Thank you for some netizens to give me some correct insights, making modifications have the right direction.

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

New Post(0)