encState := newEncoderState(nil, b)
for _, tt := range encodeT {
b.Reset()
- encodeUint(encState, tt.x)
+ encState.encodeUint(tt.x)
if !bytes.Equal(tt.b, b.Bytes()) {
t.Errorf("encodeUint: %#x encode: expected % x got % x", tt.x, tt.b, b.Bytes())
}
decState := newDecodeState(nil, &b)
for u := uint64(0); ; u = (u + 1) * 7 {
b.Reset()
- encodeUint(encState, u)
- v := decodeUint(decState)
+ encState.encodeUint(u)
+ v := decState.decodeUint()
if u != v {
t.Errorf("Encode/Decode: sent %#x received %#x", u, v)
}
defer testError(t)
var b = new(bytes.Buffer)
encState := newEncoderState(nil, b)
- encodeInt(encState, i)
+ encState.encodeInt(i)
decState := newDecodeState(nil, &b)
decState.buf = make([]byte, 8)
- j := decodeInt(decState)
+ j := decState.decodeInt()
if i != j {
t.Errorf("Encode/Decode: sent %#x received %#x", uint64(i), uint64(j))
}
func execDec(typ string, instr *decInstr, state *decodeState, t *testing.T, p unsafe.Pointer) {
defer testError(t)
- v := int(decodeUint(state))
+ v := int(state.decodeUint())
if v+state.fieldnum != 6 {
t.Fatalf("decoding field number %d, got %d", 6, v+state.fieldnum)
}
func (dec *Decoder) debugFromBuffer(indent int, countPresent bool) {
for dec.state.b.Len() > 0 {
// Receive a type id.
- id := typeId(decodeInt(dec.state))
+ id := typeId(dec.state.decodeInt())
// Is it a new type?
if id < 0 { // 0 is the error state, handled above
break
}
if countPresent {
- decodeUint(dec.state)
+ dec.state.decodeUint()
}
dec.debugPrint(indent, id)
break
if !ok && wire == nil {
errorf("type id %d not defined\n", id)
}
- decodeUint(dec.state)
+ dec.state.decodeUint()
dec.printItem(indent, id)
}
func (dec *Decoder) printArray(indent int, wire *wireType) {
elemId := wire.ArrayT.Elem
- n := int(decodeUint(dec.state))
+ n := int(dec.state.decodeUint())
for i := 0; i < n && dec.err == nil; i++ {
dec.printItem(indent, elemId)
}
func (dec *Decoder) printMap(indent int, wire *wireType) {
keyId := wire.MapT.Key
elemId := wire.MapT.Elem
- n := int(decodeUint(dec.state))
+ n := int(dec.state.decodeUint())
for i := 0; i < n && dec.err == nil; i++ {
dec.printItem(indent, keyId)
dec.printItem(indent+1, elemId)
func (dec *Decoder) printSlice(indent int, wire *wireType) {
elemId := wire.SliceT.Elem
- n := int(decodeUint(dec.state))
+ n := int(dec.state.decodeUint())
for i := 0; i < n && dec.err == nil; i++ {
dec.printItem(indent, elemId)
}
tab(indent)
switch id {
case tBool:
- if decodeInt(dec.state) == 0 {
+ if dec.state.decodeInt() == 0 {
fmt.Printf("false\n")
} else {
fmt.Printf("true\n")
}
case tInt:
- fmt.Printf("%d\n", decodeInt(dec.state))
+ fmt.Printf("%d\n", dec.state.decodeInt())
case tUint:
- fmt.Printf("%d\n", decodeUint(dec.state))
+ fmt.Printf("%d\n", dec.state.decodeUint())
case tFloat:
- fmt.Printf("%g\n", floatFromBits(decodeUint(dec.state)))
+ fmt.Printf("%g\n", floatFromBits(dec.state.decodeUint()))
case tBytes:
- b := make([]byte, decodeUint(dec.state))
+ b := make([]byte, dec.state.decodeUint())
dec.state.b.Read(b)
fmt.Printf("% x\n", b)
case tString:
- b := make([]byte, decodeUint(dec.state))
+ b := make([]byte, dec.state.decodeUint())
dec.state.b.Read(b)
fmt.Printf("%q\n", b)
case tInterface:
- b := make([]byte, decodeUint(dec.state))
+ b := make([]byte, dec.state.decodeUint())
dec.state.b.Read(b)
if len(b) == 0 {
fmt.Printf("nil interface")
state := newDecodeState(dec, dec.state.b)
state.fieldnum = -1
for dec.err == nil {
- delta := int(decodeUint(state))
+ delta := int(state.decodeUint())
if delta < 0 {
errorf("gob decode: corrupted data: negative delta")
}
// decodeUint reads an encoded unsigned integer from state.r.
// Does not check for overflow.
-func decodeUint(state *decodeState) (x uint64) {
+func (state *decodeState) decodeUint() (x uint64) {
b, err := state.b.ReadByte()
if err != nil {
error(err)
// decodeInt reads an encoded signed integer from state.r.
// Does not check for overflow.
-func decodeInt(state *decodeState) int64 {
- x := decodeUint(state)
+func (state *decodeState) decodeInt() int64 {
+ x := state.decodeUint()
if x&1 != 0 {
return ^int64(x >> 1)
}
}
func ignoreUint(i *decInstr, state *decodeState, p unsafe.Pointer) {
- decodeUint(state)
+ state.decodeUint()
}
func ignoreTwoUints(i *decInstr, state *decodeState, p unsafe.Pointer) {
- decodeUint(state)
- decodeUint(state)
+ state.decodeUint()
+ state.decodeUint()
}
func decBool(i *decInstr, state *decodeState, p unsafe.Pointer) {
}
p = *(*unsafe.Pointer)(p)
}
- *(*bool)(p) = decodeInt(state) != 0
+ *(*bool)(p) = state.decodeInt() != 0
}
func decInt8(i *decInstr, state *decodeState, p unsafe.Pointer) {
}
p = *(*unsafe.Pointer)(p)
}
- v := decodeInt(state)
+ v := state.decodeInt()
if v < math.MinInt8 || math.MaxInt8 < v {
error(i.ovfl)
} else {
}
p = *(*unsafe.Pointer)(p)
}
- v := decodeUint(state)
+ v := state.decodeUint()
if math.MaxUint8 < v {
error(i.ovfl)
} else {
}
p = *(*unsafe.Pointer)(p)
}
- v := decodeInt(state)
+ v := state.decodeInt()
if v < math.MinInt16 || math.MaxInt16 < v {
error(i.ovfl)
} else {
}
p = *(*unsafe.Pointer)(p)
}
- v := decodeUint(state)
+ v := state.decodeUint()
if math.MaxUint16 < v {
error(i.ovfl)
} else {
}
p = *(*unsafe.Pointer)(p)
}
- v := decodeInt(state)
+ v := state.decodeInt()
if v < math.MinInt32 || math.MaxInt32 < v {
error(i.ovfl)
} else {
}
p = *(*unsafe.Pointer)(p)
}
- v := decodeUint(state)
+ v := state.decodeUint()
if math.MaxUint32 < v {
error(i.ovfl)
} else {
}
p = *(*unsafe.Pointer)(p)
}
- *(*int64)(p) = int64(decodeInt(state))
+ *(*int64)(p) = int64(state.decodeInt())
}
func decUint64(i *decInstr, state *decodeState, p unsafe.Pointer) {
}
p = *(*unsafe.Pointer)(p)
}
- *(*uint64)(p) = uint64(decodeUint(state))
+ *(*uint64)(p) = uint64(state.decodeUint())
}
// Floating-point numbers are transmitted as uint64s holding the bits
}
func storeFloat32(i *decInstr, state *decodeState, p unsafe.Pointer) {
- v := floatFromBits(decodeUint(state))
+ v := floatFromBits(state.decodeUint())
av := v
if av < 0 {
av = -av
}
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.
}
p = *(*unsafe.Pointer)(p)
}
- real := floatFromBits(uint64(decodeUint(state)))
- imag := floatFromBits(uint64(decodeUint(state)))
+ real := floatFromBits(uint64(state.decodeUint()))
+ imag := floatFromBits(uint64(state.decodeUint()))
*(*complex128)(p) = cmplx(real, imag)
}
}
p = *(*unsafe.Pointer)(p)
}
- b := make([]uint8, decodeUint(state))
+ b := make([]uint8, state.decodeUint())
state.b.Read(b)
*(*[]uint8)(p) = b
}
}
p = *(*unsafe.Pointer)(p)
}
- b := make([]byte, decodeUint(state))
+ b := make([]byte, state.decodeUint())
state.b.Read(b)
*(*string)(p) = string(b)
}
func ignoreUint8Array(i *decInstr, state *decodeState, p unsafe.Pointer) {
- b := make([]byte, decodeUint(state))
+ b := make([]byte, state.decodeUint())
state.b.Read(b)
}
state := newDecodeState(dec, b)
state.fieldnum = singletonField
basep := p
- delta := int(decodeUint(state))
+ delta := int(state.decodeUint())
if delta != 0 {
errorf("gob decode: corrupted data: non-zero delta for singleton")
}
state.fieldnum = -1
basep := p
for state.b.Len() > 0 {
- delta := int(decodeUint(state))
+ delta := int(state.decodeUint())
if delta < 0 {
errorf("gob decode: corrupted data: negative delta")
}
state := newDecodeState(dec, b)
state.fieldnum = -1
for state.b.Len() > 0 {
- delta := int(decodeUint(state))
+ delta := int(state.decodeUint())
if delta < 0 {
errorf("gob ignore decode: corrupted data: negative delta")
}
if indir > 0 {
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")
}
dec.decodeArrayHelper(state, p, elemOp, elemWid, length, elemIndir, ovfl)
// that slices etc. can. We must recover a full reflection value for
// the iteration.
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++ {
key := decodeIntoValue(state, keyOp, keyIndir, reflect.MakeZero(mtyp.Key()), ovfl)
elem := decodeIntoValue(state, elemOp, elemIndir, reflect.MakeZero(mtyp.Elem()), ovfl)
}
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")
}
dec.ignoreArrayHelper(state, elemOp, length)
}
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")}
elemInstr := &decInstr{elemOp, 0, 0, 0, os.ErrorString("no error")}
for i := 0; i < n; i++ {
}
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 {
up := unsafe.Pointer(p)
if *(*unsafe.Pointer)(up) == nil {
}
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
// Create an interface reflect.Value. We need one even for the nil case.
ivalue := reflect.MakeZero(ityp).(*reflect.InterfaceValue)
// Read the name of the concrete type.
- b := make([]byte, decodeUint(state))
+ b := make([]byte, state.decodeUint())
state.b.Read(b)
name := string(b)
if name == "" {
func (dec *Decoder) ignoreInterface(state *decodeState) {
// Read the name of the concrete type.
- b := make([]byte, decodeUint(state))
+ b := make([]byte, state.decodeUint())
_, err := state.b.Read(b)
if err != nil {
error(err)
func (dec *Decoder) decodeValueFromBuffer(value reflect.Value, ignoreInterfaceValue, countPresent bool) {
for dec.state.b.Len() > 0 {
// Receive a type id.
- id := typeId(decodeInt(dec.state))
+ id := typeId(dec.state.decodeInt())
// Is it a new type?
if id < 0 { // 0 is the error state, handled above
}
// An interface value is preceded by a byte count.
if countPresent {
- count := int(decodeUint(dec.state))
+ count := int(dec.state.decodeUint())
if ignoreInterfaceValue {
// An interface value is preceded by a byte count. Just skip that many bytes.
dec.state.b.Next(int(count))
// by the byte length, negated.
// encodeUint writes an encoded unsigned integer to state.b.
-func encodeUint(state *encoderState, x uint64) {
+func (state *encoderState) encodeUint(x uint64) {
if x <= 0x7F {
err := state.b.WriteByte(uint8(x))
if err != nil {
// 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.
-func encodeInt(state *encoderState, i int64) {
+func (state *encoderState) encodeInt(i int64) {
var x uint64
if i < 0 {
x = uint64(^i<<1) | 1
} else {
x = uint64(i << 1)
}
- encodeUint(state, uint64(x))
+ state.encodeUint(uint64(x))
}
type encOp func(i *encInstr, state *encoderState, p unsafe.Pointer)
// If the instruction pointer is nil, do nothing
func (state *encoderState) update(instr *encInstr) {
if instr != nil {
- encodeUint(state, uint64(instr.field-state.fieldnum))
+ state.encodeUint(uint64(instr.field - state.fieldnum))
state.fieldnum = instr.field
}
}
if b || state.sendZero {
state.update(i)
if b {
- encodeUint(state, 1)
+ state.encodeUint(1)
} else {
- encodeUint(state, 0)
+ state.encodeUint(0)
}
}
}
v := int64(*(*int)(p))
if v != 0 || state.sendZero {
state.update(i)
- encodeInt(state, v)
+ state.encodeInt(v)
}
}
v := uint64(*(*uint)(p))
if v != 0 || state.sendZero {
state.update(i)
- encodeUint(state, v)
+ state.encodeUint(v)
}
}
v := int64(*(*int8)(p))
if v != 0 || state.sendZero {
state.update(i)
- encodeInt(state, v)
+ state.encodeInt(v)
}
}
v := uint64(*(*uint8)(p))
if v != 0 || state.sendZero {
state.update(i)
- encodeUint(state, v)
+ state.encodeUint(v)
}
}
v := int64(*(*int16)(p))
if v != 0 || state.sendZero {
state.update(i)
- encodeInt(state, v)
+ state.encodeInt(v)
}
}
v := uint64(*(*uint16)(p))
if v != 0 || state.sendZero {
state.update(i)
- encodeUint(state, v)
+ state.encodeUint(v)
}
}
v := int64(*(*int32)(p))
if v != 0 || state.sendZero {
state.update(i)
- encodeInt(state, v)
+ state.encodeInt(v)
}
}
v := uint64(*(*uint32)(p))
if v != 0 || state.sendZero {
state.update(i)
- encodeUint(state, v)
+ state.encodeUint(v)
}
}
v := *(*int64)(p)
if v != 0 || state.sendZero {
state.update(i)
- encodeInt(state, v)
+ state.encodeInt(v)
}
}
v := *(*uint64)(p)
if v != 0 || state.sendZero {
state.update(i)
- encodeUint(state, v)
+ state.encodeUint(v)
}
}
v := uint64(*(*uintptr)(p))
if v != 0 || state.sendZero {
state.update(i)
- encodeUint(state, v)
+ state.encodeUint(v)
}
}
if f != 0 || state.sendZero {
v := floatBits(float64(f))
state.update(i)
- encodeUint(state, v)
+ state.encodeUint(v)
}
}
if f != 0 || state.sendZero {
v := floatBits(float64(f))
state.update(i)
- encodeUint(state, v)
+ state.encodeUint(v)
}
}
if f != 0 || state.sendZero {
state.update(i)
v := floatBits(f)
- encodeUint(state, v)
+ state.encodeUint(v)
}
}
rpart := floatBits(float64(real(c)))
ipart := floatBits(float64(imag(c)))
state.update(i)
- encodeUint(state, rpart)
- encodeUint(state, ipart)
+ state.encodeUint(rpart)
+ state.encodeUint(ipart)
}
}
rpart := floatBits(float64(real(c)))
ipart := floatBits(float64(imag(c)))
state.update(i)
- encodeUint(state, rpart)
- encodeUint(state, ipart)
+ state.encodeUint(rpart)
+ state.encodeUint(ipart)
}
}
rpart := floatBits(real(c))
ipart := floatBits(imag(c))
state.update(i)
- encodeUint(state, rpart)
- encodeUint(state, ipart)
+ state.encodeUint(rpart)
+ state.encodeUint(ipart)
}
}
b := *(*[]byte)(p)
if len(b) > 0 || state.sendZero {
state.update(i)
- encodeUint(state, uint64(len(b)))
+ state.encodeUint(uint64(len(b)))
state.b.Write(b)
}
}
s := *(*string)(p)
if len(s) > 0 || state.sendZero {
state.update(i)
- encodeUint(state, uint64(len(s)))
+ state.encodeUint(uint64(len(s)))
io.WriteString(state.b, s)
}
}
// The end of a struct is marked by a delta field number of 0.
func encStructTerminator(i *encInstr, state *encoderState, p unsafe.Pointer) {
- encodeUint(state, 0)
+ state.encodeUint(0)
}
// Execution engine
state := newEncoderState(enc, b)
state.fieldnum = -1
state.sendZero = true
- encodeUint(state, uint64(length))
+ state.encodeUint(uint64(length))
for i := 0; i < length; i++ {
elemp := p
up := unsafe.Pointer(elemp)
state.fieldnum = -1
state.sendZero = true
keys := mv.Keys()
- encodeUint(state, uint64(len(keys)))
+ state.encodeUint(uint64(len(keys)))
for _, key := range keys {
encodeReflectValue(state, key, keyOp, keyIndir)
encodeReflectValue(state, mv.Elem(key), elemOp, elemIndir)
state.fieldnum = -1
state.sendZero = true
if iv.IsNil() {
- encodeUint(state, 0)
+ state.encodeUint(0)
return
}
errorf("gob: type not registered for interface: %s", typ)
}
// Send the name.
- encodeUint(state, uint64(len(name)))
+ state.encodeUint(uint64(len(name)))
_, err := io.WriteString(state.b, name)
if err != nil {
error(err)
if err != nil {
error(err)
}
- encodeUint(state, uint64(data.Len()))
+ state.encodeUint(uint64(data.Len()))
_, err = state.b.Write(data.Bytes())
if err != nil {
error(err)
// Send the data item preceded by a unsigned count of its length.
func (enc *Encoder) send() {
// Encode the length.
- encodeUint(enc.countState, uint64(enc.state.b.Len()))
+ enc.countState.encodeUint(uint64(enc.state.b.Len()))
// Build the buffer.
countLen := enc.countState.b.Len()
total := countLen + enc.state.b.Len()
}
// Send the pair (-id, type)
// Id:
- encodeInt(enc.state, -int64(info.id))
+ enc.state.encodeInt(-int64(info.id))
// Type:
enc.encode(enc.state.b, reflect.NewValue(info.wire))
enc.send()
}
// 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,