Skip to content

fix: insert zero padding after sign and prefix in string/base/format-interpolate - #16539

Open
rajanpanth wants to merge 1 commit into
stdlib-js:developfrom
rajanpanth:fix/format-interpolate-zero-padding
Open

rajanpanth wants to merge 1 commit into
stdlib-js:developfrom
rajanpanth:fix/format-interpolate-zero-padding

Conversation

@rajanpanth

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • inserts zero padding after a + or sign and after a 0x/0X prefix in string/base/format-interpolate (and thus string/format). Previously, only a leading - was moved in front of the zeros, so the zeros landed in front of the sign or the prefix.
  • always applies the zero padding requested by an integer precision on the left. Previously, the - flag (left-justify) also flipped the precision padding to the right, which changed the value: format( '%-.5d', 10 ) returned '10000'. In C, - only affects how the result is padded to the field width.
Format Value Before After (= C printf)
%+05d 42 00+42 +0042
% 05d 42 00 42 0042
%#08x 255 00000xff 0x0000ff
%+08.3f 3.14159 00+3.142 +003.142
%-.5d 10 10000 00010
%-8.3d -7 -700 -007

Related Issues

Does this pull request have any related issues?

None.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

  • One existing unit test in test/test.format_integer.js asserted the old behavior ({ 'precision': 5, 'padRight': true } with 10 returned '10000'). I updated its expected value to '00010', which is what C's printf( "%-.5d", 10 ) produces. All other existing tests pass unchanged.
  • New tests cover each row of the table above, plus zeroPad directly with sign and hexadecimal prefixes. The new test.js tests fail on develop (8 assertions), as do the new test.zero_pad.js tests (5 assertions).
  • I checked the expected values against Python's % operator, which matches C for these cases.
  • This is independent of fix: remove trailing zeros and handle zero and zero precision for %g in string/base/format-interpolate #16538 (%g), which touches format_double.js only.
  • I ran the tests for string/base/format-interpolate and string/format directly with Node.js and tape on Windows (as make was not available in my environment); I did not run the full repository lint pipeline locally.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

This PR was written with the help of Claude Code, which found the issue, drafted the implementation change and tests, and checked the results against C's printf semantics. I reviewed the changes and the test results before submitting.


@stdlib-js/reviewers

🤖 Generated with Claude Code

…-interpolate`

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rajanpanth
rajanpanth requested a review from a team November 7, 2026 17:17
@stdlib-bot stdlib-bot added Needs Review A pull request which needs code review. First-time Contributor A pull request from a contributor who has never previously committed to the project repository. labels Nov 7, 2026
@kgryte
kgryte requested a review from Planeshifter November 8, 2026 08:38
@kgryte

kgryte commented Nov 8, 2026

Copy link
Copy Markdown
Member

@Planeshifter Mind taking a look at these changes since you were the one which originally added this functionality?

@kgryte kgryte added difficulty: 1 Low degree of difficulty. Should be straightforward to implement and/or resolve. difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. and removed difficulty: 1 Low degree of difficulty. Should be straightforward to implement and/or resolve. labels Nov 8, 2026
@stdlib-bot stdlib-bot removed the First-time Contributor A pull request from a contributor who has never previously committed to the project repository. label Nov 8, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

difficulty: 2 May require some initial design or R&D, but should be straightforward to resolve and/or implement. Needs Review A pull request which needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants