Escape control characters and Unicode line terminators in string literals [patch] - #144
Merged
Merged
Conversation
…ing literals
EscapeString only escaped backslash, quote, \n, \r and \t, so a NUL,
U+0085, U+2028 or U+2029 was copied raw into the literal: Go and Python
reject a NUL in source, and C# reads the line terminators as ending the
line. Every remaining C0 control, DEL, U+0085, U+2028 and U+2029 is now
written as a numeric escape through a per-generator EscapeCodeUnit hook:
\uXXXX by default (C#, JavaScript, Python, Go), \u{XXXX} in Rust, and
octal UTF-8 bytes in C and C++, whose \x is greedy.
Fixes #98
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HsaGezbczZSL9fXfQafs7x
|
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 #98
What was wrong
LanguageGeneratorBase.EscapeStringescaped only\,",\n,\rand\t. Every other control character, and the Unicode line terminators, was copied raw into the quoted literal. That caused three failures:SyntaxError.CS1010: Newline in constant.Change
EscapeStringis now an instance method. After the named escapes, it writes every remaining C0 control, DEL, U+0085, U+2028 and U+2029 through a new virtualEscapeCodeUnithook:\uXXXX\u{XXXX}CFamilyGenerator)\NNNper UTF-8 byte\xis greedy and would swallow a following hex digit, and a universal character name may not name a control character. The UTF-8 bytes are the same bytes the character would have been written as raw, so the string's contents don't change.Tests
StringLiteralControlCharacterTestschecks one string in all seven generators. The string holds\0, ESC followed by a hex digit (b), DEL,\n, NEL, U+2028 and U+2029. For each generator the test checks two things: the exact escaped literal, and that no raw control character is left in the output. A second test checks that printable text, including non-ASCII, is still written as-is.python3,node,gcc -pedantic,go runandrustc. Each one decodes back to the original characters.🤖 Generated with Claude Code
https://claude.ai/code/session_01HsaGezbczZSL9fXfQafs7x
Generated by Claude Code