serialize, reduce by 2 bytes cells in [2^1008;2^1024[

We get this for free, and this is what the original comment
described. Also, this change is backwards compatible, ie it correctly
deserializes values that were serialized before this change.
db4
Jon Harper 2015-08-08 00:39:36 +02:00 committed by John Benediktsson
parent b5c3252d1d
commit 21013d4571
2 changed files with 29 additions and 7 deletions

View File

@ -5,7 +5,7 @@ USING: tools.test kernel serialize io io.streams.byte-array
alien arrays byte-arrays bit-arrays specialized-arrays alien arrays byte-arrays bit-arrays specialized-arrays
sequences math prettyprint parser classes math.constants sequences math prettyprint parser classes math.constants
io.encodings.binary random assocs serialize.private alien.c-types io.encodings.binary random assocs serialize.private alien.c-types
combinators.short-circuit ; combinators.short-circuit literals ;
SPECIALIZED-ARRAY: double SPECIALIZED-ARRAY: double
IN: serialize.tests IN: serialize.tests
@ -107,3 +107,25 @@ CONSTANT: objects
bytes>object bytes>object
dup keys first eq? dup keys first eq?
] unit-test ] unit-test
! Changed the serialization of numbers in [2^1008;2^1024[
! check backwards compatibility
${ 1008 2^ } [ B{
255 1 127 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
} binary [ deserialize-cell ] with-byte-reader ] unit-test
${ 1024 2^ 1 - } [ B{
255 1 128 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255
} binary [ deserialize-cell ] with-byte-reader ] unit-test

View File

@ -29,11 +29,11 @@ SYMBOL: serialized
#! Return the id of an already serialized object #! Return the id of an already serialized object
serialized get at ; serialized get at ;
! Numbers are serialized as follows: ! Positive numbers are serialized as follows:
! 0 => B{ 0 } ! 0 => B{ 0 }
! 1<=x<=126 => B{ x | 0x80 } ! 1<=x<127 => B{ x | 0x80 }
! x>127 => B{ length(x) x[0] x[1] ... } ! 127<=x<2^1024 => B{ length(x) x[0] x[1] ... }; 1<length(x)<129 fits in 1 byte
! x>2^1024 => B{ 0xff length(x) x[0] x[1] ... } ! 2^1024<=x => B{ 0xff } + serialize(length(x)) + B{ x[0] x[1] ... }
! The last case is needed because a very large number would ! The last case is needed because a very large number would
! otherwise be confused with a small number. ! otherwise be confused with a small number.
: serialize-cell ( n -- ) : serialize-cell ( n -- )
@ -42,7 +42,7 @@ SYMBOL: serialized
0x80 bitor write1 0x80 bitor write1
] [ ] [
dup log2 8 /i 1 + dup log2 8 /i 1 +
dup 0x7f >= [ dup 0x81 >= [
0xff write1 0xff write1
dup serialize-cell dup serialize-cell
] [ ] [
@ -55,7 +55,7 @@ SYMBOL: serialized
: deserialize-cell ( -- n ) : deserialize-cell ( -- n )
read1 { read1 {
{ [ dup 0xff = ] [ drop deserialize-cell read be> ] } { [ dup 0xff = ] [ drop deserialize-cell read be> ] }
{ [ dup 0x80 >= ] [ 0x80 bitxor ] } { [ dup 0x81 >= ] [ 0x80 bitxor ] }
[ read be> ] [ read be> ]
} cond ; } cond ;