From 93acbe009e791f07b689c3c6cff35573357b7bb5 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 17 Jun 2024 06:41:33 +1000 Subject: [PATCH 1/2] refactor(cli): minor cleanups --- cli/unicode_width.ts | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/cli/unicode_width.ts b/cli/unicode_width.ts index e7115b73159a..7a3d44f0e518 100644 --- a/cli/unicode_width.ts +++ b/cli/unicode_width.ts @@ -5,15 +5,12 @@ import data from "./_data.json" with { type: "json" }; import { runLengthDecode } from "./_run_length.ts"; -let tables: Uint8Array[] | null = null; -function lookupWidth(cp: number) { - if (!tables) tables = data.tables.map(runLengthDecode); +const tables = data.tables.map(runLengthDecode); - const t1Offset = (tables[0] as Uint8Array)[(cp >> 13) & 0xff] as number; - const t2Offset = - (tables[1] as Uint8Array)[128 * t1Offset + ((cp >> 6) & 0x7f)] as number; - const packedWidths = - (tables[2] as Uint8Array)[16 * t2Offset + ((cp >> 2) & 0xf)] as number; +function lookupWidth(cp: number) { + const t1Offset = tables[0]![(cp >> 13) & 0xff]!; + const t2Offset = tables[1]![128 * t1Offset + ((cp >> 6) & 0x7f)]!; + const packedWidths = tables[2]![16 * t2Offset + ((cp >> 2) & 0xf)]!; const width = (packedWidths >> (2 * (cp & 0b11))) & 0b11; @@ -21,22 +18,22 @@ function lookupWidth(cp: number) { } const cache = new Map(); -function charWidth(ch: string) { - if (cache.has(ch)) return cache.get(ch)!; +function charWidth(char: string) { + if (cache.has(char)) return cache.get(char)!; - const cp = ch.codePointAt(0)!; - let v: number | null = null; + const codePoint = char.codePointAt(0)!; + let width: number | null = null; - if (cp < 0x7f) { - v = cp >= 0x20 ? 1 : cp === 0 ? 0 : null; - } else if (cp >= 0xa0) { - v = lookupWidth(cp); + if (codePoint < 0x7f) { + width = codePoint >= 0x20 ? 1 : codePoint === 0 ? 0 : null; + } else if (codePoint >= 0xa0) { + width = lookupWidth(codePoint); } else { - v = null; + width = null; } - cache.set(ch, v); - return v; + cache.set(char, width); + return width; } /** From 4889488ac5c434fb8dfc92c17999e4e06758ce3f Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 17 Jun 2024 15:45:52 +1000 Subject: [PATCH 2/2] revert lazy initialization --- cli/unicode_width.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/unicode_width.ts b/cli/unicode_width.ts index 7a3d44f0e518..bb71a5f289f4 100644 --- a/cli/unicode_width.ts +++ b/cli/unicode_width.ts @@ -5,9 +5,9 @@ import data from "./_data.json" with { type: "json" }; import { runLengthDecode } from "./_run_length.ts"; -const tables = data.tables.map(runLengthDecode); - +let tables: Uint8Array[] | null = null; function lookupWidth(cp: number) { + if (!tables) tables = data.tables.map(runLengthDecode); const t1Offset = tables[0]![(cp >> 13) & 0xff]!; const t2Offset = tables[1]![128 * t1Offset + ((cp >> 6) & 0x7f)]!; const packedWidths = tables[2]![16 * t2Offset + ((cp >> 2) & 0xf)]!;