]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: allow for space and plus flags when computing widths
authorRob Pike <r@golang.org>
Mon, 11 May 2015 18:13:22 +0000 (11:13 -0700)
committerRob Pike <r@golang.org>
Mon, 11 May 2015 18:34:19 +0000 (18:34 +0000)
Fixes #10770.
Fixes #10771.

This time maybe for sure?

Change-Id: I43d6e5fd6846cf58427fec183832d500a932df59
Reviewed-on: https://go-review.googlesource.com/9896
Reviewed-by: Russ Cox <rsc@golang.org>
src/fmt/fmt_test.go
src/fmt/format.go

index f15a0ba8e807a881e561f6ba126218d287b1502a..93121bb3d032c4760c3a1761f86d5d32cc200052 100644 (file)
@@ -557,10 +557,13 @@ var fmtTests = []struct {
        {"%0.100f", 1.0, zeroFill("1.", 100, "")},
        {"%0.100f", -1.0, zeroFill("-1.", 100, "")},
 
-       // Used to panic: integer function didn't look at f.prec or f.unicode.
+       // Used to panic: integer function didn't look at f.prec or f.unicode or f.width.
        {"%#.80x", 42, "0x0000000000000000000000000000000000000000000000000000000000000000000000000000002a"},
        {"%.80U", 42, "U+0000000000000000000000000000000000000000000000000000000000000000000000000000002A"},
        {"%#.80U", '日', "U+000000000000000000000000000000000000000000000000000000000000000000000000000065E5 '日'"},
+       {"%+.65d", 44, "+00000000000000000000000000000000000000000000000000000000000000044"},
+       {"% .65d", 44, " 00000000000000000000000000000000000000000000000000000000000000044"},
+       {"%  +.65d", 44, "+00000000000000000000000000000000000000000000000000000000000000044"},
 
        // Comparison of padding rules with C printf.
        /*
index 099f8a5e00ace20b31e55df311d7d78839074a64..ba984cf84f946f40584f078d1092697d29353ee7 100644 (file)
@@ -163,7 +163,7 @@ func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
        }
 
        var buf []byte = f.intbuf[0:]
-       if f.widPresent || f.precPresent {
+       if f.widPresent || f.precPresent || f.plus || f.space {
                width := f.wid + f.prec // Only one will be set, both are positive; this provides the maximum.
                if base == 16 && f.sharp {
                        // Also adds "0x".
@@ -177,6 +177,11 @@ func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
                                width += 1 + 1 + utf8.UTFMax + 1
                        }
                }
+               if f.plus {
+                       width++
+               } else if f.space {
+                       width++
+               }
                if width > nByte {
                        // We're going to need a bigger boat.
                        buf = make([]byte, width)