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