Click here to Skip to main content
Licence 
First Posted 1 May 2001
Views 635,473
Bookmarked 87 times

Wildcard string compare (globbing)

By | 15 Feb 2005 | Article
Matches a string against a wildcard string such as "*.*" or "bl?h.*" etc. This is good for file globbing or to match hostmasks.

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



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
AnswerRe: PathMatchSpec instead? PinmemberMandatoryDefault10:39 31 Aug '09  
Questionwchar_t version? Pinmemberrmorales8720:16 29 Nov '08  
AnswerRe: wchar_t version? Pinmemberrazvar21:49 31 Mar '11  
Generalwildcmp in XBLite PinmemberCodeGibbon13:56 27 Nov '08  
GeneralWildcard string compare in C# Pinmemberhaiquang22:15 10 Nov '08  
GeneralRe: Wildcard string compare in C# Pinmemberhaiquang22:22 3 Aug '09  
GeneralC# Direct Port Pinmemberhempels15:10 23 Sep '08  
Well, as direct as I could come up with anyway. Makes use of unsafe to enable pointer arithmetic. Unfortunately, because fixed is required to prevent the GC from moving the pointers, I had to change it to use increment indexers instead of directly manipulating the pointers. Alternatively, you could use stackalloc to instantiate two native char[]'s and copy the values, but that seems contrary to this function's low-memory footprint, high performance goals.
 
Has been tested against every test case presented in the comments section as well as some additional cases I threw in.
 
public unsafe static bool GlobCompare( string glob, string path )
{
      fixed ( char* pGlob = glob, pPath = path )
      {
            int pGlobInc = 0;
            int pPathInc = 0;
 
            int mp = 0;
            int cp = 0;
 
            while ( ( *( pPath + pPathInc ) != 0 ) && ( *( pGlob + pGlobInc ) != '*' ) )
            {
                  if ( ( *( pGlob + pGlobInc ) != *( pPath + pPathInc ) ) && ( *( pGlob + pGlobInc ) != '?' ) )
                  {
                        return false;
                  }
                  pGlobInc++;
                  pPathInc++;
            }
 
            while ( *( pPath + pPathInc ) != 0 )
            {
                  if ( *( pGlob + pGlobInc ) == '*' )
                  {
                        if ( 0 == *( pGlob + ++pGlobInc ) )
                        {
                              return true;
                        }
                        mp = pGlobInc;
                        cp = pPathInc + 1;
                  }
                  else if ( ( *( pGlob + pGlobInc ) == *( pPath + pPathInc ) ) || ( *( pGlob + pGlobInc ) == '?' ) )
                  {
                        pGlobInc++;
                        pPathInc++;
                  }
                  else
                  {
                        pGlobInc = mp;
                        pPathInc = cp++;
                  }
            }
 
            while ( *( pGlob + pGlobInc ) == '*' )
            {
                  pGlobInc++;
            }
            return ( 0 == *( pGlob + pGlobInc ) );
      }
}
General...and yet another C# port [modified] PinmemberDVF16:59 27 Aug '10  
GeneralRe: ...and yet another C# port PinmemberVUnreal11:22 21 Sep '10  
General[Message Removed] Pinmemberstonber14:22 18 Sep '08  
GeneralUsing in Artistic Style Pinmemberjimp024:43 3 Apr '08  
GeneralGeez... Pinmemberlarryfr9:39 5 Mar '08  
QuestionConvert to a replace? Pinmemberwilliaps8:31 20 Mar '07  
GeneralC# RexExp version Pinmemberspinsane6:30 4 Nov '06  
GeneralKudos Pinmemberquantumred4:37 14 Oct '06  
GeneralRe: Kudos Pinmembermilkplus11:19 24 Feb '10  
Generalwildcmp("*&lt;*&gt;", "&lt;field1&gt;&lt;field2&gt;") not working [modified] PinmemberDaniel B.13:14 6 Sep '06  
GeneralRe: wildcmp("*&lt;*&gt;", "&lt;field1&gt;&lt;field2&gt;") not working Pinmemberradboudp0:35 16 Feb '07  
Generalreturn value type Pinmemberwdx0415:49 8 Jan '06  
General*? case match Pinmembertalimu23:42 3 Nov '05  
GeneralRe: *? case match Pinmemberkuhnm2:18 15 Sep '06  
GeneralRe: *? case match Pinmemberkuhnm4:48 18 Sep '06  
GeneralGets my 5 PinmemberFranc Morales17:05 18 Oct '05  
Generalmp and cp Pinmembertwopieman11:59 15 Mar '05  
GeneralRe: mp and cp Pinmemberradboudp1:14 16 Feb '07  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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