News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

wildcard string pattern matching ?

Started by M4D45M, August 05, 2006, 01:00:32 PM

Previous topic - Next topic

zooba

I have coded this for the next version of ASM Runtime (version 0.300 - coming soon  :wink ) and it has worked fine for everything I've tested it on (mostly files, though it should work for any other strings).

The Lower() and StringFree macros are part of ASM Runtime, however, replacing them with your preferred lowercasing function should result in a working function.

(By the way, this does some naughty stack stuff - kids, close your eyes  :toothy )

Cheers,

Zooba :U

FileMatch PROC USES esi edi spFilename:DWORD, spFilter:DWORD
    LOCAL   dwESP:DWORD
    LOCAL   dwMatchAllCount:DWORD
   
    mov     dwMatchAllCount, 0
    mov     esi, LCase(spFilename)
    mov     edi, LCase(spFilter)
    push    esi
    push    edi
   
    mov     dwESP, esp
   
    .while(BYTE PTR [edi] && BYTE PTR [esi])
@LoopStart:
        mov al, [edi]
        .break .if(!al)
        .if(al == '*')
            push    edi
            .repeat
                inc     edi
                mov     al, [edi]
            .until(al != '*')
           
            .while(BYTE PTR [esi] && BYTE PTR [esi] != al)
                inc esi
            .endw
            .break .if(BYTE PTR [esi] == 0)
            inc     dwMatchAllCount
        .elseif(al == '?')
            inc     esi
            inc     edi
            .break .if(BYTE PTR [esi] == 0)
        .else
            .break .if(al != [esi])
            inc     edi
            inc     esi
        .endif
    .endw
    .if(BYTE PTR [esi] && dwMatchAllCount)
        dec     dwMatchAllCount
        pop     edi
        jmp     @LoopStart
    .endif
   
    mov     esp, dwESP
   
    .if(BYTE PTR [esi] || BYTE PTR [edi])
        jmp @NoMatch
    .endif
       
@Match:
    or      eax, -1
    jmp     @Exit
   
@NoMatch:
    xor     eax, eax
    jmp     @Exit

@Exit:
    pop     edx
    pop     ecx
    StringFree edx, ecx
    ret
FileMatch ENDP

mrpink


TNick

Didn't had a closer look to see why, but WildMatch doesn't work.

[later]
I have tested strmatchpattern, too, but this one doesn't work, either...
Am I doing something wrong here?

Attached file updated...
[/later]

[attachment deleted by admin]

TNick

Need opinions: Does such a function need to return an offset to where the pattern has been found or it's enough to tell us if it has found the pattern or not?

Nick

zooba

Quote from: TNick on January 29, 2007, 12:45:07 PM
Need opinions: Does such a function need to return an offset to where the pattern has been found or it's enough to tell us if it has found the pattern or not?

Nick

Up to the creator. It's probably no harder to say where the pattern is, and if you return 0 for no match then you can treat it as a boolean (zero/non-zero) for whether it was found or not.

My one above doesn't, since the point is that the pattern will match from the start of the string. I have written others (and subsequently lost them in a recent hard drive failure) which will return the position of a match and one which would return a linked list of text which matches parameters specified in the pattern string.

TNick

Thanks for your reply, zooba!
I have written my own, but I have to disagree with you. Returning the offset means some more code => function will be slower. Maybe two functions - one boolean and other with offset ... yap, that seem to be the solution. :)

Regards,
Nick

zooba

How can you compare a string if you don't know where in memory the string is? If you know where the string is in memory, you can send that back.  :toothy

Okay, it's not quite that simple :P . When you start matching the pattern, store the address of the character you start at. If it matches, return that value, otherwise increment it and start again. Pattern searching simplifies down quite well to precise matching (ie. entire string matches and/or match must begin at the start of the string) in a separate function (which returns a boolean) and a search function (ie. a loop) which calls it.

If we're talking about pattern matching, as opposed to pattern searching, returning a pointer is (excuse the pun) pointless. If you are searching, a pointer is much more useful than a boolean.

Cheers,

Zooba :U

TNick

Quote from: zooba on January 31, 2007, 11:58:14 AM
How can you compare a string if you don't know where in memory the string is? If you know where the string is in memory, you can send that back.  :toothy
:lol :lol :lol

I will try to add this to WldMatch. However, when I first tried to do that, things were becoming messy. But, if you say it's easy, I should give it a second chance.

Thanks for your reply!
Nick

zooba

I've had a look at WldMatchA and I believe you when you said it got messy  :green  My take on pattern searching is that when you start matching, you need to know where you started from so you can go back there if the match fails. The upside of that approach is that the pointer to the start of the match is readily available.

(I have continued this post in your other thread where it is more relevant)