Commit 33d1926f authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Kamil Kisiel

encoder: Fix list wrt protocol version

- we can use EMPTY_LIST only if protocol >= 1

Also: since we are now using EMPTY_LIST only optionally, it is logical
to switch to

	MARK + ... + LIST

instead of

	EMPTY_LIST (or MARK + LIST @proto=0) + MARK + ... + APPENDS

which is at least 1 byte longer.

For the reference - APPENDS is also from protocol 1, while LIST is from
protocol 0.
parent 24695efa
......@@ -213,7 +213,14 @@ func (e *Encoder) encodeArray(arr reflect.Value) error {
l := arr.Len()
err := e.emit(opEmptyList, opMark)
// protocol >= 1: ø list -> EMPTY_LIST
if e.config.Protocol >= 1 && l == 0 {
return e.emit(opEmptyList)
}
// MARK + ... + LIST
// TODO detect cycles and double references to the same object
err := e.emit(opMark)
if err != nil {
return err
}
......@@ -226,7 +233,7 @@ func (e *Encoder) encodeArray(arr reflect.Value) error {
}
}
return e.emit(opAppends)
return e.emit(opList)
}
func (e *Encoder) encodeBool(b bool) error {
......
......@@ -204,9 +204,14 @@ var tests = []TestEntry{
I("((I1\nI2\ntp0\n(I3\nI4\ntp1\ntp2\n.")),
X("list([])", []interface{}{},
P0("(l."), // MARK + LIST
P1_("]."), // EMPTY_LIST
I("(lp0\n.")),
X("list([1,2,3,True])", []interface{}{int64(1), int64(2), int64(3), true},
P0("(I1\nI2\nI3\nI01\nl."), // MARK + INT + INT(True) + LIST
P1("(K\x01K\x02K\x03I01\nl."), // MARK + BININT1 + INT(True) + LIST
P2_("(K\x01K\x02K\x03\x88l."), // MARK + BININT1 + NEW_TRUE + LIST
I("(lp0\nI1\naI2\naI3\naI01\na.")),
X("str('abc')", "abc",
......
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