io.StringBytes -> strings.Bytes
io.ByteBuffer -> bytes.Buffer
left io.ByteBuffer stub around for now,
for protocol compiler.
R=r
OCL=30861
CL=30872
diff --git a/src/pkg/bytes/Makefile b/src/pkg/bytes/Makefile
index 5220d28..9607122 100644
--- a/src/pkg/bytes/Makefile
+++ b/src/pkg/bytes/Makefile
@@ -2,6 +2,7 @@
# 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
@@ -20,7 +21,7 @@
coverage: packages
gotest
- 6cov -g `pwd` | grep -v '_test\.go:'
+ 6cov -g $$(pwd) | grep -v '_test\.go:'
%.$O: %.go
$(GC) -I_obj $*.go
@@ -32,6 +33,7 @@
$(AS) $*.s
O1=\
+ buffer.$O\
bytes.$O\
@@ -39,7 +41,7 @@
_obj$D/bytes.a: phases
a1: $(O1)
- $(AR) grc _obj$D/bytes.a bytes.$O
+ $(AR) grc _obj$D/bytes.a buffer.$O bytes.$O
rm -f $(O1)
diff --git a/src/pkg/bytes/buffer.go b/src/pkg/bytes/buffer.go
new file mode 100644
index 0000000..58e06e9
--- /dev/null
+++ b/src/pkg/bytes/buffer.go
@@ -0,0 +1,125 @@
+// 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 bytes
+
+// Simple byte buffer for marshaling data.
+
+import (
+ "bytes";
+ "os";
+)
+
+func bytecopy(dst []byte, doff int, src []byte, soff int, count int) {
+ for ; count > 0; count-- {
+ dst[doff] = src[soff];
+ doff++;
+ soff++;
+ }
+}
+
+// A Buffer is a variable-sized buffer of bytes
+// with Read and Write methods.
+// The zero value for Buffer is an empty buffer ready to use.
+type Buffer struct {
+ buf []byte; // contents are the bytes buf[off : len(buf)]
+ off int; // read at &buf[off], write at &buf[len(buf)]
+}
+
+// Data returns the contents of the unread portion of the buffer;
+// len(b.Data()) == b.Len().
+func (b *Buffer) Data() []byte {
+ return b.buf[b.off : len(b.buf)]
+}
+
+// Len returns the number of bytes of the unread portion of the buffer;
+// b.Len() == len(b.Data()).
+func (b *Buffer) Len() int {
+ return len(b.buf) - b.off
+}
+
+// Truncate discards all but the first n unread bytes from the buffer.
+// It is an error to call b.Truncate(n) with n > b.Len().
+func (b *Buffer) Truncate(n int) {
+ if n == 0 {
+ // Reuse buffer space.
+ b.off = 0;
+ }
+ b.buf = b.buf[0 : b.off + n];
+}
+
+// Reset resets the buffer so it has no content.
+// b.Reset() is the same as b.Truncate(0).
+func (b *Buffer) Reset() {
+ b.Truncate(0);
+}
+
+// Write appends the contents of p to the buffer. The return
+// value n is the length of p; err is always nil.
+func (b *Buffer) Write(p []byte) (n int, err os.Error) {
+ m := b.Len();
+ n = len(p);
+
+ if len(b.buf) + n > cap(b.buf) {
+ // not enough space at end
+ buf := b.buf;
+ if m + n > cap(b.buf) {
+ // not enough space anywhere
+ buf = make([]byte, 2*cap(b.buf) + n)
+ }
+ bytecopy(buf, 0, b.buf, b.off, m);
+ b.buf = buf;
+ b.off = 0
+ }
+
+ b.buf = b.buf[0 : b.off + m + n];
+ bytecopy(b.buf, b.off + m, p, 0, n);
+ return n, nil
+}
+
+// WriteByte appends the byte c to the buffer.
+// The returned error is always nil, but is included
+// to match bufio.Writer's WriteByte.
+func (b *Buffer) WriteByte(c byte) os.Error {
+ b.Write([]byte{c});
+ return nil;
+}
+
+// Read reads the next len(p) bytes from the buffer or until the buffer
+// is drained. The return value n is the number of bytes read. If the
+// buffer has no data to return, err is os.EOF even if len(p) is zero;
+// otherwise it is nil.
+func (b *Buffer) Read(p []byte) (n int, err os.Error) {
+ if b.off >= len(b.buf) {
+ return 0, os.EOF
+ }
+ m := b.Len();
+ n = len(p);
+
+ if n > m {
+ // more bytes requested than available
+ n = m
+ }
+
+ bytecopy(p, 0, b.buf, b.off, n);
+ b.off += n;
+ return n, err
+}
+
+// ReadByte reads and returns the next byte from the buffer.
+// If no byte is available, it returns error os.EOF.
+func (b *Buffer) ReadByte() (c byte, err os.Error) {
+ if b.off >= len(b.buf) {
+ return 0, os.EOF;
+ }
+ c = b.buf[b.off];
+ b.off++;
+ return c, nil;
+}
+
+// NewBuffer creates and initializes a new Buffer
+// using buf as its initial contents.
+func NewBuffer(buf []byte) *Buffer {
+ return &Buffer{buf, 0};
+}
diff --git a/src/pkg/bytes/buffer_test.go b/src/pkg/bytes/buffer_test.go
new file mode 100644
index 0000000..0ba83e9
--- /dev/null
+++ b/src/pkg/bytes/buffer_test.go
@@ -0,0 +1,171 @@
+// 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 io
+
+import (
+ "io";
+ "rand";
+ "testing";
+)
+
+
+const N = 10000; // make this bigger for a larger (and slower) test
+var data []byte; // test data for write tests
+
+
+func init() {
+ data = make([]byte, N);
+ for i := 0; i < len(data); i++ {
+ data[i] = 'a' + byte(i % 26)
+ }
+}
+
+
+// Verify that contents of buf match the string s.
+func check(t *testing.T, testname string, buf *ByteBuffer, s string) {
+ if buf.Len() != len(buf.Data()) {
+ t.Errorf("%s: buf.Len() == %d, len(buf.Data()) == %d\n", testname, buf.Len(), len(buf.Data()))
+ }
+
+ if buf.Len() != len(s) {
+ t.Errorf("%s: buf.Len() == %d, len(s) == %d\n", testname, buf.Len(), len(s))
+ }
+
+ if string(buf.Data()) != s {
+ t.Errorf("%s: string(buf.Data()) == %q, s == %q\n", testname, string(buf.Data()), s)
+ }
+}
+
+
+// Fill buf through n writes of fub.
+// The initial contents of buf corresponds to the string s;
+// the result is the final contents of buf returned as a string.
+func fill(t *testing.T, testname string, buf *ByteBuffer, s string, n int, fub []byte) string {
+ check(t, testname + " (fill 1)", buf, s);
+ for ; n > 0; n-- {
+ m, err := buf.Write(fub);
+ if m != len(fub) {
+ t.Errorf(testname + " (fill 2): m == %d, expected %d\n", m, len(fub));
+ }
+ if err != nil {
+ t.Errorf(testname + " (fill 3): err should always be nil, found err == %s\n", err);
+ }
+ s += string(fub);
+ check(t, testname + " (fill 4)", buf, s);
+ }
+ return s;
+}
+
+
+// Empty buf through repeated reads into fub.
+// The initial contents of buf corresponds to the string s.
+func empty(t *testing.T, testname string, buf *ByteBuffer, s string, fub []byte) {
+ check(t, testname + " (empty 1)", buf, s);
+
+ for {
+ n, err := buf.Read(fub);
+ if n == 0 {
+ break;
+ }
+ if err != nil {
+ t.Errorf(testname + " (empty 2): err should always be nil, found err == %s\n", err);
+ }
+ s = s[n : len(s)];
+ check(t, testname + " (empty 3)", buf, s);
+ }
+
+ check(t, testname + " (empty 4)", buf, "");
+}
+
+
+func TestBasicOperations(t *testing.T) {
+ var buf ByteBuffer;
+
+ for i := 0; i < 5; i++ {
+ check(t, "TestBasicOperations (1)", &buf, "");
+
+ buf.Reset();
+ check(t, "TestBasicOperations (2)", &buf, "");
+
+ buf.Truncate(0);
+ check(t, "TestBasicOperations (3)", &buf, "");
+
+ n, err := buf.Write(data[0 : 1]);
+ if n != 1 {
+ t.Errorf("wrote 1 byte, but n == %d\n", n);
+ }
+ if err != nil {
+ t.Errorf("err should always be nil, but err == %s\n", err);
+ }
+ check(t, "TestBasicOperations (4)", &buf, "a");
+
+ buf.WriteByte(data[1]);
+ check(t, "TestBasicOperations (5)", &buf, "ab");
+
+ n, err = buf.Write(data[2 : 26]);
+ if n != 24 {
+ t.Errorf("wrote 25 bytes, but n == %d\n", n);
+ }
+ check(t, "TestBasicOperations (6)", &buf, string(data[0 : 26]));
+
+ buf.Truncate(26);
+ check(t, "TestBasicOperations (7)", &buf, string(data[0 : 26]));
+
+ buf.Truncate(20);
+ check(t, "TestBasicOperations (8)", &buf, string(data[0 : 20]));
+
+ empty(t, "TestBasicOperations (9)", &buf, string(data[0 : 20]), make([]byte, 5));
+ empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100));
+
+ buf.WriteByte(data[1]);
+ c, err := buf.ReadByte();
+ if err != nil {
+ t.Errorf("ReadByte unexpected eof\n");
+ }
+ if c != data[1] {
+ t.Errorf("ReadByte wrong value c=%v\n", c);
+ }
+ c, err = buf.ReadByte();
+ if err == nil {
+ t.Errorf("ReadByte unexpected not eof\n");
+ }
+ }
+}
+
+
+func TestLargeWrites(t *testing.T) {
+ var buf ByteBuffer;
+ for i := 3; i < 30; i += 3 {
+ s := fill(t, "TestLargeWrites (1)", &buf, "", 5, data);
+ empty(t, "TestLargeWrites (2)", &buf, s, make([]byte, len(data)/i));
+ }
+ check(t, "TestLargeWrites (3)", &buf, "");
+}
+
+
+func TestLargeReads(t *testing.T) {
+ var buf ByteBuffer;
+ for i := 3; i < 30; i += 3 {
+ s := fill(t, "TestLargeReads (1)", &buf, "", 5, data[0 : len(data)/i]);
+ empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)));
+ }
+ check(t, "TestLargeReads (3)", &buf, "");
+}
+
+
+func TestMixedReadsAndWrites(t *testing.T) {
+ var buf ByteBuffer;
+ s := "";
+ for i := 0; i < 50; i++ {
+ wlen := rand.Intn(len(data));
+ s = fill(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0 : wlen]);
+
+ rlen := rand.Intn(len(data));
+ fub := make([]byte, rlen);
+ n, err := buf.Read(fub);
+ s = s[n : len(s)];
+ }
+ empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len()));
+}
diff --git a/src/pkg/bytes/bytes_test.go b/src/pkg/bytes/bytes_test.go
index 01adbcc..a3e4426 100644
--- a/src/pkg/bytes/bytes_test.go
+++ b/src/pkg/bytes/bytes_test.go
@@ -6,7 +6,7 @@
import (
"bytes";
- "io";
+ "strings";
"testing";
)
@@ -59,8 +59,8 @@
func TestCompare(t *testing.T) {
for i := 0; i < len(comparetests); i++ {
tt := comparetests[i];
- a := io.StringBytes(tt.a);
- b := io.StringBytes(tt.b);
+ a := strings.Bytes(tt.a);
+ b := strings.Bytes(tt.b);
cmp := Compare(a, b);
eql := Equal(a, b);
if cmp != tt.cmp {
@@ -85,7 +85,7 @@
}
func TestExplode(t *testing.T) {
for _, tt := range(explodetests) {
- a := explode(io.StringBytes(tt.s), tt.n);
+ a := explode(strings.Bytes(tt.s), tt.n);
result := arrayOfString(a);
if !eq(result, tt.a) {
t.Errorf(`Explode("%s", %d) = %v; want %v`, tt.s, tt.n, result, tt.a);
@@ -122,13 +122,13 @@
}
func TestSplit(t *testing.T) {
for _, tt := range splittests {
- a := Split(io.StringBytes(tt.s), io.StringBytes(tt.sep), tt.n);
+ a := Split(strings.Bytes(tt.s), strings.Bytes(tt.sep), tt.n);
result := arrayOfString(a);
if !eq(result, tt.a) {
t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a);
continue;
}
- s := Join(a, io.StringBytes(tt.sep));
+ s := Join(a, strings.Bytes(tt.sep));
if string(s) != tt.s {
t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s);
}
@@ -154,8 +154,8 @@
func TestCopy(t *testing.T) {
for i := 0; i < len(copytests); i++ {
tt := copytests[i];
- dst := io.StringBytes(tt.a);
- n := Copy(dst, io.StringBytes(tt.b));
+ dst := strings.Bytes(tt.a);
+ n := Copy(dst, strings.Bytes(tt.b));
result := string(dst);
if result != tt.res || n != tt.n {
t.Errorf(`Copy(%q, %q) = %d, %q; want %d, %q`, tt.a, tt.b, n, result, tt.n, tt.res);