fix(strings): don't halve backslashes in interpolated string literals#2764
Open
truffle-dev wants to merge 1 commit into
Open
fix(strings): don't halve backslashes in interpolated string literals#2764truffle-dev wants to merge 1 commit into
truffle-dev wants to merge 1 commit into
Conversation
String literals in expressions have their escapes decoded once by the lexer
(processEscapeCharacters), then interpolate() decoded every backslash pair a
second time, halving runs of backslashes (\\ -> \, \\\\ -> \\). Output
from an expression diverged from the same string read as input, and from jq.
interpolate() only needs to treat a backslash pair as an escape when it guards
an interpolation paren (\\( -> literal \(); a standalone pair must pass
through unchanged. Guard the skip on a following '(' so plain backslash runs
survive, mirroring the lexer's own \\( special case.
Fixes mikefarah#2561
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.
Fixes #2561.
What
A string literal in an expression loses half of any run of backslashes, so the value differs from the same string read as input (and from
jq):Why
Escapes in an expression string literal are processed twice. The lexer runs
processEscapeCharacters, which already decodes\\->\(while deliberately preserving\\(so interpolation escaping survives).interpolate()then collapses every remaining backslash pair a second time, halving runs of backslashes.That second collapse is only needed for the
\\(-> literal\(escape. A standalone pair should pass through untouched. The fix guards the skip on a following(, mirroring the special case the lexer already uses, so\\(still yields a literal\(but plain backslash runs survive.Testing
operator_strings_test.go(\\\\and\\\\\\); both fail onmasterand pass with the fix."\\","Hi \\(.value)", real\(...)interpolation) still pass unchanged.Thanks to the reporter for the clear before/after repro.