mirror of
https://github.com/cesanta/slre.git
synced 2024-11-24 11:25:30 +00:00
Flags setting moved to the API
This commit is contained in:
parent
dfc6c21b5b
commit
1c59641398
19
slre.c
19
slre.c
@ -66,10 +66,9 @@ struct regex_info {
|
||||
struct slre_cap *caps;
|
||||
int num_caps;
|
||||
|
||||
/* E.g. IGNORE_CASE. See enum below */
|
||||
/* E.g. SLRE_IGNORE_CASE. See enum below */
|
||||
int flags;
|
||||
};
|
||||
enum { IGNORE_CASE = 1 };
|
||||
|
||||
static int is_metacharacter(const unsigned char *s) {
|
||||
static const char *metacharacters = "^$().[]*+?|\\Ssd";
|
||||
@ -147,7 +146,7 @@ static int match_op(const unsigned char *re, const unsigned char *s,
|
||||
case '.': result++; break;
|
||||
|
||||
default:
|
||||
if (info->flags & IGNORE_CASE) {
|
||||
if (info->flags & SLRE_IGNORE_CASE) {
|
||||
FAIL_IF(tolower(*re) != tolower(*s), SLRE_NO_MATCH);
|
||||
} else {
|
||||
FAIL_IF(*re != *s, SLRE_NO_MATCH);
|
||||
@ -169,7 +168,7 @@ static int match_set(const char *re, int re_len, const char *s,
|
||||
/* Support character range */
|
||||
if (re[len] != '-' && re[len + 1] == '-' && re[len + 2] != ']' &&
|
||||
re[len + 2] != '\0') {
|
||||
result = info->flags && IGNORE_CASE ?
|
||||
result = info->flags && SLRE_IGNORE_CASE ?
|
||||
*s >= re[len] && *s <= re[len + 2] :
|
||||
tolower(*s) >= tolower(re[len]) && tolower(*s) <= tolower(re[len + 2]);
|
||||
len += 3;
|
||||
@ -422,21 +421,15 @@ static int foo(const char *re, int re_len, const char *s, int s_len,
|
||||
}
|
||||
|
||||
int slre_match(const char *regexp, const char *s, int s_len,
|
||||
struct slre_cap *caps, int num_caps) {
|
||||
struct slre_cap *caps, int num_caps, int flags) {
|
||||
struct regex_info info;
|
||||
|
||||
/* Initialize info structure */
|
||||
info.flags = info.num_brackets = info.num_branches = 0;
|
||||
info.flags = flags;
|
||||
info.num_brackets = info.num_branches = 0;
|
||||
info.num_caps = num_caps;
|
||||
info.caps = caps;
|
||||
|
||||
DBG(("========================> [%s] [%.*s]\n", regexp, s_len, s));
|
||||
|
||||
/* Handle regexp flags. At the moment, only 'i' is supported */
|
||||
if (memcmp(regexp, "(?i)", 4) == 0) {
|
||||
info.flags |= IGNORE_CASE;
|
||||
regexp += 4;
|
||||
}
|
||||
|
||||
return foo(regexp, strlen(regexp), s, s_len, &info);
|
||||
}
|
||||
|
7
slre.h
7
slre.h
@ -34,8 +34,13 @@ struct slre_cap {
|
||||
int len;
|
||||
};
|
||||
|
||||
|
||||
int slre_match(const char *regexp, const char *buf, int buf_len,
|
||||
struct slre_cap *caps, int num_caps);
|
||||
struct slre_cap *caps, int num_caps, int flags);
|
||||
|
||||
/* Possible flags for slre_match() */
|
||||
enum { SLRE_IGNORE_CASE = 1 };
|
||||
|
||||
|
||||
/* slre_match() failure codes */
|
||||
#define SLRE_NO_MATCH -1
|
||||
|
251
unit_test.c
251
unit_test.c
@ -26,7 +26,7 @@ static char *slre_replace(const char *regex, const char *buf,
|
||||
|
||||
do {
|
||||
s_len = s == NULL ? 0 : strlen(s);
|
||||
if ((n = slre_match(regex, buf, len, &cap, 1)) > 0) {
|
||||
if ((n = slre_match(regex, buf, len, &cap, 1, 0)) > 0) {
|
||||
n1 = cap.ptr - buf, n2 = strlen(sub),
|
||||
n3 = &buf[n] - &cap.ptr[cap.len];
|
||||
} else {
|
||||
@ -49,163 +49,166 @@ int main(void) {
|
||||
struct slre_cap caps[10];
|
||||
|
||||
/* Metacharacters */
|
||||
ASSERT(slre_match("$", "abcd", 4, NULL, 0) == 4);
|
||||
ASSERT(slre_match("^", "abcd", 4, NULL, 0) == 0);
|
||||
ASSERT(slre_match("x|^", "abcd", 4, NULL, 0) == 0);
|
||||
ASSERT(slre_match("x|$", "abcd", 4, NULL, 0) == 4);
|
||||
ASSERT(slre_match("x", "abcd", 4, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match(".", "abcd", 4, NULL, 0) == 1);
|
||||
ASSERT(slre_match("(?i)^.*\\\\.*$", "c:\\Tools", 8, NULL, 0) == 8);
|
||||
ASSERT(slre_match("\\", "a", 1, NULL, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("\\x", "a", 1, NULL, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("\\x1", "a", 1, NULL, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("\\x20", " ", 1, NULL, 0) == 1);
|
||||
ASSERT(slre_match("$", "abcd", 4, NULL, 0, 0) == 4);
|
||||
ASSERT(slre_match("^", "abcd", 4, NULL, 0, 0) == 0);
|
||||
ASSERT(slre_match("x|^", "abcd", 4, NULL, 0, 0) == 0);
|
||||
ASSERT(slre_match("x|$", "abcd", 4, NULL, 0, 0) == 4);
|
||||
ASSERT(slre_match("x", "abcd", 4, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match(".", "abcd", 4, NULL, 0, 0) == 1);
|
||||
ASSERT(slre_match("^.*\\\\.*$", "c:\\Tools", 8, NULL, 0, SLRE_IGNORE_CASE)
|
||||
== 8);
|
||||
ASSERT(slre_match("\\", "a", 1, NULL, 0, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("\\x", "a", 1, NULL, 0, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("\\x1", "a", 1, NULL, 0, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("\\x20", " ", 1, NULL, 0, 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(slre_match("^.+$", "", 0, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("^(.+)$", "", 0, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("^([\\+-]?)([\\d]+)$", "+", 1,
|
||||
caps, 10, SLRE_IGNORE_CASE) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("^([\\+-]?)([\\d]+)$", "+27", 3,
|
||||
caps, 10, SLRE_IGNORE_CASE) == 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);
|
||||
"tel:+1-201-555-0123;a=b", 23, caps, 10, 0) == 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);
|
||||
ASSERT(slre_match("(?i)[abc]", "1C2", 3, NULL, 0) == 2);
|
||||
ASSERT(slre_match("[.2]", "1C2", 3, NULL, 0) == 1);
|
||||
ASSERT(slre_match("[\\S]+", "ab cd", 5, NULL, 0) == 2);
|
||||
ASSERT(slre_match("[\\S]+\\s+[tyc]*", "ab cd", 5, NULL, 0) == 4);
|
||||
ASSERT(slre_match("[\\d]", "ab cd", 5, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("[^\\d]", "ab cd", 5, NULL, 0) == 1);
|
||||
ASSERT(slre_match("[^\\d]+", "abc123", 6, NULL, 0) == 3);
|
||||
ASSERT(slre_match("[1-5]+", "123456789", 9, NULL, 0) == 5);
|
||||
ASSERT(slre_match("[1-5a-c]+", "123abcdef", 9, NULL, 0) == 6);
|
||||
ASSERT(slre_match("[1-5a-]+", "123abcdef", 9, NULL, 0) == 4);
|
||||
ASSERT(slre_match("[1-5a-]+", "123a--2oo", 9, NULL, 0) == 7);
|
||||
ASSERT(slre_match("[htps]+://", "https://", 8, NULL, 0) == 8);
|
||||
ASSERT(slre_match("[^\\s]+", "abc def", 7, NULL, 0) == 3);
|
||||
ASSERT(slre_match("[^fc]+", "abc def", 7, NULL, 0) == 2);
|
||||
ASSERT(slre_match("[^d\\sf]+", "abc def", 7, NULL, 0) == 3);
|
||||
ASSERT(slre_match("[abc]", "1c2", 3, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("[abc]", "1C2", 3, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("[abc]", "1C2", 3, NULL, 0, SLRE_IGNORE_CASE) == 2);
|
||||
ASSERT(slre_match("[.2]", "1C2", 3, NULL, 0, 0) == 1);
|
||||
ASSERT(slre_match("[\\S]+", "ab cd", 5, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("[\\S]+\\s+[tyc]*", "ab cd", 5, NULL, 0, 0) == 4);
|
||||
ASSERT(slre_match("[\\d]", "ab cd", 5, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("[^\\d]", "ab cd", 5, NULL, 0, 0) == 1);
|
||||
ASSERT(slre_match("[^\\d]+", "abc123", 6, NULL, 0, 0) == 3);
|
||||
ASSERT(slre_match("[1-5]+", "123456789", 9, NULL, 0, 0) == 5);
|
||||
ASSERT(slre_match("[1-5a-c]+", "123abcdef", 9, NULL, 0, 0) == 6);
|
||||
ASSERT(slre_match("[1-5a-]+", "123abcdef", 9, NULL, 0, 0) == 4);
|
||||
ASSERT(slre_match("[1-5a-]+", "123a--2oo", 9, NULL, 0, 0) == 7);
|
||||
ASSERT(slre_match("[htps]+://", "https://", 8, NULL, 0, 0) == 8);
|
||||
ASSERT(slre_match("[^\\s]+", "abc def", 7, NULL, 0, 0) == 3);
|
||||
ASSERT(slre_match("[^fc]+", "abc def", 7, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("[^d\\sf]+", "abc def", 7, NULL, 0, 0) == 3);
|
||||
|
||||
/* Flags - case sensitivity */
|
||||
ASSERT(slre_match("FO", "foo", 3, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("(?i)FO", "foo", 3, NULL, 0) == 2);
|
||||
ASSERT(slre_match("(?m)FO", "foo", 3, NULL, 0) == SLRE_UNEXPECTED_QUANTIFIER);
|
||||
ASSERT(slre_match("(?m)x", "foo", 3, NULL, 0) == SLRE_UNEXPECTED_QUANTIFIER);
|
||||
ASSERT(slre_match("FO", "foo", 3, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("FO", "foo", 3, NULL, 0, SLRE_IGNORE_CASE) == 2);
|
||||
ASSERT(slre_match("(?m)FO", "foo", 3, NULL, 0, 0) ==
|
||||
SLRE_UNEXPECTED_QUANTIFIER);
|
||||
ASSERT(slre_match("(?m)x", "foo", 3, NULL, 0, 0) ==
|
||||
SLRE_UNEXPECTED_QUANTIFIER);
|
||||
|
||||
ASSERT(slre_match("fo", "foo", 3, NULL, 0) == 2);
|
||||
ASSERT(slre_match(".+", "foo", 3, NULL, 0) == 3);
|
||||
ASSERT(slre_match(".+k", "fooklmn", 7, NULL, 0) == 4);
|
||||
ASSERT(slre_match(".+k.", "fooklmn", 7, NULL, 0) == 5);
|
||||
ASSERT(slre_match("p+", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("ok", "fooklmn", 7, NULL, 0) == 4);
|
||||
ASSERT(slre_match("lmno", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("mn.", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("o", "fooklmn", 7, NULL, 0) == 2);
|
||||
ASSERT(slre_match("^o", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("^", "fooklmn", 7, NULL, 0) == 0);
|
||||
ASSERT(slre_match("n$", "fooklmn", 7, NULL, 0) == 7);
|
||||
ASSERT(slre_match("n$k", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("l$", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match(".$", "fooklmn", 7, NULL, 0) == 7);
|
||||
ASSERT(slre_match("a?", "fooklmn", 7, NULL, 0) == 0);
|
||||
ASSERT(slre_match("^a*CONTROL", "CONTROL", 7, NULL, 0) == 7);
|
||||
ASSERT(slre_match("^[a]*CONTROL", "CONTROL", 7, NULL, 0) == 7);
|
||||
ASSERT(slre_match("^(a*)CONTROL", "CONTROL", 7, NULL, 0) == 7);
|
||||
ASSERT(slre_match("^(a*)?CONTROL", "CONTROL", 7, NULL, 0) == 7);
|
||||
ASSERT(slre_match("fo", "foo", 3, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match(".+", "foo", 3, NULL, 0, 0) == 3);
|
||||
ASSERT(slre_match(".+k", "fooklmn", 7, NULL, 0, 0) == 4);
|
||||
ASSERT(slre_match(".+k.", "fooklmn", 7, NULL, 0, 0) == 5);
|
||||
ASSERT(slre_match("p+", "fooklmn", 7, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("ok", "fooklmn", 7, NULL, 0, 0) == 4);
|
||||
ASSERT(slre_match("lmno", "fooklmn", 7, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("mn.", "fooklmn", 7, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("o", "fooklmn", 7, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("^o", "fooklmn", 7, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("^", "fooklmn", 7, NULL, 0, 0) == 0);
|
||||
ASSERT(slre_match("n$", "fooklmn", 7, NULL, 0, 0) == 7);
|
||||
ASSERT(slre_match("n$k", "fooklmn", 7, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("l$", "fooklmn", 7, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match(".$", "fooklmn", 7, NULL, 0, 0) == 7);
|
||||
ASSERT(slre_match("a?", "fooklmn", 7, NULL, 0, 0) == 0);
|
||||
ASSERT(slre_match("^a*CONTROL", "CONTROL", 7, NULL, 0, 0) == 7);
|
||||
ASSERT(slre_match("^[a]*CONTROL", "CONTROL", 7, NULL, 0, 0) == 7);
|
||||
ASSERT(slre_match("^(a*)CONTROL", "CONTROL", 7, NULL, 0, 0) == 7);
|
||||
ASSERT(slre_match("^(a*)?CONTROL", "CONTROL", 7, NULL, 0, 0) == 7);
|
||||
|
||||
ASSERT(slre_match("\\_", "abc", 3, NULL, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("+", "fooklmn", 7, NULL, 0) == SLRE_UNEXPECTED_QUANTIFIER);
|
||||
ASSERT(slre_match("()+", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("\\x", "12", 2, NULL, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("\\xhi", "12", 2, NULL, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("\\x20", "_ J", 3, NULL, 0) == 2);
|
||||
ASSERT(slre_match("\\x4A", "_ J", 3, NULL, 0) == 3);
|
||||
ASSERT(slre_match("\\d+", "abc123def", 9, NULL, 0) == 6);
|
||||
ASSERT(slre_match("\\_", "abc", 3, NULL, 0, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("+", "fooklmn", 7, NULL, 0, 0) == SLRE_UNEXPECTED_QUANTIFIER);
|
||||
ASSERT(slre_match("()+", "fooklmn", 7, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("\\x", "12", 2, NULL, 0, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("\\xhi", "12", 2, NULL, 0, 0) == SLRE_INVALID_METACHARACTER);
|
||||
ASSERT(slre_match("\\x20", "_ J", 3, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("\\x4A", "_ J", 3, NULL, 0, 0) == 3);
|
||||
ASSERT(slre_match("\\d+", "abc123def", 9, NULL, 0, 0) == 6);
|
||||
|
||||
/* Balancing brackets */
|
||||
ASSERT(slre_match("(x))", "fooklmn", 7, NULL, 0) == SLRE_UNBALANCED_BRACKETS);
|
||||
ASSERT(slre_match("(", "fooklmn", 7, NULL, 0) == SLRE_UNBALANCED_BRACKETS);
|
||||
ASSERT(slre_match("(x))", "fooklmn", 7, NULL, 0, 0) == SLRE_UNBALANCED_BRACKETS);
|
||||
ASSERT(slre_match("(", "fooklmn", 7, NULL, 0, 0) == SLRE_UNBALANCED_BRACKETS);
|
||||
|
||||
ASSERT(slre_match("klz?mn", "fooklmn", 7, NULL, 0) == 7);
|
||||
ASSERT(slre_match("fa?b", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("klz?mn", "fooklmn", 7, NULL, 0, 0) == 7);
|
||||
ASSERT(slre_match("fa?b", "fooklmn", 7, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
|
||||
/* Brackets & capturing */
|
||||
ASSERT(slre_match("^(te)", "tenacity subdues all", 20, caps, 10) == 2);
|
||||
ASSERT(slre_match("(bc)", "abcdef", 6, caps, 10) == 3);
|
||||
ASSERT(slre_match(".(d.)", "abcdef", 6, caps, 10) == 5);
|
||||
ASSERT(slre_match(".(d.)\\)?", "abcdef", 6, caps, 10) == 5);
|
||||
ASSERT(slre_match("^(te)", "tenacity subdues all", 20, caps, 10, 0) == 2);
|
||||
ASSERT(slre_match("(bc)", "abcdef", 6, caps, 10, 0) == 3);
|
||||
ASSERT(slre_match(".(d.)", "abcdef", 6, caps, 10, 0) == 5);
|
||||
ASSERT(slre_match(".(d.)\\)?", "abcdef", 6, caps, 10, 0) == 5);
|
||||
ASSERT(caps[0].len == 2);
|
||||
ASSERT(memcmp(caps[0].ptr, "de", 2) == 0);
|
||||
ASSERT(slre_match("(.+)", "123", 3, caps, 10) == 3);
|
||||
ASSERT(slre_match("(2.+)", "123", 3, caps, 10) == 3);
|
||||
ASSERT(slre_match("(.+)", "123", 3, caps, 10, 0) == 3);
|
||||
ASSERT(slre_match("(2.+)", "123", 3, caps, 10, 0) == 3);
|
||||
ASSERT(caps[0].len == 2);
|
||||
ASSERT(memcmp(caps[0].ptr, "23", 2) == 0);
|
||||
ASSERT(slre_match("(.+2)", "123", 3, caps, 10) == 2);
|
||||
ASSERT(slre_match("(.+2)", "123", 3, caps, 10, 0) == 2);
|
||||
ASSERT(caps[0].len == 2);
|
||||
ASSERT(memcmp(caps[0].ptr, "12", 2) == 0);
|
||||
ASSERT(slre_match("(.*(2.))", "123", 3, caps, 10) == 3);
|
||||
ASSERT(slre_match("(.)(.)", "123", 3, caps, 10) == 2);
|
||||
ASSERT(slre_match("(\\d+)\\s+(\\S+)", "12 hi", 5, caps, 10) == 5);
|
||||
ASSERT(slre_match("ab(cd)+ef", "abcdcdef", 8, NULL, 0) == 8);
|
||||
ASSERT(slre_match("ab(cd)*ef", "abcdcdef", 8, NULL, 0) == 8);
|
||||
ASSERT(slre_match("ab(cd)+?ef", "abcdcdef", 8, NULL, 0) == 8);
|
||||
ASSERT(slre_match("ab(cd)+?.", "abcdcdef", 8, NULL, 0) == 5);
|
||||
ASSERT(slre_match("ab(cd)?", "abcdcdef", 8, NULL, 0) == 4);
|
||||
ASSERT(slre_match("a(b)(cd)", "abcdcdef", 8, caps, 1) ==
|
||||
ASSERT(slre_match("(.*(2.))", "123", 3, caps, 10, 0) == 3);
|
||||
ASSERT(slre_match("(.)(.)", "123", 3, caps, 10, 0) == 2);
|
||||
ASSERT(slre_match("(\\d+)\\s+(\\S+)", "12 hi", 5, caps, 10, 0) == 5);
|
||||
ASSERT(slre_match("ab(cd)+ef", "abcdcdef", 8, NULL, 0, 0) == 8);
|
||||
ASSERT(slre_match("ab(cd)*ef", "abcdcdef", 8, NULL, 0, 0) == 8);
|
||||
ASSERT(slre_match("ab(cd)+?ef", "abcdcdef", 8, NULL, 0, 0) == 8);
|
||||
ASSERT(slre_match("ab(cd)+?.", "abcdcdef", 8, NULL, 0, 0) == 5);
|
||||
ASSERT(slre_match("ab(cd)?", "abcdcdef", 8, NULL, 0, 0) == 4);
|
||||
ASSERT(slre_match("a(b)(cd)", "abcdcdef", 8, caps, 1, 0) ==
|
||||
SLRE_CAPS_ARRAY_TOO_SMALL);
|
||||
ASSERT(slre_match("(.+/\\d+\\.\\d+)\\.jpg$", "/foo/bar/12.34.jpg", 18,
|
||||
caps, 1) == 18);
|
||||
ASSERT(slre_match("(ab|cd).*\\.(xx|yy)", "ab.yy", 5, NULL, 0) == 5);
|
||||
ASSERT(slre_match(".*a", "abcdef", 6, NULL, 0) == 1);
|
||||
ASSERT(slre_match("(.+)c", "abcdef", 6, NULL, 0) == 3);
|
||||
caps, 1, 0) == 18);
|
||||
ASSERT(slre_match("(ab|cd).*\\.(xx|yy)", "ab.yy", 5, NULL, 0, 0) == 5);
|
||||
ASSERT(slre_match(".*a", "abcdef", 6, NULL, 0, 0) == 1);
|
||||
ASSERT(slre_match("(.+)c", "abcdef", 6, NULL, 0, 0) == 3);
|
||||
|
||||
/* Greedy vs non-greedy */
|
||||
ASSERT(slre_match(".+c", "abcabc", 6, NULL, 0) == 6);
|
||||
ASSERT(slre_match(".+?c", "abcabc", 6, NULL, 0) == 3);
|
||||
ASSERT(slre_match(".*?c", "abcabc", 6, NULL, 0) == 3);
|
||||
ASSERT(slre_match(".*c", "abcabc", 6, NULL, 0) == 6);
|
||||
ASSERT(slre_match("bc.d?k?b+", "abcabc", 6, NULL, 0) == 5);
|
||||
ASSERT(slre_match(".+c", "abcabc", 6, NULL, 0, 0) == 6);
|
||||
ASSERT(slre_match(".+?c", "abcabc", 6, NULL, 0, 0) == 3);
|
||||
ASSERT(slre_match(".*?c", "abcabc", 6, NULL, 0, 0) == 3);
|
||||
ASSERT(slre_match(".*c", "abcabc", 6, NULL, 0, 0) == 6);
|
||||
ASSERT(slre_match("bc.d?k?b+", "abcabc", 6, NULL, 0, 0) == 5);
|
||||
|
||||
/* Branching */
|
||||
ASSERT(slre_match("|", "abc", 3, NULL, 0) == 0);
|
||||
ASSERT(slre_match("|.", "abc", 3, NULL, 0) == 1);
|
||||
ASSERT(slre_match("x|y|b", "abc", 3, NULL, 0) == 2);
|
||||
ASSERT(slre_match("k(xx|yy)|ca", "abcabc", 6, NULL, 0) == 4);
|
||||
ASSERT(slre_match("k(xx|yy)|ca|bc", "abcabc", 6, NULL, 0) == 3);
|
||||
ASSERT(slre_match("(|.c)", "abc", 3, caps, 10) == 3);
|
||||
ASSERT(slre_match("|", "abc", 3, NULL, 0, 0) == 0);
|
||||
ASSERT(slre_match("|.", "abc", 3, NULL, 0, 0) == 1);
|
||||
ASSERT(slre_match("x|y|b", "abc", 3, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("k(xx|yy)|ca", "abcabc", 6, NULL, 0, 0) == 4);
|
||||
ASSERT(slre_match("k(xx|yy)|ca|bc", "abcabc", 6, NULL, 0, 0) == 3);
|
||||
ASSERT(slre_match("(|.c)", "abc", 3, caps, 10, 0) == 3);
|
||||
ASSERT(caps[0].len == 2);
|
||||
ASSERT(memcmp(caps[0].ptr, "bc", 2) == 0);
|
||||
ASSERT(slre_match("a|b|c", "a", 1, NULL, 0) == 1);
|
||||
ASSERT(slre_match("a|b|c", "b", 1, NULL, 0) == 1);
|
||||
ASSERT(slre_match("a|b|c", "c", 1, NULL, 0) == 1);
|
||||
ASSERT(slre_match("a|b|c", "d", 1, NULL, 0) == SLRE_NO_MATCH);
|
||||
ASSERT(slre_match("a|b|c", "a", 1, NULL, 0, 0) == 1);
|
||||
ASSERT(slre_match("a|b|c", "b", 1, NULL, 0, 0) == 1);
|
||||
ASSERT(slre_match("a|b|c", "c", 1, NULL, 0, 0) == 1);
|
||||
ASSERT(slre_match("a|b|c", "d", 1, NULL, 0, 0) == SLRE_NO_MATCH);
|
||||
|
||||
/* Optional match at the end of the string */
|
||||
ASSERT(slre_match("^.*c.?$", "abc", 3, NULL, 0) == 3);
|
||||
ASSERT(slre_match("(?i)^.*C.?$", "abc", 3, NULL, 0) == 3);
|
||||
ASSERT(slre_match("bk?", "ab", 2, NULL, 0) == 2);
|
||||
ASSERT(slre_match("b(k?)", "ab", 2, NULL, 0) == 2);
|
||||
ASSERT(slre_match("b[k-z]*", "ab", 2, NULL, 0) == 2);
|
||||
ASSERT(slre_match("ab(k|z|y)*", "ab", 2, NULL, 0) == 2);
|
||||
ASSERT(slre_match("[b-z].*", "ab", 2, NULL, 0) == 2);
|
||||
ASSERT(slre_match("(b|z|u).*", "ab", 2, NULL, 0) == 2);
|
||||
ASSERT(slre_match("ab(k|z|y)?", "ab", 2, NULL, 0) == 2);
|
||||
ASSERT(slre_match(".*", "ab", 2, NULL, 0) == 2);
|
||||
ASSERT(slre_match(".*$", "ab", 2, NULL, 0) == 2);
|
||||
ASSERT(slre_match("a+$", "aa", 2, NULL, 0) == 2);
|
||||
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(slre_match("^.*c.?$", "abc", 3, NULL, 0, 0) == 3);
|
||||
ASSERT(slre_match("^.*C.?$", "abc", 3, NULL, 0, SLRE_IGNORE_CASE) == 3);
|
||||
ASSERT(slre_match("bk?", "ab", 2, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("b(k?)", "ab", 2, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("b[k-z]*", "ab", 2, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("ab(k|z|y)*", "ab", 2, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("[b-z].*", "ab", 2, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("(b|z|u).*", "ab", 2, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("ab(k|z|y)?", "ab", 2, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match(".*", "ab", 2, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match(".*$", "ab", 2, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("a+$", "aa", 2, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match("a*$", "aa", 2, NULL, 0, 0) == 2);
|
||||
ASSERT(slre_match( "a+$" ,"Xaa", 3, NULL, 0, 0) == 3);
|
||||
ASSERT(slre_match( "a*$" ,"Xaa", 3, NULL, 0, 0) == 3);
|
||||
|
||||
{
|
||||
/* Example: HTTP request */
|
||||
@ -213,7 +216,7 @@ int main(void) {
|
||||
struct slre_cap caps[4];
|
||||
|
||||
if (slre_match("^\\s*(\\S+)\\s+(\\S+)\\s+HTTP/(\\d)\\.(\\d)",
|
||||
request, strlen(request), caps, 4) > 0) {
|
||||
request, strlen(request), caps, 4, 0) > 0) {
|
||||
printf("Method: [%.*s], URI: [%.*s]\n",
|
||||
caps[0].len, caps[0].ptr,
|
||||
caps[1].len, caps[1].ptr);
|
||||
@ -246,7 +249,7 @@ int main(void) {
|
||||
int i, j = 0, str_len = strlen(str);
|
||||
|
||||
while (j < str_len &&
|
||||
(i = slre_match(regex, str + j, str_len - j, caps, 2)) > 0) {
|
||||
(i = slre_match(regex, str + j, str_len - j, caps, 2, 0)) > 0) {
|
||||
printf("Found URL: [%.*s]\n", caps[0].len, caps[0].ptr);
|
||||
j += i;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user