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

regex: fix fast path for -w/--word-regexp flag #2576

Merged
merged 1 commit into from
Jul 31, 2023
Merged

Conversation

BurntSushi
Copy link
Owner

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

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
<original-regex> matches non-word characters at match boundaries.

The problem is that using this formulation means that you need to
extract the capture group around <original-regex> 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
@BurntSushi BurntSushi merged commit 341a19e into master Jul 31, 2023
13 checks passed
@BurntSushi BurntSushi deleted the ag/fix-2574 branch July 31, 2023 12:51
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.

Incomplete matches when using the --word-regexp flag
1 participant