Commit e28692f0 authored by Robert Griesemer's avatar Robert Griesemer

- converted tabwriter to new naming scheme

R=r
OCL=22870
CL=22870
parent f4babf69
......@@ -13,34 +13,34 @@ import (
// ----------------------------------------------------------------------------
// Basic ByteArray support
// Basic byteArray support
type ByteArray struct {
type byteArray struct {
a []byte;
}
func (b *ByteArray) Init(initial_size int) {
func (b *byteArray) Init(initial_size int) {
b.a = make([]byte, initial_size)[0 : 0];
}
func (b *ByteArray) Len() int {
func (b *byteArray) Len() int {
return len(b.a);
}
func (b *ByteArray) Clear() {
func (b *byteArray) clear() {
b.a = b.a[0 : 0];
}
func (b *ByteArray) Slice(i, j int) []byte {
func (b *byteArray) slice(i, j int) []byte {
return b.a[i : j]; // BUG should really be &b.a[i : j]
}
func (b *ByteArray) Append(s []byte) {
func (b *byteArray) append(s []byte) {
a := b.a;
n := len(a);
m := n + len(s);
......@@ -105,7 +105,7 @@ export type Writer struct {
// current state
html_char byte; // terminating char of html tag/entity, or 0 ('>', ';', or 0)
buf ByteArray; // collected text w/o tabs and newlines
buf byteArray; // collected text w/o tabs and newlines
size int; // size of incomplete cell in bytes
width int; // width of incomplete cell in runes up to buf[pos] w/o ignored sections
pos int; // buffer position up to which width of incomplete cell has been computed
......@@ -138,7 +138,7 @@ export type Writer struct {
// buf start of incomplete cell pos
func (b *Writer) AddLine() {
func (b *Writer) addLine() {
b.lines_size.Push(array.NewIntArray(0));
b.lines_width.Push(array.NewIntArray(0));
}
......@@ -164,13 +164,13 @@ func (b *Writer) Init(writer io.Write, cellwidth, padding int, padchar byte, ali
b.lines_size.Init(0);
b.lines_width.Init(0);
b.widths.Init(0);
b.AddLine(); // the very first line
b.addLine(); // the very first line
return b;
}
func (b *Writer) Line(i int) (*array.IntArray, *array.IntArray) {
func (b *Writer) line(i int) (*array.IntArray, *array.IntArray) {
return
b.lines_size.At(i).(*array.IntArray),
b.lines_width.At(i).(*array.IntArray);
......@@ -178,14 +178,14 @@ func (b *Writer) Line(i int) (*array.IntArray, *array.IntArray) {
// debugging support
func (b *Writer) Dump() {
func (b *Writer) dump() {
pos := 0;
for i := 0; i < b.lines_size.Len(); i++ {
line_size, line_width := b.Line(i);
line_size, line_width := b.line(i);
print("(", i, ") ");
for j := 0; j < line_size.Len(); j++ {
s := line_size.At(j);
print("[", string(b.buf.Slice(pos, pos + s)), "]");
print("[", string(b.buf.slice(pos, pos + s)), "]");
pos += s;
}
print("\n");
......@@ -194,7 +194,7 @@ func (b *Writer) Dump() {
}
func (b *Writer) 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;
......@@ -203,9 +203,9 @@ func (b *Writer) Write0(buf []byte) *os.Error {
}
var Newline = []byte{'\n'}
var newline = []byte{'\n'}
func (b *Writer) WritePadding(textw, cellw int) (err *os.Error) {
func (b *Writer) writePadding(textw, cellw int) (err *os.Error) {
if b.padbytes[0] == '\t' {
// make cell width a multiple of cellwidth
cellw = ((cellw + b.cellwidth - 1) / b.cellwidth) * b.cellwidth;
......@@ -221,34 +221,34 @@ func (b *Writer) WritePadding(textw, cellw int) (err *os.Error) {
}
for n > len(b.padbytes) {
err = b.Write0(b.padbytes);
err = b.write0(b.padbytes);
if err != nil {
goto exit;
}
n -= len(b.padbytes);
}
err = b.Write0(b.padbytes[0 : n]);
err = b.write0(b.padbytes[0 : n]);
exit:
return err;
}
func (b *Writer) 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_size, line_width := b.Line(i);
line_size, line_width := b.line(i);
for j := 0; j < line_size.Len(); j++ {
s, w := line_size.At(j), line_width.At(j);
if b.align_left {
err = b.Write0(b.buf.Slice(pos, pos + s));
err = b.write0(b.buf.slice(pos, pos + s));
if err != nil {
goto exit;
}
pos += s;
if j < b.widths.Len() {
err = b.WritePadding(w, b.widths.At(j));
err = b.writePadding(w, b.widths.At(j));
if err != nil {
goto exit;
}
......@@ -257,12 +257,12 @@ func (b *Writer) WriteLines(pos0 int, line0, line1 int) (pos int, err *os.Error)
} else { // align right
if j < b.widths.Len() {
err = b.WritePadding(w, b.widths.At(j));
err = b.writePadding(w, b.widths.At(j));
if err != nil {
goto exit;
}
}
err = b.Write0(b.buf.Slice(pos, pos + s));
err = b.write0(b.buf.slice(pos, pos + s));
if err != nil {
goto exit;
}
......@@ -273,11 +273,11 @@ func (b *Writer) WriteLines(pos0 int, line0, line1 int) (pos int, err *os.Error)
if i+1 == b.lines_size.Len() {
// last buffered line - we don't have a newline, so just write
// any outstanding buffered data
err = b.Write0(b.buf.Slice(pos, pos + b.size));
err = b.write0(b.buf.slice(pos, pos + b.size));
pos += b.size;
} else {
// not the last line - write newline
err = b.Write0(Newline);
err = b.write0(newline);
}
if err != nil {
goto exit;
......@@ -289,19 +289,19 @@ exit:
}
func (b *Writer) 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;
for this := line0; this < line1; this++ {
line_size, line_width := b.Line(this);
line_size, line_width := b.line(this);
if column < line_size.Len() - 1 {
// cell exists in this column
// (note that the last cell per line is ignored)
// print unprinted lines until beginning of block
pos, err = b.WriteLines(pos, last, this);
pos, err = b.writeLines(pos, last, this);
if err != nil {
goto exit;
}
......@@ -310,7 +310,7 @@ func (b *Writer) Format(pos0 int, line0, line1 int) (pos int, err *os.Error) {
// column block begin
width := b.cellwidth; // minimal width
for ; this < line1; this++ {
line_size, line_width = b.Line(this);
line_size, line_width = b.line(this);
if column < line_size.Len() - 1 {
// cell exists in this column => update width
w := line_width.At(column) + b.padding;
......@@ -326,34 +326,34 @@ 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, last, this);
pos, err = b.format(pos, last, this);
b.widths.Pop();
last = this;
}
}
// print unprinted lines until end
pos, err = b.WriteLines(pos, last, line1);
pos, err = b.writeLines(pos, last, line1);
exit:
return pos, err;
}
/* export */ func (b *Writer) Flush() *os.Error {
dummy, err := b.Format(0, 0, b.lines_size.Len());
func (b *Writer) Flush() *os.Error {
dummy, err := b.format(0, 0, b.lines_size.Len());
// reset (even in the presence of errors)
b.buf.Clear();
b.buf.clear();
b.size, b.width = 0, 0;
b.pos = 0;
b.lines_size.Init(0);
b.lines_width.Init(0);
b.AddLine();
b.addLine();
return err;
}
func UnicodeLen(buf []byte) int {
func unicodeLen(buf []byte) int {
l := 0;
for i := 0; i < len(buf); {
if buf[i] < utf8.RuneSelf {
......@@ -368,8 +368,8 @@ func UnicodeLen(buf []byte) int {
}
func (b *Writer) Append(buf []byte) {
b.buf.Append(buf);
func (b *Writer) append(buf []byte) {
b.buf.append(buf);
b.size += len(buf);
}
......@@ -385,23 +385,23 @@ func (b *Writer) Append(buf []byte) {
// outside html tag/entity
switch ch {
case '\t', '\n':
b.Append(buf[i0 : i]);
b.append(buf[i0 : i]);
i0 = i + 1; // exclude ch from (next) cell
b.width += UnicodeLen(b.buf.Slice(b.pos, b.buf.Len()));
b.width += unicodeLen(b.buf.slice(b.pos, b.buf.Len()));
b.pos = b.buf.Len();
// terminate cell
last_size, last_width := b.Line(b.lines_size.Len() - 1);
last_size, last_width := b.line(b.lines_size.Len() - 1);
last_size.Push(b.size);
last_width.Push(b.width);
b.size, b.width = 0, 0;
if ch == '\n' {
b.AddLine();
b.addLine();
if last_size.Len() == 1 {
// The previous 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
// last cell per line is ignored by format()), thus we can
// flush the Writer contents.
err = b.Flush();
if err != nil {
......@@ -412,9 +412,9 @@ func (b *Writer) Append(buf []byte) {
case '<', '&':
if b.filter_html {
b.Append(buf[i0 : i]);
b.append(buf[i0 : i]);
i0 = i;
b.width += UnicodeLen(b.buf.Slice(b.pos, b.buf.Len()));
b.width += unicodeLen(b.buf.slice(b.pos, b.buf.Len()));
b.pos = -1; // preventative - should not be used (will cause index out of bounds)
if ch == '<' {
b.html_char = '>';
......@@ -428,7 +428,7 @@ func (b *Writer) Append(buf []byte) {
// inside html tag/entity
if ch == b.html_char {
// reached the end of tag/entity
b.Append(buf[i0 : i + 1]);
b.append(buf[i0 : i + 1]);
i0 = i + 1;
if b.html_char == ';' {
b.width++; // count as one char
......@@ -440,7 +440,7 @@ func (b *Writer) Append(buf []byte) {
}
// append leftover text
b.Append(buf[i0 : n]);
b.append(buf[i0 : n]);
return n, nil;
}
......
......@@ -12,22 +12,22 @@ import (
)
type Buffer struct {
type buffer struct {
a []byte;
}
func (b *Buffer) Init(n int) {
func (b *buffer) init(n int) {
b.a = make([]byte, n)[0 : 0];
}
func (b *Buffer) Clear() {
func (b *buffer) clear() {
b.a = b.a[0 : 0];
}
func (b *Buffer) Write(buf []byte) (written int, err *os.Error) {
func (b *buffer) Write(buf []byte) (written int, err *os.Error) {
n := len(b.a);
m := len(buf);
if n + m <= cap(b.a) {
......@@ -42,12 +42,12 @@ func (b *Buffer) Write(buf []byte) (written int, err *os.Error) {
}
func (b *Buffer) String() string {
func (b *buffer) String() string {
return string(b.a);
}
func Write(t *testing.T, w *tabwriter.Writer, src string) {
func write(t *testing.T, w *tabwriter.Writer, src string) {
written, err := io.WriteString(w, src);
if err != nil {
t.Errorf("--- src:\n%s\n--- write error: %v\n", src, err);
......@@ -58,7 +58,7 @@ func Write(t *testing.T, w *tabwriter.Writer, src string) {
}
func Verify(t *testing.T, w *tabwriter.Writer, b *Buffer, src, expected string) {
func verify(t *testing.T, w *tabwriter.Writer, b *buffer, src, expected string) {
err := w.Flush();
if err != nil {
t.Errorf("--- src:\n%s\n--- flush error: %v\n", src, err);
......@@ -71,142 +71,142 @@ func Verify(t *testing.T, w *tabwriter.Writer, b *Buffer, src, expected string)
}
func Check(t *testing.T, tabwidth, padding int, padchar byte, align_left, filter_html bool, src, expected string) {
var b Buffer;
b.Init(1000);
func check(t *testing.T, tabwidth, padding int, padchar byte, align_left, filter_html bool, src, expected string) {
var b buffer;
b.init(1000);
var w tabwriter.Writer;
w.Init(&b, tabwidth, padding, padchar, align_left, filter_html);
// write all at once
b.Clear();
Write(t, &w, src);
Verify(t, &w, &b, src, expected);
b.clear();
write(t, &w, src);
verify(t, &w, &b, src, expected);
// write byte-by-byte
b.Clear();
b.clear();
for i := 0; i < len(src); i++ {
Write(t, &w, src[i : i+1]);
write(t, &w, src[i : i+1]);
}
Verify(t, &w, &b, src, expected);
verify(t, &w, &b, src, expected);
// write using Fibonacci slice sizes
b.Clear();
b.clear();
for i, d := 0, 0; i < len(src); {
Write(t, &w, src[i : i+d]);
write(t, &w, src[i : i+d]);
i, d = i+d, d+1;
if i+d > len(src) {
d = len(src) - i;
}
}
Verify(t, &w, &b, src, expected);
verify(t, &w, &b, src, expected);
}
export func Test(t *testing.T) {
Check(
check(
t, 8, 1, '.', true, false,
"",
""
);
Check(
check(
t, 8, 1, '.', true, false,
"\n\n\n",
"\n\n\n"
);
Check(
check(
t, 8, 1, '.', true, false,
"a\nb\nc",
"a\nb\nc"
);
Check(
check(
t, 8, 1, '.', true, false,
"\t", // '\t' terminates an empty cell on last line - nothing to print
""
);
Check(
check(
t, 8, 1, '.', false, false,
"\t", // '\t' terminates an empty cell on last line - nothing to print
""
);
Check(
check(
t, 8, 1, '.', true, false,
"*\t*",
"**"
);
Check(
check(
t, 8, 1, '.', true, false,
"*\t*\n",
"*.......*\n"
);
Check(
check(
t, 8, 1, '.', true, false,
"*\t*\t",
"*.......*"
);
Check(
check(
t, 8, 1, '.', false, false,
"*\t*\t",
".......**"
);
Check(
check(
t, 8, 1, '.', true, false,
"\t\n",
"........\n"
);
Check(
check(
t, 8, 1, '.', true, false,
"a) foo",
"a) foo"
);
Check(
check(
t, 8, 1, ' ', true, false,
"b) foo\tbar", // "bar" is not in any cell - not formatted, just flushed
"b) foobar"
);
Check(
check(
t, 8, 1, '.', true, false,
"c) foo\tbar\t",
"c) foo..bar"
);
Check(
check(
t, 8, 1, '.', true, false,
"d) foo\tbar\n",
"d) foo..bar\n"
);
Check(
check(
t, 8, 1, '.', true, false,
"e) foo\tbar\t\n",
"e) foo..bar.....\n"
);
Check(
check(
t, 8, 1, '.', true, true,
"e) f&lt;o\t<b>bar</b>\t\n",
"e) f&lt;o..<b>bar</b>.....\n"
);
Check(
check(
t, 8, 1, '*', true, false,
"Hello, world!\n",
"Hello, world!\n"
);
Check(
check(
t, 0, 0, '.', true, false,
"1\t2\t3\t4\n"
"11\t222\t3333\t44444\n",
......@@ -215,19 +215,19 @@ export func Test(t *testing.T) {
"11222333344444\n"
);
Check(
check(
t, 5, 0, '.', true, false,
"1\t2\t3\t4\n",
"1....2....3....4\n"
);
Check(
check(
t, 5, 0, '.', true, false,
"1\t2\t3\t4\t\n",
"1....2....3....4....\n"
);
Check(
check(
t, 8, 1, '.', true, false,
"本\tb\tc\n"
"aa\t\u672c\u672c\u672c\tcccc\tddddd\n"
......@@ -238,7 +238,7 @@ export func Test(t *testing.T) {
"aaa.....bbbb\n"
);
Check(
check(
t, 8, 1, ' ', false, false,
"a\tè\tc\t\n"
"aa\tèèè\tcccc\tddddd\t\n"
......@@ -249,7 +249,7 @@ export func Test(t *testing.T) {
" aaa èèèè\n"
);
Check(
check(
t, 2, 0, ' ', true, false,
"a\tb\tc\n"
"aa\tbbb\tcccc\n"
......@@ -260,7 +260,7 @@ export func Test(t *testing.T) {
"aaabbbb\n"
);
Check(
check(
t, 8, 1, '_', true, false,
"a\tb\tc\n"
"aa\tbbb\tcccc\n"
......@@ -271,7 +271,7 @@ export func Test(t *testing.T) {
"aaa_____bbbb\n"
);
Check(
check(
t, 4, 1, '-', true, false,
"4444\t日本語\t22\t1\t333\n"
"999999999\t22\n"
......@@ -290,7 +290,7 @@ export func Test(t *testing.T) {
"1------1------999999999-0000000000\n"
);
Check(
check(
t, 4, 3, '.', true, false,
"4444\t333\t22\t1\t333\n"
"999999999\t22\n"
......@@ -309,7 +309,7 @@ export func Test(t *testing.T) {
"1........1........999999999...0000000000\n"
);
Check(
check(
t, 8, 1, '\t', true, true,
"4444\t333\t22\t1\t333\n"
"999999999\t22\n"
......@@ -328,7 +328,7 @@ export func Test(t *testing.T) {
"1\t1\t<font color=red attr=日本語>999999999</font>\t0000000000\n"
);
Check(
check(
t, 0, 2, ' ', false, false,
".0\t.3\t2.4\t-5.1\t\n"
"23.0\t12345678.9\t2.4\t-989.4\t\n"
......
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