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

Cleanup more (?:) from patterns #196

Merged
merged 9 commits into from
Jan 22, 2018

Conversation

josephfrazier
Copy link
Collaborator

@josephfrazier josephfrazier commented May 1, 2017

Following up on #164, this
change prevents a (?:) from being inserted in the following places:

  • At the beginning of a non-capturing group or (negated) lookahead (the end is already handled)
  • Before or after a |
  • At the beginning or the end of the pattern

This solution isn't as complete as the one suggested in
#179, but it's a decent
stopgap.

Following up on slevithan#164, this
change prevents a `(?:)` from being inserted in the following places:

* At the beginning of a non-capturing group (the end is already handled)
* Before or after a `|`
* At the beginning or the end of the pattern

This solution isn't as complete as the one suggested in
slevithan#179, but it's a decent
stopgap.
@josephfrazier
Copy link
Collaborator Author

Any thoughts on this one, @slevithan? I'd like to land it if there's no objections.

src/xregexp.js Outdated
// No need to separate tokens if at the beginning of a non-capturing group
match.input.slice(match.index - 3, 3) === '(?:' ||
// No need to separate tokens if at the beginning of a non-capturing group or lookahead
match.input.slice(match.index - 3, 3).match(/\(\?[:=!]/) ||
Copy link
Owner

Choose a reason for hiding this comment

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

What happens if match.index - 3 is e.g. -1? Even if this works, it's not precise in its meaning so might be better to clarify.

Also, this should be using nativ.match or preferably nativ.test (nativ.test.call(regex, str)), at least as long as those still exist. XRegExp never uses native regex methods directly since (until v4.0.0) the native methods could be overridden. As a minor optimization, the regex should start with ^ so it isn't tested at all of the string's character positions if it fails to match.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for pointing this out, I switched to nativ.test in 2bdd982. Also, it turns out that slice was the wrong method to use, so I switched to substr in 75c8402, and documented its behavior with negative indices in fd9940b.

src/xregexp.js Outdated
match.input[match.index - 1] === '|' ||
match.input[match.index + match[0].length] === '|' ||
// No need to separate tokens if at the beginning or end of the pattern
match.input[match.index - 1] === undefined ||
Copy link
Owner

Choose a reason for hiding this comment

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

Can you just check the value of match.index - 1 directly, without involving match.input?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point, fixed in ee766c2 and 222eda9

src/xregexp.js Outdated
match.input[match.index + match[0].length] === '|' ||
// No need to separate tokens if at the beginning or end of the pattern
match.input[match.index - 1] === undefined ||
match.input[match.index + match[0].length] === undefined ||
Copy link
Owner

Choose a reason for hiding this comment

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

Ditto. Should probably be comparing to the length of match.input rather than referencing a specific character in the string.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also fixed in ee766c2

@slevithan slevithan merged commit 7d47baa into slevithan:master Jan 22, 2018
@josephfrazier josephfrazier deleted the strip-more-useless-groups branch January 22, 2018 05:19
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.

None yet

2 participants