Fixed bracketed capture

This commit is contained in:
Sergey Lyubka 2014-02-24 11:21:55 +00:00
parent e9ce51b28f
commit 5262ccd677
2 changed files with 18 additions and 3 deletions

3
slre.c
View File

@ -258,6 +258,7 @@ static int bar(const char *re, int re_len, const char *s, int s_len,
FAIL_IF(n <= 0, SLRE_NO_MATCH);
j += n;
} else if (re[i] == '(') {
n = SLRE_NO_MATCH;
bi++;
FAIL_IF(bi >= info->num_brackets, SLRE_INTERNAL_ERROR);
DBG(("CAPTURING [%.*s] [%.*s] [%s]\n",
@ -268,7 +269,7 @@ static int bar(const char *re, int re_len, const char *s, int s_len,
n = doh(s + j, s_len - j, info, bi);
} else {
int j2;
for (j2 = 0; j2 < s_len - j; j2++) {
for (j2 = 0; j2 <= s_len - j; j2++) {
if ((n = doh(s + j, s_len - (j + j2), info, bi)) >= 0 &&
bar(re + i + step, re_len - (i + step),
s + j + n, s_len - (j + n), info, bi) >= 0) break;

View File

@ -46,7 +46,6 @@ static char *slre_replace(const char *regex, const char *buf,
}
int main(void) {
const char *msg = "";
struct slre_cap caps[10];
/* Metacharacters */
@ -62,6 +61,22 @@ int main(void) {
ASSERT(slre_match("\\x1", "a", 1, NULL, 0) == SLRE_INVALID_METACHARACTER);
ASSERT(slre_match("\\x20", " ", 1, NULL, 0) == 1);
ASSERT(slre_match("^.+$", "", 0, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("^(.+)$", "", 0, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("(?i)^([\\+\\-]?)([\\d]+)$", "+", 1,
caps, 10) == SLRE_NO_MATCH);
ASSERT(slre_match("(?i)^([\\+\\-]?)([\\d]+)$", "+27", 3,
caps, 10) == 3);
ASSERT(caps[0].len == 1);
ASSERT(caps[0].ptr[0] == '+');
ASSERT(caps[1].len == 2);
ASSERT(memcmp(caps[1].ptr, "27", 2) == 0);
ASSERT(slre_match("tel:\\+(\\d+[\\d-]+\\d)",
"tel:+1-201-555-0123;a=b", 23, caps, 10) == 19);
ASSERT(caps[0].len == 14);
ASSERT(memcmp(caps[0].ptr, "1-201-555-0123", 14) == 0);
/* Character sets */
ASSERT(slre_match("[abc]", "1c2", 3, NULL, 0) == 2);
ASSERT(slre_match("[abc]", "1C2", 3, NULL, 0) == SLRE_NO_MATCH);
@ -191,7 +206,6 @@ int main(void) {
ASSERT(slre_match("a*$", "aa", 2, NULL, 0) == 2);
ASSERT(slre_match( "a+$" ,"Xaa", 3, NULL, 0) == 3);
ASSERT(slre_match( "a*$" ,"Xaa", 3, NULL, 0) == 3);
ASSERT(msg[0] == '\0');
{
/* Example: HTTP request */