1
0
mirror of https://github.com/golang/go synced 2024-10-03 10:21:22 -06:00

pass the state to the encoders and decoders so error handling can be centralized.

R=rsc
DELTA=172  (40 added, 6 deleted, 126 changed)
OCL=30941
CL=30944
This commit is contained in:
Rob Pike 2009-06-30 16:20:31 -07:00
parent f6f825141a
commit 79b2cf92d9
3 changed files with 124 additions and 90 deletions

View File

@ -36,26 +36,30 @@ var encodeT = []EncodeT {
// Test basic encode/decode routines for unsigned integers // Test basic encode/decode routines for unsigned integers
func TestUintCodec(t *testing.T) { func TestUintCodec(t *testing.T) {
var b = new(bytes.Buffer); b := new(bytes.Buffer);
encState := new(EncState);
encState.w = b;
for i, tt := range encodeT { for i, tt := range encodeT {
b.Reset(); b.Reset();
err := EncodeUint(b, tt.x); EncodeUint(encState, tt.x);
if err != nil { if encState.err != nil {
t.Error("EncodeUint:", tt.x, err) t.Error("EncodeUint:", tt.x, encState.err)
} }
if !bytes.Equal(tt.b, b.Data()) { if !bytes.Equal(tt.b, b.Data()) {
t.Errorf("EncodeUint: expected % x got % x", tt.b, b.Data()) t.Errorf("EncodeUint: expected % x got % x", tt.b, b.Data())
} }
} }
decState := new(DecState);
decState.r = b;
for u := uint64(0); ; u = (u+1) * 7 { for u := uint64(0); ; u = (u+1) * 7 {
b.Reset(); b.Reset();
err := EncodeUint(b, u); EncodeUint(encState, u);
if err != nil { if encState.err != nil {
t.Error("EncodeUint:", u, err) t.Error("EncodeUint:", u, encState.err)
} }
v, err := DecodeUint(b); v := DecodeUint(decState);
if err != nil { if decState.err != nil {
t.Error("DecodeUint:", u, err) t.Error("DecodeUint:", u, decState.err)
} }
if u != v { if u != v {
t.Errorf("Encode/Decode: sent %#x received %#x\n", u, v) t.Errorf("Encode/Decode: sent %#x received %#x\n", u, v)
@ -68,13 +72,17 @@ func TestUintCodec(t *testing.T) {
func verifyInt(i int64, t *testing.T) { func verifyInt(i int64, t *testing.T) {
var b = new(bytes.Buffer); var b = new(bytes.Buffer);
err := EncodeInt(b, i); encState := new(EncState);
if err != nil { encState.w = b;
t.Error("EncodeInt:", i, err) EncodeInt(encState, i);
if encState.err != nil {
t.Error("EncodeInt:", i, encState.err)
} }
j, err := DecodeInt(b); decState := new(DecState);
if err != nil { decState.r = b;
t.Error("DecodeInt:", i, err) j := DecodeInt(decState);
if decState.err != nil {
t.Error("DecodeInt:", i, decState.err)
} }
if i != j { if i != j {
t.Errorf("Encode/Decode: sent %#x received %#x\n", uint64(i), uint64(j)) t.Errorf("Encode/Decode: sent %#x received %#x\n", uint64(i), uint64(j))
@ -109,7 +117,7 @@ var floatResult = []byte{0x80, 0x40, 0xe2, 0x81, 0x40, 0xe2, 0x82, 0x40, 0xe2}
// Do not run the machine yet; instead do individual instructions crafted by hand. // Do not run the machine yet; instead do individual instructions crafted by hand.
func TestScalarEncInstructions(t *testing.T) { func TestScalarEncInstructions(t *testing.T) {
var b = new(bytes.Buffer); var b = new(bytes.Buffer);
var state encState; var state EncState;
// bool // bool
{ {

View File

@ -7,34 +7,48 @@ package gob
import ( import (
"io"; "io";
"os"; "os";
"unsafe";
) )
// DecodeUint reads an encoded unsigned integer from r. // The global execution state of an instance of the decoder.
func DecodeUint(r io.Reader) (x uint64, err os.Error) { type DecState struct {
var buf [1]byte; r io.Reader;
err os.Error;
base uintptr;
buf [1]byte; // buffer used by the decoder; here to avoid allocation.
}
// DecodeUint reads an encoded unsigned integer from state.r.
// Sets state.err. If state.err is already non-nil, it does nothing.
func DecodeUint(state *DecState) (x uint64) {
if state.err != nil {
return
}
for shift := uint(0);; shift += 7 { for shift := uint(0);; shift += 7 {
n, err := r.Read(&buf); var n int;
n, state.err = state.r.Read(&state.buf);
if n != 1 { if n != 1 {
return 0, err return 0
} }
b := uint64(buf[0]); b := uint64(state.buf[0]);
x |= b << shift; x |= b << shift;
if b&0x80 != 0 { if b&0x80 != 0 {
x &^= 0x80 << shift; x &^= 0x80 << shift;
break break
} }
} }
return x, nil; return x;
} }
// DecodeInt reads an encoded signed integer from r. // DecodeInt reads an encoded signed integer from state.r.
func DecodeInt(r io.Reader) (i int64, err os.Error) { // Sets state.err. If state.err is already non-nil, it does nothing.
x, err := DecodeUint(r); func DecodeInt(state *DecState) int64 {
if err != nil { x := DecodeUint(state);
return if state.err != nil {
return 0
} }
if x & 1 != 0 { if x & 1 != 0 {
return ^int64(x>>1), nil return ^int64(x>>1)
} }
return int64(x >> 1), nil return int64(x >> 1)
} }

View File

@ -11,51 +11,63 @@ import (
"unsafe"; "unsafe";
) )
// The global execution state of an instance of the encoder.
type EncState struct {
w io.Writer;
base uintptr; // the base address of the data structure being written
err os.Error; // error encountered during encoding;
buf [16]byte; // buffer used by the encoder; here to avoid allocation.
}
// Integers encode as a variant of Google's protocol buffer varint (varvarint?). // Integers encode as a variant of Google's protocol buffer varint (varvarint?).
// The variant is that the continuation bytes have a zero top bit instead of a one. // The variant is that the continuation bytes have a zero top bit instead of a one.
// That way there's only one bit to clear and the value is a little easier to see if // That way there's only one bit to clear and the value is a little easier to see if
// you're the unfortunate sort of person who must read the hex to debug. // you're the unfortunate sort of person who must read the hex to debug.
// EncodeUint writes an encoded unsigned integer to w. // EncodeUint writes an encoded unsigned integer to state.w. Sets state.err.
func EncodeUint(w io.Writer, x uint64) os.Error { // If state.err is already non-nil, it does nothing.
var buf [16]byte; func EncodeUint(state *EncState, x uint64) {
var n int; var n int;
if state.err != nil {
return
}
for n = 0; x > 127; n++ { for n = 0; x > 127; n++ {
buf[n] = uint8(x & 0x7F); state.buf[n] = uint8(x & 0x7F);
x >>= 7; x >>= 7;
} }
buf[n] = 0x80 | uint8(x); state.buf[n] = 0x80 | uint8(x);
nn, err := w.Write(buf[0:n+1]); var nn int;
return err; nn, state.err = state.w.Write(state.buf[0:n+1]);
} }
// EncodeInt writes an encoded signed integer to w. // EncodeInt writes an encoded signed integer to state.w.
// The low bit of the encoding says whether to bit complement the (other bits of the) uint to recover the int. // The low bit of the encoding says whether to bit complement the (other bits of the) uint to recover the int.
func EncodeInt(w io.Writer, i int64) os.Error { // Sets state.err. If state.err is already non-nil, it does nothing.
func EncodeInt(state *EncState, i int64){
var x uint64; var x uint64;
if i < 0 { if i < 0 {
x = uint64(^i << 1) | 1 x = uint64(^i << 1) | 1
} else { } else {
x = uint64(i << 1) x = uint64(i << 1)
} }
return EncodeUint(w, uint64(x)) EncodeUint(state, uint64(x))
}
// The global execution state of an instance of the encoder.
type encState struct {
w io.Writer;
base uintptr;
} }
// The 'instructions' of the encoding machine // The 'instructions' of the encoding machine
type encInstr struct { type encInstr struct {
op func(i *encInstr, state *encState); op func(i *encInstr, state *EncState);
field int; // field number field int; // field number
indir int; // how many pointer indirections to reach the value in the struct indir int; // how many pointer indirections to reach the value in the struct
offset uintptr; // offset in the structure of the field to encode offset uintptr; // offset in the structure of the field to encode
} }
func encBool(i *encInstr, state *encState) { // Each encoder is responsible for handling any indirections associated
// with the data structure. If any pointer so reached is nil, no bytes are written.
// If the data item is zero, no bytes are written.
// Otherwise, the output (for a scalar) is the field number, as an encoded integer,
// followed by the field data in its appropriate format.
func encBool(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -65,12 +77,12 @@ func encBool(i *encInstr, state *encState) {
} }
b := *(*bool)(p); b := *(*bool)(p);
if b { if b {
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeUint(state.w, 1); EncodeUint(state, 1);
} }
} }
func encInt(i *encInstr, state *encState) { func encInt(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -80,12 +92,12 @@ func encInt(i *encInstr, state *encState) {
} }
v := int64(*(*int)(p)); v := int64(*(*int)(p));
if v != 0 { if v != 0 {
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeInt(state.w, v); EncodeInt(state, v);
} }
} }
func encUint(i *encInstr, state *encState) { func encUint(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -95,12 +107,12 @@ func encUint(i *encInstr, state *encState) {
} }
v := uint64(*(*uint)(p)); v := uint64(*(*uint)(p));
if v != 0 { if v != 0 {
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeUint(state.w, v); EncodeUint(state, v);
} }
} }
func encInt8(i *encInstr, state *encState) { func encInt8(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -110,12 +122,12 @@ func encInt8(i *encInstr, state *encState) {
} }
v := int64(*(*int8)(p)); v := int64(*(*int8)(p));
if v != 0 { if v != 0 {
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeInt(state.w, v); EncodeInt(state, v);
} }
} }
func encUint8(i *encInstr, state *encState) { func encUint8(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -125,12 +137,12 @@ func encUint8(i *encInstr, state *encState) {
} }
v := uint64(*(*uint8)(p)); v := uint64(*(*uint8)(p));
if v != 0 { if v != 0 {
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeUint(state.w, v); EncodeUint(state, v);
} }
} }
func encInt16(i *encInstr, state *encState) { func encInt16(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -140,12 +152,12 @@ func encInt16(i *encInstr, state *encState) {
} }
v := int64(*(*int16)(p)); v := int64(*(*int16)(p));
if v != 0 { if v != 0 {
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeInt(state.w, v); EncodeInt(state, v);
} }
} }
func encUint16(i *encInstr, state *encState) { func encUint16(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -155,12 +167,12 @@ func encUint16(i *encInstr, state *encState) {
} }
v := uint64(*(*uint16)(p)); v := uint64(*(*uint16)(p));
if v != 0 { if v != 0 {
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeUint(state.w, v); EncodeUint(state, v);
} }
} }
func encInt32(i *encInstr, state *encState) { func encInt32(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -170,12 +182,12 @@ func encInt32(i *encInstr, state *encState) {
} }
v := int64(*(*int32)(p)); v := int64(*(*int32)(p));
if v != 0 { if v != 0 {
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeInt(state.w, v); EncodeInt(state, v);
} }
} }
func encUint32(i *encInstr, state *encState) { func encUint32(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -185,12 +197,12 @@ func encUint32(i *encInstr, state *encState) {
} }
v := uint64(*(*uint32)(p)); v := uint64(*(*uint32)(p));
if v != 0 { if v != 0 {
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeUint(state.w, v); EncodeUint(state, v);
} }
} }
func encInt64(i *encInstr, state *encState) { func encInt64(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -200,12 +212,12 @@ func encInt64(i *encInstr, state *encState) {
} }
v := *(*int64)(p); v := *(*int64)(p);
if v != 0 { if v != 0 {
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeInt(state.w, v); EncodeInt(state, v);
} }
} }
func encUint64(i *encInstr, state *encState) { func encUint64(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -215,8 +227,8 @@ func encUint64(i *encInstr, state *encState) {
} }
v := *(*uint64)(p); v := *(*uint64)(p);
if v != 0 { if v != 0 {
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeUint(state.w, v); EncodeUint(state, v);
} }
} }
@ -236,7 +248,7 @@ func floatBits(f float64) uint64 {
return v; return v;
} }
func encFloat(i *encInstr, state *encState) { func encFloat(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -247,12 +259,12 @@ func encFloat(i *encInstr, state *encState) {
f := float(*(*float)(p)); f := float(*(*float)(p));
if f != 0 { if f != 0 {
v := floatBits(float64(f)); v := floatBits(float64(f));
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeUint(state.w, v); EncodeUint(state, v);
} }
} }
func encFloat32(i *encInstr, state *encState) { func encFloat32(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -263,12 +275,12 @@ func encFloat32(i *encInstr, state *encState) {
f := float32(*(*float32)(p)); f := float32(*(*float32)(p));
if f != 0 { if f != 0 {
v := floatBits(float64(f)); v := floatBits(float64(f));
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeUint(state.w, v); EncodeUint(state, v);
} }
} }
func encFloat64(i *encInstr, state *encState) { func encFloat64(i *encInstr, state *EncState) {
p := unsafe.Pointer(state.base+i.offset); p := unsafe.Pointer(state.base+i.offset);
for indir := i.indir; indir > 0; indir-- { for indir := i.indir; indir > 0; indir-- {
p = *(*unsafe.Pointer)(p); p = *(*unsafe.Pointer)(p);
@ -279,7 +291,7 @@ func encFloat64(i *encInstr, state *encState) {
f := *(*float64)(p); f := *(*float64)(p);
if f != 0 { if f != 0 {
v := floatBits(f); v := floatBits(f);
EncodeUint(state.w, uint64(i.field)); EncodeUint(state, uint64(i.field));
EncodeUint(state.w, v); EncodeUint(state, v);
} }
} }