Commit d984f989 authored by Rob Pike's avatar Rob Pike

minor improvement to formatting: don't allocate padding strings every time.

R=rsc
https://golang.org/cl/164090
parent 272d1563
...@@ -8,12 +8,22 @@ import ( ...@@ -8,12 +8,22 @@ import (
"strconv"; "strconv";
) )
const (
nByte = 64;
nPows10 = 160;
const nByte = 64 ldigits = "0123456789abcdef";
const nPows10 = 160 udigits = "0123456789ABCDEF";
)
var ldigits string = "0123456789abcdef" // var not const because we take its address const padZeros = "0000000000000000000000000000000000000000000000000000000000000000"
var udigits string = "0123456789ABCDEF" 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. Fmt is the raw formatter used by Printf etc. Not meant for normal use.
...@@ -125,22 +135,19 @@ func (f *Fmt) pad(s string) { ...@@ -125,22 +135,19 @@ func (f *Fmt) pad(s string) {
w = -w; w = -w;
} }
w -= len(s); w -= len(s);
padchar := byte(' '); padding := padSpaces;
if left && f.zero { if left && f.zero {
padchar = '0' padding = padZeros
} }
if w > 0 { if w > 0 {
if w > nByte { if w > nByte {
w = nByte w = nByte
} }
buf := make([]byte, w); padding = padding[0:w];
for i := 0; i < w; i++ {
buf[i] = padchar
}
if left { if left {
s = string(buf) + s s = padding + s
} else { } else {
s = s + string(buf) s += padding
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment