-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: change vlq encoding to encode only positive ints (#389)
removes the 3rd party libary for vlq.
- Loading branch information
Showing
11 changed files
with
144 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,7 +87,6 @@ | |
"dependencies": { | ||
"cli-table3": "0.6.3", | ||
"colorette": "2.0.19", | ||
"vlq": "2.0.4", | ||
"yargs": "17.6.1" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
Copyright 2022 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
const alpha = | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
|
||
const charToIdx = alpha.split('').reduce((acc, char, idx) => { | ||
acc.set(char, idx); | ||
return acc; | ||
}, new Map()); | ||
|
||
const LEAST_5_BIT_MASK = 0b011111; | ||
const CONTINUATION_BIT_MASK = 0b100000; | ||
const MAX_INT = 2147483647; | ||
|
||
/** | ||
* Encoding variable length integer into base64 (6-bit): | ||
* | ||
* 1 N N N N N | 0 N N N N N | ||
* | ||
* The first bit indicates if there is more data for the int. | ||
*/ | ||
export function encodeInt(num: number) { | ||
if (num < 0) { | ||
throw new Error('Only postive integers and zero are supported'); | ||
} | ||
if (num > MAX_INT) { | ||
throw new Error( | ||
'Only integers between 0 and ' + MAX_INT + ' are supported' | ||
); | ||
} | ||
const result = []; | ||
do { | ||
let payload = num & LEAST_5_BIT_MASK; | ||
num >>>= 5; | ||
if (num > 0) payload |= CONTINUATION_BIT_MASK; | ||
result.push(alpha[payload]); | ||
} while (num !== 0); | ||
return result.join(''); | ||
} | ||
|
||
export function encode(nums: number[]): string { | ||
const parts = []; | ||
for (const num of nums) { | ||
parts.push(encodeInt(num)); | ||
} | ||
return parts.join(''); | ||
} | ||
|
||
export function decode(str: string) { | ||
const results = []; | ||
const chrs = str.split(''); | ||
|
||
let result = 0; | ||
let shift = 0; | ||
for (const ch of chrs) { | ||
const num = charToIdx.get(ch); | ||
result |= (num & LEAST_5_BIT_MASK) << shift; | ||
shift += 5; | ||
const hasMore = num & CONTINUATION_BIT_MASK; | ||
if (!hasMore) { | ||
results.push(result); | ||
result = 0; | ||
shift = 0; | ||
} | ||
} | ||
|
||
return results; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
Copyright 2022 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { encodeInt, decode, encode } from '../src/vlq.js'; | ||
import { assert } from 'chai'; | ||
|
||
describe('vlq', () => { | ||
it('should encode', () => { | ||
assert.strictEqual(encodeInt(0), 'A'); | ||
assert.strictEqual(encodeInt(1), 'B'); | ||
assert.strictEqual(encodeInt(123), '7D'); | ||
assert.strictEqual(encodeInt(123) + encodeInt(123456789), '7D1oz31D'); | ||
assert.strictEqual(encodeInt(123456789), '1oz31D'); | ||
assert.strictEqual(encodeInt(2147483647), '//////B'); | ||
}); | ||
it('should decode', () => { | ||
assert.deepStrictEqual(decode('A'), [0]); | ||
assert.deepStrictEqual(decode('C'), [2]); | ||
assert.deepStrictEqual(decode('D'), [3]); | ||
assert.deepStrictEqual(decode('7D'), [123]); | ||
assert.deepStrictEqual(decode('1oz31D'), [123456789]); | ||
assert.deepStrictEqual(decode('7D1oz31D'), [123, 123456789]); | ||
assert.deepStrictEqual(decode('//////B'), [2147483647]); | ||
}); | ||
it('should encode array', () => { | ||
assert.strictEqual(encode([0, 1, 123]), 'AB7D'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters