Skip to content

Commit 1232c00

Browse files
committed
some fixes
- commenting the bit shifts - switching to Uint32Array - conditional for TypedArray - fixing Math.floor slowdown in bisearch
1 parent 5d87b54 commit 1232c00

File tree

1 file changed

+120
-99
lines changed

1 file changed

+120
-99
lines changed

src/InputHandler.ts

Lines changed: 120 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,92 +1494,92 @@ export const wcwidth = (function(opts) {
14941494
// extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
14951495
// combining characters
14961496
const COMBINING_BMP = [
1497-
[0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
1498-
[0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
1499-
[0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
1500-
[0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
1501-
[0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
1502-
[0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
1503-
[0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
1504-
[0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
1505-
[0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
1506-
[0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
1507-
[0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
1508-
[0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
1509-
[0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
1510-
[0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
1511-
[0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
1512-
[0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
1513-
[0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
1514-
[0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
1515-
[0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
1516-
[0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
1517-
[0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
1518-
[0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
1519-
[0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
1520-
[0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
1521-
[0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
1522-
[0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
1523-
[0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
1524-
[0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
1525-
[0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
1526-
[0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
1527-
[0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
1528-
[0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
1529-
[0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
1530-
[0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
1531-
[0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
1532-
[0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
1533-
[0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
1534-
[0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
1535-
[0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
1536-
[0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
1537-
[0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
1538-
[0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
1539-
[0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
1497+
[0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
1498+
[0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
1499+
[0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
1500+
[0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
1501+
[0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
1502+
[0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
1503+
[0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
1504+
[0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
1505+
[0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
1506+
[0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
1507+
[0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
1508+
[0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
1509+
[0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
1510+
[0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
1511+
[0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
1512+
[0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
1513+
[0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
1514+
[0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
1515+
[0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
1516+
[0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
1517+
[0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
1518+
[0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
1519+
[0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
1520+
[0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
1521+
[0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
1522+
[0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
1523+
[0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
1524+
[0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
1525+
[0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
1526+
[0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
1527+
[0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
1528+
[0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
1529+
[0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
1530+
[0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
1531+
[0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
1532+
[0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
1533+
[0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
1534+
[0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
1535+
[0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
1536+
[0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
1537+
[0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
1538+
[0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
1539+
[0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
15401540
];
15411541
const COMBINING_HIGH = [
1542-
[0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
1543-
[0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
1544-
[0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
1545-
[0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
1546-
[0xE0100, 0xE01EF]
1542+
[0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
1543+
[0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
1544+
[0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
1545+
[0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
1546+
[0xE0100, 0xE01EF]
15471547
];
15481548
// binary search
15491549
function bisearch(ucs, data) {
1550-
let min = 0;
1551-
let max = data.length - 1;
1552-
let mid;
1553-
if (ucs < data[0][0] || ucs > data[max][1])
1554-
return false;
1555-
while (max >= min) {
1556-
mid = Math.floor((min + max) / 2);
1557-
if (ucs > data[mid][1])
1558-
min = mid + 1;
1559-
else if (ucs < data[mid][0])
1560-
max = mid - 1;
1561-
else
1562-
return true;
1563-
}
1550+
let min = 0;
1551+
let max = data.length - 1;
1552+
let mid;
1553+
if (ucs < data[0][0] || ucs > data[max][1])
15641554
return false;
1555+
while (max >= min) {
1556+
mid = (min + max) >> 1;
1557+
if (ucs > data[mid][1])
1558+
min = mid + 1;
1559+
else if (ucs < data[mid][0])
1560+
max = mid - 1;
1561+
else
1562+
return true;
1563+
}
1564+
return false;
15651565
}
15661566
function wcwidthBMP(ucs) {
1567-
// test for 8-bit control characters
1568-
if (ucs === 0)
1569-
return opts.nul;
1570-
if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
1571-
return opts.control;
1572-
// binary search in table of non-spacing characters
1573-
if (bisearch(ucs, COMBINING_BMP))
1574-
return 0;
1575-
// if we arrive here, ucs is not a combining or C0/C1 control character
1576-
if (isWideBMP(ucs)) {
1577-
return 2;
1578-
}
1579-
return 1;
1567+
// test for 8-bit control characters
1568+
if (ucs === 0)
1569+
return opts.nul;
1570+
if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
1571+
return opts.control;
1572+
// binary search in table of non-spacing characters
1573+
if (bisearch(ucs, COMBINING_BMP))
1574+
return 0;
1575+
// if we arrive here, ucs is not a combining or C0/C1 control character
1576+
if (isWideBMP(ucs)) {
1577+
return 2;
1578+
}
1579+
return 1;
15801580
}
15811581
function isWideBMP(ucs) {
1582-
return (
1582+
return (
15831583
ucs >= 0x1100 && (
15841584
ucs <= 0x115f || // Hangul Jamo init. consonants
15851585
ucs === 0x2329 ||
@@ -1593,29 +1593,50 @@ export const wcwidth = (function(opts) {
15931593
(ucs >= 0xffe0 && ucs <= 0xffe6)));
15941594
}
15951595
function wcwidthHigh(ucs) {
1596-
if (bisearch(ucs, COMBINING_HIGH))
1597-
return 0;
1598-
if ((ucs >= 0x20000 && ucs <= 0x2fffd) || (ucs >= 0x30000 && ucs <= 0x3fffd)) {
1599-
return 2;
1600-
}
1601-
return 1;
1602-
}
1603-
let table = new Uint8Array(16384);
1604-
for (let i = 0; i < 16384; ++i) {
1605-
let j = i * 4;
1606-
let num = 0;
1607-
num |= (wcwidthBMP(j + 3));
1608-
num <<= 2;
1609-
num |= (wcwidthBMP(j + 2));
1610-
num <<= 2;
1611-
num |= (wcwidthBMP(j + 1));
1612-
num <<= 2;
1613-
num |= (wcwidthBMP(j));
1614-
table[i] = num;
1615-
}
1596+
if (bisearch(ucs, COMBINING_HIGH))
1597+
return 0;
1598+
if ((ucs >= 0x20000 && ucs <= 0x2fffd) || (ucs >= 0x30000 && ucs <= 0x3fffd)) {
1599+
return 2;
1600+
}
1601+
return 1;
1602+
}
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;
1618+
}
1619+
// get width from lookup table for num < 65536:
1620+
// position in container : num / CODEPOINTS_PER_ITEM
1621+
// ==> n = table[Math.floor(num / 16)]
1622+
// ==> n = table[num >> 4]
1623+
// 16 codepoints per number: FFEEDDCCBBAA99887766554433221100
1624+
// position in number : (num % CODEPOINTS_PER_ITEM) * BITWIDTH
1625+
// ==> m = (n % 16) * 2
1626+
// ==> m = (num & 15) << 1
1627+
// right shift to position m
1628+
// ==> n = n >> m e.g. m=12 000000000000FFEEDDCCBBAA99887766
1629+
// we are only interested in 2 LSBs, cut off higher
1630+
// ==> n = n & 3 e.g. 000000000000000000000000000000XX
16161631
return function (num) {
1617-
num = num | 0;
1618-
return (num < 65536) ? (table[num >> 2] >> ((num & 3) * 2)) & 3 : wcwidthHigh(num);
1632+
num = num | 0; // get asm.js like optimization under V8
1633+
if (num < 32)
1634+
return opts.control;
1635+
else if (num < 127)
1636+
return 1;
1637+
else if (num < 65536)
1638+
return table[num >> 4] >> ((num & 15) << 1) & 3;
1639+
// do a full search for high codepoints
1640+
else return wcwidthHigh(num);
16191641
};
16201642
})({nul: 0, control: 0}); // configurable options
1621-

0 commit comments

Comments
 (0)