]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: handle precision 0 format stings in standard way
authorMichael T. Jones <mtj@google.com>
Thu, 21 Jul 2011 06:46:51 +0000 (16:46 +1000)
committerRob Pike <r@golang.org>
Thu, 21 Jul 2011 06:46:51 +0000 (16:46 +1000)
The C-stdlib heritage of printf/fprintf/sprintf has two odd
aspects for precisions of zero with integers. First, the zero
can be specified in any of these ways, "%4.0d", "%.0d" and
"%.d" which was not previously supported here. Secondly, the
seemingly universal interpretation of precision for integers
is that precision==0 and value==0 means print nothing at all.
The code here now handles this for integers just as the code
in big/int.c does the same for the Int type. New tests are
added to fmt_test.go to verify these changes.

R=r, r
CC=golang-dev
https://golang.org/cl/4717045

src/pkg/fmt/fmt_test.go
src/pkg/fmt/format.go
src/pkg/fmt/print.go

index 2ede11cfc89767e8ef3980acb327293d3d564849..d13d09c1b623db9d8e74adff6daf8452d2a8a8ba 100644 (file)
@@ -180,6 +180,8 @@ var fmttests = []struct {
        {"%+d", 0, "+0"},
        {"% d", 0, " 0"},
        {"% d", 12345, " 12345"},
+       {"%.0d", 0, ""},
+       {"%.d", 0, ""},
 
        // unicode format
        {"%U", 0x1, "U+0001"},
index bec55f75ba0d60a53e4eb04b6b6df33684e0b384..24b15a286b7c5a685321587dd7d2be8258bb2a52 100644 (file)
@@ -166,6 +166,11 @@ func (f *fmt) fmt_boolean(v bool) {
 // integer; interprets prec but not wid.  Once formatted, result is sent to pad()
 // and then flags are cleared.
 func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
+       // precision of 0 and value of 0 means "print nothing"
+       if f.precPresent && f.prec == 0 && a == 0 {
+               return
+       }
+
        var buf []byte = f.intbuf[0:]
        negative := signedness == signed && a < 0
        if negative {
index 53c39f18dab4c03fe628d405b0f2100c240e1a50..738734908079a4eb6a8f2e5ce406a993b3b0f2da 100644 (file)
@@ -928,6 +928,10 @@ func (p *pp) doPrintf(format string, a []interface{}) {
                                }
                        } else {
                                p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i+1, end)
+                               if !p.fmt.precPresent {
+                                       p.fmt.prec = 0
+                                       p.fmt.precPresent = true
+                               }
                        }
                }
                if i >= end {