wildcard matching

Tony Bielobockie Tony.Bielobockie at urchin.fidonet.org
Sat Dec 29 04:45:43 AEST 1990


To: Alex Martelli
 
AM> Optimizing this, and in particular eliminating the recursive call,
AM> is, on the other hand, a rather interesting exercise, but one I'd
AM> undertake only if wildcard-matching was proven-by-profiling to be
AM> a bottleneck in my application... which is rather unlikely, I think.
 
I think that the recursive call is unnessicary.  Consider that anything
trailing the '*' character is ignored anyway, at least in MS-DOS, and
OS/2. What need is there for further checking if a '*' character is
encountered?  Does that hold true in the UNIX would too?
 
The following function will return the same results, with the exceptional
case being when the '*' character is not the last character in the 
wildcard
string.  Plus it is a little speedier, due to the fact that no data is
copied from the original string.
 
int wildmatch( char *pat, char *str )
        {
        while ( (*pat != '*') && (*pat != 0) && (*str != 0) )
                {
                if ( (*pat != '?') && (*str != *pat) )
                        {
                        return 0;
                        }
                pat++;
                str++;
                if ( (!*pat && *str) || (!*str && *pat) )
                        {
                        return 0;
                        }
                }
        return( 1 );
        }
 



More information about the Comp.lang.c mailing list