1
0
mirror of https://github.com/golang/go synced 2024-11-24 00:40:12 -07:00

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
This commit is contained in:
Rob Pike 2014-06-16 10:45:05 -07:00
parent be0079abe1
commit 311e28636a
2 changed files with 8 additions and 4 deletions

View File

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

View File

@ -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