Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@nathanrpage97
Copy link

Allow named groups flag "n" to be available

@kmarius
Copy link
Owner

kmarius commented Aug 4, 2022

I think it would be a good idea to also store a copy of the named group at the corresponding index. With your current solution, ipairs won't work and pairs doesn't respect insertion order. Then again, keeping named groups in the same table seems a bit awkward. It seems in javascript, indexed groups are accessed by indexing the match result (i.e. stored in the top level "table") and the named groups accessed via the groups field.

@nathanrpage97
Copy link
Author

How about a separate named_groups table is created and the results are duplicated?

jsregexp.c Outdated
}
lua_setfield(lstate, -2, "groups");

Copy link
Owner

Choose a reason for hiding this comment

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

whitespace


const char* group_names = NULL;
if (named_groups) {
group_names = lre_get_groupnames(r->bc);
Copy link
Owner

Choose a reason for hiding this comment

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

I think we can just push the table for named groups here so we don't have to pop it later. Need to switch the stack indexes -2 and -3 in the loop, of course.

Copy link
Author

@nathanrpage97 nathanrpage97 Aug 5, 2022

Choose a reason for hiding this comment

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

Uhh I’m not quite sure how to do this without having some sort of ternarys to specify stack position based on if named groups is true

Copy link
Owner

Choose a reason for hiding this comment

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

I should have made this clearer. If the table of the normal indexed matches is on top, one can always access it with rawseti(lstate, -2, ...). If named_groups is true, have the table of named groups below that so it is accessed with setfield(lstate, -3, ...). Just make sure you push a second table when named_groups is true.

Copy link
Author

Choose a reason for hiding this comment

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

I have updated it to work somewhat as you describe. I'm pretty sure you have to do some branching for stack positions as is done here

@kmarius kmarius mentioned this pull request Aug 16, 2022
4 tasks
@kmarius kmarius marked this pull request as ready for review August 16, 2022 16:11
@kmarius kmarius merged commit ec72ef4 into kmarius:master Aug 16, 2022
@kmarius
Copy link
Owner

kmarius commented Aug 16, 2022

I went ahead and merged because I thought it would be easier merging before #5. Thanks for the contribution!

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.

2 participants