Commit 9053359b authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Kamil Kisiel

encoder: Fix int wrt protocol version

We can use BININT* opcodes only starting from protocol >= 1.
At protocol 0 we must use ASCII INT.
parent 6e6a8aa3
......@@ -263,21 +263,24 @@ func (e *Encoder) encodeFloat(f float64) error {
func (e *Encoder) encodeInt(k reflect.Kind, i int64) error {
// FIXME: need support for 64-bit ints
switch {
case i > 0 && i < math.MaxUint8:
return e.emit(opBinint1, byte(i))
case i > 0 && i < math.MaxUint16:
return e.emit(opBinint2, byte(i), byte(i >> 8))
case i >= math.MinInt32 && i <= math.MaxInt32:
var b = [1+4]byte{opBinint}
binary.LittleEndian.PutUint32(b[1:], uint32(i))
return e.emitb(b[:])
default: // int64, but as a string :/
return e.emitf("%c%d\n", opInt, i)
// protocol >= 1: BININT*
if e.config.Protocol >= 1 {
switch {
case i > 0 && i < math.MaxUint8:
return e.emit(opBinint1, byte(i))
case i > 0 && i < math.MaxUint16:
return e.emit(opBinint2, byte(i), byte(i >> 8))
case i >= math.MinInt32 && i <= math.MaxInt32:
var b = [1+4]byte{opBinint}
binary.LittleEndian.PutUint32(b[1:], uint32(i))
return e.emitb(b[:])
}
}
// protocol 0: INT
return e.emitf("%c%d\n", opInt, i)
}
func (e *Encoder) encodeLong(b *big.Int) error {
......
......@@ -155,13 +155,16 @@ var tests = []TestEntry{
P2_("\x89.")), // NEWFALSE
X("int(5)", int64(5),
I("I5\n.")), // INT
P0("I5\n."), // INT
P1_("K\x05.")), // BININT1
X("int(0x123)", int64(0x123),
I("I291\n.")), // INT
P0("I291\n."), // INT
P1_("M\x23\x01.")), // BININT2
X("int(0x12345)", int64(0x12345),
I("I74565\n.")), // INT
P0("I74565\n."), // INT
P1_("J\x45\x23\x01\x00.")), // BININT
X("float", float64(1.23),
I("F1.23\n.")), // FLOAT
......
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