Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(encoding): prepare for noUncheckedIndexedAccess #4275

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions encoding/ascii85.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ export function encodeAscii85(
}
break;
case "RFC 1924":
output = output.map((val) => rfc1924[val.charCodeAt(0) - 33]);
output = output.map((val) => rfc1924[val.charCodeAt(0) - 33]) as string[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
output = output.map((val) => rfc1924[val.charCodeAt(0) - 33]) as string[];
output = output.map((val) => rfc1924[val.charCodeAt(0) - 33]!);

Ditto where appropriate. Non-null assertions are a little cleaner than type castings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but I think it's better to express what type it's, if it just only one type, except undefined. And that's why I used as more than !.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type-castings make more sense if the variable has more than one non-null type. E.g. string | number | undefined.

break;
case "Z85":
output = output.map((val) => Z85[val.charCodeAt(0) - 33]);
output = output.map((val) => Z85[val.charCodeAt(0) - 33]) as string[];
break;
}
return output.slice(0, output.length - difference).join("");
Expand Down
12 changes: 7 additions & 5 deletions encoding/ascii85_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ for (const [standard, tests] of Object.entries(testCasesNoDelimiter)) {
fn() {
for (const [bin, b85] of tests) {
assertEquals(
encodeAscii85(bin, {
encodeAscii85(bin as string, {
standard: standard as Ascii85Standard,
}),
b85,
Expand All @@ -137,7 +137,9 @@ for (const [standard, tests] of Object.entries(testCasesNoDelimiter)) {
fn() {
for (const [bin, b85] of tests) {
assertEquals(
decodeAscii85(b85, { standard: standard as Ascii85Standard }),
decodeAscii85(b85 as string, {
standard: standard as Ascii85Standard,
}),
utf8encoder.encode(bin),
);
}
Expand All @@ -151,7 +153,7 @@ for (const [standard, tests] of Object.entries(testCasesDelimiter)) {
fn() {
for (const [bin, b85] of tests) {
assertEquals(
encodeAscii85(bin, {
encodeAscii85(bin as string, {
standard: standard as Ascii85Standard,
delimiter: true,
}),
Expand All @@ -166,7 +168,7 @@ for (const [standard, tests] of Object.entries(testCasesDelimiter)) {
fn() {
for (const [bin, b85] of tests) {
assertEquals(
decodeAscii85(b85, {
decodeAscii85(b85 as string, {
standard: standard as Ascii85Standard,
delimiter: true,
}),
Expand Down Expand Up @@ -204,7 +206,7 @@ Deno.test({

for (const [input, expect] of tests) {
assertEquals(
decodeAscii85(input),
decodeAscii85(input as string),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unnecessary if the tests variable is declared using as const (const assertion). This fact should be checked whenever a non-changing variable is used.

utf8encoder.encode(expect),
);
}
Expand Down
87 changes: 44 additions & 43 deletions encoding/base32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ const revLookup: number[] = [];

// RFC4648 base32
const code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
for (let i = 0, len = code.length; i < len; ++i) {
lookup[i] = code[i];
code.split("").forEach((c, i) => {
lookup[i] = c;
revLookup[code.charCodeAt(i)] = i;
}
});

const placeHolderPadLookup = [0, 1, , 2, 3, , 4];
function _getPadLen(placeHoldersLen: number): number {
Expand Down Expand Up @@ -79,55 +79,55 @@ export function decodeBase32(b32: string): Uint8Array {

let i: number;
for (i = 0; i < len; i += 8) {
tmp = (revLookup[b32.charCodeAt(i)] << 20) |
(revLookup[b32.charCodeAt(i + 1)] << 15) |
(revLookup[b32.charCodeAt(i + 2)] << 10) |
(revLookup[b32.charCodeAt(i + 3)] << 5) |
revLookup[b32.charCodeAt(i + 4)];
tmp = (revLookup[b32.charCodeAt(i)] as number << 20) |
(revLookup[b32.charCodeAt(i + 1)] as number << 15) |
(revLookup[b32.charCodeAt(i + 2)] as number << 10) |
(revLookup[b32.charCodeAt(i + 3)] as number << 5) |
revLookup[b32.charCodeAt(i + 4)] as number;
arr[curByte++] = (tmp >> 17) & 0xff;
arr[curByte++] = (tmp >> 9) & 0xff;
arr[curByte++] = (tmp >> 1) & 0xff;

tmp = ((tmp & 1) << 15) |
(revLookup[b32.charCodeAt(i + 5)] << 10) |
(revLookup[b32.charCodeAt(i + 6)] << 5) |
revLookup[b32.charCodeAt(i + 7)];
(revLookup[b32.charCodeAt(i + 5)] as number << 10) |
(revLookup[b32.charCodeAt(i + 6)] as number << 5) |
revLookup[b32.charCodeAt(i + 7)] as number;
arr[curByte++] = (tmp >> 8) & 0xff;
arr[curByte++] = tmp & 0xff;
}

if (placeHoldersLen === 1) {
tmp = (revLookup[b32.charCodeAt(i)] << 20) |
(revLookup[b32.charCodeAt(i + 1)] << 15) |
(revLookup[b32.charCodeAt(i + 2)] << 10) |
(revLookup[b32.charCodeAt(i + 3)] << 5) |
revLookup[b32.charCodeAt(i + 4)];
tmp = (revLookup[b32.charCodeAt(i)] as number << 20) |
(revLookup[b32.charCodeAt(i + 1)] as number << 15) |
(revLookup[b32.charCodeAt(i + 2)] as number << 10) |
(revLookup[b32.charCodeAt(i + 3)] as number << 5) |
revLookup[b32.charCodeAt(i + 4)] as number;
arr[curByte++] = (tmp >> 17) & 0xff;
arr[curByte++] = (tmp >> 9) & 0xff;
arr[curByte++] = (tmp >> 1) & 0xff;
tmp = ((tmp & 1) << 7) |
(revLookup[b32.charCodeAt(i + 5)] << 2) |
(revLookup[b32.charCodeAt(i + 6)] >> 3);
(revLookup[b32.charCodeAt(i + 5)] as number << 2) |
(revLookup[b32.charCodeAt(i + 6)] as number >> 3);
arr[curByte++] = tmp & 0xff;
} else if (placeHoldersLen === 3) {
tmp = (revLookup[b32.charCodeAt(i)] << 19) |
(revLookup[b32.charCodeAt(i + 1)] << 14) |
(revLookup[b32.charCodeAt(i + 2)] << 9) |
(revLookup[b32.charCodeAt(i + 3)] << 4) |
(revLookup[b32.charCodeAt(i + 4)] >> 1);
tmp = (revLookup[b32.charCodeAt(i)] as number << 19) |
(revLookup[b32.charCodeAt(i + 1)] as number << 14) |
(revLookup[b32.charCodeAt(i + 2)] as number << 9) |
(revLookup[b32.charCodeAt(i + 3)] as number << 4) |
(revLookup[b32.charCodeAt(i + 4)] as number >> 1);
arr[curByte++] = (tmp >> 16) & 0xff;
arr[curByte++] = (tmp >> 8) & 0xff;
arr[curByte++] = tmp & 0xff;
} else if (placeHoldersLen === 4) {
tmp = (revLookup[b32.charCodeAt(i)] << 11) |
(revLookup[b32.charCodeAt(i + 1)] << 6) |
(revLookup[b32.charCodeAt(i + 2)] << 1) |
(revLookup[b32.charCodeAt(i + 3)] >> 4);
tmp = (revLookup[b32.charCodeAt(i)] as number << 11) |
(revLookup[b32.charCodeAt(i + 1)] as number << 6) |
(revLookup[b32.charCodeAt(i + 2)] as number << 1) |
(revLookup[b32.charCodeAt(i + 3)] as number >> 4);
arr[curByte++] = (tmp >> 8) & 0xff;
arr[curByte++] = tmp & 0xff;
} else if (placeHoldersLen === 6) {
tmp = (revLookup[b32.charCodeAt(i)] << 3) |
(revLookup[b32.charCodeAt(i + 1)] >> 2);
tmp = (revLookup[b32.charCodeAt(i)] as number << 3) |
(revLookup[b32.charCodeAt(i + 1)] as number >> 2);
arr[curByte++] = tmp & 0xff;
}

Expand All @@ -138,16 +138,16 @@ function encodeChunk(uint8: Uint8Array, start: number, end: number): string {
let tmp: number;
const output = [];
for (let i = start; i < end; i += 5) {
tmp = ((uint8[i] << 16) & 0xff0000) |
((uint8[i + 1] << 8) & 0xff00) |
(uint8[i + 2] & 0xff);
tmp = ((uint8[i] as number << 16) & 0xff0000) |
((uint8[i + 1] as number << 8) & 0xff00) |
(uint8[i + 2] as number & 0xff);
output.push(lookup[(tmp >> 19) & 0x1f]);
output.push(lookup[(tmp >> 14) & 0x1f]);
output.push(lookup[(tmp >> 9) & 0x1f]);
output.push(lookup[(tmp >> 4) & 0x1f]);
tmp = ((tmp & 0xf) << 16) |
((uint8[i + 3] << 8) & 0xff00) |
(uint8[i + 4] & 0xff);
((uint8[i + 3] as number << 8) & 0xff00) |
(uint8[i + 4] as number & 0xff);
output.push(lookup[(tmp >> 15) & 0x1f]);
output.push(lookup[(tmp >> 10) & 0x1f]);
output.push(lookup[(tmp >> 5) & 0x1f]);
Expand Down Expand Up @@ -191,37 +191,38 @@ export function encodeBase32(data: ArrayBuffer | Uint8Array | string): string {

// pad the end with zeros, but make sure to not forget the extra bytes
if (extraBytes === 4) {
tmp = ((uint8[len2] & 0xff) << 16) |
((uint8[len2 + 1] & 0xff) << 8) |
(uint8[len2 + 2] & 0xff);
tmp = ((uint8[len2] as number & 0xff) << 16) |
((uint8[len2 + 1] as number & 0xff) << 8) |
(uint8[len2 + 2] as number & 0xff);
parts.push(lookup[(tmp >> 19) & 0x1f]);
parts.push(lookup[(tmp >> 14) & 0x1f]);
parts.push(lookup[(tmp >> 9) & 0x1f]);
parts.push(lookup[(tmp >> 4) & 0x1f]);
tmp = ((tmp & 0xf) << 11) | (uint8[len2 + 3] << 3);
tmp = ((tmp & 0xf) << 11) | (uint8[len2 + 3] as number << 3);
parts.push(lookup[(tmp >> 10) & 0x1f]);
parts.push(lookup[(tmp >> 5) & 0x1f]);
parts.push(lookup[tmp & 0x1f]);
parts.push("=");
} else if (extraBytes === 3) {
tmp = ((uint8[len2] & 0xff) << 17) |
((uint8[len2 + 1] & 0xff) << 9) |
((uint8[len2 + 2] & 0xff) << 1);
tmp = ((uint8[len2] as number & 0xff) << 17) |
((uint8[len2 + 1] as number & 0xff) << 9) |
((uint8[len2 + 2] as number & 0xff) << 1);
parts.push(lookup[(tmp >> 20) & 0x1f]);
parts.push(lookup[(tmp >> 15) & 0x1f]);
parts.push(lookup[(tmp >> 10) & 0x1f]);
parts.push(lookup[(tmp >> 5) & 0x1f]);
parts.push(lookup[tmp & 0x1f]);
parts.push("===");
} else if (extraBytes === 2) {
tmp = ((uint8[len2] & 0xff) << 12) | ((uint8[len2 + 1] & 0xff) << 4);
tmp = ((uint8[len2] as number & 0xff) << 12) |
((uint8[len2 + 1] as number & 0xff) << 4);
parts.push(lookup[(tmp >> 15) & 0x1f]);
parts.push(lookup[(tmp >> 10) & 0x1f]);
parts.push(lookup[(tmp >> 5) & 0x1f]);
parts.push(lookup[tmp & 0x1f]);
parts.push("====");
} else if (extraBytes === 1) {
tmp = (uint8[len2] & 0xff) << 2;
tmp = (uint8[len2] as number & 0xff) << 2;
parts.push(lookup[(tmp >> 5) & 0x1f]);
parts.push(lookup[tmp & 0x1f]);
parts.push("======");
Expand Down
4 changes: 2 additions & 2 deletions encoding/base32_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Deno.test({
name: "encodeBase32()",
fn() {
for (const [bin, b32] of testCases) {
assertEquals(encodeBase32(fromHexString(bin)), b32);
assertEquals(encodeBase32(fromHexString(bin as string)), b32);
}
},
});
Expand All @@ -100,7 +100,7 @@ Deno.test({
name: "decodeBase32()",
fn() {
for (const [bin, b32] of testCases) {
assertEquals(toHexString(decodeBase32(b32)), bin);
assertEquals(toHexString(decodeBase32(b32 as string)), bin);
}
},
});
Expand Down
4 changes: 3 additions & 1 deletion encoding/base58.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ export function encodeBase58(data: ArrayBuffer | Uint8Array | string): string {
strResult.fill("1", 0, zeroes);
}

b58Encoding.forEach((byteValue) => strResult.push(base58alphabet[byteValue]));
b58Encoding.forEach((byteValue) =>
strResult.push(base58alphabet[byteValue] as string)
);

return strResult.join("");
}
Expand Down
2 changes: 1 addition & 1 deletion encoding/base58_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const testSetBinary = testSetString.map(([data, b58]) => {

Deno.test("encodeBase58() encodes string", () => {
for (const [input, output] of testSetString) {
assertEquals(encodeBase58(input), output);
assertEquals(encodeBase58(input!), output);
}
});

Expand Down
27 changes: 18 additions & 9 deletions encoding/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,31 @@ export function encodeBase64(data: ArrayBuffer | Uint8Array | string): string {
i;
const l = uint8.length;
for (i = 2; i < l; i += 3) {
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
result += base64abc[((uint8[i - 1] & 0x0f) << 2) | (uint8[i] >> 6)];
result += base64abc[uint8[i] & 0x3f];
result += base64abc[(uint8[i - 2] as number) >> 2];
result += base64abc[
(((uint8[i - 2] as number) & 0x03) << 4) |
((uint8[i - 1] as number) >> 4)
];
result += base64abc[
(((uint8[i - 1] as number) & 0x0f) << 2) |
((uint8[i] as number) >> 6)
];
result += base64abc[(uint8[i] as number) & 0x3f];
}
if (i === l + 1) {
// 1 octet yet to write
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[(uint8[i - 2] & 0x03) << 4];
result += base64abc[(uint8[i - 2] as number) >> 2];
result += base64abc[((uint8[i - 2] as number) & 0x03) << 4];
result += "==";
}
if (i === l) {
// 2 octets yet to write
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
result += base64abc[(uint8[i - 1] & 0x0f) << 2];
result += base64abc[(uint8[i - 2] as number) >> 2];
result += base64abc[
(((uint8[i - 2] as number) & 0x03) << 4) |
((uint8[i - 1] as number) >> 4)
];
result += base64abc[((uint8[i - 1] as number) & 0x0f) << 2];
result += "=";
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion encoding/base64_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const testsetBinary = testsetString.map(([str, b64]) => [

Deno.test("encodeBase64() encodes string", () => {
for (const [input, output] of testsetString) {
assertEquals(encodeBase64(input), output);
assertEquals(encodeBase64(input as string), output);
}
});

Expand Down
2 changes: 1 addition & 1 deletion encoding/base64url_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const testsetInvalid = [

Deno.test("encodeBase64Url() encodes string", () => {
for (const [input, output] of testsetString) {
assertEquals(encodeBase64Url(input), output);
assertEquals(encodeBase64Url(input as string), output);
}
});

Expand Down
12 changes: 6 additions & 6 deletions encoding/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export function encodeHex(src: string | Uint8Array | ArrayBuffer): string {

const dst = new Uint8Array(u8.length * 2);
for (let i = 0; i < dst.length; i++) {
const v = u8[i];
dst[i * 2] = hexTable[v >> 4];
dst[i * 2 + 1] = hexTable[v & 0x0f];
const v = u8[i] as number;
dst[i * 2] = hexTable[v >> 4] as number;
dst[i * 2 + 1] = hexTable[v & 0x0f] as number;
}
return textDecoder.decode(dst);
}
Expand All @@ -92,15 +92,15 @@ export function decodeHex(src: string): Uint8Array {
const u8 = textEncoder.encode(src);
const dst = new Uint8Array(u8.length / 2);
for (let i = 0; i < dst.length; i++) {
const a = fromHexChar(u8[i * 2]);
const b = fromHexChar(u8[i * 2 + 1]);
const a = fromHexChar(u8[i * 2] as number);
const b = fromHexChar(u8[i * 2 + 1] as number);
dst[i] = (a << 4) | b;
}

if (u8.length % 2 === 1) {
// Check for invalid char before reporting bad length,
// since the invalid char (if present) is an earlier problem.
fromHexChar(u8[dst.length * 2]);
fromHexChar(u8[dst.length * 2] as number);
throw errLength();
}

Expand Down
4 changes: 2 additions & 2 deletions encoding/varint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function decode(buf: Uint8Array, offset = 0): [bigint, number] {
let byte;
do {
// Get a single byte from the buffer
byte = buf[i];
byte = buf[i]!;

// 1. Take the lower 7 bits of the byte.
// 2. Shift the bits into the correct position.
Expand Down Expand Up @@ -120,7 +120,7 @@ export function decode32(buf: Uint8Array, offset = 0): [number, number] {
i <= len;
i += 1, shift += SHIFT
) {
const byte = buf[i];
const byte = buf[i] as number;
decoded += (byte & REST) * Math.pow(2, shift);
if (!(byte & MSB)) return [decoded, i + 1];
}
Expand Down
Loading