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

Allow %p on reference types, for debugging.

(Also fix case sensitivity in test for PTR inside fmt_test.go)
Fixes #441.

R=rsc, iant
CC=golang-dev
https://golang.org/cl/180112
parent d2a39861
......@@ -217,6 +217,11 @@ var fmttests = []fmtTest{
fmtTest{"%+v", B{1, 2}, `{i:<1> j:2}`},
fmtTest{"%+v", C{1, B{2, 3}}, `{i:1 B:{i:<2> j:3}}`},
// %p on non-pointers
fmtTest{"%p", make(chan int), "PTR"},
fmtTest{"%p", make(map[int]int), "PTR"},
fmtTest{"%p", make([]int, 1), "PTR"},
// go syntax
fmtTest{"%#v", A{1, 2, "a", []int{1, 2}}, `fmt_test.A{i:1, j:0x2, s:"a", x:[]int{1, 2}}`},
fmtTest{"%#v", &b, "(*uint8)(PTR)"},
......@@ -233,7 +238,7 @@ func TestSprintf(t *testing.T) {
j := i + 2
for ; j < len(s); j++ {
c := s[j]
if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') {
break
}
}
......
......@@ -380,14 +380,6 @@ func getFloat64(v reflect.Value) (val float64, ok bool) {
return
}
func getPtr(v reflect.Value) (val uintptr, ok bool) {
switch v := v.(type) {
case *reflect.PtrValue:
return uintptr(v.Get()), true
}
return
}
// Convert ASCII to integer. n is 0 (and got is false) if no number present.
func parsenum(s string, start, end int) (n int, got bool, newi int) {
......@@ -808,16 +800,16 @@ func (p *pp) doprintf(format string, v *reflect.StructValue) {
goto badtype
}
// pointer
// pointer, including addresses of reference types.
case 'p':
if v, ok := getPtr(field); ok {
if v == 0 {
p.buf.Write(nilAngleBytes)
} else {
switch v := field.(type) {
case *reflect.PtrValue:
p.fmt.fmt_s("0x")
p.fmt.fmt_uX64(uint64(v))
}
} else {
p.fmt.fmt_uX64(uint64(v.Get()))
case *reflect.ChanValue, *reflect.MapValue, *reflect.SliceValue:
p.fmt.fmt_s("0x")
p.fmt.fmt_uX64(uint64(field.Addr()))
default:
goto badtype
}
......
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