]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: don't put 0x on every byte of a compact hex-encoded string
authorRob Pike <r@golang.org>
Mon, 16 Jun 2014 17:45:05 +0000 (10:45 -0700)
committerRob Pike <r@golang.org>
Mon, 16 Jun 2014 17:45:05 +0000 (10:45 -0700)
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

src/pkg/fmt/fmt_test.go
src/pkg/fmt/format.go

index a55a665033d981e8eff6336eef10b339b1cbefec..2865b966eea05145977aa74369e1da5ae2222fcc 100644 (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.
 
index c1d948c5f70e0d55c1d2711a21c42a108f342fec..20baa4bd53d283d946003e393aa6dafa0299e6a8 100644 (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