Disable all literal optimizations when a pattern is partially anchored.#281
Merged
BurntSushi merged 1 commit intorust-lang:masterfrom Sep 12, 2016
Merged
Disable all literal optimizations when a pattern is partially anchored.#281BurntSushi merged 1 commit intorust-lang:masterfrom
BurntSushi merged 1 commit intorust-lang:masterfrom
Conversation
When a pattern is partially anchored and literal prefixes are detected, we would scan for those prefixes like normal. On a match, we'd verify whether the text starting at that prefix matched the rest of the regex. This process doesn't work that well for partially anchored regexes, since the prefix match presupposes that the regex is allowed to match at that position. But if, say, a regex like `^z|a` finds a `z` in the middle of the string, then it has lost the fact that `z` needs to appear at the beginning of the string, and can therefore falsely report a match. We could spend some effort and make this case work, but the literal optimizer is already too complex. We need to simplify it to make future optimizations like this possible. Fixes rust-lang#280.
This was referenced Oct 13, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When a pattern is partially anchored and literal prefixes are detected,
we would scan for those prefixes like normal. On a match, we'd verify
whether the text starting at that prefix matched the rest of the regex.
This process doesn't work that well for partially anchored regexes, since
the prefix match presupposes that the regex is allowed to match at that
position. But if, say, a regex like
^z|afinds azin the middle ofthe string, then it has lost the fact that
zneeds to appear at thebeginning of the string, and can therefore falsely report a match.
We could spend some effort and make this case work, but the literal
optimizer is already too complex. We need to simplify it to make future
optimizations like this possible.
Fixes #280.