Commit 977e18f0 authored by Kamil Kisiel's avatar Kamil Kisiel

Merge pull request #3 from dgryski/more-encoding-decoding-work

A few more small fixes to the encoder and decoder.
parents 66358694 9112562f
...@@ -132,13 +132,16 @@ func (e *Encoder) encodeInt(k reflect.Kind, i int64) { ...@@ -132,13 +132,16 @@ func (e *Encoder) encodeInt(k reflect.Kind, i int64) {
// FIXME: need support for 64-bit ints // FIXME: need support for 64-bit ints
switch { switch {
case i > 0 && i < math.MaxInt8: case i > 0 && i < math.MaxUint8:
e.w.Write([]byte{opBinint1, byte(i)}) e.w.Write([]byte{opBinint1, byte(i)})
case i > 0 && i < math.MaxInt16: case i > 0 && i < math.MaxUint16:
e.w.Write([]byte{opBinint2, byte(i >> 8), byte(i)}) e.w.Write([]byte{opBinint2, byte(i), byte(i >> 8)})
default: case i >= math.MinInt32 && i <= math.MaxInt32:
e.w.Write([]byte{opBinint}) e.w.Write([]byte{opBinint})
binary.Write(e.w, binary.LittleEndian, int32(i)) binary.Write(e.w, binary.LittleEndian, int32(i))
default: // int64, but as a string :/
e.w.Write([]byte{opInt})
fmt.Fprintf(e.w, "%d\n", i)
} }
} }
......
...@@ -334,7 +334,13 @@ func (d *Decoder) loadLong() error { ...@@ -334,7 +334,13 @@ func (d *Decoder) loadLong() error {
// Push a 2-byte unsigned int // Push a 2-byte unsigned int
func (d *Decoder) loadBinInt2() error { func (d *Decoder) loadBinInt2() error {
return errNotImplemented var u uint16
err := binary.Read(d.r, binary.LittleEndian, &u)
if err != nil {
return err
}
d.push(int64(u))
return nil
} }
// Push None // Push None
...@@ -388,13 +394,27 @@ func (d *Decoder) loadString() error { ...@@ -388,13 +394,27 @@ func (d *Decoder) loadString() error {
} }
func (d *Decoder) loadBinString() error { func (d *Decoder) loadBinString() error {
return errNotImplemented var v int32
err := binary.Read(d.r, binary.LittleEndian, &v)
if err != nil {
return err
}
s := make([]byte, v)
_, err = io.ReadFull(d.r, s)
if err != nil {
return err
}
d.push(string(s))
return nil
} }
func (d *Decoder) loadShortBinString() error { func (d *Decoder) loadShortBinString() error {
b, _ := d.r.ReadByte() b, _ := d.r.ReadByte()
s := make([]byte, b) s := make([]byte, b)
d.r.Read(s) _, err := io.ReadFull(d.r, s)
if err != nil {
return err
}
d.push(string(s)) d.push(string(s))
return nil return nil
} }
...@@ -491,7 +511,9 @@ func (d *Decoder) get() error { ...@@ -491,7 +511,9 @@ func (d *Decoder) get() error {
} }
func (d *Decoder) binGet() error { func (d *Decoder) binGet() error {
return errNotImplemented b, _ := d.r.ReadByte()
d.push(d.memo[strconv.Itoa(int(b))])
return nil
} }
func (d *Decoder) inst() error { func (d *Decoder) inst() error {
...@@ -499,7 +521,10 @@ func (d *Decoder) inst() error { ...@@ -499,7 +521,10 @@ func (d *Decoder) inst() error {
} }
func (d *Decoder) longBinGet() error { func (d *Decoder) longBinGet() error {
return errNotImplemented var v int32
binary.Read(d.r, binary.LittleEndian, &v)
d.push(d.memo[strconv.Itoa(int(v))])
return nil
} }
func (d *Decoder) loadList() error { func (d *Decoder) loadList() error {
...@@ -569,9 +594,8 @@ func (d *Decoder) loadSetItems() error { ...@@ -569,9 +594,8 @@ func (d *Decoder) loadSetItems() error {
} }
func (d *Decoder) binFloat() error { func (d *Decoder) binFloat() error {
f := make([]byte, 8) var u uint64
d.r.Read(f) binary.Read(d.r, binary.BigEndian, &u)
u := binary.BigEndian.Uint64(f)
d.stack = append(d.stack, math.Float64frombits(u)) d.stack = append(d.stack, math.Float64frombits(u))
return nil return nil
} }
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