regex: fix fast path for -w/--word-regexp flag #2576
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It turns out our fast path for -w/--word-regexp wasn't quite correct in some cases. Namely, we use
(?m:^|\W)(<original-regex>)(?m:\W|$)
as the implementation of -w/--word-regexp since\b(<original-regex>)\b
has some unintuitive results in certain cases, specifically when matches non-word characters at match boundaries.The problem is that using this formulation means that you need to extract the capture group around to find the "real" match, since the surrounding (^|\W) and (\W|$) aren't part of the match. This is fine, but the capture group engine is usually slow, so we have a fast path where we try to deduce the correct match boundary after an initial match (before running capture groups). The problem is that doing this is rather tricky because it's hard to know, in general, whether the
^
or the\W
matched.This still doesn't seem quite right overall, but we at least fix one more case.
Fixes #2574