]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: empty byte slices should print nothing in hex
authorRob Pike <r@golang.org>
Mon, 13 Apr 2015 19:41:11 +0000 (12:41 -0700)
committerRob Pike <r@golang.org>
Mon, 13 Apr 2015 20:50:13 +0000 (20:50 +0000)
The documentation is clear that formats like %02x applied to a
byte slice are per-element, so the result should be nothing if the
slice is empty. It's not, because the top-level padding routine is called.
It shouldn't be: the loop does the padding for us.

Fixes #10430.

Change-Id: I04ea0e804c0f2e70fff3701e5bf22acc90e890da
Reviewed-on: https://go-review.googlesource.com/8864
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/fmt/fmt_test.go
src/fmt/format.go

index c14bd2f45c0f469934ece8e9f1210ed4ffc174b2..146977ace117c4086bd1a51af0c1ecbdaa647cb1 100644 (file)
@@ -394,6 +394,8 @@ var fmtTests = []struct {
        {"%v", &slice, "&[1 2 3 4 5]"},
        {"%v", &islice, "&[1 hello 2.5 <nil>]"},
        {"%v", &bslice, "&[1 2 3 4 5]"},
+       {"%v", []byte{1}, "[1]"},
+       {"%v", []byte{}, "[]"},
 
        // complexes with %v
        {"%v", 1 + 2i, "(1+2i)"},
@@ -447,6 +449,12 @@ var fmtTests = []struct {
        {"%d", []int{1, 2, 15}, `[1 2 15]`},
        {"%d", []byte{1, 2, 15}, `[1 2 15]`},
        {"%q", []string{"a", "b"}, `["a" "b"]`},
+       {"% 02x", []byte{1}, "01"},
+       {"% 02x", []byte{1, 2, 3}, "01 02 03"},
+       // Special care for empty slices.
+       {"%x", []byte{}, ""},
+       {"%02x", []byte{}, ""},
+       {"% 02x", []byte{}, ""},
 
        // renamings
        {"%v", renamedBool(true), "true"},
index 4d97d1443eda1292c403fd086acf7fc627b1c88a..86673aba6ae183ffececcd5cbd6f06067fad35d1 100644 (file)
@@ -335,7 +335,7 @@ func (f *fmt) fmt_sbx(s string, b []byte, digits string) {
                }
                buf = append(buf, digits[c>>4], digits[c&0xF])
        }
-       f.pad(buf)
+       f.buf.Write(buf)
 }
 
 // fmt_sx formats a string as a hexadecimal encoding of its bytes.