It is currently Thu Jun 20, 2013 10:31 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 17 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Wildcard String Compare
PostPosted: Tue May 31, 2011 8:44 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Jan 15, 2010 2:03 pm
Posts: 144
Location: Triad, NC
so... my searchfu must have been pretty weak before I had written my own function...but now that I am DONE I found this thread! : viewtopic.php?f=12&t=8578&hilit=findstring+wildcard

bah... lots of good solutions in here, but here is my implementation of a FindString with Wildcard. so far it has worked for my test cases.
Now I'll have to go back and review the others.

Code:
Procedure.i explodeArray(Array sArr.s(1),  string.s, separator.s = ",", trimString.i = #False, limit.i = 0) ; String to Array
  ;From Flype's Functions - http://www.purebasic.fr/english/viewtopic.php?f=12&t=21495&start=0
  ;takes a list of delimited string and explodes out into an array 
  Protected.i index, size = CountString(string, separator)
  If Len(separator) > 1
    ProcedureReturn 0
  EndIf

  If (limit > 0)
    size = limit - 1
  ElseIf (limit < 0)
    size + limit
  EndIf

  ReDim sArr.s(size)

  For index = 0 To size
    If trimString
      sArr(index) = Trim(StringField(string, index + 1, separator))
    Else
      sArr(index) = StringField(string, index + 1, separator)
    EndIf
  Next index

  ProcedureReturn size
EndProcedure


Procedure.i FindStringWC(String.s,StringToFind.s, wildcard.s = "*", caseSensitive.i=#False)
  ;tests to see if a string is found, and supports single or multiple wildcard use.
  ; Returns 0 or 1
  ; String.s is the string to search
  ; StringToFind is the pattern to search for
  ; ie.
;   Debug "these should be true: "
;   Debug FindStringWC("this is a test","this is a test","*")                           ; true
;   Debug FindStringWC("this is a test","this is *","*")                                ; true
;   Debug FindStringWC("this is still a test","this is *","*")                          ; true
;   Debug FindStringWC("this is a test","*a test","*")                                  ; True 
;   Debug FindStringWC("this is a test","this * test","*")                              ; true
;   Debug FindStringWC("blahblahblah this is a test blahblah","*this is a test*")       ; true 
;   Debug FindStringWC("blahblahblah this is a test blahblah","*this is a test*")       ; true 
;   Debug FindStringWC("this is a test","this * test")                                  ; true
;   Debug FindStringWC("this test is true","this test is *")                            ; true
;   Debug FindStringWC("true this test is","* this test is")                            ; true
;   Debug "these should all be false:"
;   Debug FindStringWC("this is a test and should be false","this is test","*")         ; f
;   Debug FindStringWC("blahblahblah this is a false test blahblah","*this is a test*") ; f
;   Debug FindStringWC("this test is false","this * test")                              ; f
;   Debug FindStringWC("not this test","this test is *")                                ; f
;   Debug FindStringWC("this test is false","* this test is")                           ; f
;   Debug FindStringWC("blahblahblah this is a false test blahblah","*this is a test*") ; f
;   Debug FindStringWC("this is a False","this * test","*")                             ; f
;   Debug FindStringWC("this is a test","a test*","*")                                  ; f
 
  If Not caseSensitive
    string = UCase(String)
    StringToFind = UCase(StringToFind)
  EndIf
 
  Protected.i AnyPos = -1 ;wild
  Protected.i i, j, ub
  Protected Dim sSection.s(0)
    explodearray(sSection(),StringToFind,wildcard,#False)
    ub = ArraySize(sSection())
  Protected Dim iPos.i(ub)
  Protected.i nStart = AnyPos
 
  For i = 0 To ub   
    If sSection(i) = ""
      iPos(i) = AnyPos ;wild
    Else
      If i > 0
        nStart = iPos(i-1)
      EndIf
      If nStart <=0
        nStart = 1
      EndIf
      iPos(i) = FindString(String,sSection(i),nStart)
      If iPos(i) = 0 ;not found
        ProcedureReturn #False
      ElseIf i = 0 And sSection(i) <> "" And iPos(i) <> 1 ;verify the left side
        ;left side not wild, but string2find not at beginning
        ProcedureReturn #False
      EndIf
    EndIf
  Next i
  ;now verify the right side:
  If sSection(ub) <> "" ;right side not wild
    If iPos(ub) <> Len(String)-Len(sSection(ub)) + 1
      ProcedureReturn #False
    EndIf
  EndIf
 
  ProcedureReturn 1
EndProcedure

  Debug "these should be true: "
  Debug FindStringWC("this is a test","this is a test","*")                           ; true
  Debug FindStringWC("this is a test","this is *","*")                                ; true
  Debug FindStringWC("this is still a test","this is *","*")                          ; true
  Debug FindStringWC("this is a test","*a test","*")                                  ; True 
  Debug FindStringWC("this is a test","this * test","*")                              ; true
  Debug FindStringWC("blahblahblah this is a test blahblah","*this is a test*")       ; true 
  Debug FindStringWC("blahblahblah this is a test blahblah","*this is a test*")       ; true 
  Debug FindStringWC("this is a test","this * test")                                  ; true
  Debug FindStringWC("this test is true","this test is *")                            ; true
  Debug FindStringWC("true this test is","* this test is")                            ; true
  Debug "these should all be false:"
  Debug FindStringWC("this is a test and should be false","this is test","*")         ; f
  Debug FindStringWC("blahblahblah this is a false test blahblah","*this is a test*") ; f
  Debug FindStringWC("this test is false","this * test")                              ; f
  Debug FindStringWC("not this test","this test is *")                                ; f
  Debug FindStringWC("this test is false","* this test is")                           ; f
  Debug FindStringWC("blahblahblah this is a false test blahblah","*this is a test*") ; f
  Debug FindStringWC("this is a False","this * test","*")                             ; f
  Debug FindStringWC("this is a test","a test*","*")                                  ; f


End



_________________
-Jon

Fedora user
But I work with Win7


Top
 Profile  
 
 Post subject: Re: Wildcard String Compare
PostPosted: Fri Jun 03, 2011 8:04 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat May 17, 2003 11:31 am
Posts: 5812
Then, of course, there's also RegExp :-)

_________________
( PB5.11 Win7 x64 Dell XPS710 Raid 0 VelociRaptor Intel Q6600 nForce 5 NVidia GTS450 )
( You have two options: psychotherapy, or the PureBasic Survival Guide... )


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page Previous  1, 2

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye