Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!rutgers!otello!gear!cadlab!staff From: staff@cadlab.sublink.ORG (Alex Martelli) Newsgroups: comp.lang.c Subject: Re: string compare with wildcards Message-ID: <552@cadlab.sublink.ORG> Date: 14 Dec 90 09:00:48 GMT References: <1990Dec13.133412.11981@iesd.auc.dk> Organization: CAD.LAB, Bologna, Italia Lines: 33 claus@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@cadlab.sublink.org, (home:) alex@am.sublink.org Phone: (work:) ++39 (51) 371099, (home:) ++39 (51) 250434; Fax: ++39 (51) 366964 (work only), Fidonet: 332/401.3 (home only).