Skip to content

Commit bacf465

Browse files
committed
Remove error checks when decoding VLQ
1 parent 7f4e9a4 commit bacf465

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

src/vlq.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2025 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import { describe, it } from "jsr:@std/testing/bdd";
6+
import { TokenIterator } from "./vlq.ts";
7+
import { assertFalse, assertStrictEquals } from "jsr:@std/assert";
8+
9+
describe("TokenIterator", () => {
10+
describe("nextUnsignedVLQ", () => {
11+
it("returns zero when no more characters are available", () => {
12+
const iter = new TokenIterator("");
13+
assertFalse(iter.hasNext());
14+
15+
assertStrictEquals(iter.nextUnsignedVLQ(), 0);
16+
});
17+
18+
it("treats unknown characters as 0", () => {
19+
const iter = new TokenIterator("h,C"); // 'h' has the continuation bit set. ',' is treated as 0 so we stop decoding.
20+
21+
assertStrictEquals(iter.nextUnsignedVLQ(), 1);
22+
assertStrictEquals(iter.peek(), "C");
23+
});
24+
25+
it("treats unknown unicode characters as 0", () => {
26+
const iter = new TokenIterator("hæC"); // 'h' has the continuation bit set. 'æ' falls outside the array so has 'undefined as its digit value.
27+
28+
assertStrictEquals(iter.nextUnsignedVLQ(), 1);
29+
assertStrictEquals(iter.peek(), "C");
30+
});
31+
});
32+
33+
describe("nextSignedVLQ", () => {
34+
it("returns zero when no more characters are available", () => {
35+
const iter = new TokenIterator("");
36+
assertFalse(iter.hasNext());
37+
38+
assertStrictEquals(iter.nextSignedVLQ(), 0);
39+
});
40+
41+
it("treats unknown characters as 0", () => {
42+
const iter = new TokenIterator("i,C"); // 'i' has the continuation bit set. ',' is treated as 0 so we stop decoding.
43+
44+
assertStrictEquals(iter.nextSignedVLQ(), 1);
45+
assertStrictEquals(iter.peek(), "C");
46+
});
47+
48+
it("treats unknown unicode characters as 0", () => {
49+
const iter = new TokenIterator("iæC"); // 'i' has the continuation bit set. 'æ' falls outside the array so has 'undefined as its digit value.
50+
51+
assertStrictEquals(iter.nextSignedVLQ(), 1);
52+
assertStrictEquals(iter.peek(), "C");
53+
});
54+
});
55+
56+
describe("current", () => {
57+
it("returns the empty string when calling it without advancing the iterator", () => {
58+
const iter = new TokenIterator("CCCC");
59+
60+
assertStrictEquals(iter.currentChar(), "");
61+
});
62+
});
63+
});

src/vlq.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,15 @@ export class TokenIterator {
8585
let shift = 0;
8686
let digit: number = VLQ_CONTINUATION_MASK;
8787
while (digit & VLQ_CONTINUATION_MASK) {
88-
if (!this.hasNext()) {
89-
throw new Error("Unexpected end of input while decodling VLQ number!");
90-
}
9188
const charCode = this.nextCharCode();
9289
digit = BASE64_CODES[charCode];
93-
if (charCode !== 65 /* 'A' */ && digit === 0) {
94-
throw new Error(
95-
`Unexpected char '${
96-
String.fromCharCode(charCode)
97-
}' encountered while decoding`,
98-
);
99-
}
10090
result += (digit & VLQ_BASE_MASK) << shift;
10191
shift += VLQ_BASE_SHIFT;
10292
}
10393
return result;
10494
}
10595

10696
currentChar(): string {
107-
if (this.#position === 0) throw new Error("Move the iterator first!");
10897
return this.#string.charAt(this.#position - 1);
10998
}
11099
}

0 commit comments

Comments
 (0)