Revised C filename wildcard routine

Lars Henrik Mathiesen thorinn at diku.dk
Sat Jan 5 05:46:39 AEST 1991


rsalz at bbn.com (Rich Salz) writes:
>This is a revised version of my pattern-matching routine.  It has been
>posted to the net several times, and shows up in several places including
>Gilmore's public domain TAR.  You might want to test this, but I did
>and it seems solid.
>	/r$

>X**  Special thanks to Lars Mathiesen for the ABORT code.  This can greatly
>X**  speed up failing wildcard patterns.  For example:

>Xstatic int
>XStar(s, p)
>X    register char	*s;
>X    register char	*p;
>X{
>X    while (wildmat(s, p) == FALSE)
>X	if (*++s == '\0')
>X	    return ABORT;
>X    return TRUE;
>X}

I just thought I'd mention that in the patch I sent to Rich, the body
of the Star routine read like this:

{
    register int ret;

    while ((ret = DoMatch(s, p)) == FALSE)
	if (*++s == '\0')
	    return ABORT;
    return ret;
}

(Except that I used wildmatch for DoMatch.) Rich's version gives the
correct results, but the recursion goes through the wildmat function
maps all returns of ABORT to FALSE --- so the time complexity is still
the same as for traditional versions, with double the function call
overhead. (The whole point of the ABORT return code is that it is
supposed to propagate through the Star function, aborting the loop.)

I have just noted that the test on *++s in Star is repeated (with the
same result) at the top of the loop in DoMatch, so I suggest that it
should be eliminated as in the following version of Star:

static int
Star(s, p)
    register char       *s;
    register char       *p;
{
    register int ret;

    do
	ret = DoMatch(s++, p);
    while (ret == FALSE);
    return ret;
}

It may cause one more call of DoMatch in the failing case, but I think
it is clearer.

--
Lars Mathiesen, DIKU, U of Copenhagen, Denmark      [uunet!]mcsun!diku!thorinn
Institute of Datalogy -- we're scientists, not engineers.      thorinn at diku.dk



More information about the Alt.sources mailing list