Skip to content

Commit 03450fb

Browse files
committed
Repeat with fewer writes
1 parent 29dc210 commit 03450fb

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

encode.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ func (enc *TableEncoder) header() {
410410

411411
if enc.title != nil && enc.title.Width != 0 {
412412
maxWidth := ((enc.tableWidth() - enc.title.Width) / 2) + enc.title.Width
413-
enc.writeAligned(enc.title.Buf, rs.filler, AlignRight, maxWidth-enc.title.Width)
413+
enc.writeAligned(enc.title.Buf, &rs, AlignRight, maxWidth-enc.title.Width)
414414
enc.w.Write(enc.newline)
415415
}
416416
// draw top border
@@ -472,7 +472,7 @@ func (enc TableEncoder) rowStyle(r [4]rune) rowStyle {
472472
wrapper: []byte(string(enc.lineStyle.Wrap[1])),
473473
middle: []byte(middle),
474474
right: []byte(right + string(enc.newline)),
475-
filler: []byte(filler),
475+
filler: bytes.Repeat([]byte(filler), 8),
476476
hasWrapping: runewidth.RuneWidth(enc.lineStyle.Row[1]) > 0,
477477
}
478478
}
@@ -497,11 +497,11 @@ func (enc *TableEncoder) divider(rs rowStyle) {
497497

498498
for i, width := range enc.maxWidths {
499499
// column
500-
repeat(enc.w, rs.filler, width)
500+
rs.filler = repeat(enc.w, rs.filler, width)
501501

502502
// line feed indicator
503503
if rs.hasWrapping && enc.border >= 1 {
504-
enc.w.Write(rs.filler)
504+
enc.w.Write(rs.filler[:1])
505505
}
506506

507507
// middle separator
@@ -614,11 +614,10 @@ func (enc *TableEncoder) row(vals []*Value, rs rowStyle) {
614614
if enc.border <= 1 && i == len(vals)-1 && (!rs.hasWrapping || l >= len(v.Newlines)) {
615615
padding = 0
616616
}
617-
enc.writeAligned(v.Buf[start:end], rs.filler, v.Align, padding)
617+
enc.writeAligned(v.Buf[start:end], &rs, v.Align, padding)
618618
} else {
619619
if enc.border > 1 || i != len(vals)-1 {
620-
enc.w.Write(bytes.Repeat(rs.filler, enc.maxWidths[i]))
621-
repeat(enc.w, rs.filler, enc.maxWidths[i])
620+
rs.filler = repeat(enc.w, rs.filler, enc.maxWidths[i])
622621
}
623622
}
624623

@@ -627,7 +626,7 @@ func (enc *TableEncoder) row(vals []*Value, rs rowStyle) {
627626
if l < len(v.Newlines) {
628627
enc.w.Write(rs.wrapper)
629628
} else {
630-
enc.w.Write(rs.filler)
629+
enc.w.Write(rs.filler[:1])
631630
}
632631
}
633632

@@ -651,7 +650,7 @@ func (enc *TableEncoder) row(vals []*Value, rs rowStyle) {
651650
}
652651
}
653652

654-
func (enc *TableEncoder) writeAligned(b, filler []byte, a Align, padding int) {
653+
func (enc *TableEncoder) writeAligned(b []byte, rs *rowStyle, a Align, padding int) {
655654
// calc padding
656655
paddingLeft := 0
657656
paddingRight := 0
@@ -669,15 +668,15 @@ func (enc *TableEncoder) writeAligned(b, filler []byte, a Align, padding int) {
669668

670669
// add padding left
671670
if paddingLeft > 0 {
672-
repeat(enc.w, filler, paddingLeft)
671+
rs.filler = repeat(enc.w, rs.filler, paddingLeft)
673672
}
674673

675674
// write
676675
enc.w.Write(b)
677676

678677
// add padding right
679678
if paddingRight > 0 {
680-
repeat(enc.w, filler, paddingRight)
679+
rs.filler = repeat(enc.w, rs.filler, paddingRight)
681680
}
682681
}
683682

@@ -950,10 +949,10 @@ func (enc *ExpandedEncoder) record(i int, vals []*Value, rs rowStyle) {
950949
enc.w.WriteString(header)
951950
padding := enc.maxWidths[0] + enc.maxWidths[1] + runewidth.StringWidth(string(headerRS.middle))*2 - len(header) - 1
952951
if padding > 0 {
953-
repeat(enc.w, headerRS.filler, padding)
952+
headerRS.filler = repeat(enc.w, headerRS.filler, padding)
954953
}
955954
// write newline wrap value
956-
enc.w.Write(headerRS.filler)
955+
enc.w.Write(headerRS.filler[:1])
957956
enc.w.Write(headerRS.right)
958957
}
959958

util.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,15 @@ func condWrite(w io.Writer, repeat int, runes ...rune) error {
108108
return err
109109
}
110110

111-
func repeat(w io.Writer, b []byte, c int) {
112-
for i := 0; i < c; i++ {
113-
w.Write(b)
111+
// repeat b c times and write it to w, returning b with increased capacity
112+
func repeat(w io.Writer, b []byte, c int) []byte {
113+
if len(b) >= c {
114+
w.Write(b[:c])
115+
return b
114116
}
117+
for len(b) < c {
118+
b = append(b, b...)
119+
}
120+
w.Write(b[:c])
121+
return b
115122
}

util_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os/exec"
1313
"strconv"
1414
"strings"
15+
"testing"
1516
"time"
1617
"unicode"
1718
)
@@ -356,3 +357,20 @@ type noopWriter struct {
356357
func (*noopWriter) Write(buf []byte) (int, error) {
357358
return len(buf), nil
358359
}
360+
361+
func TestRepeat(t *testing.T) {
362+
buf := new(bytes.Buffer)
363+
filler := []byte("x")
364+
filler = repeat(buf, filler, 10)
365+
filler = repeat(buf, filler, 10)
366+
if cap(filler) != 16 {
367+
t.Errorf("Expected filler to have cap of 16, got %d", cap(filler))
368+
}
369+
if len(filler) != 1 {
370+
t.Errorf("Expected filler to have len of 1, got %d", len(filler))
371+
}
372+
actual := buf.String()
373+
if actual != "xxxxxxxxxxxxxxxxxxxx" {
374+
t.Errorf("Expected buf to be 20 x's, got %s", actual)
375+
}
376+
}

0 commit comments

Comments
 (0)