From 6b6d5c15c09327e882f1a72c171fa4674d50dfc4 Mon Sep 17 00:00:00 2001 From: Basile Burg Date: Sun, 19 Aug 2018 19:03:48 +0200 Subject: [PATCH] fix issue 18838 - Formatting the number zero with separator doesn't obey width specifier --- std/format.d | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/std/format.d b/std/format.d index 089cd634f39..dd415f10888 100644 --- a/std/format.d +++ b/std/format.d @@ -2356,13 +2356,14 @@ private void formatUnsigned(Writer, T, Char) size_t leftpad = 0; size_t rightpad = 0; + immutable size_t dlen = digits.length == 0 ? 0 : digits.length - 1; immutable ptrdiff_t spacesToPrint = fs.width - ( (prefix1 != 0) + (prefix2 != 0) + zerofill + digits.length - + ((fs.flSeparator != 0) * ((digits.length - 1) / fs.separators)) + + ((fs.flSeparator != 0) * (dlen / fs.separators)) ); if (spacesToPrint > 0) // need to do some padding { @@ -2429,6 +2430,11 @@ private void formatUnsigned(Writer, T, Char) put(w, ' '); } +@safe pure unittest // bugzilla 18838 +{ + assert("%12,d".format(0) == " 0"); +} + @safe pure unittest { assert(collectExceptionMsg!FormatException(format("%c", 5)).back == 'c');