Implement matchregexp() using fnmatch()
This commit is contained in:
parent
2db8cc269f
commit
d045a86c5c
91
src/regex.c
91
src/regex.c
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
|
||||
* Copyright (C) 2021 Kim Woelders
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
|
@ -22,97 +23,13 @@
|
|||
*/
|
||||
#include "E.h"
|
||||
|
||||
static int
|
||||
isafter(int p, const char *s1, const char *s2)
|
||||
{
|
||||
int i, j;
|
||||
int len, len2;
|
||||
int match;
|
||||
|
||||
len = strlen(s1);
|
||||
len2 = strlen(s2);
|
||||
|
||||
match = 0;
|
||||
for (i = p; i < len; i++)
|
||||
{
|
||||
if (s1[i] == s2[0])
|
||||
{
|
||||
match = 1;
|
||||
for (j = 0; j < len2; j++)
|
||||
{
|
||||
if ((i + j) >= len)
|
||||
return -1;
|
||||
if (s1[i + j] != s2[j])
|
||||
match = 0;
|
||||
}
|
||||
}
|
||||
if (match)
|
||||
return i + len2;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
#include <fnmatch.h>
|
||||
|
||||
int
|
||||
matchregexp(const char *rx, const char *s)
|
||||
{
|
||||
int i, l, m;
|
||||
int len, lenr;
|
||||
int match;
|
||||
char rx2[1024];
|
||||
|
||||
if (!s)
|
||||
return 0;
|
||||
if (!rx)
|
||||
if (!rx || !s)
|
||||
return 0;
|
||||
|
||||
len = strlen(s);
|
||||
l = 0;
|
||||
lenr = 0;
|
||||
match = 1;
|
||||
if ((strcmp(rx, "*") || rx[0] == 0) && s[0] == 0)
|
||||
return 0;
|
||||
|
||||
if (rx[0] != '*')
|
||||
{
|
||||
m = 0;
|
||||
while ((rx[l] != '*') && (rx[l]) && (m < 1023))
|
||||
rx2[m++] = rx[l++];
|
||||
rx2[m] = 0;
|
||||
lenr = strlen(rx2);
|
||||
if (lenr > len)
|
||||
return 0;
|
||||
for (i = 0; i < lenr; i++)
|
||||
{
|
||||
if (s[i] != rx[i])
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if ((!rx[l]) && (s[lenr]))
|
||||
return 0;
|
||||
for (i = lenr; i < len;)
|
||||
{
|
||||
if (rx[l])
|
||||
l++;
|
||||
if (rx[l])
|
||||
{
|
||||
m = 0;
|
||||
while ((rx[l] != '*') && (rx[l]) && (m < 1023))
|
||||
rx2[m++] = rx[l++];
|
||||
rx2[m] = 0;
|
||||
i = isafter(i, s, rx2);
|
||||
if (i < 0)
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We arrived at the end of the regex BUT if it doesn't
|
||||
// end with the wildcard * and there are more chars
|
||||
// in s remaining to be matched, we should return 0
|
||||
if ((i < len) && (rx[l - 1] != '*'))
|
||||
return 0;
|
||||
else
|
||||
return match;
|
||||
}
|
||||
}
|
||||
return match;
|
||||
return !fnmatch(rx, s, 0);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue