Skip to content

Commit

Permalink
feat: add varint package
Browse files Browse the repository at this point in the history
  • Loading branch information
David Brockman Smoliansky committed Jul 24, 2019
1 parent 2cd18c5 commit c1cabe1
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 0 deletions.
21 changes: 21 additions & 0 deletions code/varint/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 David Brockman Smoliansky

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions code/varint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `@dbrockman/varint`

Encode integers of varying sizes.

This is copied from [varint](https://github.com/chrisdickinson/varint) and [signed-varint](https://github.com/dominictarr/signed-varint) with minor tweaks to the interface.

```sh
yarn add @dbrockman/varint
```
23 changes: 23 additions & 0 deletions code/varint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@dbrockman/varint",
"version": "1.0.1",
"description": "Encode integers of varying sizes.",
"private": true,
"main": "build/main.js",
"types": "build/main.d.ts",
"files": ["build"],
"scripts": {
"clean": "del 'build'",
"build": "tsc -p tsconfig.build.json",
"prebuild": "yarn run clean",
"postbuild": "del 'build/**/__*__/'"
},
"dependencies": {},
"devDependencies": {},
"publishConfig": {
"access": "public"
},
"license": "MIT",
"repository": "git@github.com:dbrockman/monode.git",
"homepage": "https://github.com/dbrockman/monode/tree/master/code/varint#readme"
}
43 changes: 43 additions & 0 deletions code/varint/src/__tests__/signed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { readSignedVarint, signedVarintByteLength, writeSignedVarint } from "../main";

function encodeDecode(v: number, bytes: number) {
const a = [];
const c = writeSignedVarint(v, a, 0);
expect(a.length).toBe(bytes);
expect(readSignedVarint(a, 0)).toEqual({ length: bytes, value: v });
expect(c).toBe(bytes);
expect(signedVarintByteLength(v)).toBe(bytes);
}

test("single byte", () => {
encodeDecode(1, 1);
encodeDecode(-1, 1);
encodeDecode(63, 1);
encodeDecode(-64, 1);
});
test("double byte", () => {
encodeDecode(64, 2);
encodeDecode(-65, 2);
encodeDecode(127, 2);
encodeDecode(-128, 2);
encodeDecode(128, 2);
encodeDecode(-129, 2);
encodeDecode(255, 2);
encodeDecode(-256, 2);
});
test("tripple", () => {
encodeDecode(0x4000, 3);
encodeDecode(-0x4001, 3);
encodeDecode(1048574, 3);
encodeDecode(-1048575, 3);
});

test("quad", () => {
encodeDecode(134217726, 4);
encodeDecode(-134217727, 4);
});

test("large int", () => {
encodeDecode(0x80000000000, 7);
encodeDecode(-0x80000000000, 7);
});
107 changes: 107 additions & 0 deletions code/varint/src/__tests__/unsigned.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { readUnsignedVarint, unsignedVarintByteLength, writeUnsignedVarint } from "../main";

test("fuzz test", () => {
for (var i = 0, len = 100; i < len; ++i) {
const num = randint(0x7fffffff);
const encoded = [];
const bytes_written = writeUnsignedVarint(num, encoded, 0);
expect(encoded.length).toBe(bytes_written);
expect(bytes_written).toBe(unsignedVarintByteLength(num));
expect(readUnsignedVarint(encoded, 0)).toEqual({ length: bytes_written, value: num });
}
});

test("test single byte works as expected", () => {
const buf = new Uint8Array(2);
buf[0] = 172;
buf[1] = 2;
expect(readUnsignedVarint(buf, 0)).toEqual({ length: 2, value: 300 });
});

test("test encode works as expected", () => {
const out = [];
const n_bytes = writeUnsignedVarint(300, out, 0);
expect(n_bytes).toBe(2);
expect(out).toEqual([0xac, 0x02]);
});

test("test decode single bytes", () => {
const num = randint(127);
const buf = new Uint8Array(1);
buf[0] = num;
expect(readUnsignedVarint(buf, 0)).toEqual({ length: 1, value: num });
});

test("test decode multiple bytes with zero", () => {
const num = randint(127);
const buf = new Uint8Array(2);
buf[0] = 128;
buf[1] = num;
expect(readUnsignedVarint(buf, 0)).toEqual({ length: 2, value: num << 7 });
});

test("encode single byte", () => {
const expected = randint(127);
const arr = [];
expect(writeUnsignedVarint(expected, arr, 0)).toBe(1);
expect(arr).toEqual([expected]);
});

