mirror of
https://github.com/cesanta/slre.git
synced 2024-11-24 03:15:30 +00:00
Using integer return codes
This commit is contained in:
parent
d5e5706066
commit
402300e070
29
README.md
29
README.md
@ -42,12 +42,12 @@ Under development: Unicode support.
|
||||
## API
|
||||
|
||||
int slre_match(const char *regexp, const char *buf, int buf_len,
|
||||
struct slre_cap *caps, int num_caps, const char **error_msg);
|
||||
struct slre_cap *caps, int num_caps);
|
||||
|
||||
`slre_match()` matches string buffer `buf` of length `buf_len` against
|
||||
regular expression `regexp`, which should conform the syntax outlined
|
||||
above. If regular expression `regexp` contains brackets, `slre_match()`
|
||||
may capture the respective substrings into the array of `struct slre_cap`
|
||||
can capture the respective substrings into the array of `struct slre_cap`
|
||||
structures:
|
||||
|
||||
/* Stores matched fragment for the expression inside brackets */
|
||||
@ -56,24 +56,35 @@ structures:
|
||||
int len; /* Length of the matched fragment */
|
||||
};
|
||||
|
||||
N-th member of the `caps` array will contain fragment that corresponds
|
||||
to the N-th opening bracket in the `regex`.
|
||||
N-th member of the `caps` array will contain fragment that corresponds to the
|
||||
N-th opening bracket in the `regex`, N is zero-based. `slre_match()` returns
|
||||
number of bytes scanned from the beginning of the string. If return value is
|
||||
greater or equal to 0, there is a match. If return value is less then 0, there
|
||||
is no match. Negative return codes are as follows:
|
||||
|
||||
#define SLRE_NO_MATCH -1
|
||||
#define SLRE_UNEXPECTED_QUANTIFIER -2
|
||||
#define SLRE_UNBALANCED_BRACKETS -3
|
||||
#define SLRE_INTERNAL_ERROR -4
|
||||
#define SLRE_INVALID_CHARACTER_SET -5
|
||||
#define SLRE_INVALID_METACHARACTER -6
|
||||
#define SLRE_CAPS_ARRAY_TOO_SMALL -7
|
||||
#define SLRE_TOO_MANY_BRANCHES -8
|
||||
#define SLRE_TOO_MANY_BRACKETS -9
|
||||
|
||||
`slre_match()` returns 0 if there is no match found. Otherwise, it returns
|
||||
the number scanned bytes from the beginning of the string.
|
||||
|
||||
## Example: parsing HTTP request line
|
||||
|
||||
const char *error_msg, *request = " GET /index.html HTTP/1.0\r\n\r\n";
|
||||
const char *request = " GET /index.html HTTP/1.0\r\n\r\n";
|
||||
struct slre_cap caps[4];
|
||||
|
||||
if (slre_match("^\\s*(\\S+)\\s+(\\S+)\\s+HTTP/(\\d)\\.(\\d)",
|
||||
request, strlen(request), caps, 4, &error_msg)) {
|
||||
request, strlen(request), caps, 4) > 0) {
|
||||
printf("Method: [%.*s], URI: [%.*s]\n",
|
||||
caps[0].len, caps[0].ptr,
|
||||
caps[1].len, caps[1].ptr);
|
||||
} else {
|
||||
printf("Error parsing [%s]: [%s]\n", request, error_msg);
|
||||
printf("Error parsing [%s]\n", request);
|
||||
}
|
||||
|
||||
## Example: find all URLs in a string
|
||||
|
332
slre.c
332
slre.c
@ -24,21 +24,10 @@
|
||||
|
||||
#include "slre.h"
|
||||
|
||||
static const char *static_error_no_match = "No match";
|
||||
static const char *static_error_unexpected_quantifier = "Unexpected quantifier";
|
||||
static const char *static_error_unbalanced_brackets = "Unbalanced brackets";
|
||||
static const char *static_error_internal = "Internal error";
|
||||
static const char *static_error_invalid_set = "Invalid [] spec";
|
||||
static const char *static_error_invalid_metacharacter = "Invalid metacharacter";
|
||||
static const char *static_error_more_caps = "Caps array is too small";
|
||||
|
||||
static const char *static_metacharacters = "^$().[]*+?|\\Ssd";
|
||||
|
||||
#define MAX_BRANCHES 100
|
||||
#define MAX_BRACKETS 100
|
||||
#define ARRAY_SIZE(ar) (int) (sizeof(ar) / sizeof((ar)[0]))
|
||||
#define FAIL_IF(cond,msg) do { if (cond) \
|
||||
{info->error_msg = msg; return -1; }} while (0)
|
||||
#define FAIL_IF(condition, error_code) if (condition) return (error_code)
|
||||
|
||||
#ifdef SLRE_DEBUG
|
||||
#define DBG(x) printf x
|
||||
@ -78,16 +67,14 @@ struct regex_info {
|
||||
struct slre_cap *caps;
|
||||
int num_caps;
|
||||
|
||||
/* Error message to be returned to the user */
|
||||
const char *error_msg;
|
||||
|
||||
/* E.g. IGNORE_CASE. See enum below */
|
||||
int flags;
|
||||
};
|
||||
enum { IGNORE_CASE = 1 };
|
||||
|
||||
static int is_metacharacter(const unsigned char *s) {
|
||||
return strchr(static_metacharacters, *s) != NULL;
|
||||
static const char *metacharacters = "^$().[]*+?|\\Ssd";
|
||||
return strchr(metacharacters, *s) != NULL;
|
||||
}
|
||||
|
||||
static int op_len(const char *re) {
|
||||
@ -128,43 +115,43 @@ static int match_op(const unsigned char *re, const unsigned char *s,
|
||||
/* Metacharacters */
|
||||
switch (re[1]) {
|
||||
case 'S':
|
||||
FAIL_IF(isspace(*s), static_error_no_match);
|
||||
FAIL_IF(isspace(*s), SLRE_NO_MATCH);
|
||||
result++;
|
||||
break;
|
||||
|
||||
case 's':
|
||||
FAIL_IF(!isspace(*s), static_error_no_match);
|
||||
FAIL_IF(!isspace(*s), SLRE_NO_MATCH);
|
||||
result++;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
FAIL_IF(!isdigit(*s), static_error_no_match);
|
||||
FAIL_IF(!isdigit(*s), SLRE_NO_MATCH);
|
||||
result++;
|
||||
break;
|
||||
|
||||
case 'x':
|
||||
/* Match byte, \xHH where HH is hexadecimal byte representaion */
|
||||
FAIL_IF(hextoi(re + 2) != *s, static_error_no_match);
|
||||
FAIL_IF(hextoi(re + 2) != *s, SLRE_NO_MATCH);
|
||||
result++;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Valid metacharacter check is done in bar() */
|
||||
FAIL_IF(re[1] != s[0], static_error_no_match);
|
||||
FAIL_IF(re[1] != s[0], SLRE_NO_MATCH);
|
||||
result++;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case '|': FAIL_IF(1, static_error_internal); break;
|
||||
case '$': FAIL_IF(1, static_error_no_match); break;
|
||||
case '|': FAIL_IF(1, SLRE_INTERNAL_ERROR); break;
|
||||
case '$': FAIL_IF(1, SLRE_NO_MATCH); break;
|
||||
case '.': result++; break;
|
||||
|
||||
default:
|
||||
if (info->flags & IGNORE_CASE) {
|
||||
FAIL_IF(tolower(*re) != tolower(*s), static_error_no_match);
|
||||
FAIL_IF(tolower(*re) != tolower(*s), SLRE_NO_MATCH);
|
||||
} else {
|
||||
FAIL_IF(*re != *s, static_error_no_match);
|
||||
FAIL_IF(*re != *s, SLRE_NO_MATCH);
|
||||
}
|
||||
result++;
|
||||
break;
|
||||
@ -211,8 +198,8 @@ static int bar(const char *re, int re_len, const char *s, int s_len,
|
||||
DBG(("%s [%.*s] [%.*s] re_len=%d step=%d i=%d j=%d\n", __func__,
|
||||
re_len - i, re + i, s_len - j, s + j, re_len, step, i, j));
|
||||
|
||||
FAIL_IF(is_quantifier(&re[i]), static_error_unexpected_quantifier);
|
||||
FAIL_IF(step <= 0, static_error_invalid_set);
|
||||
FAIL_IF(is_quantifier(&re[i]), SLRE_UNEXPECTED_QUANTIFIER);
|
||||
FAIL_IF(step <= 0, SLRE_INVALID_CHARACTER_SET);
|
||||
|
||||
if (i + step < re_len && is_quantifier(re + i + step)) {
|
||||
DBG(("QUANTIFIER: [%.*s]%c [%.*s]\n", step, re + i,
|
||||
@ -243,11 +230,11 @@ static int bar(const char *re, int re_len, const char *s, int s_len,
|
||||
j2 += n1;
|
||||
}
|
||||
DBG(("STAR/PLUS END: %d %d %d\n", j, nj, re_len - ni));
|
||||
FAIL_IF(re[i + step] == '+' && nj == j, static_error_no_match);
|
||||
FAIL_IF(re[i + step] == '+' && nj == j, SLRE_NO_MATCH);
|
||||
|
||||
/* If while loop body above was not executed for the * quantifier, */
|
||||
/* make sure the rest of the regex matches */
|
||||
FAIL_IF(nj == j && ni < re_len && n2 < 0, static_error_no_match);
|
||||
FAIL_IF(nj == j && ni < re_len && n2 < 0, SLRE_NO_MATCH);
|
||||
|
||||
/* Returning here cause we've matched the rest of RE already */
|
||||
return nj;
|
||||
@ -258,28 +245,28 @@ static int bar(const char *re, int re_len, const char *s, int s_len,
|
||||
if (re[i] == '[') {
|
||||
n = match_set(re + i + 1, re_len - (i + 2), s + j, info);
|
||||
DBG(("SET %.*s [%.*s] -> %d\n", step, re + i, s_len - j, s + j, n));
|
||||
FAIL_IF(n <= 0, static_error_no_match);
|
||||
FAIL_IF(n <= 0, SLRE_NO_MATCH);
|
||||
j += n;
|
||||
} else if (re[i] == '(') {
|
||||
bi++;
|
||||
FAIL_IF(bi >= info->num_brackets, static_error_internal);
|
||||
FAIL_IF(bi >= info->num_brackets, SLRE_INTERNAL_ERROR);
|
||||
DBG(("CAPTURING [%.*s] [%.*s]\n", step, re + i, s_len - j, s + j));
|
||||
n = doh(s + j, s_len - j, info, bi);
|
||||
DBG(("CAPTURED [%.*s] [%.*s]:%d\n", step, re + i, s_len - j, s + j, n));
|
||||
FAIL_IF(n < 0, info->error_msg);
|
||||
FAIL_IF(n < 0, n);
|
||||
if (info->caps != NULL) {
|
||||
info->caps[bi - 1].ptr = s + j;
|
||||
info->caps[bi - 1].len = n;
|
||||
}
|
||||
j += n;
|
||||
} else if (re[i] == '^') {
|
||||
FAIL_IF(j != 0, static_error_no_match);
|
||||
FAIL_IF(j != 0, SLRE_NO_MATCH);
|
||||
} else if (re[i] == '$') {
|
||||
FAIL_IF(j != s_len, static_error_no_match);
|
||||
FAIL_IF(j != s_len, SLRE_NO_MATCH);
|
||||
} else {
|
||||
FAIL_IF(j >= s_len, static_error_no_match);
|
||||
FAIL_IF(j >= s_len, SLRE_NO_MATCH);
|
||||
n = match_op((unsigned char *) (re + i), (unsigned char *) (s + j), info);
|
||||
FAIL_IF(n <= 0, info->error_msg);
|
||||
FAIL_IF(n <= 0, n);
|
||||
j += n;
|
||||
}
|
||||
}
|
||||
@ -368,34 +355,34 @@ static int foo(const char *re, int re_len, const char *s, int s_len,
|
||||
|
||||
if (re[i] == '|') {
|
||||
FAIL_IF(info->num_branches >= ARRAY_SIZE(info->branches),
|
||||
"Too many |. Increase MAX_BRANCHES");
|
||||
SLRE_TOO_MANY_BRANCHES);
|
||||
info->branches[info->num_branches].bracket_index =
|
||||
info->brackets[info->num_brackets - 1].len == -1 ?
|
||||
info->num_brackets - 1 : depth;
|
||||
info->branches[info->num_branches].schlong = &re[i];
|
||||
info->num_branches++;
|
||||
} else if (re[i] == '\\') {
|
||||
FAIL_IF(i >= re_len - 1, static_error_invalid_metacharacter);
|
||||
FAIL_IF(i >= re_len - 1, SLRE_INVALID_METACHARACTER);
|
||||
if (re[i + 1] == 'x') {
|
||||
/* Hex digit specification must follow */
|
||||
FAIL_IF(re[i + 1] == 'x' && i >= re_len - 3,
|
||||
static_error_invalid_metacharacter);
|
||||
SLRE_INVALID_METACHARACTER);
|
||||
FAIL_IF(re[i + 1] == 'x' && !(isxdigit(re[i + 2]) &&
|
||||
isxdigit(re[i + 3])), static_error_invalid_metacharacter);
|
||||
isxdigit(re[i + 3])), SLRE_INVALID_METACHARACTER);
|
||||
} else {
|
||||
FAIL_IF(!is_metacharacter((unsigned char *) re + i + 1),
|
||||
static_error_invalid_metacharacter);
|
||||
SLRE_INVALID_METACHARACTER);
|
||||
}
|
||||
} else if (re[i] == '(') {
|
||||
FAIL_IF(info->num_brackets >= ARRAY_SIZE(info->brackets),
|
||||
"Too many (. Increase MAX_BRACKETS");
|
||||
SLRE_TOO_MANY_BRACKETS);
|
||||
depth++; /* Order is important here. Depth increments first. */
|
||||
stack[depth] = &re[i];
|
||||
info->brackets[info->num_brackets].ptr = re + i + 1;
|
||||
info->brackets[info->num_brackets].len = -1;
|
||||
info->num_brackets++;
|
||||
FAIL_IF(info->num_caps > 0 && info->num_brackets - 1 > info->num_caps,
|
||||
static_error_more_caps);
|
||||
SLRE_CAPS_ARRAY_TOO_SMALL);
|
||||
} else if (re[i] == ')') {
|
||||
int ind = info->brackets[info->num_brackets - 1].len == -1 ?
|
||||
info->num_brackets - 1 : depth;
|
||||
@ -403,25 +390,23 @@ static int foo(const char *re, int re_len, const char *s, int s_len,
|
||||
DBG(("SETTING BRACKET %d [%.*s]\n",
|
||||
ind, info->brackets[ind].len, info->brackets[ind].ptr));
|
||||
depth--;
|
||||
FAIL_IF(depth < 0, static_error_unbalanced_brackets);
|
||||
FAIL_IF(i > 0 && re[i - 1] == '(', static_error_no_match);
|
||||
FAIL_IF(depth < 0, SLRE_UNBALANCED_BRACKETS);
|
||||
FAIL_IF(i > 0 && re[i - 1] == '(', SLRE_NO_MATCH);
|
||||
}
|
||||
}
|
||||
|
||||
FAIL_IF(depth != 0, static_error_unbalanced_brackets);
|
||||
FAIL_IF(depth != 0, SLRE_UNBALANCED_BRACKETS);
|
||||
setup_branch_points(info);
|
||||
|
||||
return baz(s, s_len, info);
|
||||
}
|
||||
|
||||
int slre_match(const char *regexp, const char *s, int s_len,
|
||||
struct slre_cap *caps, int num_caps, const char **error_msg) {
|
||||
struct slre_cap *caps, int num_caps) {
|
||||
struct regex_info info;
|
||||
int result;
|
||||
|
||||
/* Initialize info structure */
|
||||
info.flags = info.num_brackets = info.num_branches = 0;
|
||||
info.error_msg = "";
|
||||
info.num_caps = num_caps;
|
||||
info.caps = caps;
|
||||
|
||||
@ -433,21 +418,15 @@ int slre_match(const char *regexp, const char *s, int s_len,
|
||||
regexp += 4;
|
||||
}
|
||||
|
||||
result = foo(regexp, strlen(regexp), s, s_len, &info);
|
||||
|
||||
if (error_msg != NULL) {
|
||||
*error_msg = result > 0 ? "" : info.error_msg;
|
||||
}
|
||||
|
||||
return result > 0 ? result : 0;
|
||||
return foo(regexp, strlen(regexp), s, s_len, &info);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/********************************** UNIT TEST ********************************/
|
||||
/*****************************************************************************/
|
||||
/* To run a test under UNIX, do: */
|
||||
/* cc slre.c -DSLRE_UNIT_TEST -o /tmp/a && /tmp/a */
|
||||
/* To run a test under UNIX, do: */
|
||||
/* cc slre.c -W -Wall -ansi -pedantic -DSLRE_UNIT_TEST -o /tmp/a && /tmp/a */
|
||||
|
||||
#ifdef SLRE_UNIT_TEST
|
||||
static int static_total_tests = 0;
|
||||
@ -472,7 +451,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, NULL)) > 0) {
|
||||
if ((n = slre_match(regex, buf, len, &cap, 1)) > 0) {
|
||||
n1 = cap.ptr - buf, n2 = strlen(sub),
|
||||
n3 = &buf[n] - &cap.ptr[cap.len];
|
||||
} else {
|
||||
@ -496,165 +475,154 @@ int main(void) {
|
||||
struct slre_cap caps[10];
|
||||
|
||||
/* Metacharacters */
|
||||
ASSERT(slre_match("$", "abcd", 4, NULL, 0, &msg) == 4);
|
||||
ASSERT(slre_match("^", "abcd", 4, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("x|^", "abcd", 4, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("x|$", "abcd", 4, NULL, 0, &msg) == 4);
|
||||
ASSERT(slre_match("x", "abcd", 4, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match(".", "abcd", 4, NULL, 0, &msg) == 1);
|
||||
ASSERT(slre_match("(?i)^.*\\\\.*$", "c:\\Tools", 8, NULL, 0, &msg) == 8);
|
||||
ASSERT(slre_match("\\", "a", 1, NULL, 0, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_invalid_metacharacter) == 0);
|
||||
ASSERT(slre_match("\\x", "a", 1, NULL, 0, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_invalid_metacharacter) == 0);
|
||||
ASSERT(slre_match("\\x1", "a", 1, NULL, 0, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_invalid_metacharacter) == 0);
|
||||
ASSERT(slre_match("\\x20", " ", 1, NULL, 0, &msg) == 1);
|
||||
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);
|
||||
|
||||
/* Character sets */
|
||||
ASSERT(slre_match("[abc]", "1c2", 3, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("[abc]", "1C2", 3, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("(?i)[abc]", "1C2", 3, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("[.2]", "1C2", 3, NULL, 0, &msg) == 1);
|
||||
ASSERT(slre_match("[\\S]+", "ab cd", 5, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("[\\S]+\\s+[tyc]*", "ab cd", 5, NULL, 0, &msg) == 4);
|
||||
ASSERT(slre_match("[\\d]", "ab cd", 5, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("[^\\d]", "ab cd", 5, NULL, 0, &msg) == 1);
|
||||
ASSERT(slre_match("[^\\d]+", "abc123", 6, NULL, 0, &msg) == 3);
|
||||
ASSERT(slre_match("[1-5]+", "123456789", 9, NULL, 0, &msg) == 5);
|
||||
ASSERT(slre_match("[1-5a-c]+", "123abcdef", 9, NULL, 0, &msg) == 6);
|
||||
ASSERT(slre_match("[1-5a-]+", "123abcdef", 9, NULL, 0, &msg) == 4);
|
||||
ASSERT(slre_match("[1-5a-]+", "123a--2oo", 9, NULL, 0, &msg) == 7);
|
||||
ASSERT(slre_match("[htps]+://", "https://", 8, NULL, 0, &msg) == 8);
|
||||
ASSERT(slre_match("[^\\s]+", "abc def", 7, NULL, 0, &msg) == 3);
|
||||
ASSERT(slre_match("[^fc]+", "abc def", 7, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("[^d\\sf]+", "abc def", 7, NULL, 0, &msg) == 3);
|
||||
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);
|
||||
|
||||
/* Flags - case sensitivity */
|
||||
ASSERT(slre_match("FO", "foo", 3, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("(?i)FO", "foo", 3, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("(?m)FO", "foo", 3, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("(?m)x", "foo", 3, NULL, 0, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_unexpected_quantifier) == 0);
|
||||
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, &msg) == 2);
|
||||
ASSERT(slre_match(".+", "foo", 3, NULL, 0, &msg) == 3);
|
||||
ASSERT(slre_match(".+k", "fooklmn", 7, NULL, 0, &msg) == 4);
|
||||
ASSERT(slre_match(".+k.", "fooklmn", 7, NULL, 0, &msg) == 5);
|
||||
ASSERT(slre_match("p+", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("ok", "fooklmn", 7, NULL, 0, &msg) == 4);
|
||||
ASSERT(slre_match("lmno", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("mn.", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("o", "fooklmn", 7, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("^o", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("^", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("n$", "fooklmn", 7, NULL, 0, &msg) == 7);
|
||||
ASSERT(slre_match("n$k", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("l$", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match(".$", "fooklmn", 7, NULL, 0, &msg) == 7);
|
||||
ASSERT(slre_match("a?", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
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("\\_", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_invalid_metacharacter) == 0);
|
||||
ASSERT(slre_match("+", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_unexpected_quantifier) == 0);
|
||||
ASSERT(slre_match("()+", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_no_match) == 0);
|
||||
ASSERT(slre_match("\\x", "12", 2, NULL, 0, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_invalid_metacharacter) == 0);
|
||||
ASSERT(slre_match("\\xhi", "12", 2, NULL, 0, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_invalid_metacharacter) == 0);
|
||||
ASSERT(slre_match("\\x20", "_ J", 3, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("\\x4A", "_ J", 3, NULL, 0, &msg) == 3);
|
||||
ASSERT(slre_match("\\d+", "abc123def", 9, NULL, 0, &msg) == 6);
|
||||
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);
|
||||
|
||||
/* Balancing brackets */
|
||||
ASSERT(slre_match("(x))", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_unbalanced_brackets) == 0);
|
||||
ASSERT(slre_match("(", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_unbalanced_brackets) == 0);
|
||||
ASSERT(slre_match("(x))", "fooklmn", 7, NULL, 0) == SLRE_UNBALANCED_BRACKETS);
|
||||
ASSERT(slre_match("(", "fooklmn", 7, NULL, 0) == SLRE_UNBALANCED_BRACKETS);
|
||||
|
||||
ASSERT(slre_match("klz?mn", "fooklmn", 7, NULL, 0, &msg) == 7);
|
||||
ASSERT(slre_match("fa?b", "fooklmn", 7, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("klz?mn", "fooklmn", 7, NULL, 0) == 7);
|
||||
ASSERT(slre_match("fa?b", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
|
||||
|
||||
/* Brackets & capturing */
|
||||
ASSERT(slre_match("^(te)", "tenacity subdues all", 20, caps, 10, &msg) == 2);
|
||||
ASSERT(slre_match("(bc)", "abcdef", 6, caps, 10, &msg) == 3);
|
||||
ASSERT(slre_match(".(d.)", "abcdef", 6, caps, 10, &msg) == 5);
|
||||
ASSERT(slre_match(".(d.)\\)?", "abcdef", 6, caps, 10, &msg) == 5);
|
||||
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(caps[0].len == 2);
|
||||
ASSERT(memcmp(caps[0].ptr, "de", 2) == 0);
|
||||
ASSERT(slre_match("(.+)", "123", 3, caps, 10, &msg) == 3);
|
||||
ASSERT(slre_match("(2.+)", "123", 3, caps, 10, &msg) == 3);
|
||||
ASSERT(slre_match("(.+)", "123", 3, caps, 10) == 3);
|
||||
ASSERT(slre_match("(2.+)", "123", 3, caps, 10) == 3);
|
||||
ASSERT(caps[0].len == 2);
|
||||
ASSERT(memcmp(caps[0].ptr, "23", 2) == 0);
|
||||
ASSERT(slre_match("(.+2)", "123", 3, caps, 10, &msg) == 2);
|
||||
ASSERT(slre_match("(.+2)", "123", 3, caps, 10) == 2);
|
||||
ASSERT(caps[0].len == 2);
|
||||
ASSERT(memcmp(caps[0].ptr, "12", 2) == 0);
|
||||
ASSERT(slre_match("(.*(2.))", "123", 3, caps, 10, &msg) == 3);
|
||||
ASSERT(slre_match("(.)(.)", "123", 3, caps, 10, &msg) == 2);
|
||||
ASSERT(slre_match("(\\d+)\\s+(\\S+)", "12 hi", 5, caps, 10, &msg) == 5);
|
||||
ASSERT(slre_match("ab(cd)+ef", "abcdcdef", 8, NULL, 0, &msg) == 8);
|
||||
ASSERT(slre_match("ab(cd)*ef", "abcdcdef", 8, NULL, 0, &msg) == 8);
|
||||
ASSERT(slre_match("ab(cd)+?ef", "abcdcdef", 8, NULL, 0, &msg) == 8);
|
||||
ASSERT(slre_match("ab(cd)+?.", "abcdcdef", 8, NULL, 0, &msg) == 5);
|
||||
ASSERT(slre_match("ab(cd)?", "abcdcdef", 8, NULL, 0, &msg) == 4);
|
||||
ASSERT(slre_match("a(b)(cd)", "abcdcdef", 8, caps, 1, &msg) == 0);
|
||||
ASSERT(strcmp(msg, static_error_more_caps) == 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) ==
|
||||
SLRE_CAPS_ARRAY_TOO_SMALL);
|
||||
ASSERT(slre_match("(.+/\\d+\\.\\d+)\\.jpg$", "/foo/bar/12.34.jpg", 18,
|
||||
caps, 1, &msg) == 18);
|
||||
caps, 1) == 18);
|
||||
|
||||
/* Greedy vs non-greedy */
|
||||
ASSERT(slre_match(".+c", "abcabc", 6, NULL, 0, &msg) == 6);
|
||||
ASSERT(slre_match(".+?c", "abcabc", 6, NULL, 0, &msg) == 3);
|
||||
ASSERT(slre_match(".*?c", "abcabc", 6, NULL, 0, &msg) == 3);
|
||||
ASSERT(slre_match(".*c", "abcabc", 6, NULL, 0, &msg) == 6);
|
||||
ASSERT(slre_match("bc.d?k?b+", "abcabc", 6, NULL, 0, &msg) == 5);
|
||||
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);
|
||||
|
||||
/* Branching */
|
||||
ASSERT(slre_match("|", "abc", 3, NULL, 0, &msg) == 0);
|
||||
ASSERT(slre_match("|.", "abc", 3, NULL, 0, &msg) == 1);
|
||||
ASSERT(slre_match("x|y|b", "abc", 3, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("k(xx|yy)|ca", "abcabc", 6, NULL, 0, &msg) == 4);
|
||||
ASSERT(slre_match("k(xx|yy)|ca|bc", "abcabc", 6, NULL, 0, &msg) == 3);
|
||||
ASSERT(slre_match("(|.c)", "abc", 3, caps, 10, &msg) == 3);
|
||||
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(caps[0].len == 2);
|
||||
ASSERT(memcmp(caps[0].ptr, "bc", 2) == 0);
|
||||
ASSERT(slre_match("a|b|c", "a", 1, NULL, 0, &msg) == 1);
|
||||
ASSERT(slre_match("a|b|c", "b", 1, NULL, 0, &msg) == 1);
|
||||
ASSERT(slre_match("a|b|c", "c", 1, NULL, 0, &msg) == 1);
|
||||
ASSERT(slre_match("a|b|c", "d", 1, NULL, 0, &msg) == 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);
|
||||
|
||||
/* Optional match at the end of the string */
|
||||
ASSERT(slre_match("^.*c.?$", "abc", 3, NULL, 0, &msg) == 3);
|
||||
ASSERT(slre_match("(?i)^.*C.?$", "abc", 3, NULL, 0, &msg) == 3);
|
||||
ASSERT(slre_match("bk?", "ab", 2, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("b(k?)", "ab", 2, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("b[k-z]*", "ab", 2, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("ab(k|z|y)*", "ab", 2, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("[b-z].*", "ab", 2, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("(b|z|u).*", "ab", 2, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("ab(k|z|y)?", "ab", 2, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match(".*", "ab", 2, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match(".*$", "ab", 2, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("a+$", "aa", 2, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match("a*$", "aa", 2, NULL, 0, &msg) == 2);
|
||||
ASSERT(slre_match( "a+$" ,"Xaa", 3, NULL, 0, &msg) == 3);
|
||||
ASSERT(slre_match( "a*$" ,"Xaa", 3, NULL, 0, &msg) == 3);
|
||||
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(msg[0] == '\0');
|
||||
|
||||
{
|
||||
/* Example: HTTP request */
|
||||
const char *error_msg, *request = " GET /index.html HTTP/1.0\r\n\r\n";
|
||||
const char *request = " GET /index.html HTTP/1.0\r\n\r\n";
|
||||
struct slre_cap caps[4];
|
||||
|
||||
if (slre_match("^\\s*(\\S+)\\s+(\\S+)\\s+HTTP/(\\d)\\.(\\d)",
|
||||
request, strlen(request), caps, 4, &error_msg)) {
|
||||
request, strlen(request), caps, 4) > 0) {
|
||||
printf("Method: [%.*s], URI: [%.*s]\n",
|
||||
caps[0].len, caps[0].ptr,
|
||||
caps[1].len, caps[1].ptr);
|
||||
} else {
|
||||
printf("Error parsing [%s]: [%s]\n", request, error_msg);
|
||||
printf("Error parsing [%s]\n", request);
|
||||
}
|
||||
|
||||
ASSERT(caps[1].len == 11);
|
||||
@ -682,7 +650,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, NULL)) > 0) {
|
||||
(i = slre_match(regex, str + j, str_len - j, caps, 2)) > 0) {
|
||||
printf("Found URL: [%.*s]\n", caps[0].len, caps[0].ptr);
|
||||
j += i;
|
||||
}
|
||||
|
13
slre.h
13
slre.h
@ -35,7 +35,18 @@ struct slre_cap {
|
||||
};
|
||||
|
||||
int slre_match(const char *regexp, const char *buf, int buf_len,
|
||||
struct slre_cap *caps, int num_caps, const char **error_msg);
|
||||
struct slre_cap *caps, int num_caps);
|
||||
|
||||
/* slre_match() failure codes */
|
||||
#define SLRE_NO_MATCH -1
|
||||
#define SLRE_UNEXPECTED_QUANTIFIER -2
|
||||
#define SLRE_UNBALANCED_BRACKETS -3
|
||||
#define SLRE_INTERNAL_ERROR -4
|
||||
#define SLRE_INVALID_CHARACTER_SET -5
|
||||
#define SLRE_INVALID_METACHARACTER -6
|
||||
#define SLRE_CAPS_ARRAY_TOO_SMALL -7
|
||||
#define SLRE_TOO_MANY_BRANCHES -8
|
||||
#define SLRE_TOO_MANY_BRACKETS -9
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user