Commit f2b5f750 authored by Robert Griesemer's avatar Robert Griesemer

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 <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent b9a36568
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
package tabwriter package tabwriter
import ( import (
"bytes"
"io" "io"
"unicode/utf8" "unicode/utf8"
) )
...@@ -99,7 +98,7 @@ type Writer struct { ...@@ -99,7 +98,7 @@ type Writer struct {
flags uint flags uint
// current state // current state
buf bytes.Buffer // collected text excluding tabs or line breaks buf []byte // collected text excluding tabs or line breaks
pos int // buffer position up to which cell.width of incomplete cell has been computed 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 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) endChar byte // terminating char of escaped sequence (Escape for escapes, '>', ';' for HTML tags/entities, or 0)
...@@ -111,7 +110,7 @@ func (b *Writer) addLine() { b.lines = append(b.lines, []cell{}) } ...@@ -111,7 +110,7 @@ func (b *Writer) addLine() { b.lines = append(b.lines, []cell{}) }
// Reset the current state. // Reset the current state.
func (b *Writer) reset() { func (b *Writer) reset() {
b.buf.Reset() b.buf = b.buf[:0]
b.pos = 0 b.pos = 0
b.cell = cell{} b.cell = cell{}
b.endChar = 0 b.endChar = 0
...@@ -212,7 +211,7 @@ func (b *Writer) dump() { ...@@ -212,7 +211,7 @@ func (b *Writer) dump() {
for i, line := range b.lines { for i, line := range b.lines {
print("(", i, ") ") print("(", i, ") ")
for _, c := range line { for _, c := range line {
print("[", string(b.buf.Bytes()[pos:pos+c.size]), "]") print("[", string(b.buf[pos:pos+c.size]), "]")
pos += c.size pos += c.size
} }
print("\n") print("\n")
...@@ -294,7 +293,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int) { ...@@ -294,7 +293,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int) {
// non-empty cell // non-empty cell
useTabs = false useTabs = false
if b.flags&AlignRight == 0 { // align left 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 pos += c.size
if j < len(b.widths) { if j < len(b.widths) {
b.writePadding(c.width, b.widths[j], false) b.writePadding(c.width, b.widths[j], false)
...@@ -303,7 +302,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int) { ...@@ -303,7 +302,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int) {
if j < len(b.widths) { if j < len(b.widths) {
b.writePadding(c.width, b.widths[j], false) 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 pos += c.size
} }
} }
...@@ -312,7 +311,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int) { ...@@ -312,7 +311,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int) {
if i+1 == len(b.lines) { if i+1 == len(b.lines) {
// last buffered line - we don't have a newline, so just write // last buffered line - we don't have a newline, so just write
// any outstanding buffered data // 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 pos += b.cell.size
} else { } else {
// not the last line - write newline // not the last line - write newline
...@@ -387,14 +386,14 @@ func (b *Writer) format(pos0 int, line0, line1 int) (pos int) { ...@@ -387,14 +386,14 @@ func (b *Writer) format(pos0 int, line0, line1 int) (pos int) {
// Append text to current cell. // Append text to current cell.
func (b *Writer) append(text []byte) { func (b *Writer) append(text []byte) {
b.buf.Write(text) b.buf = append(b.buf, text...)
b.cell.size += len(text) b.cell.size += len(text)
} }
// Update the cell width. // Update the cell width.
func (b *Writer) updateWidth() { func (b *Writer) updateWidth() {
b.cell.width += utf8.RuneCount(b.buf.Bytes()[b.pos:b.buf.Len()]) b.cell.width += utf8.RuneCount(b.buf[b.pos:])
b.pos = b.buf.Len() b.pos = len(b.buf)
} }
// To escape a text segment, bracket it with Escape characters. // To escape a text segment, bracket it with Escape characters.
...@@ -434,7 +433,7 @@ func (b *Writer) endEscape() { ...@@ -434,7 +433,7 @@ func (b *Writer) endEscape() {
case ';': case ';':
b.cell.width++ // entity, count as one rune b.cell.width++ // entity, count as one rune
} }
b.pos = b.buf.Len() b.pos = len(b.buf)
b.endChar = 0 b.endChar = 0
} }
......
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