Click here to Skip to main content
Click here to Skip to main content

Wildcard string compare (globbing)

By , 15 Feb 2005
 

Usage:

This is a fast, lightweight, and simple pattern matching function.

if (wildcmp("bl?h.*", "blah.jpg")) {
  //we have a match!
} else {
  //no match =(
}

Function:

int wildcmp(const char *wild, const char *string) {
  // Written by Jack Handy - <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A>
  const char *cp = NULL, *mp = NULL;

  while ((*string) && (*wild != '*')) {
    if ((*wild != *string) && (*wild != '?')) {
      return 0;
    }
    wild++;
    string++;
  }

  while (*string) {
    if (*wild == '*') {
      if (!*++wild) {
        return 1;
      }
      mp = wild;
      cp = string+1;
    } else if ((*wild == *string) || (*wild == '?')) {
      wild++;
      string++;
    } else {
      wild = mp;
      string = cp++;
    }
  }

  while (*wild == '*') {
    wild++;
  }
  return !*wild;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jack Handy
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralCase Insensitive wildcmpmemberTechiex16 Mar '04 - 9:36 
I want case insenstive wildcmp function. Could anyone help me?
GeneralRe: Case Insensitive wildcmpmemberNeville Franks16 Mar '04 - 9:52 
Simply wrap the code that compares charaters in toupper() calls.
 
eg.
    if ((toupper(*wild) != toupper(*string)) && (*wild != '?')) {

 
Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
 

GeneralRe: Case Insensitive wildcmpmemberTechiex23 Mar '04 - 9:33 
toupper doesnt support char characters. You could optimize this code.
This code compares also multilingual characters
 


#include

#define BIT5 0x20

char buf[] = "this is ®Ñê test";
char *pbuf;

int lower(int ch);


int lower(int ch)
{
if ((ch==64)||(ch==91)||(ch==92)||(ch==93))
return ch &= ~(ch & BIT5);
return ch |= BIT5;
}

int wildcmp(char *wild, char *string) {
char *cp, *mp;

while ((*string) && (*wild != '*')) {
if ((*wild != *string) && (*wild != '?')) {
return 0;
}
wild++;
string++;
}

while (*string) {
if (*wild == '*') {
if (!*++wild) {
return 1;
}
mp = wild;
cp = string+1;
} else if ((*wild == *string) || (*wild == '?')) {
wild++;
string++;
} else {
wild = mp;
string = cp++;
}
}
 
while (*wild == '*') {
wild++;
}
return !*wild;
}
 

int main() {

for (pbuf = &buf[0]; *pbuf; ++pbuf)
*pbuf= lower (*pbuf);

if (wildcmp("*®ñê*", buf)){
printf ("match : %s \n", buf);
} else {
printf ("not match: %s \n",buf);
}
}



GeneralRe: Case Insensitive wildcmpmemberDavidCrow23 Feb '05 - 2:24 
Techiex wrote:
toupper doesnt support char characters
 
Since when?
 
ASSERT('A' == toupper('a'));

 

"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow


GeneralRe: Case Insensitive wildcmpmemberVic Mackey23 Feb '05 - 8:00 
Be careful when using toupper(), some of the CRT variations of this function _only_ work when the input is known to be lowercase. For example, the return value is invalid for _toupper('A') and some implementations of toupper('A') as well...check the documentation.

GeneralRe: Case Insensitive wildcmpmemberDavidCrow23 Feb '05 - 8:22 
Vic Mackey wrote:
Be careful when using toupper(), some of the CRT variations of this function _only_ work when the input is known to be lowercase.
 
Which is why I specified toupper() instead of _toupper(). The latter is nothing but a #define directive that does no checking.
 
In any case, I was simply responding to Techiex's statement that "toupper doesnt support char characters."
 

"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow


GeneralRe: Case Insensitive wildcmpmemberf_randy12 Jun '06 - 18:13 
Doesnt seems to work for me.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 15 Feb 2005
Article Copyright 2001 by Jack Handy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid