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

Conversation

skyformat99
Copy link

@skyformat99 skyformat99 commented Aug 14, 2020

Support for multiple patterns

When multiple patterns are used simultaneously, the existing code will fail,
the PR fix it. example:

int match_length;
const char* string_to_search = "ahem.. 'hello world !' ..";

re_t p1 = re_compile("[Hh]ello [Ww]orld\s*[!]?");
re_t p2 = re_compile("hello[0-9]");
re_t p3 = re_compile("fadsf*");

int match_idx;
match_idx = re_matchp(p1, string_to_search, &match_length);
match_idx = re_matchp(p2, string_to_search, &match_length);
match_idx = re_matchp(p3, string_to_search, &match_length);

re_freecompile(p1);
re_freecompile(p2);
re_freecompile(p3);

@Nable80
Copy link

Nable80 commented Aug 14, 2020

Hi!
Thank you for your interest in this library!
I have to say that your patch uses dynamic memory allocation (calloc/free), while this library is mostly targeted at embedded usage where it's important to avoid anything that isn't predictable at compilation time.
It's explicitly mentioned in README: "No use of dynamic memory allocation (i.e. no calls to malloc / free)".

I think it's still possible to add optional support for dynamic allocation that is configured by preprocessor definitions (#if/#ifdef...#endif) but it definitely shouldn't break existing use-cases and default behavior. Do you agree with this idea?

@skyformat99
Copy link
Author

thanks, you are right.
Added preprocessor definition (# if / # IFDEF...# endif).

@Nable80
Copy link

Nable80 commented Aug 14, 2020

Let's wait for some reply from the repo owner, I'm just a regular user here after all.

I'll try to add a code review for your changes anyway, just in hope anyone finds it useful.

re.c Outdated
@@ -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.c Outdated
@@ -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.

re.h Outdated
Comment on lines 59 to 66
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.

@skyformat99
Copy link
Author

thanks,
All fixed, ^_^

@Nable80
Copy link

Nable80 commented Aug 14, 2020

Btw, you can also squash commits and re-push to avoid unnecessary intermediate changes. Probably it's even possible to do it via Github UI, I'm not sure (command-line tools are good enough for me).

I should also say that it's better to wait for repo owner's reply, his opinion about some questionable parts may be different. I.e. you don't have to change things immediately after getting the first reply, sometimes it's better to wait for more comments and only then decide what to do next.

@Nable80
Copy link

Nable80 commented Aug 21, 2020

@kokke
What do you think about merging this feature as an optional extension?

@marler8997
Copy link

I think my PR might be an alternative to this: #58

@skyformat99
Copy link
Author

I think my PR might be an alternative to this: #58

good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants