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