]> Cypherpunks repositories - gostls13.git/commitdiff
Allow %p on reference types, for debugging.
authorRob Pike <r@golang.org>
Tue, 22 Dec 2009 06:02:00 +0000 (17:02 +1100)
committerRob Pike <r@golang.org>
Tue, 22 Dec 2009 06:02:00 +0000 (17:02 +1100)
(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

src/pkg/fmt/fmt_test.go
src/pkg/fmt/print.go

index 78d4cf29a3a46b03f07c03575b3692e38b02bbf3..c89a6acacac20e5accd0063e8db9fb5505e33dcd 100644 (file)
@@ -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
                                }
                        }
index d4ef3c62f6345037d341e9913daead4a25ff5b20..044ac1702c48120dbc3f72aef34c106811cd6993 100644 (file)
@@ -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 {
-                                       p.fmt.fmt_s("0x")
-                                       p.fmt.fmt_uX64(uint64(v))
-                               }
-                       } else {
+                       switch v := field.(type) {
+                       case *reflect.PtrValue:
+                               p.fmt.fmt_s("0x")
+                               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
                        }