regex in filenames

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;
}
 
B

Ben Bacarisse

Malcolm McLean said:
tcc - tiny C compiler. It's a free and very nice minimal compiler
which I use because there are few installation hassles or Microsoft
death threats for using the compiler for open source projects. But
it's a one man project.

I had a look at the source for tcc and the parser is seriously broken.
Basically, all the assignment operators are parsed with the highest
possible binding after unary operators. I though I might be able to
suggest a patch but the parser also generates the code on the fly and
the whole thing is something of a mess.
 
K

Kenny McCormack

tcc - tiny C compiler. It's a free and very nice minimal compiler
which I use because there are few installation hassles or Microsoft
death threats for using the compiler for open source projects. But
it's a one man project.

Yes, but what a man! I'll take one Bellard over a roomful of Microsoft
programmers any day of the week.

--
"The anti-regulation business ethos is based on the charmingly naive notion
that people will not do unspeakable things for money." - Dana Carpender

Quoted by Paul Ciszek (pciszek at panix dot com). But what I want to know
is why is this diet/low-carb food author doing making pithy political/economic
statements?

Nevertheless, the above quote is dead-on, because, the thing is - business
in one breath tells us they don't need to be regulated (which is to say:
that they can morally self-regulate), then in the next breath tells us that
corporations are amoral entities which have no obligations to anyone except
their officers and shareholders, then in the next breath they tell us they
don't need to be regulated (that they can morally self-regulate) ...
 
B

Ben Bacarisse

Malcolm McLean said:
tcc - tiny C compiler. It's a free and very nice minimal compiler
which I use because there are few installation hassles or Microsoft
death threats for using the compiler for open source projects. But
it's a one man project.

I've offered a patch for this parsing bug to the tinycc-devel mailing
list. It seems to work, but compiler patches need a lot more testing
than I've had time to do. I can mail it to you if you'd like to have
assignment expressions parsed with the correct binding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top