mirror of
https://github.com/cesanta/slre.git
synced 2024-11-24 11:25:30 +00:00
Added \n,\r,\t,\f,\v,\b metacharacters
This commit is contained in:
parent
c2b1fc15b2
commit
901b739932
25
slre.c
25
slre.c
@ -74,7 +74,7 @@ struct regex_info {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static int is_metacharacter(const unsigned char *s) {
|
static int is_metacharacter(const unsigned char *s) {
|
||||||
static const char *metacharacters = "^$().[]*+?|\\Ssd";
|
static const char *metacharacters = "^$().[]*+?|\\Ssdbfnrtv";
|
||||||
return strchr(metacharacters, *s) != NULL;
|
return strchr(metacharacters, *s) != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,20 +115,15 @@ static int match_op(const unsigned char *re, const unsigned char *s,
|
|||||||
case '\\':
|
case '\\':
|
||||||
/* Metacharacters */
|
/* Metacharacters */
|
||||||
switch (re[1]) {
|
switch (re[1]) {
|
||||||
case 'S':
|
case 'S': FAIL_IF(isspace(*s), SLRE_NO_MATCH); result++; break;
|
||||||
FAIL_IF(isspace(*s), SLRE_NO_MATCH);
|
case 's': FAIL_IF(!isspace(*s), SLRE_NO_MATCH); result++; break;
|
||||||
result++;
|
case 'd': FAIL_IF(!isdigit(*s), SLRE_NO_MATCH); result++; break;
|
||||||
break;
|
case 'b': FAIL_IF(*s != '\b', SLRE_NO_MATCH); result++; break;
|
||||||
|
case 'f': FAIL_IF(*s != '\f', SLRE_NO_MATCH); result++; break;
|
||||||
case 's':
|
case 'n': FAIL_IF(*s != '\n', SLRE_NO_MATCH); result++; break;
|
||||||
FAIL_IF(!isspace(*s), SLRE_NO_MATCH);
|
case 'r': FAIL_IF(*s != '\r', SLRE_NO_MATCH); result++; break;
|
||||||
result++;
|
case 't': FAIL_IF(*s != '\t', SLRE_NO_MATCH); result++; break;
|
||||||
break;
|
case 'v': FAIL_IF(*s != '\v', SLRE_NO_MATCH); result++; break;
|
||||||
|
|
||||||
case 'd':
|
|
||||||
FAIL_IF(!isdigit(*s), SLRE_NO_MATCH);
|
|
||||||
result++;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'x':
|
case 'x':
|
||||||
/* Match byte, \xHH where HH is hexadecimal byte representaion */
|
/* Match byte, \xHH where HH is hexadecimal byte representaion */
|
||||||
|
@ -171,6 +171,9 @@ int main(void) {
|
|||||||
ASSERT(slre_match("(ab|cd).*\\.(xx|yy)", "ab.yy", 5, NULL, 0, 0) == 5);
|
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(".*a", "abcdef", 6, NULL, 0, 0) == 1);
|
||||||
ASSERT(slre_match("(.+)c", "abcdef", 6, NULL, 0, 0) == 3);
|
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 */
|
/* Greedy vs non-greedy */
|
||||||
ASSERT(slre_match(".+c", "abcabc", 6, NULL, 0, 0) == 6);
|
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'\"<>]*)";
|
static const char *regex = "(?i)((https?://)[^\\s/'\"<>]+/?[^\\s'\"<>]*)";
|
||||||
struct slre_cap caps[2];
|
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 &&
|
while (j < str_len &&
|
||||||
(i = slre_match(regex, str + j, str_len - j, caps, 2, 0)) > 0) {
|
(i = slre_match(regex, str + j, str_len - j, caps, 2, 0)) > 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user