string compare with wildcards
Alex Martelli
staff at cadlab.sublink.ORG
Fri Dec 14 20:00:48 AEST 1990
claus at iesd.auc.dk (Claus S. Jensen) writes:
...
> Does anybody out there have a routine that compares
>two strings, one of which contains wildcards (*,?).
It's real easy, thanks to recursion (no "stylistic" flames, please!-):
int wildmatch(pat, str) char *pat, *str; {
int pat_ch, str_ch;
while(pat_ch = *pat++) {
if(pat_ch == '*') {
if(*pat == 0) return 1;
for(;;) {
if(wildmatch(pat, str)) return 1;
if((str_ch = *str++)==0) return 0;
}
} else {
if((str_ch = *str++)==0) return 0;
if(pat_ch != '?' && str_ch != pat_ch) return 0;
}
}
return *str == 0;
}
Optimizing this, and in particular eliminating the recursive call,
is, on the other hand, a rather interesting exercise, but one I'd
undertake only if wildcard-matching was proven-by-profiling to be
a bottleneck in my application... which is rather unlikely, I think.
--
Alex Martelli - CAD.LAB s.p.a., v. Stalingrado 45, Bologna, Italia
Email: (work:) staff at cadlab.sublink.org, (home:) alex at am.sublink.org
Phone: (work:) ++39 (51) 371099, (home:) ++39 (51) 250434;
Fax: ++39 (51) 366964 (work only), Fidonet: 332/401.3 (home only).
More information about the Comp.lang.c
mailing list