From 73be5d82d443def1f721474bff9819155eb5bdf0 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 24 Aug 2016 22:15:07 -0700 Subject: [PATCH] cmd/compile: use printer in vconv Change-Id: Ib30ed686448c4c0a5777cdf1d505ea06eb8b2a47 Reviewed-on: https://go-review.googlesource.com/27917 Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/gc/fmt.go | 54 ++++++++++++++++++------------ 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/src/cmd/compile/internal/gc/fmt.go b/src/cmd/compile/internal/gc/fmt.go index 423e2fc081..9352dbea7d 100644 --- a/src/cmd/compile/internal/gc/fmt.go +++ b/src/cmd/compile/internal/gc/fmt.go @@ -338,6 +338,8 @@ func jconv(n *Node, flag FmtFlag) string { // Fmt "%V": Values func vconv(v Val, flag FmtFlag) string { + var p printer + switch u := v.U.(type) { case *Mpint: if !u.Rune { @@ -347,17 +349,19 @@ func vconv(v Val, flag FmtFlag) string { return bconv(u, 0) } - x := u.Int64() - if ' ' <= x && x < utf8.RuneSelf && x != '\\' && x != '\'' { - return fmt.Sprintf("'%c'", int(x)) + switch x := u.Int64(); { + case ' ' <= x && x < utf8.RuneSelf && x != '\\' && x != '\'': + p.f("'%c'", int(x)) + + case 0 <= x && x < 1<<16: + p.f("'\\u%04x'", uint(int(x))) + + case 0 <= x && x <= utf8.MaxRune: + p.f("'\\U%08x'", uint64(x)) + + default: + p.f("('\\x00' + %v)", u) } - if 0 <= x && x < 1<<16 { - return fmt.Sprintf("'\\u%04x'", uint(int(x))) - } - if 0 <= x && x <= utf8.MaxRune { - return fmt.Sprintf("'\\U%08x'", uint64(x)) - } - return fmt.Sprintf("('\\x00' + %v)", u) case *Mpflt: if flag&FmtSharp != 0 { @@ -366,19 +370,22 @@ func vconv(v Val, flag FmtFlag) string { return fconv(u, FmtSharp) case *Mpcplx: - if flag&FmtSharp != 0 { - return fmt.Sprintf("(%v+%vi)", &u.Real, &u.Imag) - } - if v.U.(*Mpcplx).Real.CmpFloat64(0) == 0 { - return fmt.Sprintf("%vi", fconv(&u.Imag, FmtSharp)) - } - if v.U.(*Mpcplx).Imag.CmpFloat64(0) == 0 { + switch { + case flag&FmtSharp != 0: + p.f("(%v+%vi)", &u.Real, &u.Imag) + + case v.U.(*Mpcplx).Real.CmpFloat64(0) == 0: + p.f("%vi", fconv(&u.Imag, FmtSharp)) + + case v.U.(*Mpcplx).Imag.CmpFloat64(0) == 0: return fconv(&u.Real, FmtSharp) + + case v.U.(*Mpcplx).Imag.CmpFloat64(0) < 0: + p.f("(%v%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp)) + + default: + p.f("(%v+%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp)) } - if v.U.(*Mpcplx).Imag.CmpFloat64(0) < 0 { - return fmt.Sprintf("(%v%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp)) - } - return fmt.Sprintf("(%v+%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp)) case string: return strconv.Quote(u) @@ -391,9 +398,12 @@ func vconv(v Val, flag FmtFlag) string { case *NilVal: return "nil" + + default: + p.f("", v.Ctype()) } - return fmt.Sprintf("", v.Ctype()) + return p.String() } /*