test("encode multiple byte with zero first byte", () => {
const expected = 0x0f00;
const arr = [];
expect(writeUnsignedVarint(expected, arr, 0)).toBe(2);
expect(arr).toEqual([0x80, 0x1e]);
});

test("big integers", () => {
const bigs: number[] = [];
for (var i = 32; i <= 53; i++) {
const n = 2 ** i;
bigs.push(n - 1, n);
}

bigs.forEach((n) => {
const arr = [];
writeUnsignedVarint(n, arr, 0);
const { length, value } = readUnsignedVarint(arr, 0);
expect(length).toBe(arr.length);
expect(value).toBe(n);
expect(value).not.toBe(n - 1);
});
});

test("fuzz test - big", () => {
const MAX_INTD = 2 ** 55;
const MAX_INT = 2 ** 31;
for (var i = 0, len = 100; i < len; ++i) {
const expected = randint(MAX_INTD - MAX_INT) + MAX_INT;
const arr = [];
writeUnsignedVarint(expected, arr, 0);
expect(readUnsignedVarint(arr, 0)).toEqual({ length: arr.length, value: expected });
}
});

test("unsignedVarintByteLength", () => {
for (var i = 0; i <= 53; i++) {
const n = 2 ** i;
const arr = [];
const count = writeUnsignedVarint(n, arr, 0);
expect(count).toBe(arr.length);
expect(unsignedVarintByteLength(n)).toBe(count);
}
});

test("RangeError", () => {
const arr = [];
expect(writeUnsignedVarint(300, arr, 0)).toBe(2);
expect(arr).toEqual([172, 2]);
arr.pop();
expect(() => {
readUnsignedVarint(arr, 0);
}).toThrowErrorMatchingInlineSnapshot(`"Could not decode varint"`);
});

function randint(range: number): number {
return Math.floor(Math.random() * range);
}
88 changes: 88 additions & 0 deletions code/varint/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const N1 = 2 ** 7;
const N2 = 2 ** 14;
const N3 = 2 ** 21;
const N4 = 2 ** 28;
const N5 = 2 ** 35;
const N6 = 2 ** 42;
const N7 = 2 ** 49;
const N8 = 2 ** 56;
const N9 = 2 ** 63;

const MSB = 0x80;
const REST = 0x7f;
const MSBALL = ~REST;
const INT = 2 ** 31;

// const U32_MAX = 0xffffffff;
// const I32_MAX = 0x7fffffff;

export function unsignedVarintByteLength(value: number): number {
return value < N1
? 1
: value < N2
? 2
: value < N3
? 3
: value < N4
? 4
: value < N5
? 5
: value < N6
? 6
: value < N7
? 7
: value < N8
? 8
: value < N9
? 9
: 10;
}

export function writeUnsignedVarint(value: number, target: Buffer | Uint8Array | number[], offset: number): number {
let i = offset;
while (value >= INT) {
target[i++] = (value & 0xff) | MSB;
value /= 128;
}
while (value & MSBALL) {
target[i++] = (value & 0xff) | MSB;
value >>>= 7;
}
target[i] = value | 0;
return i - offset + 1;
}

export interface ReadRecord {
length: number;
value: number;
}

export function readUnsignedVarint(data: Buffer | Uint8Array | number[], offset: number): ReadRecord {
let value = 0;
let shift = 0;
let i = offset;
let b = 0;
do {
if (i >= data.length) {
throw new RangeError("Could not decode varint");
}
b = data[i++];
value += shift < 28 ? (b & REST) << shift : (b & REST) * 2 ** shift;
shift += 7;
} while (b >= MSB);
return { length: i - offset, value };
}

export function signedVarintByteLength(value: number): number {
return unsignedVarintByteLength(value >= 0 ? value * 2 : value * -2 - 1);
}

export function writeSignedVarint(value: number, target: Buffer | number[], offset: number): number {
return writeUnsignedVarint(value >= 0 ? value * 2 : value * -2 - 1, target, offset);
}

export function readSignedVarint(data: Buffer | number[], offset: number): ReadRecord {
let { length, value } = readUnsignedVarint(data, offset);
value = value & 1 ? (value + 1) / -2 : value / 2;
return { length, value };
}
7 changes: 7 additions & 0 deletions code/varint/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.build.json",
"include": ["src/**/*"],
"compilerOptions": {
"outDir": "./build"
}
}
4 changes: 4 additions & 0 deletions code/varint/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*"]
}

0 comments on commit c1cabe1

Please sign in to comment.