]> Cypherpunks repositories - gostls13.git/commitdiff
fmt/print: remove a TODO regarding printing renamed byte slices.
authorRob Pike <r@golang.org>
Mon, 16 Aug 2010 22:34:40 +0000 (08:34 +1000)
committerRob Pike <r@golang.org>
Mon, 16 Aug 2010 22:34:40 +0000 (08:34 +1000)
the solution must work around a weakness in the reflection library:
there is no way to do type-safe conversions under reflection.

R=rsc
CC=golang-dev
https://golang.org/cl/2000041

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

index 57fef2197c7042691fd704b955e0486538e4f794..97fd497fbd2bd977eee46b311dd17623a1be0af2 100644 (file)
@@ -334,8 +334,7 @@ var fmttests = []fmtTest{
        fmtTest{"%X", renamedUint64(17), "11"},
        fmtTest{"%o", renamedUintptr(18), "22"},
        fmtTest{"%x", renamedString("thing"), "7468696e67"},
-       // TODO: It would be nice if this one worked, but it's hard.
-       //      fmtTest{"%q", renamedBytes([]byte("hello")), `"hello"`},
+       fmtTest{"%q", renamedBytes([]byte("hello")), `"hello"`},
        fmtTest{"%v", renamedFloat(11), "11"},
        fmtTest{"%v", renamedFloat32(22), "22"},
        fmtTest{"%v", renamedFloat64(33), "33"},
index bf13a7c9c1ce127e783bd669ac33db17ac963c73..b272c26a4516b60c98f1fbb3d2f217c04b6206aa 100644 (file)
@@ -697,6 +697,22 @@ BigSwitch:
                        return p.printField(value.Interface(), verb, plus, goSyntax, depth+1)
                }
        case reflect.ArrayOrSliceValue:
+               // Byte slices are special.
+               if f.Type().(reflect.ArrayOrSliceType).Elem().Kind() == reflect.Uint8 {
+                       // We know it's a slice of bytes, but we also know it does not have static type
+                       // []byte, or it would have been caught above.  Therefore we cannot convert
+                       // it directly in the (slightly) obvious way: f.Interface().([]byte); it doesn't have
+                       // that type, and we can't write an expression of the right type and do a
+                       // conversion because we don't have a static way to write the right type.
+                       // So we build a slice by hand.  This is a rare case but it would be nice
+                       // if reflection could help a little more.
+                       bytes := make([]byte, f.Len())
+                       for i := range bytes {
+                               bytes[i] = byte(f.Elem(i).(*reflect.UintValue).Get())
+                       }
+                       p.fmtBytes(bytes, verb, goSyntax, depth, field)
+                       return verb == 's'
+               }
                if goSyntax {
                        p.buf.WriteString(reflect.Typeof(field).String())
                        p.buf.WriteByte('{')
@@ -804,7 +820,7 @@ func (p *pp) doPrintf(format string, a []interface{}) {
                i += w
                // percent is special - absorbs no operand
                if c == '%' {
-                       p.buf.WriteByte('%') // TODO: should we bother with width & prec?
+                       p.buf.WriteByte('%') // We ignore width and prec.
                        continue
                }
                if fieldnum >= len(a) { // out of operands