]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: split doPrint into two specialized functions
authorMartin Möhrmann <martisch@uos.de>
Thu, 17 Mar 2016 22:18:14 +0000 (23:18 +0100)
committerRob Pike <r@golang.org>
Sat, 26 Mar 2016 12:12:04 +0000 (12:12 +0000)
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 <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/fmt/print.go

index 06d3049b8f4ab14d9d2315d2460d7fe89592352f..c80223269ba2d0711840adfef800e5a038693dba 100644 (file)
@@ -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')
 }