]> gitweb.factorcode.org Git - factor.git/commitdiff
serialize, reduce by 2 bytes cells in [2^1008;2^1024[
authorJon Harper <jon.harper87@gmail.com>
Fri, 7 Aug 2015 22:39:36 +0000 (00:39 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 8 Aug 2015 03:29:23 +0000 (20:29 -0700)
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.

basis/serialize/serialize-tests.factor
basis/serialize/serialize.factor

index 0dfa1ee051fca58c26c20d24bc31e5b118596363..937f7c5e024891348fd2259af01d41a0f63cab2e 100644 (file)
@@ -5,7 +5,7 @@ USING: tools.test kernel serialize io io.streams.byte-array
 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
 
@@ -107,3 +107,25 @@ CONSTANT: objects
     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
index 2a79beabf29ef77ba0295750f8d3f20bdc1dab4d..cdd58e3c995b8271ba770649221bdfec72a52e03 100644 (file)
@@ -29,11 +29,11 @@ SYMBOL: serialized
     #! 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 -- )
@@ -42,7 +42,7 @@ SYMBOL: serialized
             0x80 bitor write1
         ] [
             dup log2 8 /i 1 +
-            dup 0x7f >= [
+            dup 0x81 >= [
                 0xff write1
                 dup serialize-cell
             ] [
@@ -55,7 +55,7 @@ SYMBOL: serialized
 : deserialize-cell ( -- n )
     read1 {
         { [ dup 0xff = ] [ drop deserialize-cell read be> ] }
-        { [ dup 0x80 >= ] [ 0x80 bitxor ] }
+        { [ dup 0x81 >= ] [ 0x80 bitxor ] }
         [ read be> ]
     } cond ;