1
0
mirror of https://github.com/golang/go synced 2024-11-19 22:04:44 -07:00

fmt: empty byte slices should print nothing in hex

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>
This commit is contained in:
Rob Pike 2015-04-13 12:41:11 -07:00
parent 63cced7b31
commit 57058327c8
2 changed files with 9 additions and 1 deletions

View 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"},

View 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.