]> Cypherpunks repositories - gostls13.git/commitdiff
minor improvement to formatting: don't allocate padding strings every time.
authorRob Pike <r@golang.org>
Thu, 3 Dec 2009 08:04:40 +0000 (00:04 -0800)
committerRob Pike <r@golang.org>
Thu, 3 Dec 2009 08:04:40 +0000 (00:04 -0800)
R=rsc
https://golang.org/cl/164090

src/pkg/fmt/format.go

index bf13ac3144eba917f7debf124620e31123cd2170..c7b7b9bf37c8842389909a79f605cbebda4bd113 100644 (file)
@@ -8,12 +8,22 @@ import (
        "strconv";
 )
 
+const (
+       nByte   = 64;
+       nPows10 = 160;
 
-const nByte = 64
-const nPows10 = 160
+       ldigits = "0123456789abcdef";
+       udigits = "0123456789ABCDEF";
+)
 
-var ldigits string = "0123456789abcdef"        // var not const because we take its address
-var udigits string = "0123456789ABCDEF"
+const padZeros = "0000000000000000000000000000000000000000000000000000000000000000"
+const padSpaces = "                                                                "
+
+func init() {
+       if len(padZeros) != nByte || len(padSpaces) != nByte {
+               panic("fmt padding wrong length")
+       }
+}
 
 /*
        Fmt is the raw formatter used by Printf etc.  Not meant for normal use.
@@ -125,22 +135,19 @@ func (f *Fmt) pad(s string) {
                        w = -w;
                }
                w -= len(s);
-               padchar := byte(' ');
+               padding := padSpaces;
                if left && f.zero {
-                       padchar = '0'
+                       padding = padZeros
                }
                if w > 0 {
                        if w > nByte {
                                w = nByte
                        }
-                       buf := make([]byte, w);
-                       for i := 0; i < w; i++ {
-                               buf[i] = padchar
-                       }
+                       padding = padding[0:w];
                        if left {
-                               s = string(buf) + s
+                               s = padding + s
                        } else {
-                               s = s + string(buf)
+                               s += padding
                        }
                }
        }