M
Malcolm McLean
On Oct 21, 2:03 pm, Malcolm McLean <[email protected]>
wrote:
Hopefully everyone will be happy with this version.
#include <string.h>
static int chmatch(const char *target, const char *pat);
/*
wildcard matcher.
Params: str - the target string
pattern - pattern to match
Returns: 1 if match, 0 if not.
Notes: ? - match any character
* - match zero or more characters
[?], [*], escapes,
[abc], match a, b or c.
[A-Z] [0-9] [*-x], match range.
[[] - match '['.
[][abc] match ], [, a, b or c
*/
int matchwild(const char *str, const char *pattern)
{
const char *target = str;
const char *pat = pattern;
int gobble;
while( (gobble = chmatch(target, pat)) )
{
target++;
pat += gobble;
}
if(*target == 0 && *pat == 0)
return 1;
else if(*pat == '*')
{
while(pat[1] == '*')
pat++;
if(pat[1] == 0)
return 1;
while(*target)
if(matchwild(target++, pat+1))
return 1;
}
return 0;
}
/*
match a character.
Parmas: target - target string
pat - pattern string.
Returns: number of pat character matched.
Notes: means that a * in pat will return zero
*/
static int chmatch(const char *target, const char *pat)
{
char *end, *ptr;
if(*pat == '[' && (end = strchr(pat, ']')) )
{
/* treat close bracket following open bracket as character */
if(end == pat + 1)
{
end = strchr(pat+2, ']');
/* make "[]" with no close mismatch all */
if(end == 0)
return 0;
}
/* allow [A-Z] and like syntax */
if(end - pat == 4 && pat[2] == '-' && pat[1] <= pat[3])
if(*target >= pat[1] && *target <= pat[3])
return 5;
else
return 0;
/* search for character list contained within brackets */
ptr = strchr(pat+1, *target);
if(ptr != 0 && ptr < end)
return end - pat + 1;
else
return 0;
}
if(*pat == '?' && *target != 0)
return 1;
if(*pat == '*')
return 0;
if(*target == 0 || *pat == 0)
return 0;
if(*target == *pat)
return 1;
return 0;
}
wrote:
Hopefully everyone will be happy with this version.
#include <string.h>
static int chmatch(const char *target, const char *pat);
/*
wildcard matcher.
Params: str - the target string
pattern - pattern to match
Returns: 1 if match, 0 if not.
Notes: ? - match any character
* - match zero or more characters
[?], [*], escapes,
[abc], match a, b or c.
[A-Z] [0-9] [*-x], match range.
[[] - match '['.
[][abc] match ], [, a, b or c
*/
int matchwild(const char *str, const char *pattern)
{
const char *target = str;
const char *pat = pattern;
int gobble;
while( (gobble = chmatch(target, pat)) )
{
target++;
pat += gobble;
}
if(*target == 0 && *pat == 0)
return 1;
else if(*pat == '*')
{
while(pat[1] == '*')
pat++;
if(pat[1] == 0)
return 1;
while(*target)
if(matchwild(target++, pat+1))
return 1;
}
return 0;
}
/*
match a character.
Parmas: target - target string
pat - pattern string.
Returns: number of pat character matched.
Notes: means that a * in pat will return zero
*/
static int chmatch(const char *target, const char *pat)
{
char *end, *ptr;
if(*pat == '[' && (end = strchr(pat, ']')) )
{
/* treat close bracket following open bracket as character */
if(end == pat + 1)
{
end = strchr(pat+2, ']');
/* make "[]" with no close mismatch all */
if(end == 0)
return 0;
}
/* allow [A-Z] and like syntax */
if(end - pat == 4 && pat[2] == '-' && pat[1] <= pat[3])
if(*target >= pat[1] && *target <= pat[3])
return 5;
else
return 0;
/* search for character list contained within brackets */
ptr = strchr(pat+1, *target);
if(ptr != 0 && ptr < end)
return end - pat + 1;
else
return 0;
}
if(*pat == '?' && *target != 0)
return 1;
if(*pat == '*')
return 0;
if(*target == 0 || *pat == 0)
return 0;
if(*target == *pat)
return 1;
return 0;
}