Commit 33f3ed77 authored by Rob Pike's avatar Rob Pike

casify fixup for bufio

R=rsc
DELTA=88  (0 added, 12 deleted, 76 changed)
OCL=22884
CL=22890
parent 3decb42e
...@@ -18,7 +18,7 @@ import ( ...@@ -18,7 +18,7 @@ import (
// - buffered output // - buffered output
const ( const (
DefaultBufSize = 4096 defaultBufSize = 4096
) )
export var ( export var (
...@@ -30,7 +30,7 @@ export var ( ...@@ -30,7 +30,7 @@ export var (
ShortWrite = os.NewError("short write"); ShortWrite = os.NewError("short write");
) )
func CopySlice(dst []byte, src []byte) { func copySlice(dst []byte, src []byte) {
for i := 0; i < len(dst); i++ { for i := 0; i < len(dst); i++ {
dst[i] = src[i] dst[i] = src[i]
} }
...@@ -57,9 +57,7 @@ export func NewBufReadSize(rd io.Read, size int) (b *BufRead, err *os.Error) { ...@@ -57,9 +57,7 @@ export func NewBufReadSize(rd io.Read, size int) (b *BufRead, err *os.Error) {
} }
export func NewBufRead(rd io.Read) (b *BufRead, err *os.Error) { export func NewBufRead(rd io.Read) (b *BufRead, err *os.Error) {
// 6g BUG return NewBufReadSize(rd, DefaultBufSize) return NewBufReadSize(rd, defaultBufSize);
r, e := NewBufReadSize(rd, DefaultBufSize);
return r, e
} }
// Read a new chunk into the buffer. // Read a new chunk into the buffer.
...@@ -70,7 +68,7 @@ func (b *BufRead) Fill() *os.Error { ...@@ -70,7 +68,7 @@ func (b *BufRead) Fill() *os.Error {
// Slide existing data to beginning. // Slide existing data to beginning.
if b.w > b.r { if b.w > b.r {
CopySlice(b.buf[0:b.w-b.r], b.buf[b.r:b.w]); copySlice(b.buf[0:b.w-b.r], b.buf[b.r:b.w]);
b.w -= b.r; b.w -= b.r;
} else { } else {
b.w = 0 b.w = 0
...@@ -107,7 +105,7 @@ func (b *BufRead) Read(p []byte) (nn int, err *os.Error) { ...@@ -107,7 +105,7 @@ func (b *BufRead) Read(p []byte) (nn int, err *os.Error) {
if n > b.w - b.r { if n > b.w - b.r {
n = b.w - b.r n = b.w - b.r
} }
CopySlice(p[0:n], b.buf[b.r:b.r+n]); copySlice(p[0:n], b.buf[b.r:b.r+n]);
p = p[n:len(p)]; p = p[n:len(p)];
b.r += n; b.r += n;
nn += n nn += n
...@@ -170,7 +168,7 @@ func (b *BufRead) ReadRune() (rune int, size int, err *os.Error) { ...@@ -170,7 +168,7 @@ func (b *BufRead) ReadRune() (rune int, size int, err *os.Error) {
// Helper function: look for byte c in array p, // Helper function: look for byte c in array p,
// returning its index or -1. // returning its index or -1.
func FindByte(p []byte, c byte) int { func findByte(p []byte, c byte) int {
for i := 0; i < len(p); i++ { for i := 0; i < len(p); i++ {
if p[i] == c { if p[i] == c {
return i return i
...@@ -197,7 +195,7 @@ func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err *os.Error) { ...@@ -197,7 +195,7 @@ func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err *os.Error) {
} }
// Look in buffer. // Look in buffer.
if i := FindByte(b.buf[b.r:b.w], delim); i >= 0 { if i := findByte(b.buf[b.r:b.w], delim); i >= 0 {
line1 := b.buf[b.r:b.r+i+1]; line1 := b.buf[b.r:b.r+i+1];
b.r += i+1; b.r += i+1;
return line1, nil return line1, nil
...@@ -217,7 +215,7 @@ func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err *os.Error) { ...@@ -217,7 +215,7 @@ func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err *os.Error) {
} }
// Search new part of buffer // Search new part of buffer
if i := FindByte(b.buf[n:b.w], delim); i >= 0 { if i := findByte(b.buf[n:b.w], delim); i >= 0 {
line := b.buf[0:n+i+1]; line := b.buf[0:n+i+1];
b.r = n+i+1; b.r = n+i+1;
return line, nil return line, nil
...@@ -304,10 +302,10 @@ func (b *BufRead) ReadLineBytes(delim byte) (line []byte, err *os.Error) { ...@@ -304,10 +302,10 @@ func (b *BufRead) ReadLineBytes(delim byte) (line []byte, err *os.Error) {
buf := make([]byte, n); buf := make([]byte, n);
n = 0; n = 0;
for i := 0; i < nfull; i++ { for i := 0; i < nfull; i++ {
CopySlice(buf[n:n+len(full[i])], full[i]); copySlice(buf[n:n+len(full[i])], full[i]);
n += len(full[i]) n += len(full[i])
} }
CopySlice(buf[n:n+len(frag)], frag); copySlice(buf[n:n+len(frag)], frag);
return buf, err return buf, err
} }
...@@ -346,9 +344,7 @@ export func NewBufWriteSize(wr io.Write, size int) (b *BufWrite, err *os.Error) ...@@ -346,9 +344,7 @@ export func NewBufWriteSize(wr io.Write, size int) (b *BufWrite, err *os.Error)
} }
export func NewBufWrite(wr io.Write) (b *BufWrite, err *os.Error) { export func NewBufWrite(wr io.Write) (b *BufWrite, err *os.Error) {
// 6g BUG return NewBufWriteSize(wr, DefaultBufSize) return NewBufWriteSize(wr, defaultBufSize);
r, e := NewBufWriteSize(wr, DefaultBufSize);
return r, e
} }
// Flush the output buffer. // Flush the output buffer.
...@@ -365,7 +361,7 @@ func (b *BufWrite) Flush() *os.Error { ...@@ -365,7 +361,7 @@ func (b *BufWrite) Flush() *os.Error {
} }
if e != nil { if e != nil {
if n < b.n { if n < b.n {
CopySlice(b.buf[0:b.n-n], b.buf[n:b.n]) copySlice(b.buf[0:b.n-n], b.buf[n:b.n])
} }
b.n -= n; b.n -= n;
b.err = e; b.err = e;
...@@ -400,7 +396,7 @@ func (b *BufWrite) Write(p []byte) (nn int, err *os.Error) { ...@@ -400,7 +396,7 @@ func (b *BufWrite) Write(p []byte) (nn int, err *os.Error) {
if n > len(p) { if n > len(p) {
n = len(p) n = len(p)
} }
CopySlice(b.buf[b.n:b.n+n], p[0:n]); copySlice(b.buf[b.n:b.n+n], p[0:n]);
b.n += n; b.n += n;
nn += n; nn += n;
p = p[n:len(p)] p = p[n:len(p)]
......
...@@ -13,55 +13,47 @@ import ( ...@@ -13,55 +13,47 @@ import (
"testing"; "testing";
) )
func StringToBytes(s string) []byte {
b := make([]byte, len(s));
for i := 0; i < len(s); i++ {
b[i] = s[i]
}
return b
}
// Should be in language! // Should be in language!
func Copy(p []byte, q []byte) { func copy(p []byte, q []byte) {
for i := 0; i < len(p); i++ { for i := 0; i < len(p); i++ {
p[i] = q[i] p[i] = q[i]
} }
} }
// Reads from p. // Reads from p.
type ByteReader struct { type byteReader struct {
p []byte p []byte
} }
func NewByteReader(p []byte) io.Read { func newByteReader(p []byte) io.Read {
b := new(ByteReader); b := new(byteReader);
b.p = p; b.p = p;
return b return b
} }
func (b *ByteReader) Read(p []byte) (int, *os.Error) { func (b *byteReader) Read(p []byte) (int, *os.Error) {
n := len(p); n := len(p);
if n > len(b.p) { if n > len(b.p) {
n = len(b.p) n = len(b.p)
} }
Copy(p[0:n], b.p[0:n]); copy(p[0:n], b.p[0:n]);
b.p = b.p[n:len(b.p)]; b.p = b.p[n:len(b.p)];
return n, nil return n, nil
} }
// Reads from p but only returns half of what you asked for. // Reads from p but only returns half of what you asked for.
type HalfByteReader struct { type halfByteReader struct {
p []byte p []byte
} }
func NewHalfByteReader(p []byte) io.Read { func newHalfByteReader(p []byte) io.Read {
b := new(HalfByteReader); b := new(halfByteReader);
b.p = p; b.p = p;
return b return b
} }
func (b *HalfByteReader) Read(p []byte) (int, *os.Error) { func (b *halfByteReader) Read(p []byte) (int, *os.Error) {
n := len(p)/2; n := len(p)/2;
if n == 0 && len(p) > 0 { if n == 0 && len(p) > 0 {
n = 1 n = 1
...@@ -69,23 +61,23 @@ func (b *HalfByteReader) Read(p []byte) (int, *os.Error) { ...@@ -69,23 +61,23 @@ func (b *HalfByteReader) Read(p []byte) (int, *os.Error) {
if n > len(b.p) { if n > len(b.p) {
n = len(b.p) n = len(b.p)
} }
Copy(p[0:n], b.p[0:n]); copy(p[0:n], b.p[0:n]);
b.p = b.p[n:len(b.p)]; b.p = b.p[n:len(b.p)];
return n, nil return n, nil
} }
// Reads from a reader and rot13s the result. // Reads from a reader and rot13s the result.
type Rot13Reader struct { type rot13Reader struct {
r io.Read r io.Read
} }
func NewRot13Reader(r io.Read) *Rot13Reader { func newRot13Reader(r io.Read) *rot13Reader {
r13 := new(Rot13Reader); r13 := new(rot13Reader);
r13.r = r; r13.r = r;
return r13 return r13
} }
func (r13 *Rot13Reader) Read(p []byte) (int, *os.Error) { func (r13 *rot13Reader) Read(p []byte) (int, *os.Error) {
n, e := r13.r.Read(p); n, e := r13.r.Read(p);
if e != nil { if e != nil {
return n, e return n, e
...@@ -102,18 +94,18 @@ func (r13 *Rot13Reader) Read(p []byte) (int, *os.Error) { ...@@ -102,18 +94,18 @@ func (r13 *Rot13Reader) Read(p []byte) (int, *os.Error) {
return n, nil return n, nil
} }
type Readmaker struct { type readMaker struct {
name string; name string;
fn *([]byte) io.Read; fn *([]byte) io.Read;
} }
var readmakers = []Readmaker { var readMakers = []readMaker {
Readmaker{ "full", func(p []byte) io.Read { return NewByteReader(p) } }, readMaker{ "full", func(p []byte) io.Read { return newByteReader(p) } },
Readmaker{ "half", func(p []byte) io.Read { return NewHalfByteReader(p) } }, readMaker{ "half", func(p []byte) io.Read { return newHalfByteReader(p) } },
} }
// Call ReadLineString (which ends up calling everything else) // Call ReadLineString (which ends up calling everything else)
// to accumulate the text of a file. // to accumulate the text of a file.
func ReadLines(b *BufRead) string { func readLines(b *BufRead) string {
s := ""; s := "";
for { for {
s1, e := b.ReadLineString('\n', true); s1, e := b.ReadLineString('\n', true);
...@@ -129,7 +121,7 @@ func ReadLines(b *BufRead) string { ...@@ -129,7 +121,7 @@ func ReadLines(b *BufRead) string {
} }
// Call ReadByte to accumulate the text of a file // Call ReadByte to accumulate the text of a file
func ReadBytes(buf *BufRead) string { func readBytes(buf *BufRead) string {
var b [1000]byte; var b [1000]byte;
nb := 0; nb := 0;
for { for {
...@@ -148,7 +140,7 @@ func ReadBytes(buf *BufRead) string { ...@@ -148,7 +140,7 @@ func ReadBytes(buf *BufRead) string {
} }
// Call Read to accumulate the text of a file // Call Read to accumulate the text of a file
func Reads(buf *BufRead, m int) string { func reads(buf *BufRead, m int) string {
var b [1000]byte; var b [1000]byte;
nb := 0; nb := 0;
for { for {
...@@ -161,19 +153,19 @@ func Reads(buf *BufRead, m int) string { ...@@ -161,19 +153,19 @@ func Reads(buf *BufRead, m int) string {
return string(b[0:nb]) return string(b[0:nb])
} }
type Bufreader struct { type bufReader struct {
name string; name string;
fn *(*BufRead) string; fn *(*BufRead) string;
} }
var bufreaders = []Bufreader { var bufreaders = []bufReader {
Bufreader{ "1", func(b *BufRead) string { return Reads(b, 1) } }, bufReader{ "1", func(b *BufRead) string { return reads(b, 1) } },
Bufreader{ "2", func(b *BufRead) string { return Reads(b, 2) } }, bufReader{ "2", func(b *BufRead) string { return reads(b, 2) } },
Bufreader{ "3", func(b *BufRead) string { return Reads(b, 3) } }, bufReader{ "3", func(b *BufRead) string { return reads(b, 3) } },
Bufreader{ "4", func(b *BufRead) string { return Reads(b, 4) } }, bufReader{ "4", func(b *BufRead) string { return reads(b, 4) } },
Bufreader{ "5", func(b *BufRead) string { return Reads(b, 5) } }, bufReader{ "5", func(b *BufRead) string { return reads(b, 5) } },
Bufreader{ "7", func(b *BufRead) string { return Reads(b, 7) } }, bufReader{ "7", func(b *BufRead) string { return reads(b, 7) } },
Bufreader{ "bytes", &ReadBytes }, bufReader{ "bytes", &readBytes },
Bufreader{ "lines", &ReadLines }, bufReader{ "lines", &readLines },
} }
var bufsizes = []int { var bufsizes = []int {
...@@ -182,13 +174,13 @@ var bufsizes = []int { ...@@ -182,13 +174,13 @@ var bufsizes = []int {
} }
export func TestBufReadSimple(t *testing.T) { export func TestBufReadSimple(t *testing.T) {
b, e := NewBufRead(NewByteReader(StringToBytes("hello world"))); b, e := NewBufRead(newByteReader(io.StringBytes("hello world")));
if s := ReadBytes(b); s != "hello world" { if s := readBytes(b); s != "hello world" {
t.Errorf("simple hello world test failed: got %q", s); t.Errorf("simple hello world test failed: got %q", s);
} }
b, e = NewBufRead(NewRot13Reader(NewByteReader(StringToBytes("hello world")))); b, e = NewBufRead(newRot13Reader(newByteReader(io.StringBytes("hello world"))));
if s := ReadBytes(b); s != "uryyb jbeyq" { if s := readBytes(b); s != "uryyb jbeyq" {
t.Error("rot13 hello world test failed: got %q", s); t.Error("rot13 hello world test failed: got %q", s);
} }
} }
...@@ -206,11 +198,11 @@ export func TestBufRead(t *testing.T) { ...@@ -206,11 +198,11 @@ export func TestBufRead(t *testing.T) {
for h := 0; h < len(texts); h++ { for h := 0; h < len(texts); h++ {
text := texts[h]; text := texts[h];
textbytes := StringToBytes(text); textbytes := io.StringBytes(text);
for i := 0; i < len(readmakers); i++ { for i := 0; i < len(readMakers); i++ {
for j := 0; j < len(bufreaders); j++ { for j := 0; j < len(bufreaders); j++ {
for k := 0; k < len(bufsizes); k++ { for k := 0; k < len(bufsizes); k++ {
readmaker := readmakers[i]; readmaker := readMakers[i];
bufreader := bufreaders[j]; bufreader := bufreaders[j];
bufsize := bufsizes[k]; bufsize := bufsizes[k];
read := readmaker.fn(textbytes); read := readmaker.fn(textbytes);
...@@ -226,72 +218,72 @@ export func TestBufRead(t *testing.T) { ...@@ -226,72 +218,72 @@ export func TestBufRead(t *testing.T) {
} }
} }
type WriteBuffer interface { type writeBuffer interface {
Write(p []byte) (int, *os.Error); Write(p []byte) (int, *os.Error);
GetBytes() []byte GetBytes() []byte
} }
// Accumulates bytes into a byte array. // Accumulates bytes into a byte array.
type ByteWriter struct { type byteWriter struct {
p []byte; p []byte;
n int n int
} }
func NewByteWriter() WriteBuffer { func newByteWriter() writeBuffer {
return new(ByteWriter) return new(byteWriter)
} }
func (w *ByteWriter) Write(p []byte) (int, *os.Error) { func (w *byteWriter) Write(p []byte) (int, *os.Error) {
if w.p == nil { if w.p == nil {
w.p = make([]byte, len(p)+100) w.p = make([]byte, len(p)+100)
} else if w.n + len(p) >= len(w.p) { } else if w.n + len(p) >= len(w.p) {
newp := make([]byte, len(w.p)*2 + len(p)); newp := make([]byte, len(w.p)*2 + len(p));
Copy(newp[0:w.n], w.p[0:w.n]); copy(newp[0:w.n], w.p[0:w.n]);
w.p = newp w.p = newp
} }
Copy(w.p[w.n:w.n+len(p)], p); copy(w.p[w.n:w.n+len(p)], p);
w.n += len(p); w.n += len(p);
return len(p), nil return len(p), nil
} }
func (w *ByteWriter) GetBytes() []byte { func (w *byteWriter) GetBytes() []byte {
return w.p[0:w.n] return w.p[0:w.n]
} }
// Accumulates bytes written into a byte array // Accumulates bytes written into a byte array
// but Write only takes half of what you give it. // but Write only takes half of what you give it.
// TODO: Could toss this -- Write() is not supposed to do that. // TODO: Could toss this -- Write() is not supposed to do that.
type HalfByteWriter struct { type halfByteWriter struct {
bw WriteBuffer bw writeBuffer
} }
func NewHalfByteWriter() WriteBuffer { func newHalfByteWriter() writeBuffer {
w := new(HalfByteWriter); w := new(halfByteWriter);
w.bw = NewByteWriter(); w.bw = newByteWriter();
return w return w
} }
func (w *HalfByteWriter) Write(p []byte) (int, *os.Error) { func (w *halfByteWriter) Write(p []byte) (int, *os.Error) {
n := (len(p)+1) / 2; n := (len(p)+1) / 2;
// BUG return w.bw.Write(p[0:n]) // BUG return w.bw.Write(p[0:n])
r, e := w.bw.Write(p[0:n]); r, e := w.bw.Write(p[0:n]);
return r, e return r, e
} }
func (w *HalfByteWriter) GetBytes() []byte { func (w *halfByteWriter) GetBytes() []byte {
return w.bw.GetBytes() return w.bw.GetBytes()
} }
type Writemaker struct { type writeMaker struct {
name string; name string;
fn *()WriteBuffer; fn *()writeBuffer;
} }
export func TestBufWrite(t *testing.T) { export func TestBufWrite(t *testing.T) {
var data [8192]byte; var data [8192]byte;
var writers = []Writemaker { var writers = []writeMaker {
Writemaker{ "full", &NewByteWriter }, writeMaker{ "full", &newByteWriter },
Writemaker{ "half", &NewHalfByteWriter }, writeMaker{ "half", &newHalfByteWriter },
}; };
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
......
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