Commit 04ae91cf authored by Rob Pike's avatar Rob Pike

clean up the decode loop and fix a couple of bad prints

R=rsc
DELTA=15  (8 added, 2 deleted, 5 changed)
OCL=31738
CL=31738
parent 19022830
...@@ -19,6 +19,7 @@ type Decoder struct { ...@@ -19,6 +19,7 @@ type Decoder struct {
seen map[TypeId] *wireType; // which types we've already seen described seen map[TypeId] *wireType; // which types we've already seen described
state *decodeState; // reads data from in-memory buffer state *decodeState; // reads data from in-memory buffer
countState *decodeState; // reads counts from wire countState *decodeState; // reads counts from wire
buf []byte;
oneByte []byte; oneByte []byte;
} }
...@@ -63,10 +64,15 @@ func (dec *Decoder) Decode(e interface{}) os.Error { ...@@ -63,10 +64,15 @@ func (dec *Decoder) Decode(e interface{}) os.Error {
return err; return err;
} }
// Allocate the buffer.
if nbytes > uint64(len(dec.buf)) {
dec.buf = make([]byte, nbytes + 1000);
}
dec.state.b = bytes.NewBuffer(dec.buf[0:nbytes]);
// Read the data // Read the data
buf := make([]byte, nbytes); // TODO(r): avoid repeated allocation
var n int; var n int;
n, err = dec.r.Read(buf); n, err = dec.r.Read(dec.buf[0:nbytes]);
if err != nil { if err != nil {
return err; return err;
} }
...@@ -74,13 +80,13 @@ func (dec *Decoder) Decode(e interface{}) os.Error { ...@@ -74,13 +80,13 @@ func (dec *Decoder) Decode(e interface{}) os.Error {
return os.ErrorString("gob decode: short read"); return os.ErrorString("gob decode: short read");
} }
dec.state.b = bytes.NewBuffer(buf); // TODO(r): avoid repeated allocation
// Receive a type id. // Receive a type id.
id := TypeId(decodeInt(dec.state)); id := TypeId(decodeInt(dec.state));
if dec.state.err != nil { if dec.state.err != nil {
return dec.state.err return dec.state.err
} }
// Is it a type?
if id < 0 { // 0 is the error state, handled above if id < 0 { // 0 is the error state, handled above
// If the id is negative, we have a type. // If the id is negative, we have a type.
dec.recvType(-id); dec.recvType(-id);
...@@ -90,7 +96,7 @@ func (dec *Decoder) Decode(e interface{}) os.Error { ...@@ -90,7 +96,7 @@ func (dec *Decoder) Decode(e interface{}) os.Error {
continue; continue;
} }
// we have a value // No, it's a value.
info := getTypeInfo(rt); info := getTypeInfo(rt);
// Check type compatibility. // Check type compatibility.
......
...@@ -171,7 +171,7 @@ func TestEncoderDecoder(t *testing.T) { ...@@ -171,7 +171,7 @@ func TestEncoderDecoder(t *testing.T) {
newEt1 := new(ET1); newEt1 := new(ET1);
dec.Decode(newEt1); dec.Decode(newEt1);
if dec.state.err != nil { if dec.state.err != nil {
t.Fatalf("error decoding ET1:", dec.state.err); t.Fatal("error decoding ET1:", dec.state.err);
} }
if !reflect.DeepEqual(et1, newEt1) { if !reflect.DeepEqual(et1, newEt1) {
...@@ -185,7 +185,7 @@ func TestEncoderDecoder(t *testing.T) { ...@@ -185,7 +185,7 @@ func TestEncoderDecoder(t *testing.T) {
newEt1 = new(ET1); newEt1 = new(ET1);
dec.Decode(newEt1); dec.Decode(newEt1);
if dec.state.err != nil { if dec.state.err != nil {
t.Fatalf("round 2: error decoding ET1:", dec.state.err); t.Fatal("round 2: error decoding ET1:", dec.state.err);
} }
if !reflect.DeepEqual(et1, newEt1) { if !reflect.DeepEqual(et1, newEt1) {
t.Fatalf("round 2: invalid data for et1: expected %+v; got %+v\n", *et1, *newEt1); t.Fatalf("round 2: invalid data for et1: expected %+v; got %+v\n", *et1, *newEt1);
...@@ -202,7 +202,7 @@ func TestEncoderDecoder(t *testing.T) { ...@@ -202,7 +202,7 @@ func TestEncoderDecoder(t *testing.T) {
newEt2 := new(ET2); newEt2 := new(ET2);
dec.Decode(newEt2); dec.Decode(newEt2);
if dec.state.err == nil { if dec.state.err == nil {
t.Fatalf("round 3: expected `bad type' error decoding ET2"); t.Fatal("round 3: expected `bad type' error decoding ET2");
} }
} }
......
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