Repair javascript integer codec

This commit is contained in:
Tony Garnock-Jones 2021-03-02 16:38:40 +01:00
parent 75790f237b
commit c8c027f762
4 changed files with 5 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{
"name": "preserves",
"version": "0.6.0",
"version": "0.6.1",
"description": "Experimental data serialization format",
"homepage": "https://gitlab.com/preserves/preserves",
"license": "Apache-2.0",

View File

@ -128,7 +128,7 @@ export class Decoder<T extends object> {
if (n === 0) return 0;
let acc = this.nextbyte();
if (acc & 0x80) acc -= 256;
for (let i = 1; i < n; i++) acc = (acc << 8) | this.nextbyte();
for (let i = 1; i < n; i++) acc = (acc * 256) + this.nextbyte();
return acc;
}
@ -311,7 +311,7 @@ export class Encoder<T extends object> {
encodeint(v: number) {
// TODO: Bignums :-/
const plain_bitcount = Math.floor(Math.log2(v > 0 ? v : ~v)) + 1;
const plain_bitcount = Math.floor(Math.log2(v > 0 ? v : -(1 + v))) + 1;
const signed_bitcount = plain_bitcount + 1;
const bytecount = (signed_bitcount + 7) >> 3;
if (bytecount <= 16) {
@ -322,7 +322,7 @@ export class Encoder<T extends object> {
}
const enc = (n: number, x: number) => {
if (n > 0) {
enc(n - 1, x >> 8);
enc(n - 1, Math.floor(x / 256));
this.emitbyte(x & 255);
}
};

Binary file not shown.

View File

@ -101,6 +101,7 @@
int65535: <Test #x"a200ffff" 65535>
int65536: <Test #x"a2010000" 65536>
int131072: <Test #x"a2020000" 131072>
int2500000000: <Test #x"a4009502f900" 2500000000>
list0: <Test #x"b584" []>
list4: <Test #x"b59192939484" [1 2 3 4]>
list4a: <Test #x"b59192939484" [1, 2, 3, 4]>