From cf30ba443c8cdaf39b5b960d78894955dc71beed Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Wed, 14 Jun 2017 10:04:05 +0000 Subject: Little variation on Brian's proposal: Only prefix decimal number with '0d' when that's necessary for correct interpretation of the number when parsed back again. --- doc/format.n | 5 +++-- generic/tclStringObj.c | 8 ++++++-- tests/format.test | 21 +++++++++------------ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/doc/format.n b/doc/format.n index 4eb566d..1df255e 100644 --- a/doc/format.n +++ b/doc/format.n @@ -89,8 +89,9 @@ For \fBx\fR or \fBX\fR conversions, \fB0x\fR or \fB0X\fR (respectively) will be added to the beginning of the result unless it is zero. For \fBb\fR conversions, \fB0b\fR will be added to the beginning of the result unless it is zero. -For \fBd\fR conversions, \fB0d\fR will be added to the beginning -of the result unless it is zero. +For \fBd\fR conversions, \fB0d\fR there is no effect unless +the \fB0\fR specifier is used as well: In that case, \fB0d\fR or +\fB0D\fR (respectively) will be added to the beginning. For all floating-point conversions (\fBe\fR, \fBE\fR, \fBf\fR, \fBg\fR, and \fBG\fR) it guarantees that the result always has a decimal point. diff --git a/generic/tclStringObj.c b/generic/tclStringObj.c index b84470b..5ce7d18 100644 --- a/generic/tclStringObj.c +++ b/generic/tclStringObj.c @@ -2037,8 +2037,12 @@ Tcl_AppendFormatToObj( segmentLimit -= 2; break; case 'd': - Tcl_AppendToObj(segment, "0d", 2); - segmentLimit -= 2; +#if TCL_MAJOR_VERSION < 9 + if (gotZero) { + Tcl_AppendToObj(segment, "0d", 2); + segmentLimit -= 2; + } +#endif break; } } diff --git a/tests/format.test b/tests/format.test index 8842315..3fd72be 100644 --- a/tests/format.test +++ b/tests/format.test @@ -78,24 +78,21 @@ test format-1.11.1 {integer formatting} longIs64bit { test format-1.12 {integer formatting} { format "%b %#b %#b %llb" 5 0 5 [expr {2**100}] } {101 0b0 0b101 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000} -test format-1.13 {integer formatting} longIs32bit { +test format-1.13 {integer formatting} { format "%#d %#d %#d %#d %#d" 0 6 34 16923 -12 -1 -} {0d0 0d6 0d34 0d16923 -0d12} -test format-1.13.1 {integer formatting} longIs64bit { - format "%#d %#d %#d %#d %#d" 0 6 34 16923 -12 -1 -} {0d0 0d6 0d34 0d16923 -0d12} +} {0 6 34 16923 -12} test format-1.14 {integer formatting} longIs32bit { - format "%#5d %#20d %#20d %#20d %#20d" 0 6 34 16923 -12 -1 -} { 0d0 0d6 0d34 0d16923 -0d12} + format "%#05d %#20d %#20d %#20d %#20d" 0 6 34 16923 -12 -1 +} {0d000 6 34 16923 -12} test format-1.14.1 {integer formatting} longIs64bit { - format "%#5d %#20d %#20d %#20d %#20d" 0 6 34 16923 -12 -1 -} { 0d0 0d6 0d34 0d16923 -0d12} + format "%#05d %#20d %#20d %#20d %#20d" 0 6 34 16923 -12 -1 +} {0d000 6 34 16923 -12} test format-1.15 {integer formatting} longIs32bit { - format "%-#5d %-#20d %-#20d %-#20d %-#20d" 0 6 34 16923 -12 -1 -} {0d0 0d6 0d34 0d16923 -0d12 } + format "%-#05d %-#20d %-#20d %-#20d %-#20d" 0 6 34 16923 -12 -1 +} {0d000 6 34 16923 -12 } test format-1.15.1 {integer formatting} longIs64bit { format "%-#5d %-#20d %-#20d %-#20d %-#20d" 0 6 34 16923 -12 -1 -} {0d0 0d6 0d34 0d16923 -0d12 } +} {0d000 6 34 16923 -12 } test format-2.1 {string formatting} { -- cgit v0.12