Skip to content

fix: format + flag now correctly overrides space flag - #1089

Merged
stephenamar-db merged 1 commit into
databricks:masterfrom
He-Pin:fix/format-plus-space-flag-priority
Jul 28, 2026
Merged

fix: format + flag now correctly overrides space flag#1089
stephenamar-db merged 1 commit into
databricks:masterfrom
He-Pin:fix/format-plus-space-flag-priority

Conversation

@He-Pin

@He-Pin He-Pin commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Summary

  • When both + and space flags appear in std.format (e.g. "%+ d"), the + flag must override the space flag. sjsonnet had the condition order reversed, causing space to win.

Behavior comparison

Expression sjsonnet (before) sjsonnet (after) go-jsonnet v0.22.0 jrsonnet 0.5.0-pre99 Python 3
std.format("%+ d", [42]) " 42" "+42" "+42" "+42" '+42'
std.format("% +d", [42]) " 42" "+42" "+42" "+42" '+42'
std.format("%+ f", [3.14]) " 3.140000" "+3.140000" "+3.140000" "+3.140000" '+3.140000'
std.format("%+ d", [0]) " 0" "+0" "+0" "+0" '+0'
std.format("% d", [42]) " 42" " 42" " 42" " 42" ' 42'
std.format("%+d", [42]) "+42" "+42" "+42" "+42" '+42'
std.format("%+ d", [-42]) "-42" "-42" "-42" "-42" '-42'

Specification reference

  • C99 §7.19.6.1: "If the + and space flags both appear, the space flag is ignored."
  • Python % formatting: '%+ d' % 42'+42'
  • Jsonnet spec: format operator is "based on Python's %"

Test plan

  • ./mill 'sjsonnet.jvm[2.13.18]'.test — all tests pass, no regressions
  • Verified output matches go-jsonnet v0.22.0 and jrsonnet 0.5.0-pre99 for all flag combinations

Motivation:
When both + and space flags are present in std.format (e.g. "%+ d"),
the + flag must override the space flag per Python/C printf semantics.
sjsonnet had the condition order reversed, causing space to win.

Modification:
Swap the condition order in Format.scala so signCharacter (+) is
checked before blankBeforePositive (space).

Result:
std.format("%+ d", [42]) now produces "+42" instead of " 42",
matching go-jsonnet, jrsonnet, Python, and C99 §7.19.6.1.
@He-Pin
He-Pin force-pushed the fix/format-plus-space-flag-priority branch from 67d696f to 0219502 Compare July 27, 2026 17:25
@stephenamar-db
stephenamar-db merged commit 7e4b2cd into databricks:master Jul 28, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants