]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: integer formatting should not permanently change padding
authorMartin Möhrmann <martisch@uos.de>
Thu, 10 Mar 2016 11:33:33 +0000 (12:33 +0100)
committerRob Pike <r@golang.org>
Sat, 19 Mar 2016 23:49:18 +0000 (23:49 +0000)
Changes the integer function to restore the original f.zero value
and therefore padding type before returning.

Change-Id: I456449259a3d39bd6d62e110553120c31ec63f23
Reviewed-on: https://go-review.googlesource.com/20512
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/fmt/fmt_test.go
src/fmt/format.go

index 55f46dd833116d44479cb04c8c200ff324a2e08d..cc4c71cb09d51a120a21a4348a31cc500f56b46f 100644 (file)
@@ -914,6 +914,9 @@ var fmtTests = []struct {
        {"%06v", []interface{}{+10.0 + 10i, 10}, "[(000010+00010i) 000010]"},
        {"%06v", []interface{}{-10.0 + 10i, 10}, "[(-00010+00010i) 000010]"},
 
+       // integer formatting should not alter padding for other elements.
+       {"%03.6v", []interface{}{1, 2.0, "x"}, "[000001 002 00x]"},
+
        // Complex fmt used to leave the plus flag set for future entries in the array
        // causing +2+0i and +3+0i instead of 2+0i and 3+0i.
        {"%v", []complex64{1, 2, 3}, "[(1+0i) (2+0i) (3+0i)]"},
index b6786b9aed632ab1e73d3fefc70014fd41c9c1f6..1c612c1218069726af2b06ad73a79e4f446d8305 100644 (file)
@@ -221,6 +221,7 @@ func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
 
        // two ways to ask for extra leading zero digits: %.3d or %03d.
        // apparently the first cancels the second.
+       oldZero := f.zero // f.zero is used in f.pad but modified below; restored at end of function.
        prec := 0
        if f.precPresent {
                prec = f.prec
@@ -305,6 +306,7 @@ func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
        }
 
        f.pad(buf[i:])
+       f.zero = oldZero
 }
 
 // truncate truncates the string to the specified precision, if present.