fix: std.format %c rejects multi-character strings#1015
Open
He-Pin wants to merge 2 commits into
Open
Conversation
Motivation: The %c format specifier should only accept a single-character string or a numeric codepoint. sjsonnet silently accepted multi-character strings (e.g. "%c" % "AB" → "AB"), while C++ jsonnet, go-jsonnet, and jrsonnet all correctly reject them with an error. Modification: - Format.scala: When %c receives a Val.Str, validate it contains exactly 1 codepoint. Raise "%c expected 1-sized string got: N" if the codepoint count is != 1, matching C++ jsonnet's error format. Result: "%c" % "AB" now correctly errors instead of silently returning "AB". Cross-implementation comparison: | Expression | C++ jsonnet | go-jsonnet | jrsonnet | sjsonnet (before) | sjsonnet (after) | |-------------------|-----------------------|------------|------------------------------|-------------------|-----------------------------| | "%c" % "A" | "A" | "A" | "A" | "A" | "A" | | "%c" % "AB" | ERROR: 1-sized string | ERROR | ERROR: 1 char string got 2 | "AB" ❌ | ERROR: 1-sized string ✅ | | "%c" % "" | ERROR | ERROR | ERROR: 1 char string got 0 | "" ❌ | ERROR: 1-sized string ✅ | | "%c" % 65 | "A" | "A" | "A" | "A" | "A" | | "%c" % "世" | "世" | "世" | "世" | "世" | "世" |
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.
fix: std.format %c rejects multi-character strings
Motivation
The
%cformat specifier should only accept a single-character string or a numeric codepoint. sjsonnet silently accepted multi-character strings (e.g."%c" % "AB"→"AB"), while C++ jsonnet, go-jsonnet, and jrsonnet all correctly reject them.Modification
Format.scala: When%creceives aVal.Str, validate codepoint count is exactly 1 usingcodePointCount. Multi-char or empty strings raise"%c expected 1-sized string got: N", matching C++ jsonnet's error format.error.format_c_multi_char_string.jsonnet,error.format_c_empty_string.jsonnetResult
"%c" % "AB"and"%c" % ""now correctly error, matching all reference implementations."%c" % "A""A""A""A""A"✅"A"✅"%c" % "AB""AB"❌"%c" % """"❌"%c" % 65"A""A""A""A"✅"A"✅