diff --git a/re.c b/re.c index 9d7a783..b6f30cf 100644 --- a/re.c +++ b/re.c @@ -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) @@ -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 */ @@ -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" }; diff --git a/re.h b/re.h index b120f21..e5bf2c5 100644 --- a/re.h +++ b/re.h @@ -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 @@ -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);