Skip to content

Commit 598a729

Browse files
committed
fixes #799 - lookup table is created on first wcwidth call for char > 127
1 parent 6ed5c9e commit 598a729

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/InputHandler.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,21 +1600,26 @@ export const wcwidth = (function(opts) {
16001600
}
16011601
return 1;
16021602
}
1603-
// lookup table for BMP
1604-
const CODEPOINTS = 65536; // BMP holds 65536 codepoints
1605-
const BITWIDTH = 2; // a codepoint can have a width of 0, 1 or 2
1606-
const ITEMSIZE = 32; // using uint32_t
1607-
const CONTAINERSIZE = CODEPOINTS * BITWIDTH / ITEMSIZE;
1608-
const CODEPOINTS_PER_ITEM = ITEMSIZE / BITWIDTH;
1609-
const table = (typeof Uint32Array === 'undefined')
1610-
? new Array(CONTAINERSIZE)
1611-
: new Uint32Array(CONTAINERSIZE);
1612-
for (let i = 0; i < CONTAINERSIZE; ++i) {
1613-
let num = 0;
1614-
let pos = CODEPOINTS_PER_ITEM;
1615-
while (pos--)
1616-
num = (num << 2) | wcwidthBMP(CODEPOINTS_PER_ITEM * i + pos);
1617-
table[i] = num;
1603+
const control = opts.control | 0;
1604+
let table = null;
1605+
function init_table() {
1606+
// lookup table for BMP
1607+
const CODEPOINTS = 65536; // BMP holds 65536 codepoints
1608+
const BITWIDTH = 2; // a codepoint can have a width of 0, 1 or 2
1609+
const ITEMSIZE = 32; // using uint32_t
1610+
const CONTAINERSIZE = CODEPOINTS * BITWIDTH / ITEMSIZE;
1611+
const CODEPOINTS_PER_ITEM = ITEMSIZE / BITWIDTH;
1612+
table = (typeof Uint32Array === 'undefined')
1613+
? new Array(CONTAINERSIZE)
1614+
: new Uint32Array(CONTAINERSIZE);
1615+
for (let i = 0; i < CONTAINERSIZE; ++i) {
1616+
let num = 0;
1617+
let pos = CODEPOINTS_PER_ITEM;
1618+
while (pos--)
1619+
num = (num << 2) | wcwidthBMP(CODEPOINTS_PER_ITEM * i + pos);
1620+
table[i] = num;
1621+
}
1622+
return table;
16181623
}
16191624
// get width from lookup table
16201625
// position in container : num / CODEPOINTS_PER_ITEM
@@ -1631,12 +1636,13 @@ export const wcwidth = (function(opts) {
16311636
return function (num) {
16321637
num = num | 0; // get asm.js like optimization under V8
16331638
if (num < 32)
1634-
return opts.control;
1635-
else if (num < 127)
1639+
return control | 0;
1640+
if (num < 127)
16361641
return 1;
1637-
else if (num < 65536)
1638-
return table[num >> 4] >> ((num & 15) << 1) & 3;
1642+
let t = table || init_table();
1643+
if (num < 65536)
1644+
return t[num >> 4] >> ((num & 15) << 1) & 3;
16391645
// do a full search for high codepoints
1640-
else return wcwidthHigh(num);
1646+
return wcwidthHigh(num);
16411647
};
16421648
})({nul: 0, control: 0}); // configurable options

0 commit comments

Comments
 (0)