Commit 01b44bbf authored by Robert Griesemer's avatar Robert Griesemer

- move tabwriter into library

- added preliminary tests (more to do)
- renamed type from TabWriter -> Writer
- adjusted my code where necessary

R=r
DELTA=825  (474 added, 346 deleted, 5 changed)
OCL=19744
CL=19753
parent 67addd4e
......@@ -6,10 +6,8 @@
rm -f $GOROOT/pkg/*
for i in syscall os math fmt net time http reflect regexp
for i in syscall math os strconv container/array reflect fmt tabwriter net time http regexp
do
cd $i
make nuke
cd ..
(cd $i; make nuke)
done
......@@ -38,7 +38,8 @@ builddirs syscall\
buildfiles io.go
builddirs fmt
builddirs fmt\
tabwriter\
buildfiles flag.go\
container/vector.go\
......
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# DO NOT EDIT. Automatically generated by gobuild.
# gobuild -m >Makefile
O=6
GC=$(O)g
CC=$(O)c -w
AS=$(O)a
AR=$(O)ar
default: packages
clean:
rm -f *.$O *.a $O.out
test: packages
gotest
coverage: packages
gotest
6cov -g `pwd` | grep -v '_test\.go:'
%.$O: %.go
$(GC) $*.go
%.$O: %.c
$(CC) $*.c
%.$O: %.s
$(AS) $*.s
O1=\
tabwriter.$O\
tabwriter.a: a1
a1: $(O1)
$(AR) grc tabwriter.a tabwriter.$O
rm -f $(O1)
newpkg: clean
$(AR) grc tabwriter.a
$(O1): newpkg
nuke: clean
rm -f $(GOROOT)/pkg/tabwriter.a
packages: tabwriter.a
install: packages
cp tabwriter.a $(GOROOT)/pkg/tabwriter.a
......@@ -61,12 +61,12 @@ func (b *ByteArray) Append(s *[]byte) {
// ----------------------------------------------------------------------------
// Tabwriter is a filter implementing the IO.Write interface. It assumes
// Writer is a filter implementing the io.Write interface. It assumes
// that the incoming bytes represent ASCII encoded text consisting of
// lines of tab-separated "cells". Cells in adjacent lines constitute
// a column. Tabwriter rewrites the incoming text such that all cells
// in a column have the same width; thus it effectively aligns cells.
// It does this by adding padding where necessary.
// a column. Writer rewrites the incoming text such that all cells in
// a column have the same width; thus it effectively aligns cells. It
// does this by adding padding where necessary.
//
// Formatting can be controlled via parameters:
//
......@@ -84,7 +84,7 @@ func (b *ByteArray) Append(s *[]byte) {
// pendant cell and tab width.
export type TabWriter struct {
export type Writer struct {
// TODO should not export any of the fields
// configuration
writer io.Write;
......@@ -100,12 +100,12 @@ export type TabWriter struct {
}
func (b *TabWriter) AddLine() {
func (b *Writer) AddLine() {
b.lines.Push(array.NewIntArray(0));
}
func (b *TabWriter) Init(writer io.Write, tabwidth, padding int, usetabs bool) *TabWriter {
func (b *Writer) Init(writer io.Write, tabwidth, padding int, usetabs bool) *Writer {
b.writer = writer;
b.tabwidth = tabwidth;
b.padding = padding;
......@@ -120,18 +120,18 @@ func (b *TabWriter) Init(writer io.Write, tabwidth, padding int, usetabs bool) *
}
func (b *TabWriter) Line(i int) *array.IntArray {
func (b *Writer) Line(i int) *array.IntArray {
return b.lines.At(i).(*array.IntArray);
}
func (b *TabWriter) LastLine() *array.IntArray {
func (b *Writer) LastLine() *array.IntArray {
return b.lines.At(b.lines.Len() - 1).(*array.IntArray);
}
// debugging support
func (b *TabWriter) Dump() {
func (b *Writer) Dump() {
pos := 0;
for i := 0; i < b.lines.Len(); i++ {
line := b.Line(i);
......@@ -147,7 +147,7 @@ func (b *TabWriter) Dump() {
}
func (b *TabWriter) Write0(buf *[]byte) *os.Error {
func (b *Writer) Write0(buf *[]byte) *os.Error {
n, err := b.writer.Write(buf);
if n != len(buf) && err == nil {
err = os.EIO;
......@@ -161,7 +161,7 @@ var Blanks = &[]byte{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}
var Newline = &[]byte{'\n'}
func (b *TabWriter) WritePadding(textw, cellw int) (err *os.Error) {
func (b *Writer) WritePadding(textw, cellw int) (err *os.Error) {
if b.usetabs {
// make cell width a multiple of tabwidth
cellw = ((cellw + b.tabwidth - 1) / b.tabwidth) * b.tabwidth;
......@@ -192,7 +192,7 @@ exit:
}
func (b *TabWriter) WriteLines(pos0 int, line0, line1 int) (pos int, err *os.Error) {
func (b *Writer) WriteLines(pos0 int, line0, line1 int) (pos int, err *os.Error) {
pos = pos0;
for i := line0; i < line1; i++ {
line := b.Line(i);
......@@ -233,7 +233,7 @@ func utflen(buf *[]byte) int {
}
func (b *TabWriter) Format(pos0 int, line0, line1 int) (pos int, err *os.Error) {
func (b *Writer) Format(pos0 int, line0, line1 int) (pos int, err *os.Error) {
pos = pos0;
column := b.widths.Len();
last := line0;
......@@ -284,13 +284,13 @@ exit:
}
func (b *TabWriter) Append(buf *[]byte) {
func (b *Writer) Append(buf *[]byte) {
b.buf.Append(buf);
b.width += len(buf);
}
/* export */ func (b *TabWriter) Flush() *os.Error {
/* export */ func (b *Writer) Flush() *os.Error {
dummy, err := b.Format(0, 0, b.lines.Len());
// reset (even in the presence of errors)
b.buf.Clear();
......@@ -301,7 +301,7 @@ func (b *TabWriter) Append(buf *[]byte) {
}
/* export */ func (b *TabWriter) Write(buf *[]byte) (written int, err *os.Error) {
/* export */ func (b *Writer) Write(buf *[]byte) (written int, err *os.Error) {
i0, n := 0, len(buf);
// split text into cells
......@@ -319,7 +319,7 @@ func (b *TabWriter) Append(buf *[]byte) {
// The last line has only one cell which does not have an
// impact on the formatting of the following lines (the
// last cell per line is ignored by Format), thus we can
// flush the TabWriter contents.
// flush the Writer contents.
err = b.Flush();
if err != nil {
return i0, err;
......@@ -338,6 +338,6 @@ func (b *TabWriter) Append(buf *[]byte) {
}
export func New(writer io.Write, tabwidth, padding int, usetabs bool) *TabWriter {
return new(TabWriter).Init(writer, tabwidth, padding, usetabs)
export func New(writer io.Write, tabwidth, padding int, usetabs bool) *Writer {
return new(Writer).Init(writer, tabwidth, padding, usetabs)
}
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tabwriter
import (
"os";
"io";
"tabwriter";
"testing";
)
type Buffer struct {
a *[]byte;
}
func (b *Buffer) Init(n int) {
b.a = new([]byte, n)[0 : 0];
}
func (b *Buffer) Write(buf *[]byte) (written int, err *os.Error) {
n := len(b.a);
m := len(buf);
if n + m <= cap(b.a) {
b.a = b.a[0 : n + m];
for i := 0; i < m; i++ {
b.a[n+i] = buf[i];
}
} else {
panicln("buffer too small", n, m, cap(b.a));
}
return len(buf), nil;
}
func (b *Buffer) String() string {
return string(b.a);
}
func Check(t *testing.T, tabwidth, padding int, usetabs bool, src, expected string) {
var b Buffer;
b.Init(1000);
var w tabwriter.Writer;
w.Init(&b, tabwidth, padding, usetabs);
io.WriteString(&w, src);
res := b.String();
if res != expected {
t.Errorf("src:\n%s\nfound:\n%s\nexpected:\n%s\n", src, res, expected)
}
}
export func Test1(t *testing.T) {
Check(
t, 8, 1, false,
"\n",
"\n"
);
Check(
t, 8, 1, false,
"Hello, world!\n",
"Hello, world!\n"
);
Check(
t, 8, 1, false,
"a\tb\tc\naa\tbbb\tcccc\naaa\tbbbb\n\n",
"a b c\n"
"aa bbb cccc\n"
"aaa bbbb\n\n"
);
}
......@@ -37,9 +37,7 @@ parser.6: scanner.6 ast.6
platform.6: utils.6
printer.6: scanner.6 ast.6 tabwriter.6
untab.6: tabwriter.6
printer.6: scanner.6 ast.6
%.6: %.go
$(G) $(F) $<
......@@ -25,7 +25,7 @@ func Error(format string, params ...) {
}
func Untab(name string, src *os.FD, dst *tabwriter.TabWriter) {
func Untab(name string, src *os.FD, dst *tabwriter.Writer) {
n, err := io.Copy(src, dst);
if err != nil {
Error("error while processing %s (%v)", name, err);
......
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