Commit 57058327 authored by Rob Pike's avatar Rob Pike

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/8864Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 63cced7b
......@@ -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"},
......
......@@ -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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment