Bug 18838 – Formatting the number zero with separator doesn't obey width specifier

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-05-07T04:45:38Z
Last change time
2020-03-21T03:56:42Z
Keywords
pull
Assigned to
No Owner
Creator
Tornchi

Comments

Comment #0 by tornchi — 2018-05-07T04:45:38Z
The following code shows the problem. The width specifier is obeyed in each case except when printing the number zero. import std.stdio; void main() { "%12d".writefln(1); // Writes " 1" "%12d".writefln(0); // Writes " 0" "%12,d".writefln(1); // Writes " 1" "%12,d".writefln(0); // Writes "0" <-- WRONG "%12,d".writefln(-1); // Writes " -1" } I've tracked the cause to the formatUnsigned function in format.d specifically the spacesToPrint calculation (ln:2351). Here the "digits.length - 1" calculation overflows/underflows resulting in no left padding to occur.
Comment #1 by b2.temp — 2018-08-19T17:10:44Z
Comment #2 by github-bugzilla — 2018-08-19T19:20:09Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/6b6d5c15c09327e882f1a72c171fa4674d50dfc4 fix issue 18838 - Formatting the number zero with separator doesn't obey width specifier https://github.com/dlang/phobos/commit/a86b12303b741dc2f1f87cc29f6ea695a0635875 Merge pull request #6673 from BBasile/issue-18838 fix issue 18838 - Formatting the number zero with separator doesn't obey width specifier merged-on-behalf-of: Nathan Sashihara <[email protected]>