|
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.
|
|
|
|
|
I converted this into C# and bingo...
I tried to break it but couldn't
|
|
|
|
|
You didn't try hard enough:
wildcmp(NULL,whatever)
will break it.
Hector Santos, CTO
Santronics Software, Inc.
http:/www.santronics.com
|
|
|
|
|
The C# code would NOT break, instead the framework would throw an exception in this case. With appropriate exception handling routines, pointer checking in C# is useless overhead.
Cheers anyway,
K. C. Dorner
IBM Billing Solution
|
|
|
|
|
Exception handling in the dotnet framework is slow like hell
we will see
|
|
|
|
|
Where can I get the C# version?
- Bruce
BRCKCC
|
|
|
|
|
Hi !
Wery useful function, save at least a one sigarette lifetime
Seriously - great code.
Stanislav.
|
|
|
|
|
Yeah...good stuff. How long did it take you?
|
|
|
|
|
Could anyone explain how this code works for me? I am having trouble trying to figure out what is going on in a couple places. I would think a short explination would help out some other people like me who don't know C. Thanks in advance!
|
|
|
|