avoid closure, less allocation
Vladimir Bauer
4 years ago
| 156 | 156 |
return
|
| 157 | 157 |
}
|
| 158 | 158 |
|
| 159 | |
ow := optimisticWriter(w)
|
| 160 | |
ow(s.components[iLbound].bytes)
|
| 161 | |
defer ow(s.components[iRbound].bytes)
|
|
159 |
mustWrite(w, s.components[iLbound].bytes)
|
|
160 |
defer mustWrite(w, s.components[iRbound].bytes)
|
| 162 | 161 |
|
| 163 | 162 |
if width == 0 {
|
| 164 | 163 |
return
|
|
| 230 | 229 |
}
|
| 231 | 230 |
|
| 232 | 231 |
if s.rev {
|
| 233 | |
flush(ow, padding, filling)
|
|
232 |
flush(w, padding, filling)
|
| 234 | 233 |
} else {
|
| 235 | |
flush(ow, filling, padding)
|
| 236 | |
}
|
| 237 | |
}
|
| 238 | |
|
| 239 | |
func flush(ow func([]byte), filling, padding [][]byte) {
|
|
234 |
flush(w, filling, padding)
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
|
238 |
func flush(w io.Writer, filling, padding [][]byte) {
|
| 240 | 239 |
for i := len(filling) - 1; i >= 0; i-- {
|
| 241 | |
ow(filling[i])
|
|
240 |
mustWrite(w, filling[i])
|
| 242 | 241 |
}
|
| 243 | 242 |
for i := 0; i < len(padding); i++ {
|
| 244 | |
ow(padding[i])
|
| 245 | |
}
|
| 246 | |
}
|
| 247 | |
|
| 248 | |
func optimisticWriter(w io.Writer) func([]byte) {
|
| 249 | |
return func(p []byte) {
|
| 250 | |
_, err := w.Write(p)
|
| 251 | |
if err != nil {
|
| 252 | |
panic(err)
|
| 253 | |
}
|
| 254 | |
}
|
| 255 | |
}
|
|
243 |
mustWrite(w, padding[i])
|
|
244 |
}
|
|
245 |
}
|
|
246 |
|
|
247 |
func mustWrite(w io.Writer, p []byte) {
|
|
248 |
_, err := w.Write(p)
|
|
249 |
if err != nil {
|
|
250 |
panic(err)
|
|
251 |
}
|
|
252 |
}
|
| 1 | 1 |
|
| 2 | 2 |
import "io"
|
| 3 | 3 |
|
| 4 | |
func optimisticStringWriter(w io.Writer) func(string) {
|
| 5 | |
return func(s string) {
|
| 6 | |
_, err := io.WriteString(w, s)
|
| 7 | |
if err != nil {
|
| 8 | |
panic(err)
|
| 9 | |
}
|
|
4 |
func mustWriteString(w io.Writer, s string) {
|
|
5 |
_, err := io.WriteString(w, s)
|
|
6 |
if err != nil {
|
|
7 |
panic(err)
|
| 10 | 8 |
}
|
| 11 | 9 |
}
|
| 22 | 22 |
}
|
| 23 | 23 |
}
|
| 24 | 24 |
|
| 25 | |
osw := optimisticStringWriter(st)
|
| 26 | |
osw(strconv.FormatFloat(float64(s), 'f', prec, 64))
|
|
25 |
mustWriteString(st, strconv.FormatFloat(float64(s), 'f', prec, 64))
|
| 27 | 26 |
if st.Flag(' ') {
|
| 28 | |
osw(" ")
|
|
27 |
mustWriteString(st, " ")
|
| 29 | 28 |
}
|
| 30 | |
osw("%")
|
|
29 |
mustWriteString(st, "%")
|
| 31 | 30 |
}
|
| 32 | 31 |
|
| 33 | 32 |
// Percentage returns percentage decorator. It's a wrapper of NewPercentage.
|
| 48 | 48 |
unit = _iTiB
|
| 49 | 49 |
}
|
| 50 | 50 |
|
| 51 | |
osw := optimisticStringWriter(st)
|
| 52 | |
osw(strconv.FormatFloat(float64(self)/float64(unit), 'f', prec, 64))
|
|
51 |
mustWriteString(st, strconv.FormatFloat(float64(self)/float64(unit), 'f', prec, 64))
|
| 53 | 52 |
if st.Flag(' ') {
|
| 54 | |
osw(" ")
|
|
53 |
mustWriteString(st, " ")
|
| 55 | 54 |
}
|
| 56 | |
osw(unit.String())
|
|
55 |
mustWriteString(st, unit.String())
|
| 57 | 56 |
}
|
| 58 | 57 |
|
| 59 | 58 |
const (
|
|
| 97 | 96 |
unit = _TB
|
| 98 | 97 |
}
|
| 99 | 98 |
|
| 100 | |
osw := optimisticStringWriter(st)
|
| 101 | |
osw(strconv.FormatFloat(float64(self)/float64(unit), 'f', prec, 64))
|
|
99 |
mustWriteString(st, strconv.FormatFloat(float64(self)/float64(unit), 'f', prec, 64))
|
| 102 | 100 |
if st.Flag(' ') {
|
| 103 | |
osw(" ")
|
|
101 |
mustWriteString(st, " ")
|
| 104 | 102 |
}
|
| 105 | |
osw(unit.String())
|
|
103 |
mustWriteString(st, unit.String())
|
| 106 | 104 |
}
|
| 22 | 22 |
|
| 23 | 23 |
func (self *speedFormatter) Format(st fmt.State, verb rune) {
|
| 24 | 24 |
self.Formatter.Format(st, verb)
|
| 25 | |
optimisticStringWriter(st)("/s")
|
|
25 |
mustWriteString(st, "/s")
|
| 26 | 26 |
}
|
| 27 | 27 |
|
| 28 | 28 |
// EwmaSpeed exponential-weighted-moving-average based speed decorator.
|