]> Cypherpunks repositories - gostls13.git/commitdiff
- move tabwriter into library
authorRobert Griesemer <gri@golang.org>
Fri, 21 Nov 2008 01:39:41 +0000 (17:39 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 21 Nov 2008 01:39:41 +0000 (17:39 -0800)
- 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

src/lib/clean.bash
src/lib/make.bash
src/lib/tabwriter/Makefile [new file with mode: 0644]
src/lib/tabwriter/tabwriter.go [moved from usr/gri/pretty/tabwriter.go with 84% similarity]
src/lib/tabwriter/tabwriter_test.go [new file with mode: 0644]
usr/gri/pretty/Makefile
usr/gri/pretty/untab.go

index e32e2eb07b1488d9aa8af38b29a0cb149324562f..02f5aab49f07ab9b967706baaee0c5dcf16ee9b8 100755 (executable)
@@ -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
 
index 76ae0404e44f4d156183f80c595582886fb744f5..4398d44e3fd09ad72ad4a980469df1405ede9797 100755 (executable)
@@ -38,7 +38,8 @@ builddirs     syscall\
        
 buildfiles     io.go
 
-builddirs      fmt
+builddirs      fmt\
+               tabwriter\
 
 buildfiles     flag.go\
                container/vector.go\
diff --git a/src/lib/tabwriter/Makefile b/src/lib/tabwriter/Makefile
new file mode 100644 (file)
index 0000000..7fbdf55
--- /dev/null
@@ -0,0 +1,55 @@
+# 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
+
similarity index 84%
rename from usr/gri/pretty/tabwriter.go
rename to src/lib/tabwriter/tabwriter.go
index 58ae5ff1e6c108cce7edbe596394c8cbea2f259a..3eb0ba195b6a6ac1a2701f681e34effc232be99f 100644 (file)
@@ -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)
 }
diff --git a/src/lib/tabwriter/tabwriter_test.go b/src/lib/tabwriter/tabwriter_test.go
new file mode 100644 (file)
index 0000000..42c443f
--- /dev/null
@@ -0,0 +1,81 @@
+// 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"
+       );
+}
index 835654652e72efe8f60f4db5ad5e6cb4c9a9e2d9..50585fe1023eaf1642898e0163694f723b019884 100644 (file)
@@ -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) $<
index d2a26f4389b9f81bf44d71a290369ca5a6fb0ac3..cd5981b539a64ca97d295311a14b53c2ca84508e 100644 (file)
@@ -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);