fix: std.format specialize arg swap and %-0 flag interaction#1045
Open
He-Pin wants to merge 1 commit into
Open
fix: std.format specialize arg swap and %-0 flag interaction#1045He-Pin wants to merge 1 commit into
He-Pin wants to merge 1 commit into
Conversation
Motivation:
Two bugs found via adversarial testing against go-jsonnet, jrsonnet,
and Python's % operator:
1. std.format("%d", "abc") reported misleading "too many values to
format: 1, expected 0" instead of a type error. Root cause: the
specialize() method in Format_ swapped the format string and values
arguments when the values happened to be a string literal.
2. %-05d applied zero-padding instead of left-justification. Per
POSIX/Python convention, when both - (left-justify) and 0 (zero-pad)
flags are present, - takes precedence and 0 is ignored.
Modification:
1. Fix specialize() pattern to correctly use the first argument (format
string) as the PartialApplyFmt template, not the second argument
(values). Changed `Array(str, fmt: Val.Str)` to
`Array(fmtStr: Val.Str, vals)`.
2. Add `&& !formatted.leftAdjusted` to the zero-padding conditions in
Format.widen(), so left-justify takes precedence over zero-pad.
Result:
- std.format("%d", "abc") now correctly reports "expected number at
position 0, got string", matching go-jsonnet and jrsonnet.
- %-05d now produces "42 " (left-justified) instead of "00042"
(zero-padded), matching go-jsonnet, jrsonnet, and Python.
- %05d (zero-pad without left-justify) still works correctly.
- All tests pass on JVM 2.12, 2.13, 3.3.
93f6cb9 to
991129b
Compare
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
Two
std.formatbugs found via adversarial testing against go-jsonnet, jrsonnet, and Python's%operator:std.format("%d", "abc")reported misleading"too many values to format: 1, expected 0"instead of a type error. Root cause: thespecialize()method inFormat_swapped the format string and values arguments when the values happened to be a string literal.%-05dapplied zero-padding instead of left-justification. Per POSIX/Python convention, when both-(left-justify) and0(zero-pad) flags are present,-takes precedence.std.format("%d", "abc")too many values...Format required number...type error...TypeErrorexpected number...✅std.format("%-05d", 42)"00042""42 ""42 ""42 ""42 "✅std.format("%-05d", -42)"-0042""-42 ""-42 ""-42 ""-42 "✅Modification
StringModule.scala: Fixspecialize()pattern to correctly use the first argument (format string) as thePartialApplyFmttemplate. ChangedArray(str, fmt: Val.Str)toArray(fmtStr: Val.Str, vals).Format.scala: Add&& !formatted.leftAdjustedto zero-padding conditions inwiden(), so left-justify takes precedence over zero-pad.Result
std.format("%d", "abc")correctly reports type error.%-05dproduces"42 "(left-justified), matching go-jsonnet, jrsonnet, and Python.%05d(zero-pad only) still produces"00042"correctly.