From 2b5aaab8ae24347ab5b5c06a8840f17d0cbfc99b Mon Sep 17 00:00:00 2001 From: ygg Date: Fri, 14 Aug 2020 11:19:26 +0800 Subject: [PATCH 1/3] Support for multiple patterns --- re.c | 23 +++++++++++++++++++++-- re.h | 3 ++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/re.c b/re.c index 9d7a783..25482f5 100644 --- a/re.c +++ b/re.c @@ -71,7 +71,14 @@ static int ismetachar(char c); /* Public functions: */ int re_match(const char* pattern, const char* text, int* matchlength) { - return re_matchp(re_compile(pattern), text, matchlength); + 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); } int re_matchp(re_t pattern, const char* text, int* matchlength) @@ -113,6 +120,7 @@ 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; + re_t re_p; /* pointer to (to be created) copy of compiled regex in re_compiled */ char c; /* current char in pattern */ int i = 0; /* index into pattern */ @@ -238,7 +246,18 @@ 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; + re_p = (re_t)calloc(1, sizeof(re_compiled)); + memcpy(re_p, re_compiled, sizeof(re_compiled)); + return (re_t)re_p; +} + +void re_freecompile(re_t pattern) +{ + if (pattern) + { + free(pattern); + pattern = NULL; + } } void re_print(regex_t* pattern) diff --git a/re.h b/re.h index b120f21..e19da18 100644 --- a/re.h +++ b/re.h @@ -49,10 +49,11 @@ 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 */ +void re_freecompile(re_t pattern); /* Find matches of the txt pattern inside text (will compile automatically first). */ int re_match(const char* pattern, const char* text, int* matchlenght); From b5be87fe1c0eec81a414ba8ffeb9d4588d42f39a Mon Sep 17 00:00:00 2001 From: ygg Date: Fri, 14 Aug 2020 13:18:14 +0800 Subject: [PATCH 2/3] preprocessor definitions (#if/#ifdef...#endif) Support for multiple patterns --- re.c | 12 ++++++++++++ re.h | 9 ++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/re.c b/re.c index 25482f5..0e9fcfd 100644 --- a/re.c +++ b/re.c @@ -71,6 +71,7 @@ static int ismetachar(char c); /* Public functions: */ int re_match(const char* pattern, const char* text, int* matchlength) { +#if defined(RE_ENABLE_MULTI_PATTERNS) && (RE_ENABLE_MULTI_PATTERNS == 1) re_t re_p; /* pointer to (to be created) copy of compiled regex */ int ret = -1; @@ -79,6 +80,9 @@ int re_match(const char* pattern, const char* text, int* 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) @@ -120,7 +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 defined(RE_ENABLE_MULTI_PATTERNS) && (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 */ @@ -246,11 +252,16 @@ re_t re_compile(const char* pattern) /* 'UNUSED' is a sentinel used to indicate end-of-pattern */ re_compiled[j].type = UNUSED; +#if defined(RE_ENABLE_MULTI_PATTERNS) && (RE_ENABLE_MULTI_PATTERNS == 1) re_p = (re_t)calloc(1, sizeof(re_compiled)); memcpy(re_p, re_compiled, sizeof(re_compiled)); return (re_t)re_p; +#else + return (re_t)re_compiled; +#endif } +#if defined(RE_ENABLE_MULTI_PATTERNS) && (RE_ENABLE_MULTI_PATTERNS == 1) void re_freecompile(re_t pattern) { if (pattern) @@ -259,6 +270,7 @@ void re_freecompile(re_t pattern) pattern = NULL; } } +#endif void re_print(regex_t* pattern) { diff --git a/re.h b/re.h index e19da18..f6a59a3 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 @@ -53,10 +58,12 @@ re_t re_compile(const char* pattern); int re_matchp(re_t pattern, const char* text, int* matchlenght); /* Free memory of the compiled pattern */ +#if defined(RE_ENABLE_MULTI_PATTERNS) && (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); +int re_match(const char *pattern, const char *text, int *matchlenght); #ifdef __cplusplus From 4402b35f7872749b098820b6f17922103bd04704 Mon Sep 17 00:00:00 2001 From: ygg Date: Fri, 14 Aug 2020 15:39:19 +0800 Subject: [PATCH 3/3] fix #if defined(RE_ENABLE_MULTI_PATTERNS) && (RE_ENABLE_MULTI_PATTERNS == 1) --- re.c | 10 +++++----- re.h | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/re.c b/re.c index 0e9fcfd..b6f30cf 100644 --- a/re.c +++ b/re.c @@ -71,7 +71,7 @@ static int ismetachar(char c); /* Public functions: */ int re_match(const char* pattern, const char* text, int* matchlength) { -#if defined(RE_ENABLE_MULTI_PATTERNS) && (RE_ENABLE_MULTI_PATTERNS == 1) +#if (RE_ENABLE_MULTI_PATTERNS == 1) re_t re_p; /* pointer to (to be created) copy of compiled regex */ int ret = -1; @@ -124,7 +124,7 @@ 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 defined(RE_ENABLE_MULTI_PATTERNS) && (RE_ENABLE_MULTI_PATTERNS == 1) +#if (RE_ENABLE_MULTI_PATTERNS == 1) re_t re_p; /* pointer to (to be created) copy of compiled regex in re_compiled */ #endif @@ -252,16 +252,16 @@ re_t re_compile(const char* pattern) /* 'UNUSED' is a sentinel used to indicate end-of-pattern */ re_compiled[j].type = UNUSED; -#if defined(RE_ENABLE_MULTI_PATTERNS) && (RE_ENABLE_MULTI_PATTERNS == 1) +#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_t)re_p; + return re_p; #else return (re_t)re_compiled; #endif } -#if defined(RE_ENABLE_MULTI_PATTERNS) && (RE_ENABLE_MULTI_PATTERNS == 1) +#if (RE_ENABLE_MULTI_PATTERNS == 1) void re_freecompile(re_t pattern) { if (pattern) diff --git a/re.h b/re.h index f6a59a3..e5bf2c5 100644 --- a/re.h +++ b/re.h @@ -58,12 +58,12 @@ re_t re_compile(const char* pattern); int re_matchp(re_t pattern, const char* text, int* matchlenght); /* Free memory of the compiled pattern */ -#if defined(RE_ENABLE_MULTI_PATTERNS) && (RE_ENABLE_MULTI_PATTERNS == 1) +#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); +int re_match(const char* pattern, const char* text, int* matchlenght); #ifdef __cplusplus