summaryrefslogtreecommitdiff
path: root/src/pkg/tabwriter
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2009-12-15 15:41:46 -0800
committerRobert Griesemer <gri@golang.org>2009-12-15 15:41:46 -0800
commit3743fa38e180c74c51aae84eda082067e8e12523 (patch)
tree274d1d9bf832b7834ab60c65acdf945576271d14 /src/pkg/tabwriter
parent13ac778ef2f757c7cd636b4336a2bd6c8f403b43 (diff)
downloadgolang-3743fa38e180c74c51aae84eda082067e8e12523.tar.gz
1) Change default gofmt default settings for
parsing and printing to new syntax. Use -oldparser to parse the old syntax, use -oldprinter to print the old syntax. 2) Change default gofmt formatting settings to use tabs for indentation only and to use spaces for alignment. This will make the code alignment insensitive to an editor's tabwidth. Use -spaces=false to use tabs for alignment. 3) Manually changed src/exp/parser/parser_test.go so that it doesn't try to parse the parser's source files using the old syntax (they have new syntax now). 4) gofmt -w src misc test/bench 5th and last set of files. R=rsc CC=golang-dev http://codereview.appspot.com/180050
Diffstat (limited to 'src/pkg/tabwriter')
-rw-r--r--src/pkg/tabwriter/tabwriter.go246
-rw-r--r--src/pkg/tabwriter/tabwriter_test.go82
2 files changed, 164 insertions, 164 deletions
diff --git a/src/pkg/tabwriter/tabwriter.go b/src/pkg/tabwriter/tabwriter.go
index 316950c23..1f3ed9e33 100644
--- a/src/pkg/tabwriter/tabwriter.go
+++ b/src/pkg/tabwriter/tabwriter.go
@@ -11,11 +11,11 @@
package tabwriter
import (
- "bytes";
- "container/vector";
- "io";
- "os";
- "utf8";
+ "bytes"
+ "container/vector"
+ "io"
+ "os"
+ "utf8"
)
@@ -28,9 +28,9 @@ import (
// ('\t') terminated cell.
//
type cell struct {
- size int; // cell size in bytes
- width int; // cell width in runes
- htab bool; // true if the cell is terminated by an htab ('\t')
+ size int // cell size in bytes
+ width int // cell width in runes
+ htab bool // true if the cell is terminated by an htab ('\t')
}
@@ -78,38 +78,38 @@ type cell struct {
//
type Writer struct {
// configuration
- output io.Writer;
- minwidth int;
- tabwidth int;
- padding int;
- padbytes [8]byte;
- flags uint;
+ output io.Writer
+ minwidth int
+ tabwidth int
+ padding int
+ padbytes [8]byte
+ 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 vector.Vector; // list of lines; each line is a list of cells
- widths vector.IntVector; // list of column widths in runes - re-used during formatting
+ 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 vector.Vector // list of lines; each line is a list of cells
+ widths vector.IntVector // list of column widths in runes - re-used during formatting
}
-func (b *Writer) addLine() { b.lines.Push(new(vector.Vector)) }
+func (b *Writer) addLine() { b.lines.Push(new(vector.Vector)) }
-func (b *Writer) line(i int) *vector.Vector { return b.lines.At(i).(*vector.Vector) }
+func (b *Writer) line(i int) *vector.Vector { return b.lines.At(i).(*vector.Vector) }
// Reset the current state.
func (b *Writer) reset() {
- b.buf.Reset();
- b.pos = 0;
- b.cell = cell{};
- b.endChar = 0;
- b.lines.Resize(0, 0);
- b.widths.Resize(0, 0);
- b.addLine();
+ b.buf.Reset()
+ b.pos = 0
+ b.cell = cell{}
+ b.endChar = 0
+ b.lines.Resize(0, 0)
+ b.widths.Resize(0, 0)
+ b.addLine()
}
@@ -141,23 +141,23 @@ func (b *Writer) reset() {
const (
// Ignore html tags and treat entities (starting with '&'
// and ending in ';') as single characters (width = 1).
- FilterHTML uint = 1 << iota;
+ FilterHTML uint = 1 << iota
// Force right-alignment of cell content.
// Default is left-alignment.
- AlignRight;
+ AlignRight
// Handle empty columns as if they were not present in
// the input in the first place.
- DiscardEmptyColumns;
+ DiscardEmptyColumns
// Always use tabs for indentation columns (i.e., padding of
// leading empty cells on the left) independent of padchar.
- TabIndent;
+ TabIndent
// Print a vertical bar ('|') between columns (after formatting).
// Discarded colums appear as zero-width columns ("||").
- Debug;
+ Debug
)
@@ -185,10 +185,10 @@ func (b *Writer) Init(output io.Writer, minwidth, tabwidth, padding int, padchar
if minwidth < 0 || tabwidth < 0 || padding < 0 {
panic("negative minwidth, tabwidth, or padding")
}
- b.output = output;
- b.minwidth = minwidth;
- b.tabwidth = tabwidth;
- b.padding = padding;
+ b.output = output
+ b.minwidth = minwidth
+ b.tabwidth = tabwidth
+ b.padding = padding
for i := range b.padbytes {
b.padbytes[i] = padchar
}
@@ -196,37 +196,37 @@ func (b *Writer) Init(output io.Writer, minwidth, tabwidth, padding int, padchar
// tab padding enforces left-alignment
flags &^= AlignRight
}
- b.flags = flags;
+ b.flags = flags
- b.reset();
+ b.reset()
- return b;
+ return b
}
// debugging support (keep code around)
func (b *Writer) dump() {
- pos := 0;
+ pos := 0
for i := 0; i < b.lines.Len(); i++ {
- line := b.line(i);
- print("(", i, ") ");
+ line := b.line(i)
+ print("(", i, ") ")
for j := 0; j < line.Len(); j++ {
- c := line.At(j).(cell);
- print("[", string(b.buf.Bytes()[pos:pos+c.size]), "]");
- pos += c.size;
+ c := line.At(j).(cell)
+ print("[", string(b.buf.Bytes()[pos:pos+c.size]), "]")
+ pos += c.size
}
- print("\n");
+ print("\n")
}
- print("\n");
+ print("\n")
}
func (b *Writer) write0(buf []byte) os.Error {
- n, err := b.output.Write(buf);
+ n, err := b.output.Write(buf)
if n != len(buf) && err == nil {
err = os.EIO
}
- return err;
+ return err
}
@@ -235,15 +235,15 @@ func (b *Writer) writeN(src []byte, n int) os.Error {
if err := b.write0(src); err != nil {
return err
}
- n -= len(src);
+ n -= len(src)
}
- return b.write0(src[0:n]);
+ return b.write0(src[0:n])
}
var (
- newline = []byte{'\n'};
- tabs = []byte{'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t'};
+ newline = []byte{'\n'}
+ tabs = []byte{'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t'}
)
@@ -251,34 +251,34 @@ func (b *Writer) writePadding(textw, cellw int, useTabs bool) os.Error {
if b.padbytes[0] == '\t' || useTabs {
// padding is done with tabs
if b.tabwidth == 0 {
- return nil // tabs have no width - can't do any padding
+ return nil // tabs have no width - can't do any padding
}
// make cellw the smallest multiple of b.tabwidth
- cellw = (cellw + b.tabwidth - 1) / b.tabwidth * b.tabwidth;
- n := cellw - textw; // amount of padding
+ cellw = (cellw + b.tabwidth - 1) / b.tabwidth * b.tabwidth
+ n := cellw - textw // amount of padding
if n < 0 {
panic("internal error")
}
- return b.writeN(tabs, (n+b.tabwidth-1)/b.tabwidth);
+ return b.writeN(tabs, (n+b.tabwidth-1)/b.tabwidth)
}
// padding is done with non-tab characters
- return b.writeN(&b.padbytes, cellw-textw);
+ return b.writeN(&b.padbytes, cellw-textw)
}
var vbar = []byte{'|'}
func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int, err os.Error) {
- pos = pos0;
+ pos = pos0
for i := line0; i < line1; i++ {
- line := b.line(i);
+ line := b.line(i)
// if TabIndent is set, use tabs to pad leading empty cells
- useTabs := b.flags&TabIndent != 0;
+ useTabs := b.flags&TabIndent != 0
for j := 0; j < line.Len(); j++ {
- c := line.At(j).(cell);
+ c := line.At(j).(cell)
if j > 0 && b.flags&Debug != 0 {
if err = b.write0(vbar); err != nil {
@@ -295,18 +295,18 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int, err os.Error)
}
} else {
// non-empty cell
- useTabs = false;
- if b.flags&AlignRight == 0 { // align left
+ useTabs = false
+ if b.flags&AlignRight == 0 { // align left
if err = b.write0(b.buf.Bytes()[pos : pos+c.size]); err != nil {
return
}
- pos += c.size;
+ pos += c.size
if j < b.widths.Len() {
if err = b.writePadding(c.width, b.widths.At(j), false); err != nil {
return
}
}
- } else { // align right
+ } else { // align right
if j < b.widths.Len() {
if err = b.writePadding(c.width, b.widths.At(j), false); err != nil {
return
@@ -315,7 +315,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int, err os.Error)
if err = b.write0(b.buf.Bytes()[pos : pos+c.size]); err != nil {
return
}
- pos += c.size;
+ pos += c.size
}
}
}
@@ -326,7 +326,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int, err os.Error)
if err = b.write0(b.buf.Bytes()[pos : pos+b.cell.size]); err != nil {
return
}
- pos += b.cell.size;
+ pos += b.cell.size
} else {
// not the last line - write newline
if err = b.write0(newline); err != nil {
@@ -334,7 +334,7 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int, err os.Error)
}
}
}
- return;
+ return
}
@@ -344,10 +344,10 @@ func (b *Writer) writeLines(pos0 int, line0, line1 int) (pos int, err os.Error)
// line1 and an error, if any.
//
func (b *Writer) format(pos0 int, line0, line1 int) (pos int, err os.Error) {
- pos = pos0;
- column := b.widths.Len();
+ pos = pos0
+ column := b.widths.Len()
for this := line0; this < line1; this++ {
- line := b.line(this);
+ line := b.line(this)
if column < line.Len()-1 {
// cell exists in this column => this line
@@ -361,16 +361,16 @@ func (b *Writer) format(pos0 int, line0, line1 int) (pos int, err os.Error) {
if pos, err = b.writeLines(pos, line0, this); err != nil {
return
}
- line0 = this;
+ line0 = this
// column block begin
- width := b.minwidth; // minimal column width
- discardable := true; // true if all cells in this column are empty and "soft"
+ width := b.minwidth // minimal column width
+ discardable := true // true if all cells in this column are empty and "soft"
for ; this < line1; this++ {
- line = b.line(this);
+ line = b.line(this)
if column < line.Len()-1 {
// cell exists in this column
- c := line.At(column).(cell);
+ c := line.At(column).(cell)
// update width
if w := c.width + b.padding; w > width {
width = w
@@ -392,29 +392,29 @@ func (b *Writer) format(pos0 int, line0, line1 int) (pos int, err os.Error) {
// format and print all columns to the right of this column
// (we know the widths of this column and all columns to the left)
- b.widths.Push(width);
- pos, err = b.format(pos, line0, this);
- b.widths.Pop();
- line0 = this;
+ b.widths.Push(width)
+ pos, err = b.format(pos, line0, this)
+ b.widths.Pop()
+ line0 = this
}
}
// print unprinted lines until end
- return b.writeLines(pos, line0, line1);
+ return b.writeLines(pos, line0, line1)
}
// Append text to current cell.
func (b *Writer) append(text []byte) {
- b.buf.Write(text);
- b.cell.size += len(text);
+ b.buf.Write(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.Bytes()[b.pos:b.buf.Len()])
+ b.pos = b.buf.Len()
}
@@ -450,12 +450,12 @@ func (b *Writer) endEscape() {
switch b.endChar {
case Escape:
b.updateWidth()
- case '>': // tag of zero width
+ case '>': // tag of zero width
case ';':
- b.cell.width++ // entity, count as one rune
+ b.cell.width++ // entity, count as one rune
}
- b.pos = b.buf.Len();
- b.endChar = 0;
+ b.pos = b.buf.Len()
+ b.endChar = 0
}
@@ -463,11 +463,11 @@ func (b *Writer) endEscape() {
// current line. Returns the number of cells in that line.
//
func (b *Writer) terminateCell(htab bool) int {
- b.cell.htab = htab;
- line := b.line(b.lines.Len() - 1);
- line.Push(b.cell);
- b.cell = cell{};
- return line.Len();
+ b.cell.htab = htab
+ line := b.line(b.lines.Len() - 1)
+ line.Push(b.cell)
+ b.cell = cell{}
+ return line.Len()
}
@@ -483,16 +483,16 @@ func (b *Writer) Flush() os.Error {
// inside escape - terminate it even if incomplete
b.endEscape()
}
- b.terminateCell(false);
+ b.terminateCell(false)
}
// format contents of buffer
- _, err := b.format(0, 0, b.lines.Len());
+ _, err := b.format(0, 0, b.lines.Len())
// reset, even in the presence of errors
- b.reset();
+ b.reset()
- return err;
+ return err
}
@@ -502,20 +502,20 @@ func (b *Writer) Flush() os.Error {
//
func (b *Writer) Write(buf []byte) (n int, err os.Error) {
// split text into cells
- n = 0;
+ n = 0
for i, ch := range buf {
if b.endChar == 0 {
// outside escape
switch ch {
case '\t', '\v', '\n', '\f':
// end of cell
- b.append(buf[n:i]);
- b.updateWidth();
- n = i + 1; // ch consumed
- ncells := b.terminateCell(ch == '\t');
+ b.append(buf[n:i])
+ b.updateWidth()
+ n = i + 1 // ch consumed
+ ncells := b.terminateCell(ch == '\t')
if ch == '\n' || ch == '\f' {
// terminate line
- b.addLine();
+ b.addLine()
if ch == '\f' || ncells == 1 {
// A '\f' always forces a flush. Otherwise, if the previous
// line has only one cell which does not have an impact on
@@ -530,19 +530,19 @@ func (b *Writer) Write(buf []byte) (n int, err os.Error) {
case Escape:
// start of escaped sequence
- b.append(buf[n:i]);
- b.updateWidth();
- n = i + 1; // exclude Escape
- b.startEscape(Escape);
+ b.append(buf[n:i])
+ b.updateWidth()
+ n = i + 1 // exclude Escape
+ b.startEscape(Escape)
case '<', '&':
// possibly an html tag/entity
if b.flags&FilterHTML != 0 {
// begin of tag/entity
- b.append(buf[n:i]);
- b.updateWidth();
- n = i;
- b.startEscape(ch);
+ b.append(buf[n:i])
+ b.updateWidth()
+ n = i
+ b.startEscape(ch)
}
}
@@ -550,21 +550,21 @@ func (b *Writer) Write(buf []byte) (n int, err os.Error) {
// inside escape
if ch == b.endChar {
// end of tag/entity
- j := i + 1;
+ j := i + 1
if ch == Escape {
- j = i // exclude Escape
+ j = i // exclude Escape
}
- b.append(buf[n:j]);
- n = i + 1; // ch consumed
- b.endEscape();
+ b.append(buf[n:j])
+ n = i + 1 // ch consumed
+ b.endEscape()
}
}
}
// append leftover text
- b.append(buf[n:]);
- n = len(buf);
- return;
+ b.append(buf[n:])
+ n = len(buf)
+ return
}
diff --git a/src/pkg/tabwriter/tabwriter_test.go b/src/pkg/tabwriter/tabwriter_test.go
index 8503ddd24..c8823cfef 100644
--- a/src/pkg/tabwriter/tabwriter_test.go
+++ b/src/pkg/tabwriter/tabwriter_test.go
@@ -5,43 +5,43 @@
package tabwriter
import (
- "io";
- "os";
- "testing";
+ "io"
+ "os"
+ "testing"
)
type buffer struct {
- a []byte;
+ a []byte
}
-func (b *buffer) init(n int) { b.a = make([]byte, n)[0:0] }
+func (b *buffer) init(n int) { b.a = make([]byte, n)[0:0] }
-func (b *buffer) clear() { b.a = b.a[0:0] }
+func (b *buffer) clear() { b.a = b.a[0:0] }
func (b *buffer) Write(buf []byte) (written int, err os.Error) {
- n := len(b.a);
- m := len(buf);
+ n := len(b.a)
+ m := len(buf)
if n+m <= cap(b.a) {
- b.a = b.a[0 : n+m];
+ b.a = b.a[0 : n+m]
for i := 0; i < m; i++ {
b.a[n+i] = buf[i]
}
} else {
panicln("buffer.Write: buffer too small", n, m, cap(b.a))
}
- return len(buf), nil;
+ return len(buf), nil
}
-func (b *buffer) String() string { return string(b.a) }
+func (b *buffer) String() string { return string(b.a) }
func write(t *testing.T, testname string, w *Writer, src string) {
- written, err := io.WriteString(w, src);
+ written, err := io.WriteString(w, src)
if err != nil {
t.Errorf("--- test: %s\n--- src:\n%s\n--- write error: %v\n", testname, src, err)
}
@@ -52,12 +52,12 @@ func write(t *testing.T, testname string, w *Writer, src string) {
func verify(t *testing.T, testname string, w *Writer, b *buffer, src, expected string) {
- err := w.Flush();
+ err := w.Flush()
if err != nil {
t.Errorf("--- test: %s\n--- src:\n%s\n--- flush error: %v\n", testname, src, err)
}
- res := b.String();
+ res := b.String()
if res != expected {
t.Errorf("--- test: %s\n--- src:\n%s\n--- found:\n%s\n--- expected:\n%s\n", testname, src, res, expected)
}
@@ -65,43 +65,43 @@ func verify(t *testing.T, testname string, w *Writer, b *buffer, src, expected s
func check(t *testing.T, testname string, minwidth, tabwidth, padding int, padchar byte, flags uint, src, expected string) {
- var b buffer;
- b.init(1000);
+ var b buffer
+ b.init(1000)
- var w Writer;
- w.Init(&b, minwidth, tabwidth, padding, padchar, flags);
+ var w Writer
+ w.Init(&b, minwidth, tabwidth, padding, padchar, flags)
// write all at once
- b.clear();
- write(t, testname, &w, src);
- verify(t, testname, &w, &b, src, expected);
+ b.clear()
+ write(t, testname, &w, src)
+ verify(t, testname, &w, &b, src, expected)
// write byte-by-byte
- b.clear();
+ b.clear()
for i := 0; i < len(src); i++ {
write(t, testname, &w, src[i:i+1])
}
- verify(t, testname, &w, &b, src, expected);
+ verify(t, testname, &w, &b, src, expected)
// write using Fibonacci slice sizes
- b.clear();
+ b.clear()
for i, d := 0, 0; i < len(src); {
- write(t, testname, &w, src[i:i+d]);
- i, d = i+d, d+1;
+ write(t, testname, &w, src[i:i+d])
+ i, d = i+d, d+1
if i+d > len(src) {
d = len(src) - i
}
}
- verify(t, testname, &w, &b, src, expected);
+ verify(t, testname, &w, &b, src, expected)
}
type entry struct {
- testname string;
- minwidth, tabwidth, padding int;
- padchar byte;
- flags uint;
- src, expected string;
+ testname string
+ minwidth, tabwidth, padding int
+ padchar byte
+ flags uint
+ src, expected string
}
@@ -144,7 +144,7 @@ var tests = []entry{
entry{
"1e esc",
8, 0, 1, '.', 0,
- "abc\xff\tdef", // unterminated escape
+ "abc\xff\tdef", // unterminated escape
"abc\tdef",
},
@@ -165,14 +165,14 @@ var tests = []entry{
entry{
"4a",
8, 0, 1, '.', 0,
- "\t", // '\t' terminates an empty cell on last line - nothing to print
+ "\t", // '\t' terminates an empty cell on last line - nothing to print
"",
},
entry{
"4b",
8, 0, 1, '.', AlignRight,
- "\t", // '\t' terminates an empty cell on last line - nothing to print
+ "\t", // '\t' terminates an empty cell on last line - nothing to print
"",
},
@@ -294,7 +294,7 @@ var tests = []entry{
entry{
"9b",
1, 0, 0, '.', FilterHTML,
- "1\t2<!---\f--->\t3\t4\n" + // \f inside HTML is ignored
+ "1\t2<!---\f--->\t3\t4\n" + // \f inside HTML is ignored
"11\t222\t3333\t44444\n",
"1.2<!---\f--->..3...4\n" +
@@ -304,7 +304,7 @@ var tests = []entry{
entry{
"9c",
1, 0, 0, '.', 0,
- "1\t2\t3\t4\f" + // \f causes a newline and flush
+ "1\t2\t3\t4\f" + // \f causes a newline and flush
"11\t222\t3333\t44444\n",
"1234\n" +
@@ -314,7 +314,7 @@ var tests = []entry{
entry{
"9c debug",
1, 0, 0, '.', Debug,
- "1\t2\t3\t4\f" + // \f causes a newline and flush
+ "1\t2\t3\t4\f" + // \f causes a newline and flush
"11\t222\t3333\t44444\n",
"1|2|3|4\n" +
@@ -489,7 +489,7 @@ var tests = []entry{
entry{
"15b",
4, 0, 0, '.', DiscardEmptyColumns,
- "a\t\tb", // htabs - do not discard column
+ "a\t\tb", // htabs - do not discard column
"a.......b",
},
@@ -558,7 +558,7 @@ var tests = []entry{
entry{
"16c",
100, 100, 0, '\t', DiscardEmptyColumns,
- "a\tb\t\td\n" + // hard tabs - do not discard column
+ "a\tb\t\td\n" + // hard tabs - do not discard column
"a\tb\t\td\te\n" +
"a\n" +
"a\tb\tc\td\n" +
@@ -574,7 +574,7 @@ var tests = []entry{
entry{
"16c debug",
100, 100, 0, '\t', DiscardEmptyColumns | Debug,
- "a\tb\t\td\n" + // hard tabs - do not discard column
+ "a\tb\t\td\n" + // hard tabs - do not discard column
"a\tb\t\td\te\n" +
"a\n" +
"a\tb\tc\td\n" +