1
0
mirror of https://github.com/golang/go synced 2024-11-25 01:47:56 -07:00

gob: make (en|dec)code(Ui|I)nt methods rather than functions.

No functional (ha!) change.

R=rsc
CC=golang-dev
https://golang.org/cl/3959041
This commit is contained in:
Rob Pike 2011-01-11 17:56:45 -08:00
parent 5aeacadc5a
commit a80cdcbe0a
6 changed files with 92 additions and 92 deletions

View File

@ -53,7 +53,7 @@ func TestUintCodec(t *testing.T) {
encState := newEncoderState(nil, b) encState := newEncoderState(nil, b)
for _, tt := range encodeT { for _, tt := range encodeT {
b.Reset() b.Reset()
encodeUint(encState, tt.x) encState.encodeUint(tt.x)
if !bytes.Equal(tt.b, b.Bytes()) { if !bytes.Equal(tt.b, b.Bytes()) {
t.Errorf("encodeUint: %#x encode: expected % x got % x", tt.x, tt.b, b.Bytes()) t.Errorf("encodeUint: %#x encode: expected % x got % x", tt.x, tt.b, b.Bytes())
} }
@ -61,8 +61,8 @@ func TestUintCodec(t *testing.T) {
decState := newDecodeState(nil, &b) decState := newDecodeState(nil, &b)
for u := uint64(0); ; u = (u + 1) * 7 { for u := uint64(0); ; u = (u + 1) * 7 {
b.Reset() b.Reset()
encodeUint(encState, u) encState.encodeUint(u)
v := decodeUint(decState) v := decState.decodeUint()
if u != v { if u != v {
t.Errorf("Encode/Decode: sent %#x received %#x", u, v) t.Errorf("Encode/Decode: sent %#x received %#x", u, v)
} }
@ -76,10 +76,10 @@ func verifyInt(i int64, t *testing.T) {
defer testError(t) defer testError(t)
var b = new(bytes.Buffer) var b = new(bytes.Buffer)
encState := newEncoderState(nil, b) encState := newEncoderState(nil, b)
encodeInt(encState, i) encState.encodeInt(i)
decState := newDecodeState(nil, &b) decState := newDecodeState(nil, &b)
decState.buf = make([]byte, 8) decState.buf = make([]byte, 8)
j := decodeInt(decState) j := decState.decodeInt()
if i != j { if i != j {
t.Errorf("Encode/Decode: sent %#x received %#x", uint64(i), uint64(j)) t.Errorf("Encode/Decode: sent %#x received %#x", uint64(i), uint64(j))
} }
@ -317,7 +317,7 @@ func TestScalarEncInstructions(t *testing.T) {
func execDec(typ string, instr *decInstr, state *decodeState, t *testing.T, p unsafe.Pointer) { func execDec(typ string, instr *decInstr, state *decodeState, t *testing.T, p unsafe.Pointer) {
defer testError(t) defer testError(t)
v := int(decodeUint(state)) v := int(state.decodeUint())
if v+state.fieldnum != 6 { if v+state.fieldnum != 6 {
t.Fatalf("decoding field number %d, got %d", 6, v+state.fieldnum) t.Fatalf("decoding field number %d, got %d", 6, v+state.fieldnum)
} }

View File

@ -87,7 +87,7 @@ func (dec *Decoder) debug() {
func (dec *Decoder) debugFromBuffer(indent int, countPresent bool) { func (dec *Decoder) debugFromBuffer(indent int, countPresent bool) {
for dec.state.b.Len() > 0 { for dec.state.b.Len() > 0 {
// Receive a type id. // Receive a type id.
id := typeId(decodeInt(dec.state)) id := typeId(dec.state.decodeInt())
// Is it a new type? // Is it a new type?
if id < 0 { // 0 is the error state, handled above if id < 0 { // 0 is the error state, handled above
@ -107,7 +107,7 @@ func (dec *Decoder) debugFromBuffer(indent int, countPresent bool) {
break break
} }
if countPresent { if countPresent {
decodeUint(dec.state) dec.state.decodeUint()
} }
dec.debugPrint(indent, id) dec.debugPrint(indent, id)
break break
@ -175,7 +175,7 @@ func (dec *Decoder) debugSingle(indent int, id typeId, wire *wireType) {
if !ok && wire == nil { if !ok && wire == nil {
errorf("type id %d not defined\n", id) errorf("type id %d not defined\n", id)
} }
decodeUint(dec.state) dec.state.decodeUint()
dec.printItem(indent, id) dec.printItem(indent, id)
} }
@ -206,7 +206,7 @@ func (dec *Decoder) printItem(indent int, id typeId) {
func (dec *Decoder) printArray(indent int, wire *wireType) { func (dec *Decoder) printArray(indent int, wire *wireType) {
elemId := wire.ArrayT.Elem elemId := wire.ArrayT.Elem
n := int(decodeUint(dec.state)) n := int(dec.state.decodeUint())
for i := 0; i < n && dec.err == nil; i++ { for i := 0; i < n && dec.err == nil; i++ {
dec.printItem(indent, elemId) dec.printItem(indent, elemId)
} }
@ -219,7 +219,7 @@ func (dec *Decoder) printArray(indent int, wire *wireType) {
func (dec *Decoder) printMap(indent int, wire *wireType) { func (dec *Decoder) printMap(indent int, wire *wireType) {
keyId := wire.MapT.Key keyId := wire.MapT.Key
elemId := wire.MapT.Elem elemId := wire.MapT.Elem
n := int(decodeUint(dec.state)) n := int(dec.state.decodeUint())
for i := 0; i < n && dec.err == nil; i++ { for i := 0; i < n && dec.err == nil; i++ {
dec.printItem(indent, keyId) dec.printItem(indent, keyId)
dec.printItem(indent+1, elemId) dec.printItem(indent+1, elemId)
@ -228,7 +228,7 @@ func (dec *Decoder) printMap(indent int, wire *wireType) {
func (dec *Decoder) printSlice(indent int, wire *wireType) { func (dec *Decoder) printSlice(indent int, wire *wireType) {
elemId := wire.SliceT.Elem elemId := wire.SliceT.Elem
n := int(decodeUint(dec.state)) n := int(dec.state.decodeUint())
for i := 0; i < n && dec.err == nil; i++ { for i := 0; i < n && dec.err == nil; i++ {
dec.printItem(indent, elemId) dec.printItem(indent, elemId)
} }
@ -238,27 +238,27 @@ func (dec *Decoder) printBuiltin(indent int, id typeId) {
tab(indent) tab(indent)
switch id { switch id {
case tBool: case tBool:
if decodeInt(dec.state) == 0 { if dec.state.decodeInt() == 0 {
fmt.Printf("false\n") fmt.Printf("false\n")
} else { } else {
fmt.Printf("true\n") fmt.Printf("true\n")
} }
case tInt: case tInt:
fmt.Printf("%d\n", decodeInt(dec.state)) fmt.Printf("%d\n", dec.state.decodeInt())
case tUint: case tUint:
fmt.Printf("%d\n", decodeUint(dec.state)) fmt.Printf("%d\n", dec.state.decodeUint())
case tFloat: case tFloat:
fmt.Printf("%g\n", floatFromBits(decodeUint(dec.state))) fmt.Printf("%g\n", floatFromBits(dec.state.decodeUint()))
case tBytes: case tBytes:
b := make([]byte, decodeUint(dec.state)) b := make([]byte, dec.state.decodeUint())
dec.state.b.Read(b) dec.state.b.Read(b)
fmt.Printf("% x\n", b) fmt.Printf("% x\n", b)
case tString: case tString:
b := make([]byte, decodeUint(dec.state)) b := make([]byte, dec.state.decodeUint())
dec.state.b.Read(b) dec.state.b.Read(b)
fmt.Printf("%q\n", b) fmt.Printf("%q\n", b)
case tInterface: case tInterface:
b := make([]byte, decodeUint(dec.state)) b := make([]byte, dec.state.decodeUint())
dec.state.b.Read(b) dec.state.b.Read(b)
if len(b) == 0 { if len(b) == 0 {
fmt.Printf("nil interface") fmt.Printf("nil interface")
@ -278,7 +278,7 @@ func (dec *Decoder) debugStruct(indent int, id typeId, wire *wireType) {
state := newDecodeState(dec, dec.state.b) state := newDecodeState(dec, dec.state.b)
state.fieldnum = -1 state.fieldnum = -1
for dec.err == nil { for dec.err == nil {
delta := int(decodeUint(state)) delta := int(state.decodeUint())
if delta < 0 { if delta < 0 {
errorf("gob decode: corrupted data: negative delta") errorf("gob decode: corrupted data: negative delta")
} }

View File

@ -81,7 +81,7 @@ func decodeUintReader(r io.Reader, buf []byte) (x uint64, err os.Error) {
// decodeUint reads an encoded unsigned integer from state.r. // decodeUint reads an encoded unsigned integer from state.r.
// Does not check for overflow. // Does not check for overflow.
func decodeUint(state *decodeState) (x uint64) { func (state *decodeState) decodeUint() (x uint64) {
b, err := state.b.ReadByte() b, err := state.b.ReadByte()
if err != nil { if err != nil {
error(err) error(err)
@ -108,8 +108,8 @@ func decodeUint(state *decodeState) (x uint64) {
// decodeInt reads an encoded signed integer from state.r. // decodeInt reads an encoded signed integer from state.r.
// Does not check for overflow. // Does not check for overflow.
func decodeInt(state *decodeState) int64 { func (state *decodeState) decodeInt() int64 {
x := decodeUint(state) x := state.decodeUint()
if x&1 != 0 { if x&1 != 0 {
return ^int64(x >> 1) return ^int64(x >> 1)
} }
@ -147,12 +147,12 @@ func decIndirect(p unsafe.Pointer, indir int) unsafe.Pointer {
} }
func ignoreUint(i *decInstr, state *decodeState, p unsafe.Pointer) { func ignoreUint(i *decInstr, state *decodeState, p unsafe.Pointer) {
decodeUint(state) state.decodeUint()
} }
func ignoreTwoUints(i *decInstr, state *decodeState, p unsafe.Pointer) { func ignoreTwoUints(i *decInstr, state *decodeState, p unsafe.Pointer) {
decodeUint(state) state.decodeUint()
decodeUint(state) state.decodeUint()
} }
func decBool(i *decInstr, state *decodeState, p unsafe.Pointer) { func decBool(i *decInstr, state *decodeState, p unsafe.Pointer) {
@ -162,7 +162,7 @@ func decBool(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
*(*bool)(p) = decodeInt(state) != 0 *(*bool)(p) = state.decodeInt() != 0
} }
func decInt8(i *decInstr, state *decodeState, p unsafe.Pointer) { func decInt8(i *decInstr, state *decodeState, p unsafe.Pointer) {
@ -172,7 +172,7 @@ func decInt8(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
v := decodeInt(state) v := state.decodeInt()
if v < math.MinInt8 || math.MaxInt8 < v { if v < math.MinInt8 || math.MaxInt8 < v {
error(i.ovfl) error(i.ovfl)
} else { } else {
@ -187,7 +187,7 @@ func decUint8(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
v := decodeUint(state) v := state.decodeUint()
if math.MaxUint8 < v { if math.MaxUint8 < v {
error(i.ovfl) error(i.ovfl)
} else { } else {
@ -202,7 +202,7 @@ func decInt16(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
v := decodeInt(state) v := state.decodeInt()
if v < math.MinInt16 || math.MaxInt16 < v { if v < math.MinInt16 || math.MaxInt16 < v {
error(i.ovfl) error(i.ovfl)
} else { } else {
@ -217,7 +217,7 @@ func decUint16(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
v := decodeUint(state) v := state.decodeUint()
if math.MaxUint16 < v { if math.MaxUint16 < v {
error(i.ovfl) error(i.ovfl)
} else { } else {
@ -232,7 +232,7 @@ func decInt32(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
v := decodeInt(state) v := state.decodeInt()
if v < math.MinInt32 || math.MaxInt32 < v { if v < math.MinInt32 || math.MaxInt32 < v {
error(i.ovfl) error(i.ovfl)
} else { } else {
@ -247,7 +247,7 @@ func decUint32(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
v := decodeUint(state) v := state.decodeUint()
if math.MaxUint32 < v { if math.MaxUint32 < v {
error(i.ovfl) error(i.ovfl)
} else { } else {
@ -262,7 +262,7 @@ func decInt64(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
*(*int64)(p) = int64(decodeInt(state)) *(*int64)(p) = int64(state.decodeInt())
} }
func decUint64(i *decInstr, state *decodeState, p unsafe.Pointer) { func decUint64(i *decInstr, state *decodeState, p unsafe.Pointer) {
@ -272,7 +272,7 @@ func decUint64(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
*(*uint64)(p) = uint64(decodeUint(state)) *(*uint64)(p) = uint64(state.decodeUint())
} }
// Floating-point numbers are transmitted as uint64s holding the bits // Floating-point numbers are transmitted as uint64s holding the bits
@ -291,7 +291,7 @@ func floatFromBits(u uint64) float64 {
} }
func storeFloat32(i *decInstr, state *decodeState, p unsafe.Pointer) { func storeFloat32(i *decInstr, state *decodeState, p unsafe.Pointer) {
v := floatFromBits(decodeUint(state)) v := floatFromBits(state.decodeUint())
av := v av := v
if av < 0 { if av < 0 {
av = -av av = -av
@ -321,7 +321,7 @@ func decFloat64(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
*(*float64)(p) = floatFromBits(uint64(decodeUint(state))) *(*float64)(p) = floatFromBits(uint64(state.decodeUint()))
} }
// Complex numbers are just a pair of floating-point numbers, real part first. // Complex numbers are just a pair of floating-point numbers, real part first.
@ -343,8 +343,8 @@ func decComplex128(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
real := floatFromBits(uint64(decodeUint(state))) real := floatFromBits(uint64(state.decodeUint()))
imag := floatFromBits(uint64(decodeUint(state))) imag := floatFromBits(uint64(state.decodeUint()))
*(*complex128)(p) = cmplx(real, imag) *(*complex128)(p) = cmplx(real, imag)
} }
@ -356,7 +356,7 @@ func decUint8Array(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
b := make([]uint8, decodeUint(state)) b := make([]uint8, state.decodeUint())
state.b.Read(b) state.b.Read(b)
*(*[]uint8)(p) = b *(*[]uint8)(p) = b
} }
@ -369,13 +369,13 @@ func decString(i *decInstr, state *decodeState, p unsafe.Pointer) {
} }
p = *(*unsafe.Pointer)(p) p = *(*unsafe.Pointer)(p)
} }
b := make([]byte, decodeUint(state)) b := make([]byte, state.decodeUint())
state.b.Read(b) state.b.Read(b)
*(*string)(p) = string(b) *(*string)(p) = string(b)
} }
func ignoreUint8Array(i *decInstr, state *decodeState, p unsafe.Pointer) { func ignoreUint8Array(i *decInstr, state *decodeState, p unsafe.Pointer) {
b := make([]byte, decodeUint(state)) b := make([]byte, state.decodeUint())
state.b.Read(b) state.b.Read(b)
} }
@ -411,7 +411,7 @@ func (dec *Decoder) decodeSingle(engine *decEngine, rtyp reflect.Type, b **bytes
state := newDecodeState(dec, b) state := newDecodeState(dec, b)
state.fieldnum = singletonField state.fieldnum = singletonField
basep := p basep := p
delta := int(decodeUint(state)) delta := int(state.decodeUint())
if delta != 0 { if delta != 0 {
errorf("gob decode: corrupted data: non-zero delta for singleton") errorf("gob decode: corrupted data: non-zero delta for singleton")
} }
@ -431,7 +431,7 @@ func (dec *Decoder) decodeStruct(engine *decEngine, rtyp *reflect.StructType, b
state.fieldnum = -1 state.fieldnum = -1
basep := p basep := p
for state.b.Len() > 0 { for state.b.Len() > 0 {
delta := int(decodeUint(state)) delta := int(state.decodeUint())
if delta < 0 { if delta < 0 {
errorf("gob decode: corrupted data: negative delta") errorf("gob decode: corrupted data: negative delta")
} }
@ -459,7 +459,7 @@ func (dec *Decoder) ignoreStruct(engine *decEngine, b **bytes.Buffer) (err os.Er
state := newDecodeState(dec, b) state := newDecodeState(dec, b)
state.fieldnum = -1 state.fieldnum = -1
for state.b.Len() > 0 { for state.b.Len() > 0 {
delta := int(decodeUint(state)) delta := int(state.decodeUint())
if delta < 0 { if delta < 0 {
errorf("gob ignore decode: corrupted data: negative delta") errorf("gob ignore decode: corrupted data: negative delta")
} }
@ -493,7 +493,7 @@ func (dec *Decoder) decodeArray(atyp *reflect.ArrayType, state *decodeState, p u
if indir > 0 { if indir > 0 {
p = allocate(atyp, p, 1) // All but the last level has been allocated by dec.Indirect p = allocate(atyp, p, 1) // All but the last level has been allocated by dec.Indirect
} }
if n := decodeUint(state); n != uint64(length) { if n := state.decodeUint(); n != uint64(length) {
errorf("gob: length mismatch in decodeArray") errorf("gob: length mismatch in decodeArray")
} }
dec.decodeArrayHelper(state, p, elemOp, elemWid, length, elemIndir, ovfl) dec.decodeArrayHelper(state, p, elemOp, elemWid, length, elemIndir, ovfl)
@ -522,7 +522,7 @@ func (dec *Decoder) decodeMap(mtyp *reflect.MapType, state *decodeState, p uintp
// that slices etc. can. We must recover a full reflection value for // that slices etc. can. We must recover a full reflection value for
// the iteration. // the iteration.
v := reflect.NewValue(unsafe.Unreflect(mtyp, unsafe.Pointer((p)))).(*reflect.MapValue) v := reflect.NewValue(unsafe.Unreflect(mtyp, unsafe.Pointer((p)))).(*reflect.MapValue)
n := int(decodeUint(state)) n := int(state.decodeUint())
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
key := decodeIntoValue(state, keyOp, keyIndir, reflect.MakeZero(mtyp.Key()), ovfl) key := decodeIntoValue(state, keyOp, keyIndir, reflect.MakeZero(mtyp.Key()), ovfl)
elem := decodeIntoValue(state, elemOp, elemIndir, reflect.MakeZero(mtyp.Elem()), ovfl) elem := decodeIntoValue(state, elemOp, elemIndir, reflect.MakeZero(mtyp.Elem()), ovfl)
@ -538,14 +538,14 @@ func (dec *Decoder) ignoreArrayHelper(state *decodeState, elemOp decOp, length i
} }
func (dec *Decoder) ignoreArray(state *decodeState, elemOp decOp, length int) { func (dec *Decoder) ignoreArray(state *decodeState, elemOp decOp, length int) {
if n := decodeUint(state); n != uint64(length) { if n := state.decodeUint(); n != uint64(length) {
errorf("gob: length mismatch in ignoreArray") errorf("gob: length mismatch in ignoreArray")
} }
dec.ignoreArrayHelper(state, elemOp, length) dec.ignoreArrayHelper(state, elemOp, length)
} }
func (dec *Decoder) ignoreMap(state *decodeState, keyOp, elemOp decOp) { func (dec *Decoder) ignoreMap(state *decodeState, keyOp, elemOp decOp) {
n := int(decodeUint(state)) n := int(state.decodeUint())
keyInstr := &decInstr{keyOp, 0, 0, 0, os.ErrorString("no error")} keyInstr := &decInstr{keyOp, 0, 0, 0, os.ErrorString("no error")}
elemInstr := &decInstr{elemOp, 0, 0, 0, os.ErrorString("no error")} elemInstr := &decInstr{elemOp, 0, 0, 0, os.ErrorString("no error")}
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
@ -555,7 +555,7 @@ func (dec *Decoder) ignoreMap(state *decodeState, keyOp, elemOp decOp) {
} }
func (dec *Decoder) decodeSlice(atyp *reflect.SliceType, state *decodeState, p uintptr, elemOp decOp, elemWid uintptr, indir, elemIndir int, ovfl os.ErrorString) { func (dec *Decoder) decodeSlice(atyp *reflect.SliceType, state *decodeState, p uintptr, elemOp decOp, elemWid uintptr, indir, elemIndir int, ovfl os.ErrorString) {
n := int(uintptr(decodeUint(state))) n := int(uintptr(state.decodeUint()))
if indir > 0 { if indir > 0 {
up := unsafe.Pointer(p) up := unsafe.Pointer(p)
if *(*unsafe.Pointer)(up) == nil { if *(*unsafe.Pointer)(up) == nil {
@ -574,7 +574,7 @@ func (dec *Decoder) decodeSlice(atyp *reflect.SliceType, state *decodeState, p u
} }
func (dec *Decoder) ignoreSlice(state *decodeState, elemOp decOp) { func (dec *Decoder) ignoreSlice(state *decodeState, elemOp decOp) {
dec.ignoreArrayHelper(state, elemOp, int(decodeUint(state))) dec.ignoreArrayHelper(state, elemOp, int(state.decodeUint()))
} }
// setInterfaceValue sets an interface value to a concrete value through // setInterfaceValue sets an interface value to a concrete value through
@ -598,7 +598,7 @@ func (dec *Decoder) decodeInterface(ityp *reflect.InterfaceType, state *decodeSt
// Create an interface reflect.Value. We need one even for the nil case. // Create an interface reflect.Value. We need one even for the nil case.
ivalue := reflect.MakeZero(ityp).(*reflect.InterfaceValue) ivalue := reflect.MakeZero(ityp).(*reflect.InterfaceValue)
// Read the name of the concrete type. // Read the name of the concrete type.
b := make([]byte, decodeUint(state)) b := make([]byte, state.decodeUint())
state.b.Read(b) state.b.Read(b)
name := string(b) name := string(b)
if name == "" { if name == "" {
@ -632,7 +632,7 @@ func (dec *Decoder) decodeInterface(ityp *reflect.InterfaceType, state *decodeSt
func (dec *Decoder) ignoreInterface(state *decodeState) { func (dec *Decoder) ignoreInterface(state *decodeState) {
// Read the name of the concrete type. // Read the name of the concrete type.
b := make([]byte, decodeUint(state)) b := make([]byte, state.decodeUint())
_, err := state.b.Read(b) _, err := state.b.Read(b)
if err != nil { if err != nil {
error(err) error(err)

View File

@ -107,7 +107,7 @@ func (dec *Decoder) recv() {
func (dec *Decoder) decodeValueFromBuffer(value reflect.Value, ignoreInterfaceValue, countPresent bool) { func (dec *Decoder) decodeValueFromBuffer(value reflect.Value, ignoreInterfaceValue, countPresent bool) {
for dec.state.b.Len() > 0 { for dec.state.b.Len() > 0 {
// Receive a type id. // Receive a type id.
id := typeId(decodeInt(dec.state)) id := typeId(dec.state.decodeInt())
// Is it a new type? // Is it a new type?
if id < 0 { // 0 is the error state, handled above if id < 0 { // 0 is the error state, handled above
@ -127,7 +127,7 @@ func (dec *Decoder) decodeValueFromBuffer(value reflect.Value, ignoreInterfaceVa
} }
// An interface value is preceded by a byte count. // An interface value is preceded by a byte count.
if countPresent { if countPresent {
count := int(decodeUint(dec.state)) count := int(dec.state.decodeUint())
if ignoreInterfaceValue { if ignoreInterfaceValue {
// An interface value is preceded by a byte count. Just skip that many bytes. // An interface value is preceded by a byte count. Just skip that many bytes.
dec.state.b.Next(int(count)) dec.state.b.Next(int(count))

View File

@ -37,7 +37,7 @@ func newEncoderState(enc *Encoder, b *bytes.Buffer) *encoderState {
// by the byte length, negated. // by the byte length, negated.
// encodeUint writes an encoded unsigned integer to state.b. // encodeUint writes an encoded unsigned integer to state.b.
func encodeUint(state *encoderState, x uint64) { func (state *encoderState) encodeUint(x uint64) {
if x <= 0x7F { if x <= 0x7F {
err := state.b.WriteByte(uint8(x)) err := state.b.WriteByte(uint8(x))
if err != nil { if err != nil {
@ -62,14 +62,14 @@ func encodeUint(state *encoderState, x uint64) {
// encodeInt writes an encoded signed integer to state.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) // The low bit of the encoding says whether to bit complement the (other bits of the)
// uint to recover the int. // uint to recover the int.
func encodeInt(state *encoderState, i int64) { func (state *encoderState) encodeInt(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)
} }
encodeUint(state, uint64(x)) state.encodeUint(uint64(x))
} }
type encOp func(i *encInstr, state *encoderState, p unsafe.Pointer) type encOp func(i *encInstr, state *encoderState, p unsafe.Pointer)
@ -86,7 +86,7 @@ type encInstr struct {
// If the instruction pointer is nil, do nothing // If the instruction pointer is nil, do nothing
func (state *encoderState) update(instr *encInstr) { func (state *encoderState) update(instr *encInstr) {
if instr != nil { if instr != nil {
encodeUint(state, uint64(instr.field-state.fieldnum)) state.encodeUint(uint64(instr.field - state.fieldnum))
state.fieldnum = instr.field state.fieldnum = instr.field
} }
} }
@ -112,9 +112,9 @@ func encBool(i *encInstr, state *encoderState, p unsafe.Pointer) {
if b || state.sendZero { if b || state.sendZero {
state.update(i) state.update(i)
if b { if b {
encodeUint(state, 1) state.encodeUint(1)
} else { } else {
encodeUint(state, 0) state.encodeUint(0)
} }
} }
} }
@ -123,7 +123,7 @@ func encInt(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := int64(*(*int)(p)) v := int64(*(*int)(p))
if v != 0 || state.sendZero { if v != 0 || state.sendZero {
state.update(i) state.update(i)
encodeInt(state, v) state.encodeInt(v)
} }
} }
@ -131,7 +131,7 @@ func encUint(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := uint64(*(*uint)(p)) v := uint64(*(*uint)(p))
if v != 0 || state.sendZero { if v != 0 || state.sendZero {
state.update(i) state.update(i)
encodeUint(state, v) state.encodeUint(v)
} }
} }
@ -139,7 +139,7 @@ func encInt8(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := int64(*(*int8)(p)) v := int64(*(*int8)(p))
if v != 0 || state.sendZero { if v != 0 || state.sendZero {
state.update(i) state.update(i)
encodeInt(state, v) state.encodeInt(v)
} }
} }
@ -147,7 +147,7 @@ func encUint8(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := uint64(*(*uint8)(p)) v := uint64(*(*uint8)(p))
if v != 0 || state.sendZero { if v != 0 || state.sendZero {
state.update(i) state.update(i)
encodeUint(state, v) state.encodeUint(v)
} }
} }
@ -155,7 +155,7 @@ func encInt16(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := int64(*(*int16)(p)) v := int64(*(*int16)(p))
if v != 0 || state.sendZero { if v != 0 || state.sendZero {
state.update(i) state.update(i)
encodeInt(state, v) state.encodeInt(v)
} }
} }
@ -163,7 +163,7 @@ func encUint16(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := uint64(*(*uint16)(p)) v := uint64(*(*uint16)(p))
if v != 0 || state.sendZero { if v != 0 || state.sendZero {
state.update(i) state.update(i)
encodeUint(state, v) state.encodeUint(v)
} }
} }
@ -171,7 +171,7 @@ func encInt32(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := int64(*(*int32)(p)) v := int64(*(*int32)(p))
if v != 0 || state.sendZero { if v != 0 || state.sendZero {
state.update(i) state.update(i)
encodeInt(state, v) state.encodeInt(v)
} }
} }
@ -179,7 +179,7 @@ func encUint32(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := uint64(*(*uint32)(p)) v := uint64(*(*uint32)(p))
if v != 0 || state.sendZero { if v != 0 || state.sendZero {
state.update(i) state.update(i)
encodeUint(state, v) state.encodeUint(v)
} }
} }
@ -187,7 +187,7 @@ func encInt64(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := *(*int64)(p) v := *(*int64)(p)
if v != 0 || state.sendZero { if v != 0 || state.sendZero {
state.update(i) state.update(i)
encodeInt(state, v) state.encodeInt(v)
} }
} }
@ -195,7 +195,7 @@ func encUint64(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := *(*uint64)(p) v := *(*uint64)(p)
if v != 0 || state.sendZero { if v != 0 || state.sendZero {
state.update(i) state.update(i)
encodeUint(state, v) state.encodeUint(v)
} }
} }
@ -203,7 +203,7 @@ func encUintptr(i *encInstr, state *encoderState, p unsafe.Pointer) {
v := uint64(*(*uintptr)(p)) v := uint64(*(*uintptr)(p))
if v != 0 || state.sendZero { if v != 0 || state.sendZero {
state.update(i) state.update(i)
encodeUint(state, v) state.encodeUint(v)
} }
} }
@ -228,7 +228,7 @@ func encFloat(i *encInstr, state *encoderState, p unsafe.Pointer) {
if f != 0 || state.sendZero { if f != 0 || state.sendZero {
v := floatBits(float64(f)) v := floatBits(float64(f))
state.update(i) state.update(i)
encodeUint(state, v) state.encodeUint(v)
} }
} }
@ -237,7 +237,7 @@ func encFloat32(i *encInstr, state *encoderState, p unsafe.Pointer) {
if f != 0 || state.sendZero { if f != 0 || state.sendZero {
v := floatBits(float64(f)) v := floatBits(float64(f))
state.update(i) state.update(i)
encodeUint(state, v) state.encodeUint(v)
} }
} }
@ -246,7 +246,7 @@ func encFloat64(i *encInstr, state *encoderState, p unsafe.Pointer) {
if f != 0 || state.sendZero { if f != 0 || state.sendZero {
state.update(i) state.update(i)
v := floatBits(f) v := floatBits(f)
encodeUint(state, v) state.encodeUint(v)
} }
} }
@ -257,8 +257,8 @@ func encComplex(i *encInstr, state *encoderState, p unsafe.Pointer) {
rpart := floatBits(float64(real(c))) rpart := floatBits(float64(real(c)))
ipart := floatBits(float64(imag(c))) ipart := floatBits(float64(imag(c)))
state.update(i) state.update(i)
encodeUint(state, rpart) state.encodeUint(rpart)
encodeUint(state, ipart) state.encodeUint(ipart)
} }
} }
@ -268,8 +268,8 @@ func encComplex64(i *encInstr, state *encoderState, p unsafe.Pointer) {
rpart := floatBits(float64(real(c))) rpart := floatBits(float64(real(c)))
ipart := floatBits(float64(imag(c))) ipart := floatBits(float64(imag(c)))
state.update(i) state.update(i)
encodeUint(state, rpart) state.encodeUint(rpart)
encodeUint(state, ipart) state.encodeUint(ipart)
} }
} }
@ -279,8 +279,8 @@ func encComplex128(i *encInstr, state *encoderState, p unsafe.Pointer) {
rpart := floatBits(real(c)) rpart := floatBits(real(c))
ipart := floatBits(imag(c)) ipart := floatBits(imag(c))
state.update(i) state.update(i)
encodeUint(state, rpart) state.encodeUint(rpart)
encodeUint(state, ipart) state.encodeUint(ipart)
} }
} }
@ -292,7 +292,7 @@ func encUint8Array(i *encInstr, state *encoderState, p unsafe.Pointer) {
b := *(*[]byte)(p) b := *(*[]byte)(p)
if len(b) > 0 || state.sendZero { if len(b) > 0 || state.sendZero {
state.update(i) state.update(i)
encodeUint(state, uint64(len(b))) state.encodeUint(uint64(len(b)))
state.b.Write(b) state.b.Write(b)
} }
} }
@ -302,14 +302,14 @@ func encString(i *encInstr, state *encoderState, p unsafe.Pointer) {
s := *(*string)(p) s := *(*string)(p)
if len(s) > 0 || state.sendZero { if len(s) > 0 || state.sendZero {
state.update(i) state.update(i)
encodeUint(state, uint64(len(s))) state.encodeUint(uint64(len(s)))
io.WriteString(state.b, s) io.WriteString(state.b, s)
} }
} }
// The end of a struct is marked by a delta field number of 0. // The end of a struct is marked by a delta field number of 0.
func encStructTerminator(i *encInstr, state *encoderState, p unsafe.Pointer) { func encStructTerminator(i *encInstr, state *encoderState, p unsafe.Pointer) {
encodeUint(state, 0) state.encodeUint(0)
} }
// Execution engine // Execution engine
@ -357,7 +357,7 @@ func (enc *Encoder) encodeArray(b *bytes.Buffer, p uintptr, op encOp, elemWid ui
state := newEncoderState(enc, b) state := newEncoderState(enc, b)
state.fieldnum = -1 state.fieldnum = -1
state.sendZero = true state.sendZero = true
encodeUint(state, uint64(length)) state.encodeUint(uint64(length))
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
elemp := p elemp := p
up := unsafe.Pointer(elemp) up := unsafe.Pointer(elemp)
@ -387,7 +387,7 @@ func (enc *Encoder) encodeMap(b *bytes.Buffer, mv *reflect.MapValue, keyOp, elem
state.fieldnum = -1 state.fieldnum = -1
state.sendZero = true state.sendZero = true
keys := mv.Keys() keys := mv.Keys()
encodeUint(state, uint64(len(keys))) state.encodeUint(uint64(len(keys)))
for _, key := range keys { for _, key := range keys {
encodeReflectValue(state, key, keyOp, keyIndir) encodeReflectValue(state, key, keyOp, keyIndir)
encodeReflectValue(state, mv.Elem(key), elemOp, elemIndir) encodeReflectValue(state, mv.Elem(key), elemOp, elemIndir)
@ -403,7 +403,7 @@ func (enc *Encoder) encodeInterface(b *bytes.Buffer, iv *reflect.InterfaceValue)
state.fieldnum = -1 state.fieldnum = -1
state.sendZero = true state.sendZero = true
if iv.IsNil() { if iv.IsNil() {
encodeUint(state, 0) state.encodeUint(0)
return return
} }
@ -413,7 +413,7 @@ func (enc *Encoder) encodeInterface(b *bytes.Buffer, iv *reflect.InterfaceValue)
errorf("gob: type not registered for interface: %s", typ) errorf("gob: type not registered for interface: %s", typ)
} }
// Send the name. // Send the name.
encodeUint(state, uint64(len(name))) state.encodeUint(uint64(len(name)))
_, err := io.WriteString(state.b, name) _, err := io.WriteString(state.b, name)
if err != nil { if err != nil {
error(err) error(err)
@ -426,7 +426,7 @@ func (enc *Encoder) encodeInterface(b *bytes.Buffer, iv *reflect.InterfaceValue)
if err != nil { if err != nil {
error(err) error(err)
} }
encodeUint(state, uint64(data.Len())) state.encodeUint(uint64(data.Len()))
_, err = state.b.Write(data.Bytes()) _, err = state.b.Write(data.Bytes())
if err != nil { if err != nil {
error(err) error(err)

View File

@ -48,7 +48,7 @@ func (enc *Encoder) setError(err os.Error) {
// Send the data item preceded by a unsigned count of its length. // Send the data item preceded by a unsigned count of its length.
func (enc *Encoder) send() { func (enc *Encoder) send() {
// Encode the length. // Encode the length.
encodeUint(enc.countState, uint64(enc.state.b.Len())) enc.countState.encodeUint(uint64(enc.state.b.Len()))
// Build the buffer. // Build the buffer.
countLen := enc.countState.b.Len() countLen := enc.countState.b.Len()
total := countLen + enc.state.b.Len() total := countLen + enc.state.b.Len()
@ -112,7 +112,7 @@ func (enc *Encoder) sendType(origt reflect.Type) (sent bool) {
} }
// Send the pair (-id, type) // Send the pair (-id, type)
// Id: // Id:
encodeInt(enc.state, -int64(info.id)) enc.state.encodeInt(-int64(info.id))
// Type: // Type:
enc.encode(enc.state.b, reflect.NewValue(info.wire)) enc.encode(enc.state.b, reflect.NewValue(info.wire))
enc.send() enc.send()
@ -170,7 +170,7 @@ func (enc *Encoder) sendTypeDescriptor(rt reflect.Type) {
} }
// Identify the type of this top-level value. // Identify the type of this top-level value.
encodeInt(enc.state, int64(enc.sent[rt])) enc.state.encodeInt(int64(enc.sent[rt]))
} }
// EncodeValue transmits the data item represented by the reflection value, // EncodeValue transmits the data item represented by the reflection value,