[bmx] Search String Array by N [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : Search String Array
Author : N
Posted : 1+ years ago

Description : 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: <a href="codearcs8aad.html?code=1650" >SplitString</a>


Code :
Code (blitzmax) Select
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...