@@ -1600,21 +1600,26 @@ export const wcwidth = (function(opts) {
1600
1600
}
1601
1601
return 1 ;
1602
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 ;
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 ;
1618
1623
}
1619
1624
// get width from lookup table
1620
1625
// position in container : num / CODEPOINTS_PER_ITEM
@@ -1631,12 +1636,13 @@ export const wcwidth = (function(opts) {
1631
1636
return function ( num ) {
1632
1637
num = num | 0 ; // get asm.js like optimization under V8
1633
1638
if ( num < 32 )
1634
- return opts . control ;
1635
- else if ( num < 127 )
1639
+ return control | 0 ;
1640
+ if ( num < 127 )
1636
1641
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 ;
1639
1645
// do a full search for high codepoints
1640
- else return wcwidthHigh ( num ) ;
1646
+ return wcwidthHigh ( num ) ;
1641
1647
} ;
1642
1648
} ) ( { nul : 0 , control : 0 } ) ; // configurable options
0 commit comments