Skip to content

Commit 79e9d18

Browse files
committed
chore: update encodeFast for last bytes
1 parent 25a2729 commit 79e9d18

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/encodeFast.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,27 @@ for (let i = 0; i < 64; i++) {
3535

3636
export function encodeFast(input: Uint8Array): Uint8Array {
3737
const output32 = new Uint32Array(Math.ceil(input.length / 3));
38-
const output8 = new Uint8Array(output32.buffer);
3938
let i, j;
4039
for (i = 2, j = 0; i < input.length; i += 3, j++) {
4140
output32[j] =
4241
base64codes1[input[i - 2] | ((input[i - 1] & 0xf0) << 4)] |
4342
base64codes2[input[i] | ((input[i - 1] & 0x0f) << 8)];
4443
}
45-
const index = j * 4;
4644
if (i === input.length + 1) {
4745
// 1 octet yet to write
48-
output8[index] = base64codes[input[i - 2] >> 2];
49-
output8[index + 1] = base64codes[(input[i - 2] & 0x03) << 4];
50-
output8[index + 2] = 61;
51-
output8[index + 3] = 61;
46+
output32[j] =
47+
base64codes[input[i - 2] >> 2] |
48+
(base64codes[(input[i - 2] & 0x03) << 4] << 8) |
49+
(15677 << 16);
5250
}
5351
if (i === input.length) {
5452
// 2 octets yet to write
55-
output8[index] = base64codes[input[i - 2] >> 2];
56-
output8[index + 1] =
57-
base64codes[((input[i - 2] & 0x03) << 4) | (input[i - 1] >> 4)];
58-
output8[index + 2] = base64codes[(input[i - 1] & 0x0f) << 2];
59-
output8[index + 3] = 61;
53+
output32[j] =
54+
base64codes[input[i - 2] >> 2] |
55+
(base64codes[((input[i - 2] & 0x03) << 4) | (input[i - 1] >> 4)] << 8) |
56+
(base64codes[(input[i - 1] & 0x0f) << 2] << 16) |
57+
(61 << 24);
6058
}
59+
const output8 = new Uint8Array(output32.buffer);
6160
return output8;
6261
}

0 commit comments

Comments
 (0)