From: Rob Pike Date: Mon, 16 Jun 2014 17:45:05 +0000 (-0700) Subject: fmt: don't put 0x on every byte of a compact hex-encoded string X-Git-Tag: go1.4beta1~1286 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=311e28636ab1b41a10510a46fe1c8728e8713057;p=gostls13.git 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 --- diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go index a55a665033..2865b966ee 100644 --- a/src/pkg/fmt/fmt_test.go +++ b/src/pkg/fmt/fmt_test.go @@ -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. diff --git a/src/pkg/fmt/format.go b/src/pkg/fmt/format.go index c1d948c5f7..20baa4bd53 100644 --- a/src/pkg/fmt/format.go +++ b/src/pkg/fmt/format.go @@ -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