Using integer return codes

pull/15/head 1.2
Sergey Lyubka 11 years ago
parent d5e5706066
commit 402300e070
  1. 29
      README.md
  2. 336
      slre.c
  3. 13
      slre.h

@ -42,12 +42,12 @@ Under development: Unicode support.
## API ## API
int slre_match(const char *regexp, const char *buf, int buf_len, 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 `slre_match()` matches string buffer `buf` of length `buf_len` against
regular expression `regexp`, which should conform the syntax outlined regular expression `regexp`, which should conform the syntax outlined
above. If regular expression `regexp` contains brackets, `slre_match()` 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: structures:
/* Stores matched fragment for the expression inside brackets */ /* Stores matched fragment for the expression inside brackets */
@ -56,24 +56,35 @@ structures:
int len; /* Length of the matched fragment */ int len; /* Length of the matched fragment */
}; };
N-th member of the `caps` array will contain fragment that corresponds N-th member of the `caps` array will contain fragment that corresponds to the
to the N-th opening bracket in the `regex`. 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 ## 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]; struct slre_cap caps[4];
if (slre_match("^\\s*(\\S+)\\s+(\\S+)\\s+HTTP/(\\d)\\.(\\d)", 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", printf("Method: [%.*s], URI: [%.*s]\n",
caps[0].len, caps[0].ptr, caps[0].len, caps[0].ptr,
caps[1].len, caps[1].ptr); caps[1].len, caps[1].ptr);
} else { } else {
printf("Error parsing [%s]: [%s]\n", request, error_msg); printf("Error parsing [%s]\n", request);
} }
## Example: find all URLs in a string ## Example: find all URLs in a string

336
slre.c

