Commit 2b9ab227 authored by Rob Pike's avatar Rob Pike

fmt: make %#p suppress leading 0x

Fixes bug 1567.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/4245048
parent 7eaecb89
...@@ -311,9 +311,9 @@ var fmttests = []struct { ...@@ -311,9 +311,9 @@ var fmttests = []struct {
// go syntax // go syntax
{"%#v", A{1, 2, "a", []int{1, 2}}, `fmt_test.A{i:1, j:0x2, s:"a", x:[]int{1, 2}}`}, {"%#v", A{1, 2, "a", []int{1, 2}}, `fmt_test.A{i:1, j:0x2, s:"a", x:[]int{1, 2}}`},
{"%#v", &b, "(*uint8)(PTR)"}, {"%#v", &b, "(*uint8)(0xPTR)"},
{"%#v", TestFmtInterface, "(func(*testing.T))(PTR)"}, {"%#v", TestFmtInterface, "(func(*testing.T))(0xPTR)"},
{"%#v", make(chan int), "(chan int)(PTR)"}, {"%#v", make(chan int), "(chan int)(0xPTR)"},
{"%#v", uint64(1<<64 - 1), "0xffffffffffffffff"}, {"%#v", uint64(1<<64 - 1), "0xffffffffffffffff"},
{"%#v", 1000000000, "1000000000"}, {"%#v", 1000000000, "1000000000"},
{"%#v", map[string]int{"a": 1, "b": 2}, `map[string] int{"a":1, "b":2}`}, {"%#v", map[string]int{"a": 1, "b": 2}, `map[string] int{"a":1, "b":2}`},
...@@ -365,14 +365,15 @@ var fmttests = []struct { ...@@ -365,14 +365,15 @@ var fmttests = []struct {
{"%6T", &intVal, " *int"}, {"%6T", &intVal, " *int"},
// %p // %p
{"p0=%p", new(int), "p0=PTR"}, {"p0=%p", new(int), "p0=0xPTR"},
{"p1=%s", &pValue, "p1=String(p)"}, // String method... {"p1=%s", &pValue, "p1=String(p)"}, // String method...
{"p2=%p", &pValue, "p2=PTR"}, // ... not called with %p {"p2=%p", &pValue, "p2=0xPTR"}, // ... not called with %p
{"p4=%#p", new(int), "p4=PTR"},
// %p on non-pointers // %p on non-pointers
{"%p", make(chan int), "PTR"}, {"%p", make(chan int), "0xPTR"},
{"%p", make(map[int]int), "PTR"}, {"%p", make(map[int]int), "0xPTR"},
{"%p", make([]int, 1), "PTR"}, {"%p", make([]int, 1), "0xPTR"},
{"%p", 27, "%!p(int=27)"}, // not a pointer at all {"%p", 27, "%!p(int=27)"}, // not a pointer at all
// erroneous things // erroneous things
...@@ -388,8 +389,8 @@ var fmttests = []struct { ...@@ -388,8 +389,8 @@ var fmttests = []struct {
func TestSprintf(t *testing.T) { func TestSprintf(t *testing.T) {
for _, tt := range fmttests { for _, tt := range fmttests {
s := Sprintf(tt.fmt, tt.val) s := Sprintf(tt.fmt, tt.val)
if i := strings.Index(s, "0x"); i >= 0 && strings.Contains(tt.out, "PTR") { if i := strings.Index(tt.out, "PTR"); i >= 0 {
j := i + 2 j := i
for ; j < len(s); j++ { for ; j < len(s); j++ {
c := s[j] c := s[j]
if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') { if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') {
...@@ -399,6 +400,7 @@ func TestSprintf(t *testing.T) { ...@@ -399,6 +400,7 @@ func TestSprintf(t *testing.T) {
s = s[0:i] + "PTR" + s[j:] s = s[0:i] + "PTR" + s[j:]
} }
if s != tt.out { if s != tt.out {
println(s, "XXX", tt.out)
if _, ok := tt.val.(string); ok { if _, ok := tt.val.(string); ok {
// Don't requote the already-quoted strings. // Don't requote the already-quoted strings.
// It's too confusing to read the errors. // It's too confusing to read the errors.
......
...@@ -348,11 +348,11 @@ func (p *pp) fmtInt64(v int64, verb int, value interface{}) { ...@@ -348,11 +348,11 @@ func (p *pp) fmtInt64(v int64, verb int, value interface{}) {
} }
} }
// fmt0x64 formats a uint64 in hexadecimal and prefixes it with 0x by // fmt0x64 formats a uint64 in hexadecimal and prefixes it with 0x or
// temporarily turning on the sharp flag. // not, as requested, by temporarily setting the sharp flag.
func (p *pp) fmt0x64(v uint64) { func (p *pp) fmt0x64(v uint64, leading0x bool) {
sharp := p.fmt.sharp sharp := p.fmt.sharp
p.fmt.sharp = true // turn on 0x p.fmt.sharp = leading0x
p.fmt.integer(int64(v), 16, unsigned, ldigits) p.fmt.integer(int64(v), 16, unsigned, ldigits)
p.fmt.sharp = sharp p.fmt.sharp = sharp
} }
...@@ -384,7 +384,7 @@ func (p *pp) fmtUint64(v uint64, verb int, goSyntax bool, value interface{}) { ...@@ -384,7 +384,7 @@ func (p *pp) fmtUint64(v uint64, verb int, goSyntax bool, value interface{}) {
p.fmt.integer(int64(v), 10, unsigned, ldigits) p.fmt.integer(int64(v), 10, unsigned, ldigits)
case 'v': case 'v':
if goSyntax { if goSyntax {
p.fmt0x64(v) p.fmt0x64(v, true)
} else { } else {
p.fmt.integer(int64(v), 10, unsigned, ldigits) p.fmt.integer(int64(v), 10, unsigned, ldigits)
} }
...@@ -534,11 +534,11 @@ func (p *pp) fmtPointer(field interface{}, value reflect.Value, verb int, goSynt ...@@ -534,11 +534,11 @@ func (p *pp) fmtPointer(field interface{}, value reflect.Value, verb int, goSynt
if u == 0 { if u == 0 {
p.buf.Write(nilBytes) p.buf.Write(nilBytes)
} else { } else {
p.fmt0x64(uint64(v.Get())) p.fmt0x64(uint64(v.Get()), true)
} }
p.add(')') p.add(')')
} else { } else {
p.fmt0x64(uint64(u)) p.fmt0x64(uint64(u), !p.fmt.sharp)
} }
} }
...@@ -801,7 +801,7 @@ BigSwitch: ...@@ -801,7 +801,7 @@ BigSwitch:
if v == 0 { if v == 0 {
p.buf.Write(nilBytes) p.buf.Write(nilBytes)
} else { } else {
p.fmt0x64(uint64(v)) p.fmt0x64(uint64(v), true)
} }
p.buf.WriteByte(')') p.buf.WriteByte(')')
break break
...@@ -810,7 +810,7 @@ BigSwitch: ...@@ -810,7 +810,7 @@ BigSwitch:
p.buf.Write(nilAngleBytes) p.buf.Write(nilAngleBytes)
break break
} }
p.fmt0x64(uint64(v)) p.fmt0x64(uint64(v), true)
case uintptrGetter: case uintptrGetter:
p.fmtPointer(field, value, verb, goSyntax) p.fmtPointer(field, value, verb, goSyntax)
default: default:
......
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