regex-syntax: escape ASCII whitespace in escape() and escape_into()#1353
Closed
tryHivemind wants to merge 1 commit into
Closed
regex-syntax: escape ASCII whitespace in escape() and escape_into()#1353tryHivemind wants to merge 1 commit into
tryHivemind wants to merge 1 commit into
Conversation
escape() documents that its output "may be safely used as a literal in
a regular expression", but unescaped whitespace characters break that
guarantee when the result is embedded in a verbose-mode group (?x:...),
where ASCII whitespace is treated as insignificant and ignored:
let lit = regex_syntax::escape(" "); // returns " "
Regex::new(&format!("^(?x:{lit})$")).unwrap().is_match(" ") // false!
Fix escape_into() to emit \xNN hex escapes for ASCII whitespace
characters, making the output valid in all regex contexts including
verbose mode. Non-ASCII whitespace (e.g. U+2000 EN QUAD) is not a
concern because verbose mode only ignores ASCII whitespace per the
regex crate's own definition.
Fixes #1323
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.
escape() output is not safe inside verbose mode (?x:...) where ASCII whitespace is ignored, breaking the documented guarantee. Fix escape_into() to emit hex escapes for ASCII whitespace so output is valid in all regex contexts. Fixes #1323