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 for multiple patterns #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 32 additions & 1 deletion re.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,18 @@ static int ismetachar(char c);
/* Public functions: */
int re_match(const char* pattern, const char* text, int* matchlength)
{
#if (RE_ENABLE_MULTI_PATTERNS == 1)
re_t re_p; /* pointer to (to be created) copy of compiled regex */
int ret = -1;

re_p = re_compile(pattern);
ret = re_matchp(re_p, text, matchlength);
free(re_p);

return (ret);
#else
return re_matchp(re_compile(pattern), text, matchlength);
#endif
}

int re_matchp(re_t pattern, const char* text, int* matchlength)
Expand Down Expand Up @@ -113,6 +124,9 @@ re_t re_compile(const char* pattern)
static regex_t re_compiled[MAX_REGEXP_OBJECTS];
static unsigned char ccl_buf[MAX_CHAR_CLASS_LEN];
int ccl_bufidx = 1;
#if (RE_ENABLE_MULTI_PATTERNS == 1)
re_t re_p; /* pointer to (to be created) copy of compiled regex in re_compiled */
#endif

char c; /* current char in pattern */
int i = 0; /* index into pattern */
Expand Down Expand Up @@ -238,9 +252,26 @@ re_t re_compile(const char* pattern)
/* 'UNUSED' is a sentinel used to indicate end-of-pattern */
re_compiled[j].type = UNUSED;

return (re_t) re_compiled;
#if (RE_ENABLE_MULTI_PATTERNS == 1)
re_p = (re_t)calloc(1, sizeof(re_compiled));
memcpy(re_p, re_compiled, sizeof(re_compiled));
return re_p;
#else
return (re_t)re_compiled;
#endif
}

#if (RE_ENABLE_MULTI_PATTERNS == 1)
void re_freecompile(re_t pattern)
{
if (pattern)
{
free(pattern);
pattern = NULL;
}
}
#endif

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" };
Expand Down
10 changes: 9 additions & 1 deletion re.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
#define RE_DOT_MATCHES_NEWLINE 1
#endif

/* Define to Support for multiple patterns */
#ifndef RE_ENABLE_MULTI_PATTERNS
#define RE_ENABLE_MULTI_PATTERNS 0
#endif

#ifdef __cplusplus
extern "C"{
#endif
Expand All @@ -49,10 +54,13 @@ typedef struct regex_t* re_t;
/* Compile regex string pattern to a regex_t-array. */
re_t re_compile(const char* pattern);


/* Find matches of the compiled pattern inside text. */
int re_matchp(re_t pattern, const char* text, int* matchlenght);

/* Free memory of the compiled pattern */
#if (RE_ENABLE_MULTI_PATTERNS == 1)
void re_freecompile(re_t pattern);
#endif

/* Find matches of the txt pattern inside text (will compile automatically first). */
int re_match(const char* pattern, const char* text, int* matchlenght);
Expand Down