From 901b739932efdcc260e1513b4dcf20bbe2d2525e Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Mon, 25 Aug 2014 10:45:01 +0100 Subject: [PATCH] Added \n,\r,\t,\f,\v,\b metacharacters --- slre.c | 25 ++++++++++--------------- unit_test.c | 5 ++++- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/slre.c b/slre.c index 82752f3..4a7fd89 100644 --- a/slre.c +++ b/slre.c @@ -74,7 +74,7 @@ struct regex_info { }; static int is_metacharacter(const unsigned char *s) { - static const char *metacharacters = "^$().[]*+?|\\Ssd"; + static const char *metacharacters = "^$().[]*+?|\\Ssdbfnrtv"; return strchr(metacharacters, *s) != NULL; } @@ -115,20 +115,15 @@ static int match_op(const unsigned char *re, const unsigned char *s, case '\\': /* Metacharacters */ switch (re[1]) { - case 'S': - FAIL_IF(isspace(*s), SLRE_NO_MATCH); - result++; - break; - - case 's': - FAIL_IF(!isspace(*s), SLRE_NO_MATCH); - result++; - break; - - case 'd': - FAIL_IF(!isdigit(*s), SLRE_NO_MATCH); - result++; - break; + case 'S': FAIL_IF(isspace(*s), SLRE_NO_MATCH); result++; break; + case 's': FAIL_IF(!isspace(*s), SLRE_NO_MATCH); result++; break; + case 'd': FAIL_IF(!isdigit(*s), SLRE_NO_MATCH); result++; break; + case 'b': FAIL_IF(*s != '\b', SLRE_NO_MATCH); result++; break; + case 'f': FAIL_IF(*s != '\f', SLRE_NO_MATCH); result++; break; + case 'n': FAIL_IF(*s != '\n', SLRE_NO_MATCH); result++; break; + case 'r': FAIL_IF(*s != '\r', SLRE_NO_MATCH); result++; break; + case 't': FAIL_IF(*s != '\t', SLRE_NO_MATCH); result++; break; + case 'v': FAIL_IF(*s != '\v', SLRE_NO_MATCH); result++; break; case 'x': /* Match byte, \xHH where HH is hexadecimal byte representaion */ diff --git a/unit_test.c b/unit_test.c index 7fb2eab..edc6a0d 100644 --- a/unit_test.c +++ b/unit_test.c @@ -171,6 +171,9 @@ int main(void) { 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); + ASSERT(slre_match("\\n", "abc\ndef", 7, NULL, 0, 0) == 4); + ASSERT(slre_match("b.\\s*\\n", "aa\r\nbb\r\ncc\r\n\r\n", 14, + caps, 10, 0) == 8); /* Greedy vs non-greedy */ ASSERT(slre_match(".+c", "abcabc", 6, NULL, 0, 0) == 6); @@ -246,7 +249,7 @@ int main(void) { static const char *regex = "(?i)((https?://)[^\\s/'\"<>]+/?[^\\s'\"<>]*)"; struct slre_cap caps[2]; - int i, j = 0, str_len = strlen(str); + int i, j = 0, str_len = (int) strlen(str); while (j < str_len && (i = slre_match(regex, str + j, str_len - j, caps, 2, 0)) > 0) {