Commit 311e2863 authored by Rob Pike's avatar Rob Pike

fmt: don't put 0x on every byte of a compact hex-encoded string

Printf("%x", "abc") was "0x610x620x63"; is now "0x616263", which
is surely better.
Printf("% #x", "abc") is still "0x61 0x62 0x63".

Fixes #8080.

LGTM=bradfitz, gri
R=golang-codereviews, bradfitz, gri
CC=golang-codereviews
https://golang.org/cl/106990043
parent be0079ab
......@@ -125,13 +125,17 @@ var fmtTests = []struct {
{"%x", "xyz", "78797a"},
{"%X", "xyz", "78797A"},
{"%q", "abc", `"abc"`},
{"%#x", []byte("abc\xff"), "0x616263ff"},
{"%#X", []byte("abc\xff"), "0X616263FF"},
{"%# x", []byte("abc\xff"), "0x61 0x62 0x63 0xff"},
{"%# X", []byte("abc\xff"), "0X61 0X62 0X63 0XFF"},
// basic bytes
{"%s", []byte("abc"), "abc"},
{"%x", []byte("abc"), "616263"},
{"% x", []byte("abc\xff"), "61 62 63 ff"},
{"%#x", []byte("abc\xff"), "0x610x620x630xff"},
{"%#X", []byte("abc\xff"), "0X610X620X630XFF"},
{"%#x", []byte("abc\xff"), "0x616263ff"},
{"%#X", []byte("abc\xff"), "0X616263FF"},
{"%# x", []byte("abc\xff"), "0x61 0x62 0x63 0xff"},
{"%# X", []byte("abc\xff"), "0X61 0X62 0X63 0XFF"},
{"% X", []byte("abc\xff"), "61 62 63 FF"},
......@@ -379,7 +383,7 @@ var fmtTests = []struct {
{"%s", I(23), `<23>`},
{"%q", I(23), `"<23>"`},
{"%x", I(23), `3c32333e`},
{"%#x", I(23), `0x3c0x320x330x3e`},
{"%#x", I(23), `0x3c32333e`},
{"%# x", I(23), `0x3c 0x32 0x33 0x3e`},
{"%d", I(23), `23`}, // Stringer applies only to string formats.
......
......@@ -298,7 +298,7 @@ func (f *fmt) fmt_sbx(s string, b []byte, digits string) {
if i > 0 && f.space {
buf = append(buf, ' ')
}
if f.sharp {
if f.sharp && (f.space || i == 0) {
buf = append(buf, '0', x)
}
var c byte
......
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