Skip to content

syntax: fix literal extraction for 'ab??'#863

Merged
BurntSushi merged 1 commit into
masterfrom
ag/fix-862
May 20, 2022
Merged

syntax: fix literal extraction for 'ab??'#863
BurntSushi merged 1 commit into
masterfrom
ag/fix-862

Conversation

@BurntSushi

Copy link
Copy Markdown
Member

Previously, 'ab??' returned [Complete(ab), Complete(a)], but the order
matters here because of greediness. The correct result is [Complete(a),
Complete(ab)].

Instead of trying to actually fix literal extraction (which is a mess),
we just rewrite 'ab?' (and 'ab??') as 'ab*'. 'ab*' still produces
literals in the incorrect order, i.e., [Cut(ab), Complete(a)], but since
one is cut we are guaranteed that the regex engine will be called to
confirm the match. In so doing, it will correctly report 'a' as a match
for 'ab??' in 'ab'.

Fixes #862

Previously, 'ab??' returned [Complete(ab), Complete(a)], but the order
matters here because of greediness. The correct result is [Complete(a),
Complete(ab)].

Instead of trying to actually fix literal extraction (which is a mess),
we just rewrite 'ab?' (and 'ab??') as 'ab*'. 'ab*' still produces
literals in the incorrect order, i.e., [Cut(ab), Complete(a)], but since
one is cut we are guaranteed that the regex engine will be called to
confirm the match. In so doing, it will correctly report 'a' as a match
for 'ab??' in 'ab'.

Fixes #862
@BurntSushi BurntSushi merged commit 1c19619 into master May 20, 2022
@BurntSushi BurntSushi deleted the ag/fix-862 branch May 20, 2022 18:02
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.

Unexpected behavior of ungreedy ?? operator

1 participant