Comparison of Delphi wildcard characters

Source: Internet
Author: User
Tags comparison continue integer min

I used to think that there was no wildcard function in Delphi, and then I found Matchesmask (). I used to do a custom function to implement this function when I was in a state of freedom when I was still in the mood when I was not found.

The program's algorithm is more complex, first at the end of the substring plus '? * ', and then read the substring, find the characters between the wildcard character in the substring, that is, the substring in the substring, and then in the source string to find whether there are substrings in the substring, but to achieve it still cost a lot of trouble. This function implements the following functions:

1. In most cases, the speed is higher than the recursive algorithm and the Matchesmask ().

2. Implements a correct comparison of all cases of asterisks and question marks;//This may still take time to verify

3. Support Chinese;//asterisk and question mark to be valid in English

4. Support for case sensitive selection.

Note that there is a difference between the start and end of a substring and the addition of an asterisk. This algorithm may be similar to a function implemented using a recursive algorithm, but in fact there are some different, some improvements to recursion, may be in most cases faster than the recursive process, fast how much more difficult to decide. At least this estimate is that when the wildcard comparison is only used as a lookup substring, the time complexity of using the recursive algorithm is O (n*m), if the source string is "1111111111" substring "*11111112*". But this function I wrote would simplify the time complexity of calling the POS () function A few times, perhaps imagining the Pos () in Delphi as the O (n+m) under the Knut-Morris---Pratt (KMP) algorithm. The speed of a small number of lower and recursive algorithms is not obvious. When the source string is 100 consecutive 1, and the substring is 99 consecutive 1 with the last character 2, the test in a 1000 cycle is faster than the recursive algorithm for a few seconds faster than the Matchesmask () function for more than 20 seconds. I have actually tested many times to show that the three are sometimes the fastest, but Matchesmask () seems to be more slowly, and the speed of recursion varies a lot, and the function I write may be more average in velocity. I just write the function for reference only, out of the problem I am not responsible for OH.

function Isabclikeax (const abc,ax:widestring): Boolean; File://abc is a source string, Ax is a substring
Var
Abcstart,axstart,abclength,axlength:integer;
endpartabc,endpartax,subax:widestring;
Temp,abcwww,axwww:integer;
Begin FILE://AAA
temp:=0;
Abcstart:=1;
Axstart:=1;
Axwww:=1;
Abcwww:=1;
Abclength:=length (ABC);
Axlength:=length (AX);
Isabclikeax:=true;
While axstart<=axlength do//source string length greater than or equal to substring
begin//bbb
If abcstart> Abclength Then
Begin
if (ax[axlength]= ' * ') and (Axlength=axstart) then isabclikeax:=true
else isabclikeax:=false;//substring long past source string
Break
End
If ax[axstart]= '? ' Then
Begin
Inc (Axstart);
Inc (Abcstart);
Continue
End
If ax[axstart]= ' * ' Then
Begin
Inc (Axstart);
Temp:=1;
Axwww:=axstart;
Abcwww:=abcstart;
Continue
End
If not ((ax[axstart]= '? ') or (ax[axstart]= ' * ')) Then
Begin//ccc
Endpartax:=copy (ax,axstart,axlength-axstart+1) + '? * ';
Subax:=copy (POS (' Endpartax,1,min ', Endpartax), POS (' * ', Endpartax))-1);
Axstart:=axstart+min (POS ('? ', Endpartax), POS (' * ', Endpartax))-1;
Endpartabc:=copy (abc,abcstart,abclength-abcstart+1);
if (POS (SUBAX,ENDPARTABC) <>0) and (Temp=1)) or (POS (SUBAX,ENDPARTABC) =1) and (temp=0)) Then
Begin//ddd
If Temp=1 then temp:=0;
abcstart:=abcstart+ (POS (SUBAX,ENDPARTABC) +length (Subax)-1);
End//ddd
Else//ddd
Begin//ddd
if (temp=0) and (axwww>1) then
Begin
axstart:=axwww;
abcwww:=abcwww+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;//the source string is longer than the substring
End;//aaa
FUNCTION islike (abc,ax:string): boolean; file://-Case Sensitive functions
Begin
Islike:=isabclikeax (Abc,ax);
End
FUNCTION Widecard (abc,ax:string): boolean; file://Case insensitive functions
Begin
Abc:=uppercase (ABC);
Ax:=uppercase (AX);
Widecard:=isabclikeax (Abc,ax);
End

Note that uses MATH, because you use min (), you can use the IF statement instead of Min (), but not clear enough.

Thanks some netizens to give me some correct opinion, make the revision to have the correct direction.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.