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

Increase regex readability #391

Merged
merged 4 commits into from
Feb 6, 2025
Merged

Conversation

Vampire
Copy link
Contributor

@Vampire Vampire commented Feb 2, 2025

No description provided.

@Vampire
Copy link
Contributor Author

Vampire commented Feb 2, 2025

This PR makes all regex patterns that contain escapes raw String literals, so that you do not have to mentally double unescape to understand the pattern.

It also removes unnecessary start- and end-of-input anchors for regex that are anyway only used to match entire inputs.
If the anchors should stay, they should at least be changed to always be used, currently in most cases it was like ^foo|bar|baz$ instead of ^(?:foo|bar|baz)$, so only matching start-of-input for foo and end-of-input for baz.

@Vampire Vampire force-pushed the regex-readability branch 2 times, most recently from c5c22ca to 0656820 Compare February 3, 2025 02:16
Copy link
Collaborator

@OptimumCode OptimumCode left a comment

Choose a reason for hiding this comment

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

Hi, @Vampire. Thank you very much for the contribution

If the anchors should stay, they should at least be changed to always be used, currently in most cases it was like ^foo|bar|baz$ instead of ^(?:foo|bar|baz)$

I think it would be better to correct the patterns so the anchors are used correctly - just a precaution in case an incorrect method on Regex is used (that does not match the entire string against the pattern).

@Vampire
Copy link
Contributor Author

Vampire commented Feb 3, 2025

I think it would be better to correct the patterns so the anchors are used correctly - just a precaution in case an incorrect method on Regex is used (that does not match the entire string against the pattern).

Sure, I hope I didn't break it now. :-D

@Vampire Vampire requested a review from OptimumCode February 4, 2025 10:21
OptimumCode
OptimumCode previously approved these changes Feb 4, 2025
Copy link
Collaborator

@OptimumCode OptimumCode left a comment

Choose a reason for hiding this comment

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

All looks good to me, thank you @Vampire. @krzema12 have you had a chance to take a look at the PR? Would you have any suggestions/objections?

@krzema12
Copy link
Owner

krzema12 commented Feb 4, 2025

Generally looks sane, but I'm worried a bit about compatibility of the regexes across all targets. AFAIK, there are some minor discrepancies, and our tests aren't fully multiplatform yet (even if they are, not sure if coverage is good enough).

What I propose to do is to go to the modified file where we use something suspicious, like the non-capturing groups, and checking if the tests exercising the regexes are KMP already. If not, I'd recommend porting them first.

@Vampire
Copy link
Contributor Author

Vampire commented Feb 4, 2025

Did you mistype and mean you are worried?
Because you said you are not.
Besides that, there were already non-capturing groups used in other regexes.
They are supported in JVM and JS.
On Native and WASM I'm not sure which flavor is used as the docs just say "this can be altered in the future to match the JVM behavior" so assuming it should be fine there too.

@Vampire
Copy link
Contributor Author

Vampire commented Feb 4, 2025

From a quick look I'd say Native and WASM is an own Regex implementation also supporting non-capturing groups.
And I guess the intention is to be consistent with JVM implementation and if not fix that, hence the hint that it might change into that direction.

@krzema12
Copy link
Owner

krzema12 commented Feb 4, 2025

Did you mistype and mean you are worried? Because you said you are not.

Yes,sorry!

Yeah, things should probably work fine, although as a rule of thumb, if we touch some code, I'd prefer to have coverage for all targets. I can migrate the two tests tomorrow (they're pretty simple), and this way we'll increase confidence regarding correctness of this change.

@krzema12 krzema12 enabled auto-merge (squash) February 6, 2025 11:22
@krzema12 krzema12 merged commit b28044a into krzema12:main Feb 6, 2025
12 checks passed
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