uint8-varint
Advanced tools
Comparing version 1.0.0 to 1.0.1
import type { Uint8ArrayList } from 'uint8arraylist'; | ||
export declare const unsigned: { | ||
encodingLength(value: bigint): number; | ||
encode(value: bigint, buf: Uint8ArrayList | Uint8Array): void; | ||
encode(value: bigint, buf?: Uint8ArrayList | Uint8Array, offset?: number): Uint8ArrayList | Uint8Array; | ||
decode(buf: Uint8ArrayList | Uint8Array, offset?: number): bigint; | ||
@@ -9,3 +9,3 @@ }; | ||
encodingLength(value: bigint): number; | ||
encode(value: bigint, buf: Uint8ArrayList | Uint8Array, offset?: number): void; | ||
encode(value: bigint, buf?: Uint8ArrayList | Uint8Array, offset?: number): Uint8ArrayList | Uint8Array; | ||
decode(buf: Uint8ArrayList | Uint8Array, offset?: number): bigint; | ||
@@ -15,5 +15,5 @@ }; | ||
encodingLength(value: bigint): number; | ||
encode(value: bigint, buf: Uint8ArrayList | Uint8Array, offset?: number): void; | ||
encode(value: bigint, buf?: Uint8ArrayList | Uint8Array, offset?: number): Uint8ArrayList | Uint8Array; | ||
decode(buf: Uint8ArrayList | Uint8Array, offset?: number): bigint; | ||
}; | ||
//# sourceMappingURL=big.d.ts.map |
@@ -13,5 +13,7 @@ import accessor from 'byte-access'; | ||
}, | ||
encode(value, buf) { | ||
encode(value, buf, offset = 0) { | ||
if (buf == null) { | ||
buf = new Uint8Array(unsigned.encodingLength(value)); | ||
} | ||
const access = accessor(buf); | ||
let offset = 0; | ||
while (LIMIT < value) { | ||
@@ -22,2 +24,3 @@ access.set(offset++, Number(value & LIMIT) | 0x80); | ||
access.set(offset, Number(value)); | ||
return buf; | ||
}, | ||
@@ -36,7 +39,10 @@ decode(buf, offset = 0) { | ||
encode(value, buf, offset = 0) { | ||
if (buf == null) { | ||
buf = new Uint8Array(signed.encodingLength(value)); | ||
} | ||
if (value < 0n) { | ||
LongBits.fromBigInt(value).toBytes(buf, offset); | ||
return; | ||
return buf; | ||
} | ||
return unsigned.encode(value, buf); | ||
return unsigned.encode(value, buf, offset); | ||
}, | ||
@@ -52,3 +58,7 @@ decode(buf, offset = 0) { | ||
encode(value, buf, offset = 0) { | ||
if (buf == null) { | ||
buf = new Uint8Array(zigzag.encodingLength(value)); | ||
} | ||
LongBits.fromBigInt(value).zzEncode().toBytes(buf, offset); | ||
return buf; | ||
}, | ||
@@ -55,0 +65,0 @@ decode(buf, offset = 0) { |
import type { Uint8ArrayList } from 'uint8arraylist'; | ||
export declare const unsigned: { | ||
encodingLength(value: number): number; | ||
encode(value: number, buf: Uint8ArrayList | Uint8Array): void; | ||
encode(value: number, buf?: Uint8ArrayList | Uint8Array, offset?: number): Uint8ArrayList | Uint8Array; | ||
decode(buf: Uint8ArrayList | Uint8Array, offset?: number): number; | ||
@@ -9,10 +9,10 @@ }; | ||
encodingLength(value: number): number; | ||
encode(value: number, buf: Uint8ArrayList | Uint8Array): void; | ||
decode(data: Uint8ArrayList | Uint8Array, offset?: number): number; | ||
encode(value: number, buf?: Uint8ArrayList | Uint8Array, offset?: number): Uint8ArrayList | Uint8Array; | ||
decode(buf: Uint8ArrayList | Uint8Array, offset?: number): number; | ||
}; | ||
export declare const zigzag: { | ||
encodingLength(value: number): number; | ||
encode(value: number, buf: Uint8ArrayList | Uint8Array, offset?: number): void; | ||
decode(data: Uint8ArrayList | Uint8Array, offset?: number): number; | ||
encode(value: number, buf?: Uint8ArrayList | Uint8Array, offset?: number): Uint8ArrayList | Uint8Array; | ||
decode(buf: Uint8ArrayList | Uint8Array, offset?: number): number; | ||
}; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,7 +0,2 @@ | ||
import accessor from 'byte-access'; | ||
import { LongBits } from 'longbits'; | ||
const MSB = 0x80; | ||
const REST = 0x7F; | ||
const MSBALL = ~REST; | ||
const INT = Math.pow(2, 31); | ||
const N1 = Math.pow(2, 7); | ||
@@ -47,42 +42,14 @@ const N2 = Math.pow(2, 14); | ||
}, | ||
encode(value, buf) { | ||
let offset = 0; | ||
const access = accessor(buf); | ||
while (value >= INT) { | ||
access.set(offset++, (value & 0xFF) | MSB); | ||
value /= 128; | ||
encode(value, buf, offset = 0) { | ||
if (Number.MAX_SAFE_INTEGER != null && value > Number.MAX_SAFE_INTEGER) { | ||
throw new RangeError('Could not encode varint'); | ||
} | ||
while ((value & MSBALL) > 0) { | ||
access.set(offset++, (value & 0xFF) | MSB); | ||
value >>>= 7; | ||
if (buf == null) { | ||
buf = new Uint8Array(unsigned.encodingLength(value)); | ||
} | ||
access.set(offset, value | 0); | ||
LongBits.fromNumber(value).toBytes(buf, offset); | ||
return buf; | ||
}, | ||
decode(buf, offset = 0) { | ||
const access = accessor(buf); | ||
let value = 4294967295; // optimizer type-hint, tends to deopt otherwise (?!) | ||
value = (access.get(offset) & 127) >>> 0; | ||
if (access.get(offset++) < 128) { | ||
return value; | ||
} | ||
value = (value | (access.get(offset) & 127) << 7) >>> 0; | ||
if (access.get(offset++) < 128) { | ||
return value; | ||
} | ||
value = (value | (access.get(offset) & 127) << 14) >>> 0; | ||
if (access.get(offset++) < 128) { | ||
return value; | ||
} | ||
value = (value | (access.get(offset) & 127) << 21) >>> 0; | ||
if (access.get(offset++) < 128) { | ||
return value; | ||
} | ||
value = (value | (access.get(offset) & 15) << 28) >>> 0; | ||
if (access.get(offset++) < 128) { | ||
return value; | ||
} | ||
if ((offset += 5) > buf.length) { | ||
throw RangeError(`index out of range: ${offset} > ${buf.length}`); | ||
} | ||
return value; | ||
return LongBits.fromBytes(buf, offset).toNumber(true); | ||
} | ||
@@ -93,26 +60,18 @@ }; | ||
if (value < 0) { | ||
return 10; // 10 bytes per spec | ||
return 10; // 10 bytes per spec - https://developers.google.com/protocol-buffers/docs/encoding#signed-ints | ||
} | ||
return unsigned.encodingLength(value); | ||
}, | ||
encode(value, buf) { | ||
encode(value, buf, offset = 0) { | ||
if (buf == null) { | ||
buf = new Uint8Array(signed.encodingLength(value)); | ||
} | ||
if (value < 0) { | ||
let offset = 0; | ||
const access = accessor(buf); | ||
const bits = LongBits.fromNumber(value); | ||
while (bits.hi > 0) { | ||
access.set(offset++, bits.lo & 127 | 128); | ||
bits.lo = (bits.lo >>> 7 | bits.hi << 25) >>> 0; | ||
bits.hi >>>= 7; | ||
} | ||
while (bits.lo > 127) { | ||
access.set(offset++, bits.lo & 127 | 128); | ||
bits.lo = bits.lo >>> 7; | ||
} | ||
access.set(offset++, bits.lo); | ||
LongBits.fromNumber(value).toBytes(buf, offset); | ||
return buf; | ||
} | ||
unsigned.encode(value, buf); | ||
return unsigned.encode(value, buf); | ||
}, | ||
decode(data, offset = 0) { | ||
return unsigned.decode(data, offset) | 0; | ||
decode(buf, offset = 0) { | ||
return LongBits.fromBytes(buf, offset).toNumber(false); | ||
} | ||
@@ -122,14 +81,13 @@ }; | ||
encodingLength(value) { | ||
value = (value << 1 ^ value >> 31) >>> 0; | ||
return unsigned.encodingLength(value); | ||
return unsigned.encodingLength(value >= 0 ? value * 2 : value * -2 - 1); | ||
}, | ||
encode(value, buf, offset = 0) { | ||
value = (value << 1 ^ value >> 31) >>> 0; | ||
unsigned.encode(value, buf); | ||
value = value >= 0 ? value * 2 : (value * -2) - 1; | ||
return unsigned.encode(value, buf, offset); | ||
}, | ||
decode(data, offset = 0) { | ||
const value = unsigned.decode(data, offset); | ||
return value >>> 1 ^ -(value & 1) | 0; | ||
decode(buf, offset = 0) { | ||
const value = unsigned.decode(buf, offset); | ||
return (value & 1) !== 0 ? (value + 1) / -2 : value / 2; | ||
} | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "uint8-varint", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Read/write varints from Uint8Arrays and Uint8ArrayLists", | ||
@@ -159,8 +159,10 @@ "license": "Apache-2.0 OR MIT", | ||
"byte-access": "^1.0.0", | ||
"longbits": "^1.0.1", | ||
"longbits": "^1.1.0", | ||
"uint8arraylist": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"aegir": "^37.4.6" | ||
"@types/varint": "^6.0.0", | ||
"aegir": "^37.4.6", | ||
"varint": "^6.0.0" | ||
} | ||
} |
@@ -17,6 +17,9 @@ import type { Uint8ArrayList } from 'uint8arraylist' | ||
encode (value: bigint, buf: Uint8ArrayList | Uint8Array) { | ||
encode (value: bigint, buf?: Uint8ArrayList | Uint8Array, offset = 0): Uint8ArrayList | Uint8Array { | ||
if (buf == null) { | ||
buf = new Uint8Array(unsigned.encodingLength(value)) | ||
} | ||
const access = accessor(buf) | ||
let offset = 0 | ||
while (LIMIT < value) { | ||
@@ -28,2 +31,4 @@ access.set(offset++, Number(value & LIMIT) | 0x80) | ||
access.set(offset, Number(value)) | ||
return buf | ||
}, | ||
@@ -45,10 +50,14 @@ | ||
encode (value: bigint, buf: Uint8ArrayList | Uint8Array, offset = 0) { | ||
encode (value: bigint, buf?: Uint8ArrayList | Uint8Array, offset = 0): Uint8ArrayList | Uint8Array { | ||
if (buf == null) { | ||
buf = new Uint8Array(signed.encodingLength(value)) | ||
} | ||
if (value < 0n) { | ||
LongBits.fromBigInt(value).toBytes(buf, offset) | ||
return | ||
return buf | ||
} | ||
return unsigned.encode(value, buf) | ||
return unsigned.encode(value, buf, offset) | ||
}, | ||
@@ -66,4 +75,10 @@ | ||
encode (value: bigint, buf: Uint8ArrayList | Uint8Array, offset = 0) { | ||
encode (value: bigint, buf?: Uint8ArrayList | Uint8Array, offset = 0): Uint8ArrayList | Uint8Array { | ||
if (buf == null) { | ||
buf = new Uint8Array(zigzag.encodingLength(value)) | ||
} | ||
LongBits.fromBigInt(value).zzEncode().toBytes(buf, offset) | ||
return buf | ||
}, | ||
@@ -70,0 +85,0 @@ |
111
src/index.ts
import type { Uint8ArrayList } from 'uint8arraylist' | ||
import accessor from 'byte-access' | ||
import { LongBits } from 'longbits' | ||
const MSB = 0x80 | ||
const REST = 0x7F | ||
const MSBALL = ~REST | ||
const INT = Math.pow(2, 31) | ||
const N1 = Math.pow(2, 7) | ||
@@ -60,58 +55,18 @@ const N2 = Math.pow(2, 14) | ||
encode (value: number, buf: Uint8ArrayList | Uint8Array): void { | ||
let offset = 0 | ||
const access = accessor(buf) | ||
while (value >= INT) { | ||
access.set(offset++, (value & 0xFF) | MSB) | ||
value /= 128 | ||
encode (value: number, buf?: Uint8ArrayList | Uint8Array, offset = 0): Uint8ArrayList | Uint8Array { | ||
if (Number.MAX_SAFE_INTEGER != null && value > Number.MAX_SAFE_INTEGER) { | ||
throw new RangeError('Could not encode varint') | ||
} | ||
while ((value & MSBALL) > 0) { | ||
access.set(offset++, (value & 0xFF) | MSB) | ||
value >>>= 7 | ||
if (buf == null) { | ||
buf = new Uint8Array(unsigned.encodingLength(value)) | ||
} | ||
access.set(offset, value | 0) | ||
LongBits.fromNumber(value).toBytes(buf, offset) | ||
return buf | ||
}, | ||
decode (buf: Uint8ArrayList | Uint8Array, offset: number = 0): number { | ||
const access = accessor(buf) | ||
let value = 4294967295 // optimizer type-hint, tends to deopt otherwise (?!) | ||
value = (access.get(offset) & 127) >>> 0 | ||
if (access.get(offset++) < 128) { | ||
return value | ||
} | ||
value = (value | (access.get(offset) & 127) << 7) >>> 0 | ||
if (access.get(offset++) < 128) { | ||
return value | ||
} | ||
value = (value | (access.get(offset) & 127) << 14) >>> 0 | ||
if (access.get(offset++) < 128) { | ||
return value | ||
} | ||
value = (value | (access.get(offset) & 127) << 21) >>> 0 | ||
if (access.get(offset++) < 128) { | ||
return value | ||
} | ||
value = (value | (access.get(offset) & 15) << 28) >>> 0 | ||
if (access.get(offset++) < 128) { | ||
return value | ||
} | ||
if ((offset += 5) > buf.length) { | ||
throw RangeError(`index out of range: ${offset} > ${buf.length}`) | ||
} | ||
return value | ||
return LongBits.fromBytes(buf, offset).toNumber(true) | ||
} | ||
@@ -123,3 +78,3 @@ } | ||
if (value < 0) { | ||
return 10 // 10 bytes per spec | ||
return 10 // 10 bytes per spec - https://developers.google.com/protocol-buffers/docs/encoding#signed-ints | ||
} | ||
@@ -130,27 +85,18 @@ | ||
encode (value: number, buf: Uint8ArrayList | Uint8Array): void { | ||
encode (value: number, buf?: Uint8ArrayList | Uint8Array, offset = 0): Uint8ArrayList | Uint8Array { | ||
if (buf == null) { | ||
buf = new Uint8Array(signed.encodingLength(value)) | ||
} | ||
if (value < 0) { | ||
let offset = 0 | ||
const access = accessor(buf) | ||
const bits = LongBits.fromNumber(value) | ||
LongBits.fromNumber(value).toBytes(buf, offset) | ||
while (bits.hi > 0) { | ||
access.set(offset++, bits.lo & 127 | 128) | ||
bits.lo = (bits.lo >>> 7 | bits.hi << 25) >>> 0 | ||
bits.hi >>>= 7 | ||
} | ||
while (bits.lo > 127) { | ||
access.set(offset++, bits.lo & 127 | 128) | ||
bits.lo = bits.lo >>> 7 | ||
} | ||
access.set(offset++, bits.lo) | ||
return buf | ||
} | ||
unsigned.encode(value, buf) | ||
return unsigned.encode(value, buf) | ||
}, | ||
decode (data: Uint8ArrayList | Uint8Array, offset = 0): number { | ||
return unsigned.decode(data, offset) | 0 | ||
decode (buf: Uint8ArrayList | Uint8Array, offset = 0): number { | ||
return LongBits.fromBytes(buf, offset).toNumber(false) | ||
} | ||
@@ -161,15 +107,16 @@ } | ||
encodingLength (value: number): number { | ||
value = (value << 1 ^ value >> 31) >>> 0 | ||
return unsigned.encodingLength(value) | ||
return unsigned.encodingLength(value >= 0 ? value * 2 : value * -2 - 1) | ||
}, | ||
encode (value: number, buf: Uint8ArrayList | Uint8Array, offset = 0): void { | ||
value = (value << 1 ^ value >> 31) >>> 0 | ||
unsigned.encode(value, buf) | ||
encode (value: number, buf?: Uint8ArrayList | Uint8Array, offset = 0): Uint8ArrayList | Uint8Array { | ||
value = value >= 0 ? value * 2 : (value * -2) - 1 | ||
return unsigned.encode(value, buf, offset) | ||
}, | ||
decode (data: Uint8ArrayList | Uint8Array, offset = 0): number { | ||
const value = unsigned.decode(data, offset) | ||
return value >>> 1 ^ -(value & 1) | 0 | ||
decode (buf: Uint8ArrayList | Uint8Array, offset = 0): number { | ||
const value = unsigned.decode(buf, offset) | ||
return (value & 1) !== 0 ? (value + 1) / -2 : value / 2 | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23280
3
340
Updatedlongbits@^1.1.0