alien arrays byte-arrays bit-arrays specialized-arrays
sequences math prettyprint parser classes math.constants
io.encodings.binary random assocs serialize.private alien.c-types
-combinators.short-circuit ;
+combinators.short-circuit literals ;
SPECIALIZED-ARRAY: double
IN: serialize.tests
bytes>object
dup keys first eq?
] 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
#! Return the id of an already serialized object
serialized get at ;
-! Numbers are serialized as follows:
+! Positive numbers are serialized as follows:
! 0 => B{ 0 }
-! 1<=x<=126 => B{ x | 0x80 }
-! x>127 => B{ length(x) x[0] x[1] ... }
-! x>2^1024 => B{ 0xff length(x) x[0] x[1] ... }
+! 1<=x<127 => B{ x | 0x80 }
+! 127<=x<2^1024 => B{ length(x) x[0] x[1] ... }; 1<length(x)<129 fits in 1 byte
+! 2^1024<=x => B{ 0xff } + serialize(length(x)) + B{ x[0] x[1] ... }
! The last case is needed because a very large number would
! otherwise be confused with a small number.
: serialize-cell ( n -- )
0x80 bitor write1
] [
dup log2 8 /i 1 +
- dup 0x7f >= [
+ dup 0x81 >= [
0xff write1
dup serialize-cell
] [
: deserialize-cell ( -- n )
read1 {
{ [ dup 0xff = ] [ drop deserialize-cell read be> ] }
- { [ dup 0x80 >= ] [ 0x80 bitxor ] }
+ { [ dup 0x81 >= ] [ 0x80 bitxor ] }
[ read be> ]
} cond ;