Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support metaword '\n' #39

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions re.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#define MAX_CHAR_CLASS_LEN 40 /* Max length of character-class buffer in. */


enum { UNUSED, DOT, BEGIN, END, QUESTIONMARK, STAR, PLUS, CHAR, CHAR_CLASS, INV_CHAR_CLASS, DIGIT, NOT_DIGIT, ALPHA, NOT_ALPHA, WHITESPACE, NOT_WHITESPACE, /* BRANCH */ };
enum { UNUSED, DOT, BEGIN, END, QUESTIONMARK, STAR, PLUS, CHAR, CHAR_CLASS, INV_CHAR_CLASS, DIGIT, NOT_DIGIT, ALPHA, NOT_ALPHA, WHITESPACE, NOT_WHITESPACE, LINE_BREAK, /* BRANCH */ };

typedef struct regex_t
{
Expand Down Expand Up @@ -149,6 +149,7 @@ re_t re_compile(const char* pattern)
case 'W': { re_compiled[j].type = NOT_ALPHA; } break;
case 's': { re_compiled[j].type = WHITESPACE; } break;
case 'S': { re_compiled[j].type = NOT_WHITESPACE; } break;
case 'n': { re_compiled[j].type = LINE_BREAK; } break;

/* Escaped character, e.g. '.' or '$' */
default:
Expand Down Expand Up @@ -234,7 +235,7 @@ re_t re_compile(const char* pattern)

void re_print(regex_t* pattern)
{
const char* types[] = { "UNUSED", "DOT", "BEGIN", "END", "QUESTIONMARK", "STAR", "PLUS", "CHAR", "CHAR_CLASS", "INV_CHAR_CLASS", "DIGIT", "NOT_DIGIT", "ALPHA", "NOT_ALPHA", "WHITESPACE", "NOT_WHITESPACE", "BRANCH" };
const char* types[] = { "UNUSED", "DOT", "BEGIN", "END", "QUESTIONMARK", "STAR", "PLUS", "CHAR", "CHAR_CLASS", "INV_CHAR_CLASS", "DIGIT", "NOT_DIGIT", "ALPHA", "NOT_ALPHA", "WHITESPACE", "NOT_WHITESPACE", "LINE_BREAK", "BRANCH" };

int i;
int j;
Expand Down Expand Up @@ -284,6 +285,10 @@ static int matchwhitespace(char c)
{
return ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r') || (c == '\f') || (c == '\v'));
}
static int matchlinebreak(char c)
{
return c == '\n';
}
static int matchalphanum(char c)
{
return ((c == '_') || matchalpha(c) || matchdigit(c));
Expand All @@ -296,7 +301,7 @@ static int matchrange(char c, const char* str)
}
static int ismetachar(char c)
{
return ((c == 's') || (c == 'S') || (c == 'w') || (c == 'W') || (c == 'd') || (c == 'D'));
return ((c == 's') || (c == 'S') || (c == 'w') || (c == 'W') || (c == 'd') || (c == 'D') || (c == 'n'));
}

static int matchmetachar(char c, const char* str)
Expand All @@ -309,6 +314,7 @@ static int matchmetachar(char c, const char* str)
case 'W': return !matchalphanum(c);
case 's': return matchwhitespace(c);
case 'S': return !matchwhitespace(c);
case 'n': return matchlinebreak(c);
default: return (c == str[0]);
}
}
Expand All @@ -328,7 +334,7 @@ static int matchcharclass(char c, const char* str)
if (matchmetachar(c, str))
{
return 1;
}
}
else if ((c == str[0]) && !ismetachar(c))
{
return 1;
Expand Down Expand Up @@ -364,6 +370,7 @@ static int matchone(regex_t p, char c)
case NOT_ALPHA: return !matchalphanum(c);
case WHITESPACE: return matchwhitespace(c);
case NOT_WHITESPACE: return !matchwhitespace(c);
case LINE_BREAK: return matchlinebreak(c);
default: return (p.ch == c);
}
}
Expand Down
3 changes: 3 additions & 0 deletions re.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ int re_matchp(re_t pattern, const char* text, int* matchlenght);
int re_match(const char* pattern, const char* text, int* matchlenght);


void re_print(struct regex_t* pattern);


#ifdef __cplusplus
}
#endif