From: Robert Griesemer Date: Wed, 4 Apr 2018 23:04:22 +0000 (-0700) Subject: text/tabwriter: remove internal use of bytes.Buffer (cleanup) X-Git-Tag: go1.11beta1~967 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f2b5f750df8cf84b4f6653a7b4c9dab0e177584c;p=gostls13.git text/tabwriter: remove internal use of bytes.Buffer (cleanup) Noticed that we can simply use a []byte slice while investigating a separate issue. Did the obvious simplification. Change-Id: I921ebbb42135b5f1a10109236ceb9ae6e94ae7e2 Reviewed-on: https://go-review.googlesource.com/104757 Run-TryBot: Robert Griesemer TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/text/tabwriter/tabwriter.go b/src/text/tabwriter/tabwriter.go index ae6c7a2949..ecda758ab6 100644 --- a/src/text/tabwriter/tabwriter.go +++ b/src/text/tabwriter/tabwriter.go @@ -12,7 +12,6 @@ package tabwriter import ( - "bytes" "io" "unicode/utf8" ) @@ -99,19 +98,19 @@ type Writer struct { flags uint // current state - buf bytes.Buffer // collected text excluding tabs or line breaks - pos int // buffer position up to which cell.width of incomplete cell has been computed - cell cell // current incomplete cell; cell.width is up to buf[pos] excluding ignored sections - endChar byte // terminating char of escaped sequence (Escape for escapes, '>', ';' for HTML tags/entities, or 0) - lines [][]cell // list of lines; each line is a list of cells - widths []int // list of column widths in runes - re-used during formatting + buf []byte // collected text excluding tabs or line breaks + pos int // buffer position up to which cell.width of incomplete cell has been computed + cell cell // current incomplete cell; cell.width is up to buf[pos] excluding ignored sections + endChar byte // terminating char of escaped sequence (Escape for escapes, '>', ';' for HTML tags/entities, or 0) + lines [][]cell // list of lines; each line is a list of cells + widths []int // list of column widths in runes - re-used during formatting } func (b *Writer) addLine() { b.lines = append(b.lines, []cell{}) } // Reset the current state. func (b *Writer) reset() { - b.buf.Reset() + b.buf = b.buf[:0] b.pos = 0 b.cell = cell{} b.endChar = 0 @@ -212,7 +211,7 @@ func (b *Writer) dump() { for i, line := range b.lines { print("(", i, ") ") for _, c := range line { - print("[", string(b.buf.Bytes()[pos:pos+c.size]), "]") + print("[", string(b.buf[pos:pos+c.size]), "]") pos += c.size } print("\n") @@ -294,7 +293,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int) { // non-empty cell useTabs = false if b.flags&AlignRight == 0 { // align left - b.write0(b.buf.Bytes()[pos : pos+c.size]) + b.write0(b.buf[pos : pos+c.size]) pos += c.size if j < len(b.widths) { b.writePadding(c.width, b.widths[j], false) @@ -303,7 +302,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int) { if j < len(b.widths) { b.writePadding(c.width, b.widths[j], false) } - b.write0(b.buf.Bytes()[pos : pos+c.size]) + b.write0(b.buf[pos : pos+c.size]) pos += c.size } } @@ -312,7 +311,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int) { if i+1 == len(b.lines) { // last buffered line - we don't have a newline, so just write // any outstanding buffered data - b.write0(b.buf.Bytes()[pos : pos+b.cell.size]) + b.write0(b.buf[pos : pos+b.cell.size]) pos += b.cell.size } else { // not the last line - write newline @@ -387,14 +386,14 @@ func (b *Writer) format(pos0 int, line0, line1 int) (pos int) { // Append text to current cell. func (b *Writer) append(text []byte) { - b.buf.Write(text) + b.buf = append(b.buf, text...) b.cell.size += len(text) } // Update the cell width. func (b *Writer) updateWidth() { - b.cell.width += utf8.RuneCount(b.buf.Bytes()[b.pos:b.buf.Len()]) - b.pos = b.buf.Len() + b.cell.width += utf8.RuneCount(b.buf[b.pos:]) + b.pos = len(b.buf) } // To escape a text segment, bracket it with Escape characters. @@ -434,7 +433,7 @@ func (b *Writer) endEscape() { case ';': b.cell.width++ // entity, count as one rune } - b.pos = b.buf.Len() + b.pos = len(b.buf) b.endChar = 0 }