programming tools for Windows applications development
  Home  |   SwiftForth Archive  |   SwiftX Archive  |

A small wildcard matching algorithm.

From: Charles Melice <3d_at_forthcad.com>
Date: Tue, 17 Oct 2006 08:05:29 +0200

\ A small wildcard matching algorithm.
\ Converted from (Copyright) Jack Handy, "wildcmp" C source.
\ See https://secure.codeproject.com/string/wildcmp.asp

\ WILDCARD-MATCH ( text ntext wild nwild -- flag )
(
  : TRY-IT
    s" blah.jpg" s" bl?h.*" WILDCARD-MATCH if
      ." we have a match!"
    else
      ." no match"
    then ;
)

\ PACKAGE XXX

: andif s" dup if drop " evaluate ; immediate
: orif s" dup 0= if drop " evaluate ; immediate

: >C ( str -- c|0 ) 2@ 0> if c@ else drop 0 then ;
: an++ ( a n -- a+ n-1 ) 1- swap char+ swap ;
: str++ ( str -- ) dup 2@ an++ rot 2! ;

char * constant '*'
char ? constant '?'

\ To have 'wildcard-match' not case sensitive.
: up2 ( a b -- B A ) upper swap upper ;

\ PUBLIC

: WILDCARD-MATCH ( text nt wild nw -- flag )
    [struct
        2 cells r-field text
        2 cells r-field wild
        2 cells r-field pt
        2 cells r-field pw
    struct]
    wild 2! text 2!
    begin
        text >C andif wild >C '*' <> then
    while
        text >C wild >C up2 <> andif wild >C '?' <> then
        if false exit then
        text str++ wild str++
    repeat
    begin text >C while
        wild >C '*' = if
            wild str++
            wild >C 0= if true exit then
            wild 2@ pw 2!
            text 2@ an++ pt 2!
        else
            wild >C text >C up2 = orif wild >C '?' = then
            if
                text str++ wild str++
            else
                pw 2@ wild 2!
                pt 2@ 2dup text 2! an++ pt 2!
            then
        then
    repeat
    begin wild >C '*' = while wild str++ repeat
    wild >C 0= ;

\ END-PACKAGE

----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe" to sftalk-request_at_forth.com
For list command help, send subject "help" to sftalk-request_at_forth.com
Message archives are located at http://www.forth.com/archive/sftalk
----------------------------------------------------------------------
This list is a forum for SwiftForth users. For product support and
bug reports, please send email to support_at_forth.com
----------------------------------------------------------------------
Received on Mon Oct 16 2006 - 23:06:35 PDT

This archive was generated by hypermail 2.2.0 : Tue Dec 02 2008 - 03:04:41 PST