|
This is realy nice & and useful code. I used to write something similar, but your example is simplier and shorter.
Because it lacks comments, I spent some time to understand (before I saw comment form Targys - real tutorial ) and it is clear now. Thanks to both of you!
To 'wise' guys, flamers, and other people who has nothing to do instead of arguing:
- If the code has a bug, report but don't pretend you are a genius or a guru. If you can do it better, submit an article.
If you don't like the code, don't use it!
And about NULL pointers:
Idiot-proofing should be implemented at the level where data (function arguments) is acquired and prepared, not in such low-level function.
Besides that, I tested several functions from string.h with NULL parameters and every single one threw an exception. No further comments...
Regards, Voja
|
|
|
|
|
Great piece of code, but I have one minor improvement. It appears to me that the variable "cp" doesn't do anything and servers no purpose.
If I'm correct, then you can safely remove the line:
cp = string+1;
and also remove:
string = cp;
and replace:
cp = string++;
with:
++string;
I'm believe the results would be identical.
|
|
|
|
|
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 a test string of "testing" with the above pattern the match still failed (correctly) using both algorithms. But, there easily may be a pattern and string combination that wouldn't work without cp.
|
|
|
|
|
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 this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen
|
|
|
|
|
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.<br />
Compiled as a console app using vc6 in release mode with /O2 optimization.<br />
Ran on a pM 1.7ghz<br />
<br />
"C:\\T*s*.t?t", "C:\\Test\\File.txt"<br />
PathMatchSpec MATCH => 20.5090s<br />
wildcmp MATCH => 1.0320s<br />
<br />
"C:\\T*s*.t??t", "C:\\Test\\File.txt"<br />
PathMatchSpec NO MATCH => 45.7760s<br />
wildcmp NO MATCH => 0.8910s
There are 10 types of people in this world, those that understand binary and those who don't.
|
|
|
|
|
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
|
|
|
|
|
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 very useful for checking hostmasks for example.
If you think my function is producing bad results, can you paste an example of the string along with the wildcard?
Thanks,
Jack
There are 10 types of people in this world, those that understand binary and those who don't.
|
|
|
|
|
I want case insenstive wildcmp function. Could anyone help me?
|
|
|
|
|
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
|
|
|
|
|
toupper doesnt support char characters. You could optimize this code.
This code compares also multilingual characters
<br />
#include <stdio.h><br />
<br />
#define BIT5 0x20<br />
<br />
char buf[] = "this is ®Ñê test";<br />
char *pbuf;<br />
<br />
int lower(int ch);<br />
<br />
<br />
int lower(int ch)<br />
{<br />
if ((ch==64)||(ch==91)||(ch==92)||(ch==93))<br />
return ch &= ~(ch & BIT5);<br />
return ch |= BIT5;<br />
}<br />
<br />
int wildcmp(char *wild, char *string) {<br />
char *cp, *mp;<br />
<br />
while ((*string) && (*wild != '*')) {<br />
if ((*wild != *string) && (*wild != '?')) {<br />
return 0;<br />
}<br />
wild++;<br />
string++;<br />
}<br />
<br />
while (*string) {<br />
if (*wild == '*') {<br />
if (!*++wild) {<br />
return 1;<br />
}<br />
mp = wild;<br />
cp = string+1;<br />
} else if ((*wild == *string) || (*wild == '?')) {<br />
wild++;<br />
string++;<br />
} else {<br />
wild = mp;<br />
string = cp++;<br />
}<br />
}<br />
<br />
while (*wild == '*') {<br />
wild++;<br />
}<br />
return !*wild;<br />
}<br />
<br />
<br />
int main() {<br />
<br />
for (pbuf = &buf[0]; *pbuf; ++pbuf)<br />
*pbuf= lower (*pbuf); <br />
<br />
if (wildcmp("*®ñê*", buf)){<br />
printf ("match : %s \n", buf);<br />
} else {<br />
printf ("not match: %s \n",buf);<br />
}<br />
}<br />
<br />
<br />
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
Doesnt seems to work for me.
|
|
|
|
|
Very nice, compact, works great and is very fast. Thanks, Jack!
Best wishes,
Hans
|
|
|
|
|
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 null, that would be fine. I just don't think that this is a 'bug' (if you can even call it that) worth flaming an otherwise great function.
-Jack
There are 10 types of people in this world, those that understand binary and those who don't.
|
|
|
|
|
I agree. This is not what I meant.
Efrat
|
|
|
|
|
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.
|
|
|
|
|
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++;
|
|
|
|
|
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
|
|
|
|
|
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 advanced to point to \0,
and cp will be advanced to point to one place after the \0).
Although it is not critical,I thought it is worth fixing.
regards,
Efrat
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|