Code archives/Algorithms/Search String Array

This code has been declared by its author to be Public Domain code.

Download source code

Search String Array by N2006
Basically, this searches an array of strings for strings matching the search pattern (* is the only supported wildcard; I didn't see a need for any others).

I don't know of any problems with this, but I wrote it in about 20 minutes or so (while eating cake!), so if you find a problem, might as well post it and I'll try to fix it.

Requires this: SplitString
Function SearchStrings$[]( search$, arr$[] )
    Local clean$ = ""
    Local l$ = ""
    For Local ex% = 0 To search.Length-1
        Local p$ = Chr(search[ex])
        If p = "*" And l = "*" Then Continue
        If p = "*" Then p = "|*|"
        clean :+ p
    Next
    Local s$[] = SplitString( clean, "|" )
    
    Local resn%=arr.Length
    Local bad%[arr.Length]
    memset_(bad,0,arr.Length*4)
    
    For Local ex% = 0 To arr.Length-1
        Local e$ = arr[ex]
        If e = "" Or e = Null Then
            bad[ex] = 1
            resn :- 1
            Continue
        EndIf
        Local from% = 0, find% = 0
        
        For Local i% = 0 To s.Length-1
            Local p$ = s[i]
            
            If p = "*" And i = s.Length-1 Then
                Continue
            ElseIf p = "*" Then
                p = s[i+1]
            EndIf
            
            find = e.Find(p,from)
            If find = -1 Or (find > 0 And i = 0 And s[i] <> "*") Then
                bad[ex] = 1
                resn :- 1
                Exit
            EndIf
            from = find
        Next
    Next
    
    Local ret$[resn]
    Local n% = 0
    For Local ex% = 0 To arr.Length-1
        If bad[ex] Then Continue
        Local e$ = arr[ex]
        ret[n] = e
        n :+ 1
    Next
    
    Return ret
End Function

Comments

None.

Code Archives Forum