Skip to content

Commit

Permalink
refactor(cli): minor cleanups (#5052)
Browse files Browse the repository at this point in the history
* refactor(cli): minor cleanups

* revert lazy initialization
  • Loading branch information
iuioiua authored Jun 17, 2024
1 parent 935d665 commit 7867a60
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions cli/unicode_width.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,32 @@ import { runLengthDecode } from "./_run_length.ts";
let tables: Uint8Array[] | null = null;
function lookupWidth(cp: number) {
if (!tables) 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;
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;

return width === 3 ? 1 : width;
}

const cache = new Map<string, number | null>();
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;
}

/**
Expand Down

0 comments on commit 7867a60

Please sign in to comment.