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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Slight efficiency improvementmemberBill Buklis9 Jul '04 - 7:19 
Maybe I posted too soon. I didn't think there was a way for cp to not equal string+1. But, after thinking about it some more I found a pattern type that would:   *???c*   It's interesting how the loop keeps shifting back and forth with this type of pattern.   However, using...
QuestionPathMatchSpec (shlwapi.h)?memberpeterchen28 Jun '04 - 6:56 
Just a thought: the PathMatchSpec SLWU API could provide similar. I guess it does have some differences (e.g. allowing to specify multiple specs, separated by semicolon), but it might be a simple alternative for many similar tasks.     we are here to help each other get through...
AnswerRe: PathMatchSpec (shlwapi.h)?memberJack Handy13 Feb '05 - 9:56 
peterchen wrote:
Just a thought:
the PathMatchSpec SLWU API could provide similar. I guess it does have some differences (e.g. allowing to specify multiple specs, separated by semicolon), but it might be a simple alternative for many similar tasks.

 
The wildcmp() function is meant to be lightweight and fast.
 
If the extra functionality of multple specs is needed and you don't want to parse the input yourself then you can go ahead and use the PathMatchSpec() API.
 
Just make sure you don't mind these limitations:
 
1. Adding another dependancy to your executable by including the lib
2. Not portable (wildcmp() compiles fine under unix)
3. More memory overhead (larger code footprint)
4. The horrible slowness
 
I have ran some benchmarks and pasted the results below. I can provide the .cpp file for the benchmarks if anyone is interested.
 
-Jack
 
10MM iterations.
Compiled as a console app using vc6 in release mode with /O2 optimization.
Ran on a pM 1.7ghz
 
"C:\\T*s*.t?t", "C:\\Test\\File.txt"
PathMatchSpec MATCH => 20.5090s
wildcmp MATCH => 1.0320s
 
"C:\\T*s*.t??t", "C:\\Test\\File.txt"
PathMatchSpec NO MATCH => 45.7760s
wildcmp NO MATCH => 0.8910s

 

There are 10 types of people in this world, those that understand binary and those who don't.


GeneralDoesnt seem to work well..sussBikram Singh13 May '04 - 1:56 
Tried these wildcards, and they show different results in your code and in Windows Explorer's search command.   ??x* *so* ??so* ??so??   Lack of comments in the code also make it a bit difficult to understand. On the whole however, good job!   Bikram
GeneralRe: Doesnt seem to work well..memberJack Handy21 Jun '04 - 9:13 
The Windows Explorer search is not a straight wildcard match. It essentially adds *'s to either end of your input string so "b?r" matches "foobar.txt". I take a more literal approach. This function does not presume to be smarter than the caller. It is not only meant for files, it is also...
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); ...
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.  ...
GeneralRe: Case Insensitive wildcmpmemberf_randy12 Jun '06 - 18:13 
Doesnt seems to work for me.
GeneralExcellent code!memberHans Dietrich16 Jul '03 - 19:40 
Very nice, compact, works great and is very fast. Thanks, Jack!   Best wishes, Hans
Generalchecking for nullmemberJack Handy13 Mar '03 - 20:38 
I've seen a few people in these boards complain that I didn't check for null pointers in this function. This is a C function and the last time I checked, passing NULL to strcmp or any other C string function will segfault. I'm not saying this is great, and if you wanted to add a check for...
GeneralRe: checking for nullsussAnonymous15 Mar '03 - 20:10 
I agree. This is not what I meant.   Efrat
GeneralRe: checking for nullmemberJack Handy18 Mar '03 - 13:25 
Yeah, I wasn't talking to you, you were respectful. I was talking to the people below in the 'too complicated' thread. Namely 'The C++ Guru'.   -Jack   There are 10 types of people in this world, those that understand binary and those who don't.
Generalgreat code, but ...sussAnonymous12 Mar '03 - 3:17 
great code, but if I'm not mistaken cp can point beyon string array bounds. try: wild="*a", string="xyzab"   correction: string = cp++; should be changed to: string = cp; if(*cp) cp++;
GeneralRe: great code, but ...memberJack Handy13 Mar '03 - 20:34 
I can not reproduce this bug.   wildcmp("*a", "xyzab") returns 0 as it should.   Can you elaborate on what you are doing to cause it to function incorrectly?   Thanks,   Jack
GeneralRe: great code, but ...sussAnonymous15 Mar '03 - 20:08 
You're right, it is not a bug in functionality, but when I debugged it I saw that for the above input, the line: string = cp++; causes cp to point one place beyond the string array bounds. (When string points to the last char 'b', cp points to \0. On the next iteration, string will be...
GeneralRe: great code, but ...memberJack Handy18 Mar '03 - 13:25 
Thanks, I'll have to look into this once I get some free time.   -Jack   There are 10 types of people in this world, those that understand binary and those who don't.
GeneralRe: great code, but ...sussAnonymous19 Mar '03 - 23:13 
Actually, it was brought to my attention that it is probably legal to do that in C, since the pointer is not used afterwards. So, maybe it's just a matter of coding practice.
GeneralNicememberChris Richardson16 Jan '03 - 10:58 
I saw this one a long time ago, and finally have a use for it. Thank you very much.   Chris Richardson   Programmers find all sorts of ingenious ways to screw ourselves over. - Tim Smith
GeneralRe: NicememberJack Handy20 Jan '03 - 11:56 
No problem. I hope it serves you well.   -Jack   There are 10 types of people in this world, those that understand binary and those who don't.
GeneralCool CodesussAnonymous6 Dec '02 - 11:09 
I converted this into C# and bingo...   I tried to break it but couldn't
GeneralRe: Cool Codememberhector santos1 Mar '03 - 1:53 
You didn't try hard enough:   wildcmp(NULL,whatever)   will break it.   Hector Santos, CTO Santronics Software, Inc. http:/www.santronics.com

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

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