@ -24,21 +24,10 @@
#include "slre.h" #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_BRANCHES 100
#define MAX_BRACKETS 100 #define MAX_BRACKETS 100
#define ARRAY_SIZE(ar) (int) (sizeof(ar) / sizeof((ar)[0])) #define ARRAY_SIZE(ar) (int) (sizeof(ar) / sizeof((ar)[0]))
#define FAIL_IF(cond,msg) do { if (cond) \ #define FAIL_IF(condition, error_code) if (condition) return (error_code)
{info->error_msg = msg; return -1; }} while (0)
#ifdef SLRE_DEBUG #ifdef SLRE_DEBUG
#define DBG(x) printf x #define DBG(x) printf x
@ -78,16 +67,14 @@ struct regex_info {
struct slre_cap *caps; struct slre_cap *caps;
int num_caps; int num_caps;
/* Error message to be returned to the user */
const char *error_msg;
/* E.g. IGNORE_CASE. See enum below */ /* E.g. IGNORE_CASE. See enum below */
int flags; int flags;
}; };
enum { IGNORE_CASE = 1 }; enum { IGNORE_CASE = 1 };
static int is_metacharacter(const unsigned char *s) { 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) { static int op_len(const char *re) {
@ -128,43 +115,43 @@ static int match_op(const unsigned char *re, const unsigned char *s,
/* Metacharacters */ /* Metacharacters */
switch (re[1]) { switch (re[1]) {
case 'S': case 'S':
FAIL_IF(isspace(*s), static_error_no_match); FAIL_IF(isspace(*s), SLRE_NO_MATCH);
result++; result++;
break; break;
case 's': case 's':
FAIL_IF(!isspace(*s), static_error_no_match); FAIL_IF(!isspace(*s), SLRE_NO_MATCH);
result++; result++;
break; break;
case 'd': case 'd':
FAIL_IF(!isdigit(*s), static_error_no_match); FAIL_IF(!isdigit(*s), SLRE_NO_MATCH);
result++; result++;
break; break;
case 'x': case 'x':
/* Match byte, \xHH where HH is hexadecimal byte representaion */ /* 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++; result++;
break; break;
default: default:
/* Valid metacharacter check is done in bar() */ /* 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++; result++;
break; break;
} }
break; break;
case '|': FAIL_IF(1, static_error_internal); break; case '|': FAIL_IF(1, SLRE_INTERNAL_ERROR); break;
case '$': FAIL_IF(1, static_error_no_match); break; case '$': FAIL_IF(1, SLRE_NO_MATCH); break;
case '.': result++; break; case '.': result++; break;
default: default:
if (info->flags & IGNORE_CASE) { 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 { } else {
FAIL_IF(*re != *s, static_error_no_match); FAIL_IF(*re != *s, SLRE_NO_MATCH);
} }
result++; result++;
break; 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__, 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)); 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(is_quantifier(&re[i]), SLRE_UNEXPECTED_QUANTIFIER);
FAIL_IF(step <= 0, static_error_invalid_set); FAIL_IF(step <= 0, SLRE_INVALID_CHARACTER_SET);
if (i + step < re_len && is_quantifier(re + i + step)) { if (i + step < re_len && is_quantifier(re + i + step)) {
DBG(("QUANTIFIER: [%.*s]%c [%.*s]\n", step, re + i, 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; j2 += n1;
} }
DBG(("STAR/PLUS END: %d %d %d\n", j, nj, re_len - ni)); 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, */ /* If while loop body above was not executed for the * quantifier, */
/* make sure the rest of the regex matches */ /* 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 */ /* Returning here cause we've matched the rest of RE already */
return nj; return nj;
@ -258,28 +245,28 @@ static int bar(const char *re, int re_len, const char *s, int s_len,
if (re[i] == '[') { if (re[i] == '[') {
n = match_set(re + i + 1, re_len - (i + 2), s + j, info); 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)); 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; j += n;
} else if (re[i] == '(') { } else if (re[i] == '(') {
bi++; 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)); DBG(("CAPTURING [%.*s] [%.*s]\n", step, re + i, s_len - j, s + j));
n = doh(s + j, s_len - j, info, bi); n = doh(s + j, s_len - j, info, bi);
DBG(("CAPTURED [%.*s] [%.*s]:%d\n", step, re + i, s_len - j, s + j, n)); 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) { if (info->caps != NULL) {
info->caps[bi - 1].ptr = s + j; info->caps[bi - 1].ptr = s + j;
info->caps[bi - 1].len = n; info->caps[bi - 1].len = n;
} }
j += n; j += n;
} else if (re[i] == '^') { } else if (re[i] == '^') {
FAIL_IF(j != 0, static_error_no_match); FAIL_IF(j != 0, SLRE_NO_MATCH);
} else if (re[i] == '$') { } else if (re[i] == '$') {
FAIL_IF(j != s_len, static_error_no_match); FAIL_IF(j != s_len, SLRE_NO_MATCH);
} else { } 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); 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; j += n;
} }
} }
@ -368,34 +355,34 @@ static int foo(const char *re, int re_len, const char *s, int s_len,
if (re[i] == '|') { if (re[i] == '|') {
FAIL_IF(info->num_branches >= ARRAY_SIZE(info->branches), 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->branches[info->num_branches].bracket_index =
info->brackets[info->num_brackets - 1].len == -1 ? info->brackets[info->num_brackets - 1].len == -1 ?
info->num_brackets - 1 : depth; info->num_brackets - 1 : depth;
info->branches[info->num_branches].schlong = &re[i]; info->branches[info->num_branches].schlong = &re[i];
info->num_branches++; info->num_branches++;
} else if (re[i] == '\\') { } 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') { if (re[i + 1] == 'x') {
/* Hex digit specification must follow */ /* Hex digit specification must follow */
FAIL_IF(re[i + 1] == 'x' && i >= re_len - 3, 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]) && 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 { } else {
FAIL_IF(!is_metacharacter((unsigned char *) re + i + 1), FAIL_IF(!is_metacharacter((unsigned char *) re + i + 1),
static_error_invalid_metacharacter); SLRE_INVALID_METACHARACTER);
} }
} else if (re[i] == '(') { } else if (re[i] == '(') {
FAIL_IF(info->num_brackets >= ARRAY_SIZE(info->brackets), 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. */ depth++; /* Order is important here. Depth increments first. */
stack[depth] = &re[i]; stack[depth] = &re[i];
info->brackets[info->num_brackets].ptr = re + i + 1; info->brackets[info->num_brackets].ptr = re + i + 1;
info->brackets[info->num_brackets].len = -1; info->brackets[info->num_brackets].len = -1;
info->num_brackets++; info->num_brackets++;
FAIL_IF(info->num_caps > 0 && info->num_brackets - 1 > info->num_caps, 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] == ')') { } else if (re[i] == ')') {
int ind = info->brackets[info->num_brackets - 1].len == -1 ? int ind = info->brackets[info->num_brackets - 1].len == -1 ?
info->num_brackets - 1 : depth; 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", DBG(("SETTING BRACKET %d [%.*s]\n",
ind, info->brackets[ind].len, info->brackets[ind].ptr)); ind, info->brackets[ind].len, info->brackets[ind].ptr));
depth--; depth--;
FAIL_IF(depth < 0, static_error_unbalanced_brackets); FAIL_IF(depth < 0, SLRE_UNBALANCED_BRACKETS);
FAIL_IF(i > 0 && re[i - 1] == '(', static_error_no_match); 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); setup_branch_points(info);
return baz(s, s_len, info); return baz(s, s_len, info);
} }
int slre_match(const char *regexp, 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, const char **error_msg) { struct slre_cap *caps, int num_caps) {
struct regex_info info; struct regex_info info;
int result;
/* Initialize info structure */ /* Initialize info structure */
info.flags = info.num_brackets = info.num_branches = 0; info.flags = info.num_brackets = info.num_branches = 0;
info.error_msg = "";
info.num_caps = num_caps; info.num_caps = num_caps;
info.caps = caps; info.caps = caps;
@ -433,21 +418,15 @@ int slre_match(const char *regexp, const char *s, int s_len,
regexp += 4; regexp += 4;
} }
result = foo(regexp, strlen(regexp), s, s_len, &info); return foo(regexp, strlen(regexp), s, s_len, &info);
if (error_msg != NULL) {
*error_msg = result > 0 ? "" : info.error_msg;
}
return result > 0 ? result : 0;
} }
/*****************************************************************************/ /*****************************************************************************/
/********************************** UNIT TEST ********************************/ /********************************** UNIT TEST ********************************/
/*****************************************************************************/ /*****************************************************************************/
/* To run a test under UNIX, do: */ /* To run a test under UNIX, do: */
/* cc slre.c -DSLRE_UNIT_TEST -o /tmp/a && /tmp/a */ /* cc slre.c -W -Wall -ansi -pedantic -DSLRE_UNIT_TEST -o /tmp/a && /tmp/a */
#ifdef SLRE_UNIT_TEST #ifdef SLRE_UNIT_TEST
static int static_total_tests = 0; static int static_total_tests = 0;
@ -472,7 +451,7 @@ static char *slre_replace(const char *regex, const char *buf,
do { do {
s_len = s == NULL ? 0 : strlen(s); 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), n1 = cap.ptr - buf, n2 = strlen(sub),
n3 = &buf[n] - &cap.ptr[cap.len]; n3 = &buf[n] - &cap.ptr[cap.len];
} else { } else {
@ -496,165 +475,154 @@ int main(void) {
struct slre_cap caps[10]; struct slre_cap caps[10];
/* Metacharacters */ /* Metacharacters */
ASSERT(slre_match("$", "abcd", 4, NULL, 0, &msg) == 4); ASSERT(slre_match("$", "abcd", 4, NULL, 0) == 4);
ASSERT(slre_match("^", "abcd", 4, NULL, 0, &msg) == 0); ASSERT(slre_match("^", "abcd", 4, NULL, 0) == 0);
ASSERT(slre_match("x|^", "abcd", 4, NULL, 0, &msg) == 0); ASSERT(slre_match("x|^", "abcd", 4, NULL, 0) == 0);
ASSERT(slre_match("x|$", "abcd", 4, NULL, 0, &msg) == 4); ASSERT(slre_match("x|$", "abcd", 4, NULL, 0) == 4);
ASSERT(slre_match("x", "abcd", 4, NULL, 0, &msg) == 0); ASSERT(slre_match("x", "abcd", 4, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match(".", "abcd", 4, NULL, 0, &msg) == 1); ASSERT(slre_match(".", "abcd", 4, NULL, 0) == 1);
ASSERT(slre_match("(?i)^.*\\\\.*$", "c:\\Tools", 8, NULL, 0, &msg) == 8); ASSERT(slre_match("(?i)^.*\\\\.*$", "c:\\Tools", 8, NULL, 0) == 8);
ASSERT(slre_match("\\", "a", 1, NULL, 0, &msg) == 0); ASSERT(slre_match("\\", "a", 1, NULL, 0) == SLRE_INVALID_METACHARACTER);
ASSERT(strcmp(msg, static_error_invalid_metacharacter) == 0); ASSERT(slre_match("\\x", "a", 1, NULL, 0) == SLRE_INVALID_METACHARACTER);
ASSERT(slre_match("\\x", "a", 1, NULL, 0, &msg) == 0); ASSERT(slre_match("\\x1", "a", 1, NULL, 0) == SLRE_INVALID_METACHARACTER);
ASSERT(strcmp(msg, static_error_invalid_metacharacter) == 0); ASSERT(slre_match("\\x20", " ", 1, NULL, 0) == 1);
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);
/* Character sets */ /* Character sets */
ASSERT(slre_match("[abc]", "1c2", 3, NULL, 0, &msg) == 2); ASSERT(slre_match("[abc]", "1c2", 3, NULL, 0) == 2);
ASSERT(slre_match("[abc]", "1C2", 3, NULL, 0, &msg) == 0); ASSERT(slre_match("[abc]", "1C2", 3, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("(?i)[abc]", "1C2", 3, NULL, 0, &msg) == 2); ASSERT(slre_match("(?i)[abc]", "1C2", 3, NULL, 0) == 2);
ASSERT(slre_match("[.2]", "1C2", 3, NULL, 0, &msg) == 1); ASSERT(slre_match("[.2]", "1C2", 3, NULL, 0) == 1);
ASSERT(slre_match("[\\S]+", "ab cd", 5, NULL, 0, &msg) == 2); ASSERT(slre_match("[\\S]+", "ab cd", 5, NULL, 0) == 2);
ASSERT(slre_match("[\\S]+\\s+[tyc]*", "ab cd", 5, NULL, 0, &msg) == 4); ASSERT(slre_match("[\\S]+\\s+[tyc]*", "ab cd", 5, NULL, 0) == 4);
ASSERT(slre_match("[\\d]", "ab cd", 5, NULL, 0, &msg) == 0); ASSERT(slre_match("[\\d]", "ab cd", 5, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("[^\\d]", "ab cd", 5, NULL, 0, &msg) == 1); ASSERT(slre_match("[^\\d]", "ab cd", 5, NULL, 0) == 1);
ASSERT(slre_match("[^\\d]+", "abc123", 6, NULL, 0, &msg) == 3); ASSERT(slre_match("[^\\d]+", "abc123", 6, NULL, 0) == 3);
ASSERT(slre_match("[1-5]+", "123456789", 9, NULL, 0, &msg) == 5); ASSERT(slre_match("[1-5]+", "123456789", 9, NULL, 0) == 5);
ASSERT(slre_match("[1-5a-c]+", "123abcdef", 9, NULL, 0, &msg) == 6); ASSERT(slre_match("[1-5a-c]+", "123abcdef", 9, NULL, 0) == 6);
ASSERT(slre_match("[1-5a-]+", "123abcdef", 9, NULL, 0, &msg) == 4); ASSERT(slre_match("[1-5a-]+", "123abcdef", 9, NULL, 0) == 4);
ASSERT(slre_match("[1-5a-]+", "123a--2oo", 9, NULL, 0, &msg) == 7); ASSERT(slre_match("[1-5a-]+", "123a--2oo", 9, NULL, 0) == 7);
ASSERT(slre_match("[htps]+://", "https://", 8, NULL, 0, &msg) == 8); ASSERT(slre_match("[htps]+://", "https://", 8, NULL, 0) == 8);
ASSERT(slre_match("[^\\s]+", "abc def", 7, NULL, 0, &msg) == 3); ASSERT(slre_match("[^\\s]+", "abc def", 7, NULL, 0) == 3);
ASSERT(slre_match("[^fc]+", "abc def", 7, NULL, 0, &msg) == 2); ASSERT(slre_match("[^fc]+", "abc def", 7, NULL, 0) == 2);
ASSERT(slre_match("[^d\\sf]+", "abc def", 7, NULL, 0, &msg) == 3); ASSERT(slre_match("[^d\\sf]+", "abc def", 7, NULL, 0) == 3);
/* Flags - case sensitivity */ /* Flags - case sensitivity */
ASSERT(slre_match("FO", "foo", 3, NULL, 0, &msg) == 0); ASSERT(slre_match("FO", "foo", 3, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("(?i)FO", "foo", 3, NULL, 0, &msg) == 2); ASSERT(slre_match("(?i)FO", "foo", 3, NULL, 0) == 2);
ASSERT(slre_match("(?m)FO", "foo", 3, NULL, 0, &msg) == 0); ASSERT(slre_match("(?m)FO", "foo", 3, NULL, 0) == SLRE_UNEXPECTED_QUANTIFIER);
ASSERT(slre_match("(?m)x", "foo", 3, NULL, 0, &msg) == 0); ASSERT(slre_match("(?m)x", "foo", 3, NULL, 0) == SLRE_UNEXPECTED_QUANTIFIER);
ASSERT(strcmp(msg, static_error_unexpected_quantifier) == 0);
ASSERT(slre_match("fo", "foo", 3, NULL, 0) == 2);
ASSERT(slre_match("fo", "foo", 3, NULL, 0, &msg) == 2); ASSERT(slre_match(".+", "foo", 3, NULL, 0) == 3);
ASSERT(slre_match(".+", "foo", 3, NULL, 0, &msg) == 3); ASSERT(slre_match(".+k", "fooklmn", 7, NULL, 0) == 4);
ASSERT(slre_match(".+k", "fooklmn", 7, NULL, 0, &msg) == 4); ASSERT(slre_match(".+k.", "fooklmn", 7, NULL, 0) == 5);
ASSERT(slre_match(".+k.", "fooklmn", 7, NULL, 0, &msg) == 5); ASSERT(slre_match("p+", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("p+", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match("ok", "fooklmn", 7, NULL, 0) == 4);
ASSERT(slre_match("ok", "fooklmn", 7, NULL, 0, &msg) == 4); ASSERT(slre_match("lmno", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("lmno", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match("mn.", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("mn.", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match("o", "fooklmn", 7, NULL, 0) == 2);
ASSERT(slre_match("o", "fooklmn", 7, NULL, 0, &msg) == 2); ASSERT(slre_match("^o", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("^o", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match("^", "fooklmn", 7, NULL, 0) == 0);
ASSERT(slre_match("^", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match("n$", "fooklmn", 7, NULL, 0) == 7);
ASSERT(slre_match("n$", "fooklmn", 7, NULL, 0, &msg) == 7); ASSERT(slre_match("n$k", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("n$k", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match("l$", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("l$", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match(".$", "fooklmn", 7, NULL, 0) == 7);
ASSERT(slre_match(".$", "fooklmn", 7, NULL, 0, &msg) == 7); ASSERT(slre_match("a?", "fooklmn", 7, NULL, 0) == 0);
ASSERT(slre_match("a?", "fooklmn", 7, NULL, 0, &msg) == 0);
ASSERT(slre_match("\\_", "abc", 3, NULL, 0) == SLRE_INVALID_METACHARACTER);
ASSERT(slre_match("\\_", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match("+", "fooklmn", 7, NULL, 0) == SLRE_UNEXPECTED_QUANTIFIER);
ASSERT(strcmp(msg, static_error_invalid_metacharacter) == 0); ASSERT(slre_match("()+", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
ASSERT(slre_match("+", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match("\\x", "12", 2, NULL, 0) == SLRE_INVALID_METACHARACTER);
ASSERT(strcmp(msg, static_error_unexpected_quantifier) == 0); ASSERT(slre_match("\\xhi", "12", 2, NULL, 0) == SLRE_INVALID_METACHARACTER);
ASSERT(slre_match("()+", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match("\\x20", "_ J", 3, NULL, 0) == 2);
ASSERT(strcmp(msg, static_error_no_match) == 0); ASSERT(slre_match("\\x4A", "_ J", 3, NULL, 0) == 3);
ASSERT(slre_match("\\x", "12", 2, NULL, 0, &msg) == 0); ASSERT(slre_match("\\d+", "abc123def", 9, NULL, 0) == 6);
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);
/* Balancing brackets */ /* Balancing brackets */
ASSERT(slre_match("(x))", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match("(x))", "fooklmn", 7, NULL, 0) == SLRE_UNBALANCED_BRACKETS);
ASSERT(strcmp(msg, static_error_unbalanced_brackets) == 0); ASSERT(slre_match("(", "fooklmn", 7, NULL, 0) == SLRE_UNBALANCED_BRACKETS);
ASSERT(slre_match("(", "fooklmn", 7, NULL, 0, &msg) == 0);
ASSERT(strcmp(msg, static_error_unbalanced_brackets) == 0);
ASSERT(slre_match("klz?mn", "fooklmn", 7, NULL, 0, &msg) == 7); ASSERT(slre_match("klz?mn", "fooklmn", 7, NULL, 0) == 7);
ASSERT(slre_match("fa?b", "fooklmn", 7, NULL, 0, &msg) == 0); ASSERT(slre_match("fa?b", "fooklmn", 7, NULL, 0) == SLRE_NO_MATCH);
/* Brackets & capturing */ /* Brackets & capturing */
ASSERT(slre_match("^(te)", "tenacity subdues all", 20, caps, 10, &msg) == 2); ASSERT(slre_match("^(te)", "tenacity subdues all", 20, caps, 10) == 2);
ASSERT(slre_match("(bc)", "abcdef", 6, caps, 10, &msg) == 3); ASSERT(slre_match("(bc)", "abcdef", 6, caps, 10) == 3);
ASSERT(slre_match(".(d.)", "abcdef", 6, caps, 10, &msg) == 5); ASSERT(slre_match(".(d.)", "abcdef", 6, caps, 10) == 5);
ASSERT(slre_match(".(d.)\\)?", "abcdef", 6, caps, 10, &msg) == 5); ASSERT(slre_match(".(d.)\\)?", "abcdef", 6, caps, 10) == 5);
ASSERT(caps[0].len == 2); ASSERT(caps[0].len == 2);
ASSERT(memcmp(caps[0].ptr, "de", 2) == 0); ASSERT(memcmp(caps[0].ptr, "de", 2) == 0);
ASSERT(slre_match("(.+)", "123", 3, caps, 10, &msg) == 3); ASSERT(slre_match("(.+)", "123", 3, caps, 10) == 3);
ASSERT(slre_match("(2.+)", "123", 3, caps, 10, &msg) == 3); ASSERT(slre_match("(2.+)", "123", 3, caps, 10) == 3);
ASSERT(caps[0].len == 2); ASSERT(caps[0].len == 2);
ASSERT(memcmp(caps[0].ptr, "23", 2) == 0); 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(caps[0].len == 2);
ASSERT(memcmp(caps[0].ptr, "12", 2) == 0); ASSERT(memcmp(caps[0].ptr, "12", 2) == 0);
ASSERT(slre_match("(.*(2.))", "123", 3, caps, 10, &msg) == 3); ASSERT(slre_match("(.*(2.))", "123", 3, caps, 10) == 3);
ASSERT(slre_match("(.)(.)", "123", 3, caps, 10, &msg) == 2); ASSERT(slre_match("(.)(.)", "123", 3, caps, 10) == 2);
ASSERT(slre_match("(\\d+)\\s+(\\S+)", "12 hi", 5, caps, 10, &msg) == 5); ASSERT(slre_match("(\\d+)\\s+(\\S+)", "12 hi", 5, caps, 10) == 5);
ASSERT(slre_match("ab(cd)+ef", "abcdcdef", 8, NULL, 0, &msg) == 8); ASSERT(slre_match("ab(cd)+ef", "abcdcdef", 8, NULL, 0) == 8);
ASSERT(slre_match("ab(cd)*ef", "abcdcdef", 8, NULL, 0, &msg) == 8); ASSERT(slre_match("ab(cd)*ef", "abcdcdef", 8, NULL, 0) == 8);
ASSERT(slre_match("ab(cd)+?ef", "abcdcdef", 8, NULL, 0, &msg) == 8); ASSERT(slre_match("ab(cd)+?ef", "abcdcdef", 8, NULL, 0) == 8);
ASSERT(slre_match("ab(cd)+?.", "abcdcdef", 8, NULL, 0, &msg) == 5); ASSERT(slre_match("ab(cd)+?.", "abcdcdef", 8, NULL, 0) == 5);
ASSERT(slre_match("ab(cd)?", "abcdcdef", 8, NULL, 0, &msg) == 4); ASSERT(slre_match("ab(cd)?", "abcdcdef", 8, NULL, 0) == 4);
ASSERT(slre_match("a(b)(cd)", "abcdcdef", 8, caps, 1, &msg) == 0); ASSERT(slre_match("a(b)(cd)", "abcdcdef", 8, caps, 1) ==
ASSERT(strcmp(msg, static_error_more_caps) == 0); SLRE_CAPS_ARRAY_TOO_SMALL);
ASSERT(slre_match("(.+/\\d+\\.\\d+)\\.jpg$", "/foo/bar/12.34.jpg", 18, ASSERT(slre_match("(.+/\\d+\\.\\d+)\\.jpg$", "/foo/bar/12.34.jpg", 18,
caps, 1, &msg) == 18); caps, 1) == 18);
/* Greedy vs non-greedy */ /* Greedy vs non-greedy */
ASSERT(slre_match(".+c", "abcabc", 6, NULL, 0, &msg) == 6); ASSERT(slre_match(".+c", "abcabc", 6, NULL, 0) == 6);
ASSERT(slre_match(".+?c", "abcabc", 6, NULL, 0, &msg) == 3); ASSERT(slre_match(".+?c", "abcabc", 6, NULL, 0) == 3);
ASSERT(slre_match(".*?c", "abcabc", 6, NULL, 0, &msg) == 3); ASSERT(slre_match(".*?c", "abcabc", 6, NULL, 0) == 3);
ASSERT(slre_match(".*c", "abcabc", 6, NULL, 0, &msg) == 6); ASSERT(slre_match(".*c", "abcabc", 6, NULL, 0) == 6);
ASSERT(slre_match("bc.d?k?b+", "abcabc", 6, NULL, 0, &msg) == 5); ASSERT(slre_match("bc.d?k?b+", "abcabc", 6, NULL, 0) == 5);
/* Branching */ /* Branching */
ASSERT(slre_match("|", "abc", 3, NULL, 0, &msg) == 0); ASSERT(slre_match("|", "abc", 3, NULL, 0) == 0);
ASSERT(slre_match("|.", "abc", 3, NULL, 0, &msg) == 1); ASSERT(slre_match("|.", "abc", 3, NULL, 0) == 1);
ASSERT(slre_match("x|y|b", "abc", 3, NULL, 0, &msg) == 2); ASSERT(slre_match("x|y|b", "abc", 3, NULL, 0) == 2);
ASSERT(slre_match("k(xx|yy)|ca", "abcabc", 6, NULL, 0, &msg) == 4); ASSERT(slre_match("k(xx|yy)|ca", "abcabc", 6, NULL, 0) == 4);
ASSERT(slre_match("k(xx|yy)|ca|bc", "abcabc", 6, NULL, 0, &msg) == 3); ASSERT(slre_match("k(xx|yy)|ca|bc", "abcabc", 6, NULL, 0) == 3);
ASSERT(slre_match("(|.c)", "abc", 3, caps, 10, &msg) == 3); ASSERT(slre_match("(|.c)", "abc", 3, caps, 10) == 3);
ASSERT(caps[0].len == 2); ASSERT(caps[0].len == 2);
ASSERT(memcmp(caps[0].ptr, "bc", 2) == 0); 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", "a", 1, NULL, 0) == 1);
ASSERT(slre_match("a|b|c", "b", 1, NULL, 0, &msg) == 1); ASSERT(slre_match("a|b|c", "b", 1, NULL, 0) == 1);
ASSERT(slre_match("a|b|c", "c", 1, NULL, 0, &msg) == 1); ASSERT(slre_match("a|b|c", "c", 1, NULL, 0) == 1);
ASSERT(slre_match("a|b|c", "d", 1, NULL, 0, &msg) == 0); ASSERT(slre_match("a|b|c", "d", 1, NULL, 0) == SLRE_NO_MATCH);
/* Optional match at the end of the string */ /* Optional match at the end of the string */
ASSERT(slre_match("^.*c.?$", "abc", 3, NULL, 0, &msg) == 3); ASSERT(slre_match("^.*c.?$", "abc", 3, NULL, 0) == 3);
ASSERT(slre_match("(?i)^.*C.?$", "abc", 3, NULL, 0, &msg) == 3); ASSERT(slre_match("(?i)^.*C.?$", "abc", 3, NULL, 0) == 3);
ASSERT(slre_match("bk?", "ab", 2, NULL, 0, &msg) == 2); ASSERT(slre_match("bk?", "ab", 2, NULL, 0) == 2);
ASSERT(slre_match("b(k?)", "ab", 2, NULL, 0, &msg) == 2); ASSERT(slre_match("b(k?)", "ab", 2, NULL, 0) == 2);
ASSERT(slre_match("b[k-z]*", "ab", 2, NULL, 0, &msg) == 2); ASSERT(slre_match("b[k-z]*", "ab", 2, NULL, 0) == 2);
ASSERT(slre_match("ab(k|z|y)*", "ab", 2, NULL, 0, &msg) == 2); ASSERT(slre_match("ab(k|z|y)*", "ab", 2, NULL, 0) == 2);
ASSERT(slre_match("[b-z].*", "ab", 2, NULL, 0, &msg) == 2); ASSERT(slre_match("[b-z].*", "ab", 2, NULL, 0) == 2);
ASSERT(slre_match("(b|z|u).*", "ab", 2, NULL, 0, &msg) == 2); ASSERT(slre_match("(b|z|u).*", "ab", 2, NULL, 0) == 2);
ASSERT(slre_match("ab(k|z|y)?", "ab", 2, NULL, 0, &msg) == 2); ASSERT(slre_match("ab(k|z|y)?", "ab", 2, NULL, 0) == 2);
ASSERT(slre_match(".*", "ab", 2, NULL, 0, &msg) == 2); ASSERT(slre_match(".*", "ab", 2, NULL, 0) == 2);
ASSERT(slre_match(".*$", "ab", 2, NULL, 0, &msg) == 2); ASSERT(slre_match(".*$", "ab", 2, NULL, 0) == 2);
ASSERT(slre_match("a+$", "aa", 2, NULL, 0, &msg) == 2); ASSERT(slre_match("a+$", "aa", 2, NULL, 0) == 2);
ASSERT(slre_match("a*$", "aa", 2, NULL, 0, &msg) == 2); ASSERT(slre_match("a*$", "aa", 2, NULL, 0) == 2);
ASSERT(slre_match( "a+$" ,"Xaa", 3, NULL, 0, &msg) == 3); ASSERT(slre_match( "a+$" ,"Xaa", 3, NULL, 0) == 3);
ASSERT(slre_match( "a*$" ,"Xaa", 3, NULL, 0, &msg) == 3); ASSERT(slre_match( "a*$" ,"Xaa", 3, NULL, 0) == 3);
ASSERT(msg[0] == '\0'); ASSERT(msg[0] == '\0');
{ {
/* Example: HTTP request */ /* 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]; struct slre_cap caps[4];
if (slre_match("^\\s*(\\S+)\\s+(\\S+)\\s+HTTP/(\\d)\\.(\\d)", 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", printf("Method: [%.*s], URI: [%.*s]\n",
caps[0].len, caps[0].ptr, caps[0].len, caps[0].ptr,
caps[1].len, caps[1].ptr); caps[1].len, caps[1].ptr);
} else { } else {
printf("Error parsing [%s]: [%s]\n", request, error_msg); printf("Error parsing [%s]\n", request);
} }
ASSERT(caps[1].len == 11); ASSERT(caps[1].len == 11);
@ -682,7 +650,7 @@ int main(void) {
int i, j = 0, str_len = strlen(str); int i, j = 0, str_len = strlen(str);
while (j < str_len && 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); printf("Found URL: [%.*s]\n", caps[0].len, caps[0].ptr);
j += i; j += i;
} }

@ -35,7 +35,18 @@ struct slre_cap {
}; };
int slre_match(const char *regexp, const char *buf, int buf_len, 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 #ifdef __cplusplus
} }

Loading…
Cancel
Save