Advertisement
Mysoft

Untitled

Apr 24th, 2024
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "crt.bi"
  2.  
  3. function WildMatch( sWildText as string , sCompare as string , iCaseSensitive as boolean = false ) as boolean
  4.   dim as integer iTxt, iCmp
  5.  
  6.   'if any of them is empty... then result is direct
  7.   if sCompare[0] = 0 then return false  
  8.   do
  9.     var iCT = sWildText[iTxt] : iTxt += 1
  10.     var iCC = sCompare[iCmp] : iCmp += 1
  11.     'print iTxt-1;" ";chr(iCT),iCmp-1;" ";chr(iCC)
  12.     select case iCT
  13.     case 0        : return iCC=0 'once it text reachs end.. it's a success
  14.     case asc("^"),asc("~") 'white space (at least 1, at least 0)
  15.       dim as long iNum = 0
  16.       do
  17.         select case iCC
  18.         case asc(" "),asc(!"\r"),asc(!"\n"),asc(!"\t")
  19.           iNum += 1 : iCC = sCompare[iCmp] : iCmp += 1
  20.         case else
  21.           if iCT=asc("^") andalso iNum=0 then return false 'must at least have one blank
  22.           iCmp -= 1 : continue do,do 'continue repeating same char
  23.         end select
  24.       loop          
  25.     case asc("?") : 'if it's a single char wild
  26.       'we're done if reached end of comparsion
  27.       'and it will be true if next wildtext is also done
  28.       if iCC=0 then return sWildText[iTxt]=0        
  29.     case asc("*")      
  30.       var iCT2 = sWildText[iTxt]
  31.       'if matching anything after then it will be true if it's the end of wildtext
  32.       if iCT2 = 0 then return true
  33.       'if is not another wildcard then must continue checking
  34.       if iCT2 <> asc("?") andalso iCT2 <> asc("*") then        
  35.         'if end of compare text happened then it's false!
  36.         if iCC = 0 then return false
  37.         'if found matching char then continue matching
  38.         if iCT2 = asc("^") orelse asc("~") then
  39.           select case iCC
  40.           case asc(" "),asc(!"\r"),asc(!"\n"),asc(!"\t")
  41.             iCmp -= 1 : continue do
  42.           end select
  43.         elseif iCaseSensitive then
  44.           if iCC=iCT2 then iTxt += 1: continue do
  45.         else
  46.           if ToLower(iCC)=ToLower(iCT2) then iTxt += 1: continue do
  47.         end if
  48.         'otherwise goes back on wildtext (to compare against *) again
  49.         iTxt -= 1 : continue do
  50.       end if
  51.       'next is also a wildcard, so we will process that
  52.     case else 'is a direct comparsion...
  53.       'if compare string finished or didnt match then we failed
  54.       if iCC=0 then return false
  55.       if iCaseSensitive then
  56.         if iCC<>iCT then return false
  57.       else
  58.         if ToLower(iCC)<>ToLower(iCT) then return false
  59.       end if
  60.     end select
  61.   loop
  62. end function
  63.  
  64. #define WildMatchTest(_A,_B,_C...) WildMatch(_A,_B,_C),"'" _A "'", "'" _B "'"
  65. print "Expected","Result","WildCards","Comparsion"
  66. print string(60,"-")
  67. print true,  WildMatchTest("Hello","Hello")
  68. print false, WildMatchTest("Hell","Hello")
  69. print false, WildMatchTest("Hello","Hell")
  70. print false, WildMatchTest("Hello","HELLO",true)
  71. print false, WildMatchTest("Hello","World")
  72. print true,  WildMatchTest("B?n","Bin")
  73. print true,  WildMatchTest("b?n","Ben")
  74. print false, WildMatchTest("B?n","Bem")
  75. print true,  WildMatchTest("*us","jealous")
  76. print false, WildMatchTest("*us","jealously")
  77. print true,  WildMatchTest("Bang*","Bangaloo")
  78. print false, WildMatchTest("Leap*","lip")
  79. print false, WildMatchTest("Bi*us","Binocculus") 'want this to be true?
  80. print true,  WildMatchTest("Bi*us","Binocc?lus")
  81. print true,  wildMatchTest("*cup*","Porcupine")
  82. print true,  WildMatchTest("*Hi*Ta*","Hippopotamus")
  83. print true,  WildMatchTest(" Hello",!"\t \t Hello")
  84. print false, WildMatchTest(" Hello","Hello")
  85. print true,  WildMatchTest("~Hello~;"," Hello;")
  86. print true,  WildMatchTest("*~Hello;",!"World\t Hello;")
  87. print true,  WildMatchTest("*^Hello;",!"World Hello;")
  88. print false,  WildMatchTest("*~Hello;","WorldHello;")
  89.  
  90. sleep
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement