mirror of
https://github.com/cesanta/slre.git
synced 2024-11-24 03:15:30 +00:00
Fixed bracketed capture
This commit is contained in:
parent
e9ce51b28f
commit
5262ccd677
3
slre.c
3
slre.c
@ -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;
|
||||
|
18
unit_test.c
18
unit_test.c
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user