]> Cypherpunks repositories - gostls13.git/commitdiff
%q in fmt: if the object is a Stringer, use String() to get the value to quote.
authorRob Pike <r@golang.org>
Thu, 25 Feb 2010 06:29:37 +0000 (17:29 +1100)
committerRob Pike <r@golang.org>
Thu, 25 Feb 2010 06:29:37 +0000 (17:29 +1100)
R=rsc
CC=golang-dev
https://golang.org/cl/224051

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

index 9fdf0ddb36408f771ae745ab2bc88d9618c800a6..139036eb3758bd47f4af8b07583ff99b872d0109 100644 (file)
@@ -217,6 +217,9 @@ 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}}`},
 
+       // q on Stringable items
+       fmtTest{"%q", I(23), `"<23>"`},
+
        // %p on non-pointers
        fmtTest{"%p", make(chan int), "PTR"},
        fmtTest{"%p", make(map[int]int), "PTR"},
index b2af9da1cb48d8eb019e4938883f63de55cc2364..ffe187a3138e6822edf4368b39fa0d0f90e8ce8a 100644 (file)
@@ -912,6 +912,13 @@ func (p *pp) doprintf(format string, a []interface{}) {
                                goto badtype
                        }
                case 'q':
+                       if field != nil {
+                               // if object implements String, use the result.
+                               if stringer, ok := field.(Stringer); ok {
+                                       p.fmt.fmt_q(stringer.String())
+                                       break
+                               }
+                       }
                        if v, ok := getString(field); ok {
                                p.fmt.fmt_q(v)
                        } else {
index 369f610b2b2ad6c3180a89c291e28cedb7dfa6f3..e4e29bebb85212e518ce2fa49b9de8ea84c46bfe 100644 (file)
@@ -41,7 +41,7 @@ func (v TF) String() string   { return Sprintf("F: %f", v) }
 func (v TF32) String() string { return Sprintf("F32: %f", v) }
 func (v TF64) String() string { return Sprintf("F64: %f", v) }
 func (v TB) String() string   { return Sprintf("B: %t", v) }
-func (v TS) String() string   { return Sprintf("S: %q", v) }
+func (v TS) String() string   { return Sprintf("S: %q", string(v)) }
 
 func check(t *testing.T, got, want string) {
        if got != want {