diff --git a/slre.c b/slre.c index 4a7fd89..ec953ae 100644 --- a/slre.c +++ b/slre.c @@ -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; } diff --git a/unit_test.c b/unit_test.c index edc6a0d..a9df4b4 100644 --- a/unit_test.c +++ b/unit_test.c @@ -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);