Skip to content

Commit 4556c8a

Browse files
committed
stream: skip set lookup
1 parent ea6c9f4 commit 4556c8a

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

stream.mjs

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import './playground/polyfill-install.mjs';
22

3-
// We hardcode this denylist set because it's smaller than the alphabet.
4-
// But the algorithm would work just as well with an allowlist,
5-
// which would be robust against any future changes to the set of whitespace.
6-
let whitespace = new Set(['\u0009', '\u000A', '\u000C', '\u000D', '\u0020']);
7-
83
// This mirrors the somewhat awkward TextDecoder API.
94
// Better designs are of course possible.
105
class Base64Decoder {
@@ -26,11 +21,18 @@ class Base64Decoder {
2621

2722
let realCharacterCount = 0;
2823
let hasWhitespace = false;
29-
for (let i = 0; i < chunk.length; ++i) {
30-
if (whitespace.has(chunk[i])) {
31-
hasWhitespace = true;
32-
} else {
33-
++realCharacterCount;
24+
25+
if (options.strict) {
26+
realCharacterCount = chunk.length;
27+
} else {
28+
for (let i = 0; i < chunk.length; ++i) {
29+
// this check divides the set of legal characters into whitespace and non-whitespace.
30+
// characters outside the set of legal characters will throw a decode error anyway, so it doesn't matter what they give
31+
if (chunk[i] < '+') {
32+
hasWhitespace = true;
33+
} else {
34+
++realCharacterCount;
35+
}
3436
}
3537
}
3638

@@ -45,7 +47,7 @@ class Base64Decoder {
4547
let collected = 0;
4648
let i = chunk.length - 1;
4749
while (true) {
48-
if (!whitespace.has(chunk[i])) {
50+
if (chunk[i] >= '+') {
4951
++collected;
5052
if (collected === extraCharacterCount) {
5153
break;

0 commit comments

Comments
 (0)