some fixes

- commenting the bit shifts
- switching to Uint32Array
- conditional for TypedArray
- fixing Math.floor slowdown in bisearch
This commit is contained in:
Jörg Breitbart 2017-07-16 23:40:36 +02:00
parent 5d87b54390
commit 1232c00dd1

View File

@ -1553,7 +1553,7 @@ export const wcwidth = (function(opts) {
if (ucs < data[0][0] || ucs > data[max][1])
return false;
while (max >= min) {
mid = Math.floor((min + max) / 2);
mid = (min + max) >> 1;
if (ucs > data[mid][1])
min = mid + 1;
else if (ucs < data[mid][0])
@ -1600,22 +1600,43 @@ export const wcwidth = (function(opts) {
}
return 1;
}
let table = new Uint8Array(16384);
for (let i = 0; i < 16384; ++i) {
let j = i * 4;
// lookup table for BMP
const CODEPOINTS = 65536; // BMP holds 65536 codepoints
const BITWIDTH = 2; // a codepoint can have a width of 0, 1 or 2
const ITEMSIZE = 32; // using uint32_t
const CONTAINERSIZE = CODEPOINTS * BITWIDTH / ITEMSIZE;
const CODEPOINTS_PER_ITEM = ITEMSIZE / BITWIDTH;
const table = (typeof Uint32Array === 'undefined')
? new Array(CONTAINERSIZE)
: new Uint32Array(CONTAINERSIZE);
for (let i = 0; i < CONTAINERSIZE; ++i) {
let num = 0;
num |= (wcwidthBMP(j + 3));
num <<= 2;
num |= (wcwidthBMP(j + 2));
num <<= 2;
num |= (wcwidthBMP(j + 1));
num <<= 2;
num |= (wcwidthBMP(j));
let pos = CODEPOINTS_PER_ITEM;
while (pos--)
num = (num << 2) | wcwidthBMP(CODEPOINTS_PER_ITEM * i + pos);
table[i] = num;
}
// get width from lookup table for num < 65536:
// position in container : num / CODEPOINTS_PER_ITEM
// ==> n = table[Math.floor(num / 16)]
// ==> n = table[num >> 4]
// 16 codepoints per number: FFEEDDCCBBAA99887766554433221100
// position in number : (num % CODEPOINTS_PER_ITEM) * BITWIDTH
// ==> m = (n % 16) * 2
// ==> m = (num & 15) << 1
// right shift to position m
// ==> n = n >> m e.g. m=12 000000000000FFEEDDCCBBAA99887766
// we are only interested in 2 LSBs, cut off higher
// ==> n = n & 3 e.g. 000000000000000000000000000000XX
return function (num) {
num = num | 0;
return (num < 65536) ? (table[num >> 2] >> ((num & 3) * 2)) & 3 : wcwidthHigh(num);
num = num | 0; // get asm.js like optimization under V8
if (num < 32)
return opts.control;
else if (num < 127)
return 1;
else if (num < 65536)
return table[num >> 4] >> ((num & 15) << 1) & 3;
// do a full search for high codepoints
else return wcwidthHigh(num);
};
})({nul: 0, control: 0}); // configurable options