fix: replace surrogate codepoints with U+FFFD in std.char and %c#965
Draft
He-Pin wants to merge 1 commit into
Draft
fix: replace surrogate codepoints with U+FFFD in std.char and %c#965He-Pin wants to merge 1 commit into
He-Pin wants to merge 1 commit into
Conversation
Motivation: std.char() and the %c format specifier accepted surrogate codepoints (U+D800-U+DFFF) and passed them directly to Character.toString(), which produces lone surrogates. These corrupt UTF-16 strings and cause downstream encoding failures. go-jsonnet replaces surrogates with U+FFFD (replacement character); sjsonnet should align. Modification: - StringModule.scala (std.char): detect surrogate range and substitute U+FFFD before calling Character.toString() - Format.scala (%c): apply the same surrogate→U+FFFD substitution - UnicodeHandlingTests.scala: update tests to expect U+FFFD replacement instead of raw surrogate preservation Result: std.char(55296) and "%c" % 55296 now produce U+FFFD, matching go-jsonnet. Lone surrogates no longer leak into string values, preventing encoding errors in JSON/YAML/TOML renderers. References: - go-jsonnet behavior: std.char(0xD800) → U+FFFD - jrsonnet behavior: rejects surrogates as errors - C++ jsonnet behavior: produces invalid UTF-8 (no validation)
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.
Motivation
std.char()and the%cformat specifier accepted surrogate codepoints (U+D800-U+DFFF) and passed them directly toCharacter.toString(), which produces lone surrogates. These corrupt UTF-16 strings and cause downstream encoding failures ("malformed" errors). Aligning with go-jsonnet's behavior: replace surrogates with U+FFFD (replacement character).Modification
std.char): detect surrogate range (0xD800-0xDFFF) and substitute U+FFFD before callingCharacter.toString()%c): apply the same surrogate→U+FFFD substitution in the format operatorResult
std.char(55296)and"%c" % 55296now produce U+FFFD, matching go-jsonnetstd.codepoint(std.char(0xD800))returns 65533 (U+FFFD) instead of 55296std.char(0xD800)"%c" % 0xD800std.codepoint(std.char(0xD800))Test plan
std.char(55296)returns"\uFFFD"(U+D800 high surrogate → U+FFFD)std.char(56320)returns"\uFFFD"(U+DC00 low surrogate → U+FFFD)std.codepoint(std.char(55296))returns 65533"%c" % 55296produces U+FFFD (Format.scala fix)