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 1 commit
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
12 changes: 12 additions & 0 deletions re.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this part but in my understanding one don't have to check defined(RE_ENABLE_MULTI_PATTERNS) every time, just a single check in re.h (#ifndef ... #define ... #endif) seems to be enough and it's already there. At the same time I see that this construction is common for both this and other repos of the author, so your change is consistent with the other code. It's interesting what @kokke thinks about such checks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emmmm , fix it

re_t re_p; /* pointer to (to be created) copy of compiled regex */
int ret = -1;

Expand All @@ -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)
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re_p is already an instance of re_t, why would anyone need this cast? I'm also unsure about the similar cast below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes ,delete it.

#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)
Expand All @@ -259,6 +270,7 @@ void re_freecompile(re_t pattern)
pattern = NULL;
}
}
#endif

void re_print(regex_t* pattern)
{
Expand Down
9 changes: 8 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 @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to avoid mixing functional and style changes in one commit. This change also breaks style consistency with the rest of the code, doesn't it? Patches should preserve existing code style even if it's not your favorite one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, fix it.



#ifdef __cplusplus
Expand Down