This commit is contained in:
Sergey Lyubka 2015-05-19 14:30:48 +01:00
parent af4f5e563c
commit deb7c0dddf
2 changed files with 16 additions and 2 deletions

8
slre.c
View File

@ -231,7 +231,11 @@ static int bar(const char *re, int re_len, const char *s, int s_len,
if (nj > j && non_greedy) break;
} while (n1 > 0);
if (n1 < 0 && re[i + step] == '*' &&
/*
* Even if we found one or more pattern, this branch will be executed,
* changing the next captures.
*/
if (n1 < 0 && n1 < 0 && re[i + step] == '*' &&
(n2 = bar(re + ni, re_len - ni, s + j, s_len - j, info, bi)) > 0) {
nj = j + n2;
}
@ -275,7 +279,7 @@ static int bar(const char *re, int re_len, const char *s, int s_len,
DBG(("CAPTURED [%.*s] [%.*s]:%d\n", step, re + i, s_len - j, s + j, n));
FAIL_IF(n < 0, n);
if (info->caps != NULL) {
if (info->caps != NULL && n > 0) {
info->caps[bi - 1].ptr = s + j;
info->caps[bi - 1].len = n;
}

View File

@ -258,6 +258,16 @@ int main(void) {
}
}
{
/* Example more complex regular expression */
static const char * str = "aa 1234 xy\nz";
static const char * regex = "aa ([0-9]*) *([x-z]*)";
struct slre_cap caps[2];
ASSERT(slre_match(regex, str, strlen(str), caps, 2, 0) > 0);
ASSERT(caps[0].len == 4);
ASSERT(caps[1].len == 2); /* Fails here */
}
printf("Unit test %s (total test: %d, failed tests: %d)\n",
static_failed_tests > 0 ? "FAILED" : "PASSED",
static_total_tests, static_failed_tests);