From ad391c908b8e4532937ee912caf8ddb8f3891ef8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20Mo=CC=88hrmann?= Date: Thu, 17 Mar 2016 23:18:14 +0100 Subject: [PATCH] fmt: split doPrint into two specialized functions Remove format flag reset from doPrint. Flags will not be set in doPrint and printArg will not return with flags modified. Remove the extra arguments addspace and addnewline and split up doPrint into two simpler and specialized functions. Change-Id: Ib884d027abfbb31c6f01b008f51d6d76fc0c1a17 Reviewed-on: https://go-review.googlesource.com/21181 Run-TryBot: Rob Pike TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- src/fmt/print.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/fmt/print.go b/src/fmt/print.go index 06d3049b8f..c80223269b 100644 --- a/src/fmt/print.go +++ b/src/fmt/print.go @@ -210,7 +210,7 @@ func Errorf(format string, a ...interface{}) error { // It returns the number of bytes written and any write error encountered. func Fprint(w io.Writer, a ...interface{}) (n int, err error) { p := newPrinter() - p.doPrint(a, false, false) + p.doPrint(a) n, err = w.Write(p.buf) p.free() return @@ -227,7 +227,7 @@ func Print(a ...interface{}) (n int, err error) { // Spaces are added between operands when neither is a string. func Sprint(a ...interface{}) string { p := newPrinter() - p.doPrint(a, false, false) + p.doPrint(a) s := string(p.buf) p.free() return s @@ -242,7 +242,7 @@ func Sprint(a ...interface{}) string { // It returns the number of bytes written and any write error encountered. func Fprintln(w io.Writer, a ...interface{}) (n int, err error) { p := newPrinter() - p.doPrint(a, true, true) + p.doPrintln(a) n, err = w.Write(p.buf) p.free() return @@ -259,7 +259,7 @@ func Println(a ...interface{}) (n int, err error) { // Spaces are always added between operands and a newline is appended. func Sprintln(a ...interface{}) string { p := newPrinter() - p.doPrint(a, true, true) + p.doPrintln(a) s := string(p.buf) p.free() return s @@ -1127,20 +1127,27 @@ formatLoop: } } -func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) { +func (p *pp) doPrint(a []interface{}) { prevString := false for argNum, arg := range a { - p.fmt.clearflags() isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String - // Add a space between two non-string arguments or if - // explicitly asked for by addspace. - if argNum > 0 && (addspace || (!isString && !prevString)) { + // Add a space between two non-string arguments. + if argNum > 0 && !isString && !prevString { p.buf.WriteByte(' ') } p.printArg(arg, 'v') prevString = isString } - if addnewline { - p.buf.WriteByte('\n') +} + +// doPrintln is like doPrint but always adds a space between arguments +// and a newline after the last argument. +func (p *pp) doPrintln(a []interface{}) { + for argNum, arg := range a { + if argNum > 0 { + p.buf.WriteByte(' ') + } + p.printArg(arg, 'v') } + p.buf.WriteByte('\n') } -- 2.48.1